<template>
  <BasicModal
    v-bind="$attrs"
    :title="title"
    @register="registerInnerModal"
    @ok="handleSubmit"
    @cancel="resetForm"
    okText="生成"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script setup lang="ts">
import { BasicModal, useModalInner } from "@/components/Modal";
import { BasicForm, useForm } from "@/components/Form";
import { computed, ref, unref } from "vue";
import { maintenancePlanAdd } from "@/api/basicInfo/maintenanceRecord";
import { modalPlanSchemas } from "./maintenanceRecord.data";

defineOptions({ name: "MaintenanceRecordModal" });

const emit = defineEmits(["register", "reload"]);
const elevatorNum = ref<any>("");
const isUpdate = ref<number>(1);
const title = computed<string>(() => {
  return "生成维保计划";
});

const [registerInnerModal, { modalLoading, closeModal }] = useModalInner(
  async (data: { elevatorInfo }) => {
    modalLoading(true);
    const { elevatorInfo } = data;
    elevatorNum.value = elevatorInfo.elevatorNum;
    modalLoading(false);
  }
);

const [registerForm, { setFieldsValue, resetForm, validate, setProps }] = useForm({
  labelWidth: 100,
  showActionButtonGroup: false,
  baseColProps: { span: 24 },
  schemas: modalPlanSchemas,
});

async function handleSubmit() {
  try {
    modalLoading(true);
    const data = await validate();
    console.log(data, "data");
    const passData = {
      planTime: data.planTime,
      planType: data.planType * 1,
      elevatorNum: elevatorNum.value,
    };
    await maintenancePlanAdd(passData);
    emit("reload");
    closeModal();
    await resetForm();
  } catch (e) {
    console.warn(e);
  } finally {
    modalLoading(false);
  }
}
</script>

<style scoped></style>
