<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    :width="680"
    :visible="visible"
    :confirm-loading="loading"
    :title="isUpdate ? '编辑订单' : '添加订单'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-space>
      <a-form
        :label-col="{ md: { span: 7 }, sm: { span: 4 }, xs: { span: 24 } }"
        :wrapper-col="{ md: { span: 17 }, sm: { span: 20 }, xs: { span: 24 } }"
      >
        <a-row :gutter="16">
          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item label="订单号" v-bind="validateInfos.outTradeNo">
              <a-input
                allow-clear
                :maxlength="20"
                placeholder="请输入订单号"
                v-model:value="form.outTradeNo"
                @blur="
                  validate('outTradeNo', { trigger: 'blur' }).catch(() => {})
                "
              />
            </a-form-item>
            <a-form-item label="客户ID" v-bind="validateInfos.buyerId">
              <a-input
                allow-clear
                :maxlength="20"
                placeholder="请输入客户ID"
                v-model:value="form.buyerId"
              />
            </a-form-item>
            <a-form-item label="设备ID" v-bind="validateInfos.buyerId">
              <a-input
                allow-clear
                :maxlength="20"
                placeholder="请输入设备ID"
                v-model:value="form.merchantCode"
              />
            </a-form-item>
            <a-form-item label="支付标识" v-bind="validateInfos.code">
              <a-input
                allow-clear
                :maxlength="20"
                placeholder="支付标识"
                v-model:value="form.code"
              />
            </a-form-item>
            <a-form-item label="数量" v-bind="validateInfos.amount">
              <a-input
                allow-clear
                :maxlength="20"
                placeholder="数量"
                v-model:value="form.amount"
              />
            </a-form-item>
            <a-form-item label="支付时间" v-bind="validateInfos.payTime">
              <a-input
                allow-clear
                :maxlength="20"
                placeholder="支付时间"
                v-model:value="form.payTime"
              />
            </a-form-item>
            <a-form-item label="订单状态" v-bind="validateInfos.orderStatus">
              <a-input
                allow-clear
                :maxlength="20"
                placeholder="订单状态"
                v-model:value="form.orderStatus"
              />
            </a-form-item>
            <a-form-item label="商户名称" v-bind="validateInfos.merchantName">
              <a-input
                allow-clear
                :maxlength="20"
                placeholder="商户名称"
                v-model:value="form.merchantName"
              />
            </a-form-item>
            <a-form-item label="手机号码" v-bind="validateInfos.mobile">
              <a-input
                allow-clear
                :maxlength="20"
                placeholder="手机号码"
                v-model:value="form.mobile"
              />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item label="消息" v-bind="validateInfos.msg">
              <a-input
                allow-clear
                :maxlength="20"
                placeholder="消息"
                v-model:value="form.msg"
              />
            </a-form-item>
            <a-form-item label="支付方式" v-bind="validateInfos.payType">
              <a-input
                allow-clear
                :maxlength="20"
                placeholder="支付方式"
                v-model:value="form.payType"
              />
            </a-form-item>
            <a-form-item label="交易金额" v-bind="validateInfos.totalAmount">
              <a-input
                allow-clear
                :maxlength="20"
                placeholder="交易金额"
                v-model:value="form.totalAmount"
              />
            </a-form-item>
            <a-form-item label="实收金额" v-bind="validateInfos.receiptAmount">
              <a-input
                allow-clear
                :maxlength="20"
                placeholder="实收金额"
                v-model:value="form.receiptAmount"
              />
            </a-form-item>
            <a-form-item label="手续费" v-bind="validateInfos.feeAmount">
              <a-input
                allow-clear
                :maxlength="20"
                placeholder="第三方交易手续费"
                v-model:value="form.feeAmount"
              />
            </a-form-item>
            <a-form-item label="结算金额" v-bind="validateInfos.settleAmount">
              <a-input
                allow-clear
                :maxlength="20"
                placeholder="第三方结算金额"
                v-model:value="form.settleAmount"
              />
            </a-form-item>
            <a-form-item label="租户编号" v-bind="validateInfos.tenantId">
              <a-input
                allow-clear
                :maxlength="20"
                placeholder="租户编号"
                v-model:value="form.tenantId"
              />
            </a-form-item>
            <a-form-item label="备注" v-bind="validateInfos.orderRemark">
              <a-textarea
                :rows="4"
                :maxlength="200"
                placeholder="请输入备注"
                v-model:value="form.orderRemark"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-space>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { assignObject } from 'ele-admin-pro';
  import { addCashier, updateCashier } from '@/api/apps/cashier';
  import type { Cashier } from '@/api/apps/cashier/model';

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

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

  // 提交状态
  const loading = ref(false);

  // 用户信息
  const form = reactive<Cashier>({
    cashierId: 0,
    outTradeNo: '',
    buyerId: '',
    payType: '',
    code: '',
    amount: undefined,
    totalAmount: undefined,
    feeAmount: undefined,
    receiptAmount: undefined,
    settleAmount: undefined,
    orderRemark: '',
    orderStatus: '',
    payTime: '',
    merchantName: '',
    mobile: '',
    msg: '',
    tenantId: 2,
    sign: 'dk9mci8mcQ5Wa4xWRiojEjI6IibNJCLvl2cyVmdiwiIgwQ0NRNkICL0kjNIzojI0NWZqJWd6ICZpJISOuRpnM1JyetRWQlf0NW=='
  });

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

  // 表单验证规则
  const rules = reactive({
    outTradeNo: [
      {
        required: true,
        type: 'string',
        message: '请输入订单号',
        trigger: 'blur'
      }
    ]
  });

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

  /* 保存编辑 */
  const save = () => {
    validate()
      .then(() => {
        loading.value = true;
        const data = {
          ...form
        };
        // 转字符串
        const saveOrUpdate = isUpdate.value ? updateCashier : addCashier;
        saveOrUpdate(data)
          .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) {
        if (props.data) {
          loading.value = false;
          assignObject(form, props.data);
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    }
  );
</script>
<style lang="less"></style>
