<template>
  <BasicModal
    v-bind="$attrs"
    :title="title"
    @register="registerInnerModal"
    @ok="handleSubmit"
    :width="1000"
  >
    <Row>
      <Col :span="14">
        <BasicTable @register="registerTable"> </BasicTable>
      </Col>
      <Col :span="10">
        <p style="font-weight: bolder">填写派发数据</p>
        <BasicForm @register="registerForm" />
      </Col>
    </Row>
  </BasicModal>
</template>

<script setup lang="ts">
import { BasicModal, useModalInner } from "@/components/Modal";
import { BasicTable, useTable, TableAction } from "@/components/Table";
import { computed, ref, unref } from "vue";
import { Row, Col, Space, Avatar } from "ant-design-vue";
import { infoList } from "@/api/basicInfo/elevator";
import { formEvaSchemas, evaColumns, modalEvaSchemas } from "./workData.data";
import { BasicForm, useForm } from "@/components/Form";
import { useMessage } from "@/hooks/web/useMessage";
import { workDataDistribute } from "@/api/workPlace/workInfo";
defineOptions({ name: "WorkDataModal" });
const { createMessage } = useMessage();
const emit = defineEmits(["register", "reload"]);

const isUpdate = ref<number>(1);
const title = computed<string>(() => {
  return "派发工作";
});
const [registerTable, { getSelectRows }] = useTable({
  rowSelection: {
    type: "checkbox",
  },
  title: "电梯列表",
  showTableSetting: false,
  api: infoList,
  showIndexColumn: true,
  rowKey: "id",
  useSearchForm: true,
  formConfig: {
    schemas: formEvaSchemas,
    baseColProps: {
      xs: 24,
      sm: 24,
      md: 24,
      lg: 15,
    },
  },
  columns: evaColumns,
  // 需要添加上部门参数
});
const [registerInnerModal, { modalLoading, closeModal }] = useModalInner(
  async (data: { record?: Recordable; update: number }) => {
    modalLoading(true);
    const { record, update } = data;
    isUpdate.value = update;

    modalLoading(false);
  }
);
const [registerForm, { setFieldsValue, resetForm, validate }] = useForm({
  labelWidth: 100,
  showActionButtonGroup: false,
  baseColProps: { span: 24 },
  schemas: modalEvaSchemas,
});
async function handleSubmit() {
  try {
    modalLoading(true);
    const data = await validate();
    const elevatorList = getSelectRows().map((item) => {
      return {
        id: item.id,
        name: item.elevatorName,
      };
    });

    if (elevatorList.length <= 0) {
      createMessage.error("请选择电梯");
      return;
    }
    const passData = {
      ...data,
      elevatorList,
    };
    console.log(passData);
    await workDataDistribute(passData);
    emit("reload");
    closeModal();
    await resetForm();
  } catch (e) {
    console.warn(e);
  } finally {
    modalLoading(false);
  }
}
</script>

<style scoped></style>
