<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="卡券名称" prop="title">
        <el-input v-model="form.title" placeholder="请输入卡券名称" />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          v-model="form.type"
          placeholder="请选择类型"
          style="width: 100%"
        >
          <el-option
            v-for="dict in dict.type.coupon_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="额度" prop="quota">
        <el-input-number
          style="width: 100%"
          v-model="form.quota"
          :precision="2"
          :step="1"
          :min="0"
          :max="9999"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="使用说明" prop="description">
        <el-input
          type="textarea"
          :rows="4"
          v-model="form.description"
          placeholder="请输入使用说明"
        />
      </el-form-item>
      <el-form-item label="图片" prop="pic">
        <image-upload :limit="1" v-model="form.pic" />
      </el-form-item>
    </el-form>
    <div style="text-align: right">
      <el-button type="primary" @click="handleConfirm">确 定</el-button>
      <el-button @click="handleCancel">取 消</el-button>
    </div>
  </div>
</template>

<script>
export default {
  dicts: ["coupon_type"],
  props: {
    data: {
      type: Object,
      default: () => {},
    },
    loading: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    const validQuota = (rule, value, callback) => {
      // console.log("rule-", rule, "value-", value);
      if (value <= 0) {
        callback(new Error("卡券额度不能为0"));
      } else {
        callback();
      }
    };
    return {
      form: {
        title: "",
        type: "",
        quota: "",
        description: "",
        pic: "",
      },
      rules: {
        title: [{ required: true, trigger: "blur", message: "请输入卡券名称" }],
        type: [{ required: true, trigger: "change", message: "请选择类型" }],
        quota: [
          { required: true, trigger: "blur", message: "请输入额度" },
          { required: true, validator: validQuota, trigger: "blur" },
        ],
        description: [
          { required: true, trigger: "blur", message: "请输入使用说明" },
        ],
        pic: [{ required: true, trigger: "blur", message: "请上传图片" }],
      },
    };
  },
  watch: {
    data: {
      handler(newVal) {
        if (newVal) {
          this.form = {
            id: newVal.id || "",
            title: newVal.title || "",
            type: newVal.type || "",
            quota: newVal.quota || "",
            description: newVal.description || "",
            pic: newVal.pic || "",
          };
          this.$nextTick(() => {
            this.$refs.form && this.$refs.form.clearValidate();
          });
        }
      },
    },
  },
  methods: {
    // 确定
    handleConfirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$emit("confirm", this.form);
        }
      });
    },
    // 取消
    handleCancel() {
      this.form = {
        title: "",
        type: "",
        quota: "",
        description: "",
        pic: "",
      };
      this.$nextTick(() => {
        this.$refs.form && this.$refs.form.clearValidate();
      });
      this.$emit("cancel");
    },
  },
};
</script>

<style lang="scss" scoped></style>
