<template>
  <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @open="onOpen">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="商户" prop="mchId" v-if="visibleColumn('mchId')">
        <user-input v-model="form.mchId" placeholder="请选择商户"/>
      </el-form-item>
      <el-form-item label="等级名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入等级名称" maxlength="30" show-word-limit/>
      </el-form-item>
      <el-form-item label="折扣" prop="discount">
        <el-input-number v-model="form.discount" placeholder="请输入折扣" :min="0" :max="10"/> 折
      </el-form-item>
      <el-form-item label="描述文本" prop="description">
        <el-input v-model="form.description" type="textarea" placeholder="请输入内容" maxlength="1000" show-word-limit/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import UserInput from '@/components/Business/SmUser/UserInput.vue'
import StoreInput from '@/components/Business/Store/StoreInput.vue'
import VipLevelInput from '@/components/Business/VipLevel/VipLevelInput.vue'
import { $editDialog, $showColumns } from '@/utils/mixins'
import { addVipLevel, getVipLevel, updateVipLevel } from '@/api/ss/vipLevel'

export default {
  name: "VipLevelEditDialog",
  mixins: [$editDialog, $showColumns],
  components: { VipLevelInput, StoreInput, UserInput },
  props: {
    rules: {
      type: Object,
      default: () => ({
        mchId: [
          { required: true, message: "商户不能为空", trigger: "change" }
        ],
        name: [
          { required: true, message: "等级名称不能为空", trigger: "blur" }
        ],
      })
    },
    getApi: {
      type: Function,
      default: getVipLevel
    },
    addApi: {
      type: Function,
      default: addVipLevel
    },
    updateApi: {
      type: Function,
      default: updateVipLevel
    }
  },
  data() {
    return {
      form: {},
      title: null,
    }
  },
  methods: {
    getDetail() {
      this.getApi(this.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员等级";
      });
    },
    // 表单重置
    reset() {
      this.title = "新增会员等级";
      this.form = {
        id: null,
        mchId: null,
        name: null,
        discount: null,
        createTime: null,
        description: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.updateApi(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('success')
            });
          } else {
            this.addApi(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('success')
            });
          }
        }
      });
    },
  }
}
</script>
