<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="卡券名称" prop="name">
              <el-input v-model="formData.name" placeholder="请输入卡券名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卡券类型" prop="type">
              <el-select v-model="formData.type" placeholder="请选择卡券类型">
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.MEMBER_COUPON_TYPE)"
                           :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="适用车型" prop="carModel">
              <el-select v-model="formData.carModel" placeholder="请选择卡券类型">
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.MEMBER_CAR)"
                           :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="核销规则" prop="outRule">
              <el-select v-model="formData.outRule" placeholder="请选择卡券类型">
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.MEMBER_COUPON_OUT_RULE)"
                           :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="开始有效期" prop="beginTime">
              <el-date-picker clearable v-model="formData.beginTime" type="date" value-format="timestamp"
                              placeholder="选择开始有效期"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束有效期" prop="endTime">
              <el-date-picker clearable v-model="formData.endTime" type="date" value-format="timestamp"
                              placeholder="选择结束有效期"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="面额" prop="amount">
              <el-input-number v-model="formData.amount" :precision="2" :step="0.1" ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单价" prop="unitPrice">
              <el-input-number v-model="formData.unitPrice" :precision="2" :step="0.1" ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="效果图片">
              <ImageUpload v-model="formData.image" :limit="1"/>
            </el-form-item>
          </el-col>
        </el-row>

<!--        <el-form-item label="适用子公司" prop="corpIds">-->
<!--          <el-input v-model="formData.corpIds" placeholder="请输入适用子公司"/>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as CouponApi from '@/api/member/coupon';
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "CouponForm",
  components: {
    ImageUpload,
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        name: undefined,
        type: undefined,
        carModel: undefined,
        beginTime: undefined,
        endTime: undefined,
        corpIds: undefined,
        amount: undefined,
        image: undefined,
        remark: undefined,
        outRule: undefined,
        unitPrice: undefined,
        parentId:null
      },
      // 表单校验
      formRules: {},
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id,parentId) {
      this.dialogVisible = true;
      this.reset();
      if (parentId){
        this.formData.parentId = parentId
      }
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await CouponApi.getCoupon(id);
          this.formData = res.data;
          this.title = "修改基础卡券";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增基础卡券";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await CouponApi.updateCoupon(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await CouponApi.createCoupon(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        name: undefined,
        type: undefined,
        carModel: undefined,
        beginTime: undefined,
        endTime: undefined,
        corpIds: undefined,
        amount: undefined,
        image: undefined,
        remark: undefined,
        outRule: undefined,
        unitPrice: undefined,
        parentId:null
      };
      this.resetForm("formRef");
    }
  }
};
</script>
