<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    :width="500"
    :visible="visible"
    :confirm-loading="loading"
    :maskClosable="false"
    :maxable="maxable"
    :title="isUpdate ? '发货' : '发货'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-space>
      <a-form>
        <a-form-item label="设备编码">
          <a-input
            allow-clear
            :maxlength="30"
            placeholder="请输入设备编码"
            v-model:value="form.equipmentCode"/>
        </a-form-item>

        <a-form-item label="租用人">
          <span>
            {{props.data.realName}}
          </span>
        </a-form-item>

        <a-form-item label="续期时间">
          <a-range-picker
            v-model:value="dateRange"
            value-format="YYYY-MM-DD"
            class="ele-fluid"
          />
        </a-form-item>

        <a-form-item label="续期费用">
          <a-input-number :min="0" :max="9999999" v-model:value="form.orderPrice"/>
          <span style="marginleft: 10px"> 元</span>
        </a-form-item>
      </a-form>
    </a-space>
  </ele-modal>
</template>

<script lang="ts" setup>
import {ref, reactive, watch, computed} from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { assignObject } from 'ele-admin-pro';
  import TypeSelect from './customer-edit/type-select.vue';
  import ProgressSelect from './customer-edit/progress-select.vue';
  import SourceSelect from './customer-edit/source-select.vue';
  import { bindEquipment } from '@/api/apps/equipment';
  import type { Customer } from '@/api/oa/customer/model';
  import { createCode } from '@/utils/common';
  import { uploadFile } from '@/api/system/file';
  import type { ItemType } from 'ele-admin-pro/es/ele-image-upload/types';
  import { FILE_SERVER } from '@/config/setting';
  import { useUserStore } from '@/store/modules/user';
import { Equipment } from "@/api/apps/equipment/model";
import {Order, OrderPay} from "@/api/order/model";
import {addOrderPay} from "@/api/order";

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

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

  // 提交状态
  const loading = ref(false);
  // 是否显示最大化切换按钮
  const maxable = ref(true);

  // 表单数据
  const form = reactive<OrderPay>({
    rentOrderId:undefined,
    orderNo:undefined,
    equipmentCode: '',
    orderId: undefined,
    startTime:undefined,
    expirationTime:undefined,
    //订单金额
    orderPrice:undefined,
    payStatus:20,
  });
  // 日期范围选择
  const dateRange = ref<[string, string]>(['', '']);

  // 已上传数据, 可赋初始值用于回显
  const images = ref(<any>[]);

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

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

  /* 保存编辑 */
  const save = () => {
    const [d1, d2] = dateRange.value ?? [];

    if (dateRange.value[0] === '' || dateRange.value[1] === "" ){
      message.warn("请选择续期时间");
      return;
    }
    form.startTime =d1 + ' 00:00:00';
    form.expirationTime = d2 + ' 23:59:59';
    form.orderNo=props.data?.orderNo;
    form.rentOrderId=props.data?.rentOrderId;

    validate()
      .then(() => {
        loading.value = true;
        // 转字符串
        addOrderPay(form)
          .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>
