<!--
 * @Descripttion: (订单/tb_order 编辑弹窗)
 * @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="goodsGuid">
            <el-input v-model="formData.goodsGuid" placeholder="请输入商品guid" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="订单流水guid" prop="paymentGuid">
            <el-input v-model="formData.paymentGuid" 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="店铺guid" prop="shopGuid">
            <el-input v-model="formData.shopGuid" placeholder="请输入店铺guid" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="订单号" prop="orderNumber">
            <el-input v-model="formData.orderNumber" placeholder="请输入订单号" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="商品总金额" prop="goodsTotalAmoun">
            <el-input v-model="formData.goodsTotalAmoun" placeholder="请输入商品总金额" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="订单金额" prop="orderAmount">
            <el-input v-model="formData.orderAmount" placeholder="请输入订单金额" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="优惠券guid" prop="couponGuid">
            <el-input v-model="formData.couponGuid" placeholder="请输入优惠券guid" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="优惠券抵扣金额" prop="couponMoney">
            <el-input v-model="formData.couponMoney" placeholder="请输入优惠券抵扣金额" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="积分抵扣金额" prop="pointsMoney">
            <el-input v-model="formData.pointsMoney" placeholder="请输入积分抵扣金额" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="积分抵扣数量" prop="pointsNum">
            <el-input v-model="formData.pointsNum" placeholder="请输入积分抵扣数量" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="实际付款金额(包含运费)" prop="payPrice">
            <el-input v-model="formData.payPrice" placeholder="请输入实际付款金额(包含运费)" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="后台修改的订单金额（差价）" prop="updatePrice">
            <el-input v-model="formData.updatePrice" placeholder="请输入后台修改的订单金额（差价）" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="买家留言" prop="orderRemark">
            <el-input v-model="formData.orderRemark" placeholder="请输入买家留言" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="支付方式(1微信支付 2余额支付)" prop="payType">
            <el-select v-model="formData.payType" placeholder="请选择支付方式(1微信支付 2余额支付)">
              <el-option v-for="item in  pay_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="付款状态(1未付款 2已付款)" prop="payStatus">
            <el-select v-model="formData.payStatus" placeholder="请选择付款状态(1未付款 2已付款)">
              <el-option v-for="item in  pay_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="payTime">
            <el-date-picker v-model="formData.payTime" type="datetime" :teleported="false"
              placeholder="选择日期时间"></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="配送方式(1快递配送)" prop="deliveryType">
            <el-select v-model="formData.deliveryType" placeholder="请选择配送方式(1快递配送)">
              <el-option v-for="item in  delivery_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="expressPrice">
            <el-input v-model="formData.expressPrice" placeholder="请输入运费金额" />
          </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="logisticsCompany">
            <el-input v-model="formData.logisticsCompany" placeholder="请输入物流公司" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="物流单号" prop="logisticsTrackingNumber">
            <el-input v-model="formData.logisticsTrackingNumber" placeholder="请输入物流单号" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="发货状态(1未发货 2已发货)" prop="deliveryStatus">
            <el-select v-model="formData.deliveryStatus" placeholder="请选择发货状态(1未发货 2已发货)">
              <el-option v-for="item in  delivery_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="deliveryTime">
            <el-date-picker v-model="formData.deliveryTime" type="datetime" :teleported="false"
              placeholder="选择日期时间"></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="收货状态(1未收货 2已收货)" prop="receiptStatus">
            <el-select v-model="formData.receiptStatus" placeholder="请选择收货状态(1未收货 2已收货)">
              <el-option v-for="item in  receipt_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="receiptTime">
            <el-date-picker v-model="formData.receiptTime" type="datetime" :teleported="false"
              placeholder="选择日期时间"></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="订单状态(1进行中 2取消 3待取消 4已完成)" prop="orderStatus">
            <el-select v-model="formData.orderStatus" placeholder="请选择订单状态(1进行中 2取消 3待取消 4已完成)">
              <el-option v-for="item in  order_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="pointsBonus">
            <el-input v-model="formData.pointsBonus" placeholder="请输入赠送的积分数量" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="订单是否已结算(1未结算 2已结算)" prop="isSettled">
            <el-select v-model="formData.isSettled" placeholder="请选择订单是否已结算(1未结算 2已结算)">
              <el-option v-for="item in  is_settled " :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="transactionId">
            <el-input v-model="formData.transactionId" placeholder="请输入微信支付交易号" />
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="是否已评价(1否 2是)" prop="isComment">
            <el-select v-model="formData.isComment" placeholder="请选择是否已评价(1否 2是)">
              <el-option v-for="item in  is_comment " :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普通订单)" prop="orderSource">
            <el-input v-model="formData.orderSource" placeholder="请输入订单来源(1普通订单)" />
          </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 { addOrUpdateOrder } from "@/api/business/OrderManage/Orders/order.js";


