<template>
  <div class="pointcontrol-page">
    <el-card class="pointcontrol-card">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="pointcontrol-form">
        <el-form-item :label="$t('jifen.评论获得积分')" prop="reviewEnable">
          <el-radio-group v-model="form.reviewEnable">
            <el-radio :label="1">{{$t('jifen.开启')}}</el-radio>
            <el-radio :label="0">{{$t('jifen.关闭')}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('jifen.评论1次积分数量')" prop="reviewPoints">
          <el-input v-model="form.reviewPoints" :placeholder="$t('jifen.请输入评论1次获得积分数量')" />
        </el-form-item>
        <el-form-item :label="$t('jifen.1个积分对应的金额')" prop="vndPerpoint">
          <el-input v-model="form.vndPerpoint" :placeholder="$t('jifen.请输入1个积分对应的金额')" />
        </el-form-item>
        <el-form-item :label="$t('jifen.下单积分抵扣')" prop="orderUseEnable">
          <el-radio-group v-model="form.orderUseEnable">
            <el-radio :label="1">{{$t('jifen.开启')}}</el-radio>
            <el-radio :label="0">{{$t('jifen.关闭')}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm" style="width: 120px;">{{$t('jifen.确 定')}}</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { listControl, getControl, delControl, addControl, updateControl } from "@/api/system/pointControl";

export default {
  name: "Control",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 积分控制表格数据
      controlList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reviewEnable: null,
        orderCondition: null,
        reviewPoints: null,
        vndPerpoint: null,
        orderUseEnable: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        reviewEnable: [
          { required: true, message:this.$t('jifen.评论积分开关不能为空'), trigger: "blur" }
        ],
        orderCondition: [
          { required: true, message:this.$t('jifen.订单金额条件不能为空') , trigger: "blur" }
        ],
        reviewPoints: [
          { required: true, message: this.$t('jifen.每次评论获得积分数量不能为空'), trigger: "blur" }
        ],
        vndPerpoint: [
          { required: true, message:this.$t('jifen.1个积分对应的金额不能为空') , trigger: "blur" }
        ],
        orderUseEnable: [
          { required: true, message:this.$t('jifen.下单积分抵扣开发不能为空') , trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.handleUpdate();
  },
  methods: {
    /** 查询积分控制列表 */
    getList() {
      this.loading = true;
      listControl(this.queryParams).then(response => {
        this.controlList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        reviewEnable: null,
        orderCondition: null,
        reviewPoints: null,
        vndPerpoint: null,
        orderUseEnable: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加积分控制";
    },
    /** 修改按钮操作 */
    handleUpdate() {
      this.reset();
      const id = 1;
      getControl(id).then(response => {
        this.form = response.data;
        // this.open = true;
        // this.title = "修改积分控制";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateControl(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addControl(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除积分控制编号为"' + ids + '"的数据项？').then(function() {
        return delControl(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/control/export', {
        ...this.queryParams
      }, `control_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.pointcontrol-page {
  min-height: 100vh;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  background: #f4f6fa;
  padding-top: 40px;
  box-sizing: border-box;
}

.pointcontrol-card {
  width: 900px;
  max-width: 98vw;
  padding: 40px 48px 24px 48px;
  box-shadow: 0 4px 24px 0 rgba(0,0,0,0.08);
  border-radius: 12px;
  background: #fff;
}

.pointcontrol-form {
  margin-top: 10px;
}

.el-form-item__label {
  white-space: nowrap !important; /* 防止label换行 */
  font-size: 16px;
}
</style>
