<!-- 服务编辑弹窗 -->
<template >
  <ele-modal
    :width="400"
    :visible="visible"
    :confirm-loading="loading"
    :title="'确定强制退租吗？'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <span>订单编号：{{ data.orderNo }}</span>
    <div style="font-size: 12px;color: red;margin-top: 15px">注意：退租成功后，请到退租审核页面进行审核完成最终的退租</div>
    <a-form
      ref="formRef"
      :model="form">
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { Form, message } from 'ant-design-vue';
  import type { FormInstance, Rule } from 'ant-design-vue/es/form';
  import { storeToRefs } from 'pinia';
  import { useThemeStore } from '@/store/modules/theme';
  import useFormData from '@/utils/use-form-data';
  import type { Order } from '@/api/order/model';
  import { rentingOut } from "@/api/order";
  import {assignObject} from "ele-admin-pro";

  // 是否开启响应式布局
  const themeStore = useThemeStore();
  const { styleResponsive } = storeToRefs(themeStore);

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

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

  //
  const formRef = ref<FormInstance | null>(null);

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

  // 表单数据
  const { form } = useFormData<Order>();

  const equipmentCode = ref('')


  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    console.log("===",props.data)
    formRef.value.validate()
      .then(() => {
        loading.value = true
        props.data.isRefund = 3;//强制退租
        rentingOut(props.data)
          .then((msg) => {
            loading.value = false
            message.success("退租成功！请到退租审核页面进行审核");
            emit('done');
            loading.value = false;
          })
          .catch((e) => {
            loading.value = false;
            message.error("退租失败！" + e);
          }).finally(()=>{
          updateVisible(false);
        });
      })
      .catch(() => {});
  };

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

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        if (props.data) {
          assignObject(form, props.data);
        }
      } else {
        formRef.value?.clearValidate();
      }
    }
  );
</script>
