<!-- 编辑弹窗 -->
<template>
  <ele-modal
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :maxable="maxable"
    :title="isUpdate ? '编辑网站订单' : '添加网站订单'"
    :body-style="{ paddingBottom: '28px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="styleResponsive ? { md: 4, sm: 5, xs: 24 } : { flex: '90px' }"
      :wrapper-col="
        styleResponsive ? { md: 19, sm: 19, xs: 24 } : { flex: '1' }
      "
    >
      <a-form-item label="订单类型，0售前咨询 1售后服务 2意见反馈" name="type">
        <a-input
          allow-clear
          placeholder="请输入订单类型，0售前咨询 1售后服务 2意见反馈"
          v-model:value="form.type"
        />
      </a-form-item>
      <a-form-item label="订单标题" name="title">
        <a-input
          allow-clear
          placeholder="请输入订单标题"
          v-model:value="form.title"
        />
      </a-form-item>
      <a-form-item label="公司/团队名称" name="company">
        <a-input
          allow-clear
          placeholder="请输入公司/团队名称"
          v-model:value="form.company"
        />
      </a-form-item>
      <a-form-item label="订单内容" name="content">
        <a-input
          allow-clear
          placeholder="请输入订单内容"
          v-model:value="form.content"
        />
      </a-form-item>
      <a-form-item label="订单编号" name="orderNo">
        <a-input
          allow-clear
          placeholder="请输入订单编号"
          v-model:value="form.orderNo"
        />
      </a-form-item>
      <a-form-item label="快递/自提" name="deliveryType">
        <a-input
          allow-clear
          placeholder="请输入快递/自提"
          v-model:value="form.deliveryType"
        />
      </a-form-item>
      <a-form-item label="下单渠道，0网站 1微信小程序 2其他" name="channel">
        <a-input
          allow-clear
          placeholder="请输入下单渠道，0网站 1微信小程序 2其他"
          v-model:value="form.channel"
        />
      </a-form-item>
      <a-form-item label="微信支付交易号号" name="transactionId">
        <a-input
          allow-clear
          placeholder="请输入微信支付交易号号"
          v-model:value="form.transactionId"
        />
      </a-form-item>
      <a-form-item label="微信退款订单号" name="refundOrder">
        <a-input
          allow-clear
          placeholder="请输入微信退款订单号"
          v-model:value="form.refundOrder"
        />
      </a-form-item>
      <a-form-item label="商户ID" name="merchantId">
        <a-input
          allow-clear
          placeholder="请输入商户ID"
          v-model:value="form.merchantId"
        />
      </a-form-item>
      <a-form-item label="商户名称" name="merchantName">
        <a-input
          allow-clear
          placeholder="请输入商户名称"
          v-model:value="form.merchantName"
        />
      </a-form-item>
      <a-form-item label="商户编号" name="merchantCode">
        <a-input
          allow-clear
          placeholder="请输入商户编号"
          v-model:value="form.merchantCode"
        />
      </a-form-item>
      <a-form-item label="使用的优惠券id" name="couponId">
        <a-input
          allow-clear
          placeholder="请输入使用的优惠券id"
          v-model:value="form.couponId"
        />
      </a-form-item>
      <a-form-item label="使用的会员卡id" name="cardId">
        <a-input
          allow-clear
          placeholder="请输入使用的会员卡id"
          v-model:value="form.cardId"
        />
      </a-form-item>
      <a-form-item label="关联管理员id" name="adminId">
        <a-input
          allow-clear
          placeholder="请输入关联管理员id"
          v-model:value="form.adminId"
        />
      </a-form-item>
      <a-form-item label="核销管理员id" name="confirmId">
        <a-input
          allow-clear
          placeholder="请输入核销管理员id"
          v-model:value="form.confirmId"
        />
      </a-form-item>
      <a-form-item label="IC卡号" name="icCard">
        <a-input
          allow-clear
          placeholder="请输入IC卡号"
          v-model:value="form.icCard"
        />
      </a-form-item>
      <a-form-item label="真实姓名" name="realName">
        <a-input
          allow-clear
          placeholder="请输入真实姓名"
          v-model:value="form.realName"
        />
      </a-form-item>
      <a-form-item label="关联收货地址" name="addressId">
        <a-input
          allow-clear
          placeholder="请输入关联收货地址"
          v-model:value="form.addressId"
        />
      </a-form-item>
      <a-form-item label="收货地址" name="address">
        <a-input
          allow-clear
          placeholder="请输入收货地址"
          v-model:value="form.address"
        />
      </a-form-item>
      <a-form-item label="" name="addressLat">
        <a-input
          allow-clear
          placeholder="请输入"
          v-model:value="form.addressLat"
        />
      </a-form-item>
      <a-form-item label="" name="addressLng">
        <a-input
          allow-clear
          placeholder="请输入"
          v-model:value="form.addressLng"
        />
      </a-form-item>
      <a-form-item label="自提店铺id" name="selfTakeMerchantId">
        <a-input
          allow-clear
          placeholder="请输入自提店铺id"
          v-model:value="form.selfTakeMerchantId"
        />
      </a-form-item>
      <a-form-item label="自提店铺" name="selfTakeMerchantName">
        <a-input
          allow-clear
          placeholder="请输入自提店铺"
          v-model:value="form.selfTakeMerchantName"
        />
      </a-form-item>
      <a-form-item label="配送开始时间" name="sendStartTime">
        <a-input
          allow-clear
          placeholder="请输入配送开始时间"
          v-model:value="form.sendStartTime"
        />
      </a-form-item>
      <a-form-item label="配送结束时间" name="sendEndTime">
        <a-input
          allow-clear
          placeholder="请输入配送结束时间"
          v-model:value="form.sendEndTime"
        />
      </a-form-item>
      <a-form-item label="发货店铺id" name="expressMerchantId">
        <a-input
          allow-clear
          placeholder="请输入发货店铺id"
          v-model:value="form.expressMerchantId"
        />
      </a-form-item>
      <a-form-item label="发货店铺" name="expressMerchantName">
        <a-input
          allow-clear
          placeholder="请输入发货店铺"
          v-model:value="form.expressMerchantName"
        />
      </a-form-item>
      <a-form-item label="订单总额" name="totalPrice">
        <a-input
          allow-clear
          placeholder="请输入订单总额"
          v-model:value="form.totalPrice"
        />
      </a-form-item>
      <a-form-item label="减少的金额，使用VIP会员折扣、优惠券抵扣、优惠券折扣后减去的价格" name="reducePrice">
        <a-input
          allow-clear
          placeholder="请输入减少的金额，使用VIP会员折扣、优惠券抵扣、优惠券折扣后减去的价格"
          v-model:value="form.reducePrice"
        />
      </a-form-item>
      <a-form-item label="实际付款" name="payPrice">
        <a-input
          allow-clear
          placeholder="请输入实际付款"
          v-model:value="form.payPrice"
        />
      </a-form-item>
      <a-form-item label="用于统计" name="price">
        <a-input
          allow-clear
          placeholder="请输入用于统计"
          v-model:value="form.price"
        />
      </a-form-item>
      <a-form-item label="价钱，用于积分赠送" name="money">
        <a-input
          allow-clear
          placeholder="请输入价钱，用于积分赠送"
          v-model:value="form.money"
        />
      </a-form-item>
      <a-form-item label="取消时间" name="cancelTime">
        <a-input
          allow-clear
          placeholder="请输入取消时间"
          v-model:value="form.cancelTime"
        />
      </a-form-item>
      <a-form-item label="取消原因" name="cancelReason">
        <a-input
          allow-clear
          placeholder="请输入取消原因"
          v-model:value="form.cancelReason"
        />
      </a-form-item>
      <a-form-item label="退款金额" name="refundMoney">
        <a-input
          allow-clear
          placeholder="请输入退款金额"
          v-model:value="form.refundMoney"
        />
      </a-form-item>
      <a-form-item label="教练价格" name="coachPrice">
        <a-input
          allow-clear
          placeholder="请输入教练价格"
          v-model:value="form.coachPrice"
        />
      </a-form-item>
      <a-form-item label="购买数量" name="totalNum">
        <a-input
          allow-clear
          placeholder="请输入购买数量"
          v-model:value="form.totalNum"
        />
      </a-form-item>
      <a-form-item label="教练id" name="coachId">
        <a-input
          allow-clear
          placeholder="请输入教练id"
          v-model:value="form.coachId"
        />
      </a-form-item>
      <a-form-item label="商品ID" name="formId">
        <a-input
          allow-clear
          placeholder="请输入商品ID"
          v-model:value="form.formId"
        />
      </a-form-item>
      <a-form-item label="支付的用户id" name="payUserId">
        <a-input
          allow-clear
          placeholder="请输入支付的用户id"
          v-model:value="form.payUserId"
        />
      </a-form-item>
      <a-form-item label="0余额支付，1微信支付，2支付宝支付，3银联支付，4现金支付，5POS机支付，6免费，7积分支付" name="payType">
        <a-input
          allow-clear
          placeholder="请输入0余额支付，1微信支付，2支付宝支付，3银联支付，4现金支付，5POS机支付，6免费，7积分支付"
          v-model:value="form.payType"
        />
      </a-form-item>
      <a-form-item label="微信支付子类型：JSAPI小程序支付，NATIVE扫码支付" name="wechatPayType">
        <a-input
          allow-clear
          placeholder="请输入微信支付子类型：JSAPI小程序支付，NATIVE扫码支付"
          v-model:value="form.wechatPayType"
        />
      </a-form-item>
      <a-form-item label="0余额支付，1微信支付，2支付宝支付，3银联支付，4现金支付，5POS机支付，6免费，7积分支付" name="friendPayType">
        <a-input
          allow-clear
          placeholder="请输入0余额支付，1微信支付，2支付宝支付，3银联支付，4现金支付，5POS机支付，6免费，7积分支付"
          v-model:value="form.friendPayType"
        />
      </a-form-item>
      <a-form-item label="0未付款，1已付款" name="payStatus">
        <a-input
          allow-clear
          placeholder="请输入0未付款，1已付款"
          v-model:value="form.payStatus"
        />
      </a-form-item>
      <a-form-item label="0未使用，1已完成，2已取消，3取消中，4退款申请中，5退款被拒绝，6退款成功，7客户端申请退款" name="orderStatus">
        <a-input
          allow-clear
          placeholder="请输入0未使用，1已完成，2已取消，3取消中，4退款申请中，5退款被拒绝，6退款成功，7客户端申请退款"
          v-model:value="form.orderStatus"
        />
      </a-form-item>
      <a-form-item label="发货状态(10未发货 20已发货 30部分发货)" name="deliveryStatus">
        <a-input
          allow-clear
          placeholder="请输入发货状态(10未发货 20已发货 30部分发货)"
          v-model:value="form.deliveryStatus"
        />
      </a-form-item>
      <a-form-item label="无需发货备注" name="deliveryNote">
        <a-input
          allow-clear
          placeholder="请输入无需发货备注"
          v-model:value="form.deliveryNote"
        />
      </a-form-item>
      <a-form-item label="发货时间" name="deliveryTime">
        <a-input
          allow-clear
          placeholder="请输入发货时间"
          v-model:value="form.deliveryTime"
        />
      </a-form-item>
      <a-form-item label="评价状态(0未评价 1已评价)" name="evaluateStatus">
        <a-input
          allow-clear
          placeholder="请输入评价状态(0未评价 1已评价)"
          v-model:value="form.evaluateStatus"
        />
      </a-form-item>
      <a-form-item label="评价时间" name="evaluateTime">
        <a-input
          allow-clear
          placeholder="请输入评价时间"
          v-model:value="form.evaluateTime"
        />
      </a-form-item>
      <a-form-item label="优惠类型：0无、1抵扣优惠券、2折扣优惠券、3、VIP月卡、4VIP年卡，5VIP次卡、6VIP会员卡、7IC月卡、8IC年卡、9IC次卡、10IC会员卡、11免费订单、12VIP充值卡、13IC充值卡、14VIP季卡、15IC季卡" name="couponType">
        <a-input
          allow-clear
          placeholder="请输入优惠类型：0无、1抵扣优惠券、2折扣优惠券、3、VIP月卡、4VIP年卡，5VIP次卡、6VIP会员卡、7IC月卡、8IC年卡、9IC次卡、10IC会员卡、11免费订单、12VIP充值卡、13IC充值卡、14VIP季卡、15IC季卡"
          v-model:value="form.couponType"
        />
      </a-form-item>
      <a-form-item label="优惠说明" name="couponDesc">
        <a-input
          allow-clear
          placeholder="请输入优惠说明"
          v-model:value="form.couponDesc"
        />
      </a-form-item>
      <a-form-item label="二维码地址，保存订单号，支付成功后才生成" name="qrcode">
        <a-input
          allow-clear
          placeholder="请输入二维码地址，保存订单号，支付成功后才生成"
          v-model:value="form.qrcode"
        />
      </a-form-item>
      <a-form-item label="vip月卡年卡、ic月卡年卡回退次数" name="returnNum">
        <a-input
          allow-clear
          placeholder="请输入vip月卡年卡、ic月卡年卡回退次数"
          v-model:value="form.returnNum"
        />
      </a-form-item>
      <a-form-item label="vip充值回退金额" name="returnMoney">
        <a-input
          allow-clear
          placeholder="请输入vip充值回退金额"
          v-model:value="form.returnMoney"
        />
      </a-form-item>
      <a-form-item label="预约详情开始时间数组" name="startTime">
        <a-input
          allow-clear
          placeholder="请输入预约详情开始时间数组"
          v-model:value="form.startTime"
        />
      </a-form-item>
      <a-form-item label="是否已开具发票：0未开发票，1已开发票，2不能开具发票" name="isInvoice">
        <a-input
          allow-clear
          placeholder="请输入是否已开具发票：0未开发票，1已开发票，2不能开具发票"
          v-model:value="form.isInvoice"
        />
      </a-form-item>
      <a-form-item label="发票流水号" name="invoiceNo">
        <a-input
          allow-clear
          placeholder="请输入发票流水号"
          v-model:value="form.invoiceNo"
        />
      </a-form-item>
      <a-form-item label="商家留言" name="merchantRemarks">
        <a-input
          allow-clear
          placeholder="请输入商家留言"
          v-model:value="form.merchantRemarks"
        />
      </a-form-item>
      <a-form-item label="支付时间" name="payTime">
        <a-input
          allow-clear
          placeholder="请输入支付时间"
          v-model:value="form.payTime"
        />
      </a-form-item>
      <a-form-item label="退款时间" name="refundTime">
        <a-input
          allow-clear
          placeholder="请输入退款时间"
          v-model:value="form.refundTime"
        />
      </a-form-item>
      <a-form-item label="申请退款时间" name="refundApplyTime">
        <a-input
          allow-clear
          placeholder="请输入申请退款时间"
          v-model:value="form.refundApplyTime"
        />
      </a-form-item>
      <a-form-item label="过期时间" name="expirationTime">
        <a-input
          allow-clear
          placeholder="请输入过期时间"
          v-model:value="form.expirationTime"
        />
      </a-form-item>
      <a-form-item label="自提码" name="selfTakeCode">
        <a-input
          allow-clear
          placeholder="请输入自提码"
          v-model:value="form.selfTakeCode"
        />
      </a-form-item>
      <a-form-item label="是否已收到赠品" name="hasTakeGift">
        <a-input
          allow-clear
          placeholder="请输入是否已收到赠品"
          v-model:value="form.hasTakeGift"
        />
      </a-form-item>
      <a-form-item label="对账情况：0=未对账；1=已对账；3=已对账，金额对不上；4=未查询到该订单" name="checkBill">
        <a-input
          allow-clear
          placeholder="请输入对账情况：0=未对账；1=已对账；3=已对账，金额对不上；4=未查询到该订单"
          v-model:value="form.checkBill"
        />
      </a-form-item>
      <a-form-item label="订单是否已结算(0未结算 1已结算)" name="isSettled">
        <a-input
          allow-clear
          placeholder="请输入订单是否已结算(0未结算 1已结算)"
          v-model:value="form.isSettled"
        />
      </a-form-item>
      <a-form-item label="系统版本号 0当前版本 value=其他版本" name="version">
        <a-input
          allow-clear
          placeholder="请输入系统版本号 0当前版本 value=其他版本"
          v-model:value="form.version"
        />
      </a-form-item>
      <a-form-item label="用户id" name="userId">
        <a-input
          allow-clear
          placeholder="请输入用户id"
          v-model:value="form.userId"
        />
      </a-form-item>
      <a-form-item label="备注" name="comments">
        <a-textarea
          :rows="4"
          :maxlength="200"
          placeholder="请输入描述"
          v-model:value="form.comments"
        />
      </a-form-item>
      <a-form-item label="排序号" name="sortNumber">
        <a-input-number
          :min="0"
          :max="9999"
          class="ele-fluid"
          placeholder="请输入排序号"
          v-model:value="form.sortNumber"
        />
      </a-form-item>
      <a-form-item label="是否删除, 0否, 1是" name="deleted">
        <a-input
          allow-clear
          placeholder="请输入是否删除, 0否, 1是"
          v-model:value="form.deleted"
        />
      </a-form-item>
      <a-form-item label="修改时间" name="updateTime">
        <a-input
          allow-clear
          placeholder="请输入修改时间"
          v-model:value="form.updateTime"
        />
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { assignObject, uuid } from 'ele-admin-pro';
  import { addCmsOrder, updateCmsOrder } from '@/api/cms/cmsOrder';
  import { CmsOrder } from '@/api/cms/cmsOrder/model';
  import { useThemeStore } from '@/store/modules/theme';
  import { storeToRefs } from 'pinia';
  import { ItemType } from 'ele-admin-pro/es/ele-image-upload/types';
  import { FormInstance } from 'ant-design-vue/es/form';
  import { FileRecord } from '@/api/system/file/model';

  // 是否是修改
  const isUpdate = ref(false);
  const useForm = Form.useForm;
  // 是否开启响应式布局
  const themeStore = useThemeStore();
  const { styleResponsive } = storeToRefs(themeStore);

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 修改回显的数据
    data?: CmsOrder | null;
  }>();

  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 提交状态
  const loading = ref(false);
  // 是否显示最大化切换按钮
  const maxable = ref(true);
  // 表格选中数据
  const formRef = ref<FormInstance | null>(null);
  const images = ref<ItemType[]>([]);

  // 用户信息
  const form = reactive<CmsOrder>({
    id: undefined,
    type: undefined,
    title: undefined,
    company: undefined,
    content: undefined,
    orderNo: undefined,
    deliveryType: undefined,
    channel: undefined,
    transactionId: undefined,
    refundOrder: undefined,
    merchantId: undefined,
    merchantName: undefined,
    merchantCode: undefined,
    couponId: undefined,
    cardId: undefined,
    adminId: undefined,
    confirmId: undefined,
    icCard: undefined,
    realName: undefined,
    addressId: undefined,
    address: undefined,
    addressLat: undefined,
    addressLng: undefined,
    selfTakeMerchantId: undefined,
    selfTakeMerchantName: undefined,
    sendStartTime: undefined,
    sendEndTime: undefined,
    expressMerchantId: undefined,
    expressMerchantName: undefined,
    totalPrice: undefined,
    reducePrice: undefined,
    payPrice: undefined,
    price: undefined,
    money: undefined,
    cancelTime: undefined,
    cancelReason: undefined,
    refundMoney: undefined,
    coachPrice: undefined,
    totalNum: undefined,
    coachId: undefined,
    formId: undefined,
    payUserId: undefined,
    payType: undefined,
    wechatPayType: undefined,
    friendPayType: undefined,
    payStatus: undefined,
    orderStatus: undefined,
    deliveryStatus: undefined,
    deliveryNote: undefined,
    deliveryTime: undefined,
    evaluateStatus: undefined,
    evaluateTime: undefined,
    couponType: undefined,
    couponDesc: undefined,
    qrcode: undefined,
    returnNum: undefined,
    returnMoney: undefined,
    startTime: undefined,
    isInvoice: undefined,
    invoiceNo: undefined,
    merchantRemarks: undefined,
    payTime: undefined,
    refundTime: undefined,
    refundApplyTime: undefined,
    expirationTime: undefined,
    selfTakeCode: undefined,
    hasTakeGift: undefined,
    checkBill: undefined,
    isSettled: undefined,
	    version: undefined,
	    userId: undefined,
	    deleted: undefined,
	    tenantId: undefined,
	    updateTime: undefined,
	    createTime: undefined,
    cmsOrderId: undefined,
    cmsOrderName: '',
    status: 0,
    comments: '',
    sortNumber: 100
  });

  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  // 表单验证规则
  const rules = reactive({
    cmsOrderName: [
      {
        required: true,
        type: 'string',
        message: '请填写网站订单名称',
        trigger: 'blur'
      }
    ]
  });

  const chooseImage = (data: FileRecord) => {
    images.value.push({
      uid: data.id,
      url: data.path,
      status: 'done'
    });
    form.image = data.path;
  };

  const onDeleteItem = (index: number) => {
    images.value.splice(index, 1);
    form.image = '';
  };

  const { resetFields } = useForm(form, rules);

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const formData = {
          ...form
        };
        const saveOrUpdate = isUpdate.value ? updateCmsOrder : addCmsOrder;
        saveOrUpdate(formData)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        images.value = [];
        if (props.data) {
          assignObject(form, props.data);
          if(props.data.image){
            images.value.push({
              uid: uuid(),
              url: props.data.image,
              status: 'done'
            })
          }
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    },
    { immediate: true }
  );
</script>
