<!--
 * @Descripttion: (售后单记录表/tb_order_refund 添加弹窗)
 * @version: (1.0)
 * @Author: (lwh)
 * @Date: (2023-08-29)
 * @LastEditors: (lwh)
 * @LastEditTime: (2023-08-29)
-->
<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="订单guid" prop="orderGuid">
              <el-input v-model="formData.orderGuid" placeholder="请输入订单guid" />
            </el-form-item>
          </el-col>
       <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="客户guid" prop="customerGuid">
              <el-input v-model="formData.customerGuid" placeholder="请输入客户guid" />
            </el-form-item>
          </el-col>
              <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="售后类型(1退货退款 2退货(无需退款) 3换货)" prop="refundType">
              <el-select v-model="formData.refundType" placeholder="请选择售后类型(1退货退款 2退货(无需退款) 3换货)">
                <el-option v-for="item in  refund_type " :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="refundReason">
              <el-select v-model="formData.refundReason" placeholder="请选择客户退货原因">
                <el-option v-for="item in  refund_reason " :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="customerRefundDesc">
              <el-input v-model="formData.customerRefundDesc" placeholder="请输入客户退款说明" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="客户退款凭证" prop="customerRefundImg">
              <UploadImage ref="uploadRef" v-model="formData.customerRefundImg" :data=imgData :limit="1" :fileSize="5"
              :drag="true" />
            </el-form-item>
          </el-col>
              <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="商品收货状态(1未收到货 2已收到货)" prop="goodsReceiveStatus">
              <el-select v-model="formData.goodsReceiveStatus" placeholder="请选择商品收货状态(1未收到货 2已收到货)">
                <el-option v-for="item in  goods_receive_status " :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="商家审核状态(1待审核 2已同意 3已拒绝)" prop="merchantAuditStatus">
              <el-select v-model="formData.merchantAuditStatus" placeholder="请选择商家审核状态(1待审核 2已同意 3已拒绝)">
                <el-option v-for="item in  merchant_audit_status " :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="merchantRefuseDesc">
              <el-input v-model="formData.merchantRefuseDesc" placeholder="请输入商家拒绝原因(说明)" />
            </el-form-item>
          </el-col>
   <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="实际退款金额" prop="refundMoney">
              <el-input v-model="formData.refundMoney" placeholder="请输入实际退款金额" />
            </el-form-item>
          </el-col>
       <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="商家退货地址guid" prop="shopRefundAddressGuid">
              <el-input v-model="formData.shopRefundAddressGuid" placeholder="请输入商家退货地址guid" />
            </el-form-item>
          </el-col>
              <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="用户是否发货(1未发货 2已发货)" prop="isCustomerSend">
              <el-select v-model="formData.isCustomerSend" placeholder="请选择用户是否发货(1未发货 2已发货)">
                <el-option v-for="item in  is_customer_send " :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="customerSendTime">
              <el-date-picker v-model="formData.customerSendTime" type="datetime" :teleported="false" placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>
       <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="客户发货物流公司guid" prop="logisticsCompanyGuid">
              <el-input v-model="formData.logisticsCompanyGuid" placeholder="请输入客户发货物流公司guid" />
            </el-form-item>
          </el-col>
   <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="客户发货物流单号" prop="expressNo">
              <el-input v-model="formData.expressNo" placeholder="请输入客户发货物流单号" />
            </el-form-item>
          </el-col>
              <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="售后单状态(1进行中 2已拒绝 3已完成 4已取消)" prop="orderRefundStatus">
              <el-select v-model="formData.orderRefundStatus" placeholder="请选择售后单状态(1进行中 2已拒绝 3已完成 4已取消)">
                <el-option v-for="item in  order_refund_status " :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 key="dialog-footer">
        <el-button type="primary" @click="handleAddClick(formRef)" :loading="loadingStatus">添加</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
      </div>
    </template>
  </el-dialog>
</template>