// 打开弹窗时回调
const openDialog = async () => {
  await getpay_type()
  await getpay_status()
  await getdelivery_type()
  await getdelivery_status()
  await getreceipt_status()
  await getorder_status()
  await getis_settled()
  await getis_comment()

}

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

});

// 业务参数
// 支付方式(1微信支付 2余额支付)字典选项列表
const pay_type = ref([]);
// 付款状态(1未付款 2已付款)字典选项列表
const pay_status = ref([]);
// 配送方式(1快递配送)字典选项列表
const delivery_type = ref([]);
// 发货状态(1未发货 2已发货)字典选项列表
const delivery_status = ref([]);
// 收货状态(1未收货 2已收货)字典选项列表
const receipt_status = ref([]);
// 订单状态(1进行中 2取消 3待取消 4已完成)字典选项列表
const order_status = ref([]);
// 订单是否已结算(1未结算 2已结算)字典选项列表
const is_settled = ref([]);
// 是否已评价(1否 2是)字典选项列表
const is_comment = ref([]);

// -业务方法
//字典获取
async function getpay_type() {
  await proxy.getDicts('pay_type').then((res) => {
    pay_type.value = res.data
  })
}
//字典获取
async function getpay_status() {
  await proxy.getDicts('pay_status').then((res) => {
    pay_status.value = res.data
  })
}
//字典获取
async function getdelivery_type() {
  await proxy.getDicts('delivery_type').then((res) => {
    delivery_type.value = res.data
  })
}
//字典获取
async function getdelivery_status() {
  await proxy.getDicts('delivery_status').then((res) => {
    delivery_status.value = res.data
  })
}
//字典获取
async function getreceipt_status() {
  await proxy.getDicts('receipt_status').then((res) => {
    receipt_status.value = res.data
  })
}
//字典获取
async function getorder_status() {
  await proxy.getDicts('order_status').then((res) => {
    order_status.value = res.data
  })
}
//字典获取
async function getis_settled() {
  await proxy.getDicts('is_settled').then((res) => {
    is_settled.value = res.data
  })
}
//字典获取
async function getis_comment() {
  await proxy.getDicts('is_comment').then((res) => {
    is_comment.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: "Order"
})

// 验证
const rules = reactive({
  orderGuid: [{ required: true, message: "不能为空", trigger: "blur", type: "number" }],
  goodsGuid: [{ required: true, message: "商品guid不能为空", trigger: "blur", type: "number" }],
  paymentGuid: [{ required: true, message: "订单流水guid不能为空", trigger: "blur", type: "number" }],
  customerGuid: [{ required: true, message: "客户guid不能为空", trigger: "blur", type: "number" }],
  orderNumber: [{ required: true, message: "订单号不能为空", trigger: "blur" }],
  goodsTotalAmoun: [{ required: true, message: "商品总金额不能为空", trigger: "blur" }],
  orderAmount: [{ required: true, message: "订单金额不能为空", trigger: "blur" }],
  payPrice: [{ required: true, message: "实际付款金额(包含运费)不能为空", trigger: "blur" }],
  payType: [{ required: true, message: "支付方式(1微信支付 2余额支付)不能为空", trigger: "change", type: "number" }],
  payStatus: [{ required: true, message: "付款状态(1未付款 2已付款)不能为空", trigger: "change", type: "number" }],
  payTime: [{ required: true, message: "付款时间不能为空", trigger: "blur" }],
  deliveryType: [{ required: true, message: "配送方式(1快递配送)不能为空", trigger: "change", type: "number" }],
  expressPrice: [{ required: true, message: "运费金额不能为空", trigger: "blur" }],
  deliveryStatus: [{ required: true, message: "发货状态(1未发货 2已发货)不能为空", trigger: "change", type: "number" }],
  receiptStatus: [{ required: true, message: "收货状态(1未收货 2已收货)不能为空", trigger: "change", type: "number" }],
  orderStatus: [{ required: true, message: "订单状态(1进行中 2取消 3待取消 4已完成)不能为空", trigger: "change", type: "number" }],
  isSettled: [{ required: true, message: "订单是否已结算(1未结算 2已结算)不能为空", trigger: "change", type: "number" }],
  isComment: [{ required: true, message: "是否已评价(1否 2是)不能为空", trigger: "change", type: "number" }],
  orderSource: [{ required: true, message: "订单来源(1普通订单)不能为空", trigger: "blur", type: "number" }],
});

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


    const { code } = await addOrUpdateOrder(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>
