<template>
  <div class="pages" v-loading="loading">
    <p class="pages-title mb30">{{ currentAreaName }}审核配置</p>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="200px">
      <el-form-item label="创建卡券是否需要审核" prop="couponAdd">
        <el-switch :active-value="1" :inactive-value="0" v-model="ruleForm.couponAdd"></el-switch>
      </el-form-item>
      <el-form-item label="需要审核卡券类型" prop="couponType" v-if="ruleForm.couponAdd">
        <el-checkbox-group v-model="ruleForm.couponType">
          <!-- 0抵金券 1折扣券 2兑换券 -->
          <el-checkbox v-for="item in couponType" :key="item.vaule" :label="item.value">{{ item.label }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="编辑卡券是否需要审核" prop="couponUpdate">
        <el-switch :active-value="1" :inactive-value="0" v-model="ruleForm.couponUpdate"></el-switch>
      </el-form-item>
      <el-form-item label="调整卡券库存是否需要审核" prop="couponStockModify">
        <el-switch :active-value="1" :inactive-value="0" v-model="ruleForm.couponStockModify"></el-switch>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" class="w64" @click="submitForm">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import api from '@/api/gic-card';
const { auditSettingInfo, auditSettingSave } = api;
export default {
  name: 'CardAppConfig',
  data() {

    let validateCheckList = (rule, value, callback) => {
      if (this.ruleForm.couponAdd && !value.length) {
        callback(new Error('请勾选卡券类型'));
      } else {
        callback();
      }
    };
    return {
      loading: false,
      couponType: [ { label: '抵金券', value: 0 }, { label: '折扣券', value: 1 }, { label: '兑换券', value: 2 } ],
      ruleForm: {
        couponAdd: false,
        couponType: [],
        couponUpdate: false,
        couponStockModify: false
      },
      rules: {
        couponType: [ { validator: validateCheckList, trigger: 'blur' } ],
      }
    };
  },
  created() {
    this.$emit('updateBread', [ { breadName: '卡券列表', breadPath: '/gic-card/list' }, { breadName: '应用配置' } ]);
  },
  mounted() {
    this.getDetail();
  },
  methods: {
    submitForm() {
      if (this.loading) {
        return;
      }
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) {
          PublicMethods.jumpToFormError(); // eslint-disable-line
          return;
        }
        let params = { ...this.ruleForm };
        params.couponType = params.couponType.join();
        params.memberCardId = this.currentAreaId;
        this.loading = true;
        auditSettingSave(params).then(res => {
          this.loading = false;
          this.$tips({ message: '保存成功', type: 'success' });
        }).finally(() => {
          this.loading = false;
        });
      });
    },
    // 获取配置详情
    getDetail() {
      auditSettingInfo({ memberCardId: this.currentAreaId }).then(res => {
        this.ruleForm = { ...this.ruleForm, ...res.result };
        this.ruleForm.couponType = res.result.couponType ? res.result.couponType.split(',').map(v => {
          return Number(v);
        }) : [];
      });
    }
  },
  computed: {
    currentAreaName() {
      return this.$store.state.currentAreaName;
    }
  },
};
</script>