<script setup>
import { reactive, ref, watch } from "vue";
import { ElMessage } from 'element-plus'
import modal from '@/plugins/modal.js'
import {  addOrUpdateOrderRefund } from '@/api/business/OrderManage/OrderRefunds/orderRefund.js';


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

   await getrefund_type()
   await getrefund_reason()
   await getgoods_receive_status()
   await getmerchant_audit_status()
   await getis_customer_send()
   await getorder_refund_status()

}

// -业务参数
// 售后类型(1退货退款 2退货(无需退款) 3换货)字典选项列表
const refund_type = ref([]);
// 客户退货原因字典选项列表
const refund_reason = ref([]);
// 商品收货状态(1未收到货 2已收到货)字典选项列表
const goods_receive_status = ref([]);
// 商家审核状态(1待审核 2已同意 3已拒绝)字典选项列表
const merchant_audit_status = ref([]);
// 用户是否发货(1未发货 2已发货)字典选项列表
const is_customer_send = ref([]);
// 售后单状态(1进行中 2已拒绝 3已完成 4已取消)字典选项列表
const order_refund_status = ref([]);


// -业务方法
// 字典获取
async function getrefund_type() {
  await proxy.getDicts('refund_type').then((res) => {
    refund_type.value = res.data
  })
}
// 字典获取
async function getrefund_reason() {
  await proxy.getDicts('refund_reason').then((res) => {
    refund_reason.value = res.data
  })
}
// 字典获取
async function getgoods_receive_status() {
  await proxy.getDicts('goods_receive_status').then((res) => {
    goods_receive_status.value = res.data
  })
}
// 字典获取
async function getmerchant_audit_status() {
  await proxy.getDicts('merchant_audit_status').then((res) => {
    merchant_audit_status.value = res.data
  })
}
// 字典获取
async function getis_customer_send() {
  await proxy.getDicts('is_customer_send').then((res) => {
    is_customer_send.value = res.data
  })
}
// 字典获取
async function getorder_refund_status() {
  await proxy.getDicts('order_refund_status').then((res) => {
    order_refund_status.value = res.data
  })
}



// -基础参数
const loadingStatus = ref(false)
const labelWidth = 100;
const formRef = ref();
const { proxy } = getCurrentInstance()
const emits = defineEmits(["update:modelValue"]);
const formData = reactive({
});
const props = defineProps({
  modelValue: Boolean,
  done: Function,
});
const imgData = ref({
  fileDir: "OrderRefund"
})

// 验证
const rules = reactive({
    orderRefundGuid: [{ required: true, message: "不能为空", trigger: "blur", type: "number" }],
    orderGuid: [{ required: true, message: "订单guid不能为空", trigger: "blur", type: "number" }],
    customerGuid: [{ required: true, message: "客户guid不能为空", trigger: "blur", type: "number" }],
    refundType: [{ required: true, message: "售后类型(1退货退款 2退货(无需退款) 3换货)不能为空", trigger: "change", type: "number" }],
    refundReason: [{ required: true, message: "客户退货原因不能为空", trigger: "change", type: "number" }],
    goodsReceiveStatus: [{ required: true, message: "商品收货状态(1未收到货 2已收到货)不能为空", trigger: "change", type: "number" }],
    merchantAuditStatus: [{ required: true, message: "商家审核状态(1待审核 2已同意 3已拒绝)不能为空", trigger: "change", type: "number" }],
    refundMoney: [{ required: true, message: "实际退款金额不能为空", trigger: "blur" }],
    isCustomerSend: [{ required: true, message: "用户是否发货(1未发货 2已发货)不能为空", trigger: "change", type: "number" }],
    orderRefundStatus: [{ required: true, message: "售后单状态(1进行中 2已拒绝 3已完成 4已取消)不能为空", trigger: "change", type: "number" }],
});

// -基础方法

// 提交
const handleAddClick = async (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }
    loadingStatus.value = true


    const { code } = await addOrUpdateOrderRefund(formData);
    if (code == 200) {
      modal.msgSuccess('添加成功')
      closeDialog();
      loadingStatus.value = false
    }
  });
};
const closeDialog = () => {
  handleResetClick(formRef.value);
  props.done();
  emits("update:modelValue", false);
};
const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>