<!--
 * @Descripttion: (领券记录/tb_customer_coupon 编辑弹窗)
 * @version: (1.0)
 * @Author: (lwh)
 * @Date: (2023-07-31)
 * @LastEditors: (lwh)
 * @LastEditTime: (2023-07-31)
-->
<template>
  <el-dialog v-model="props.modelValue" title="修改领券记录信息" width="900px" @closed="closeDialog" @open="openDialog">
    <el-form ref="formRef" :model="formData" :rules="rules">
      <el-row :gutter="20">





        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="优惠券" prop="couponGuid">
            <el-input v-model='formData.couponName' disabled type="text">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="客户" prop="customerGuid">
            <el-input v-model='formData.customerName' disabled type="text">
            </el-input>
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="是否过期" prop="customerCouponIsExpired">
            <el-select v-model="formData.customerCouponIsExpired" placeholder="请选择是否过期">
              <el-option v-for="item in  is_expired " :key="item.dictValue" :label="item.dictLabel"
                :value="parseInt(item.dictValue)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="是否已使用" prop="customerCouponIsUsed">
            <el-select v-model="formData.customerCouponIsUsed" placeholder="请选择是否已使用">
              <el-option v-for="item in  is_used " :key="item.dictValue" :label="item.dictLabel"
                :value="parseInt(item.dictValue)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>






      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleEditClick(formRef)" :loading="loadingStatus">编辑</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import modal from '@/plugins/modal.js'
import { reactive, ref, watch } from "vue";
import { addOrUpdateCustomerCoupon } from "@/api/business/Marketing/CouponManage/CustomerCoupons/customerCoupon.js";


// 打开弹窗时回调
const openDialog = async () => {
  await getis_expired()
  await getis_used()

}

const formData = ref({
  ...props.data,
});
watch(props, async (v) => {
  formData.value = v.data;

});

// 业务参数
// 是否过期字典选项列表
const is_expired = ref([]);
// 是否已使用字典选项列表
const is_used = ref([]);

// -业务方法
//字典获取
async function getis_expired() {
  await proxy.getDicts('is_expired').then((res) => {
    is_expired.value = res.data
  })
}
//字典获取
async function getis_used() {
  await proxy.getDicts('is_used').then((res) => {
    is_used.value = res.data
  })
}



// -基础参数
const props = defineProps({
  modelValue: Boolean,
  data: Object,
  done: Function,
});

const loadingStatus = ref(false)
const labelWidth = 100;
const formRef = ref();
const { proxy } = getCurrentInstance()
const emits = defineEmits(["update:modelValue"]);
const imgData = ref({
  fileDir: "CustomerCoupon"
})

// 验证
const rules = reactive({
  customerCouponGuid: [{ required: true, message: "不能为空", trigger: "blur", type: "number" }],
  customerCouponIsExpired: [{ required: true, message: "是否过期不能为空", trigger: "change", type: "number" }],
  customerCouponIsUsed: [{ required: true, message: "是否已使用不能为空", trigger: "change", type: "number" }],
});

// -基础方法
// 提交
const handleEditClick = async (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }
    loadingStatus.value = true


    const { code } = await addOrUpdateCustomerCoupon(formData.value);
    if (code == 200) {
      modal.msgSuccess('修改成功')
      closeDialog();
      loadingStatus.value = false
    }
  });
}

const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
}
const closeDialog = () => {
  props.done();
  emits("update:modelValue", false);
};

</script>
