<!-- 编辑弹窗 -->
<template>
  <ele-modal
    :width="1000"
    :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="订单编号" name="orderNo">
        <a-input
          allow-clear
          placeholder="请输入订单编号"
          :disabled="true"
          v-model:value="form.orderNo"
        />
      </a-form-item>
      <a-form-item label="微信支付订单号" name="transactionId">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入微信支付订单号"
          v-model:value="form.transactionId"
        />
      </a-form-item>
      <a-form-item label="微信退款订单号" name="refundOrder">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入微信退款订单号"
          v-model:value="form.refundOrder"
        />
      </a-form-item>
      <a-form-item label="商户ID" name="merchantId">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入商户ID"
          v-model:value="form.merchantId"
        />
      </a-form-item>
      <a-form-item label="使用的优惠券id" name="couponId">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入使用的优惠券id"
          v-model:value="form.couponId"
        />
      </a-form-item>
      <a-form-item label="使用的会员卡id" name="cardId">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入使用的会员卡id"
          v-model:value="form.cardId"
        />
      </a-form-item>
      <a-form-item label="IC卡号" name="icCard">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入IC卡号"
          v-model:value="form.icCard"
        />
      </a-form-item>
      <a-form-item label="真实姓名" name="realName">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入真实姓名"
          v-model:value="form.realName"
        />
      </a-form-item>
      <a-form-item label="手机号码" name="phone">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入手机号码"
          v-model:value="form.phone"
        />
      </a-form-item>
      <a-form-item label="订单总额" name="totalPrice">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入订单总额"
          v-model:value="form.totalPrice"
        />
      </a-form-item>
      <a-form-item label="减少的金额" name="reducePrice">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入减少的金额，使用VIP会员折扣、优惠券抵扣、优惠券折扣后减去的价格"
          v-model:value="form.reducePrice"
        />
      </a-form-item>
      <a-form-item label="实际付款" name="payPrice">
        <a-input
          allow-clear
          :disabled="true"
          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="refundMoney" v-if="form.refundMoney">
        <a-input
          allow-clear
          :disabled="true"
          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="教练id" name="coachId">-->
      <!--        <a-input-->
      <!--          allow-clear-->
      <!--          placeholder="请输入教练id"-->
      <!--          v-model:value="form.coachId"-->
      <!--        />-->
      <!--      </a-form-item>-->
      <a-form-item label="支付方式" name="payType">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入支付方式 0余额支付, 1微信支付，2积分，3支付宝，4现金，5POS机，6VIP月卡，7VIP年卡，8VIP次卡，9IC月卡，10IC年卡，11IC次卡，12免费，13VIP充值卡，14IC充值卡，15积分支付，16VIP季卡，17IC季卡"
          v-model:value="form.payType"
        />
      </a-form-item>
      <a-form-item label="支付状态" name="payStatus">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入0未付款，1已付款"
          v-model:value="form.payStatus"
        />
      </a-form-item>
      <a-form-item label="订单状态" name="orderStatus">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入0未使用，1已完成，2已取消，3取消中，4退款申请中，5退款被拒绝，6退款成功，7客户端申请退款"
          v-model:value="form.orderStatus"
        />
      </a-form-item>
      <a-form-item label="优惠类型" name="couponType">
        <a-input
          allow-clear
          :disabled="true"
          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
          :disabled="true"
          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
          :disabled="true"
          placeholder="请输入预约详情开始时间数组"
          v-model:value="form.startTime"
        />
      </a-form-item>
      <a-form-item label="是否已开具发票" name="isInvoice">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入是否已开具发票：0未开发票，1已开发票，2不能开具发票"
          v-model:value="form.isInvoice"
        />
      </a-form-item>
      <a-form-item label="支付时间" name="payTime">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入支付时间"
          v-model:value="form.payTime"
        />
      </a-form-item>
      <a-form-item label="退款时间" name="refundTime">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入退款时间"
          v-model:value="form.refundTime"
        />
      </a-form-item>
      <a-form-item label="申请退款时间" name="refundApplyTime">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入申请退款时间"
          v-model:value="form.refundApplyTime"
        />
      </a-form-item>
      <a-form-item label="对账情况" name="checkBill">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入对账情况：0=未对账；1=已对账；3=已对账，金额对不上；4=未查询到该订单"
          v-model:value="form.checkBill"
        />
      </a-form-item>
      <a-form-item label="订单是否已结算" name="isSettled">
        <a-input
          allow-clear
          :disabled="true"
          placeholder="请输入订单是否已结算(0未结算 1已结算)"
          v-model:value="form.isSettled"
        />
      </a-form-item>
      <a-form-item label="备注" name="comments">
        <a-textarea
          :rows="4"
          :maxlength="200"
          :disabled="true"
          placeholder="请输入描述"
          v-model:value="form.comments"
        />
      </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 { addOrder, updateOrder } from '@/api/shop/order';
  import { Order } from '@/api/shop/order/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?: Order | 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<Order>({
    orderId: undefined,
    orderNo: undefined,
    transactionId: undefined,
    refundOrder: undefined,
    merchantId: undefined,
    couponId: undefined,
    cardId: undefined,
    adminId: undefined,
    confirmId: undefined,
    icCard: undefined,
    realName: undefined,
    phone: undefined,
    totalPrice: undefined,
    reducePrice: undefined,
    payPrice: undefined,
    price: undefined,
    money: undefined,
    refundMoney: undefined,
    coachPrice: undefined,
    coachId: undefined,
    payType: undefined,
    payStatus: undefined,
    orderStatus: undefined,
    couponType: undefined,
    couponDesc: undefined,
    qrcode: undefined,
    returnNum: undefined,
    returnMoney: undefined,
    startTime: undefined,
    isInvoice: undefined,
    payTime: undefined,
    refundTime: undefined,
    refundApplyTime: undefined,
    checkBill: undefined,
    isSettled: undefined,
    version: undefined,
    userId: undefined,
    deleted: undefined,
    tenantId: undefined,
    updateTime: undefined,
    createTime: undefined,
    status: 0,
    comments: '',
    sortNumber: 100
  });

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

  // 表单验证规则
  const rules = reactive({
    orderName: [
      {
        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 ? updateOrder : addOrder;
        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>
