<template>
  <PageWrapper dense>
    <Row>
      <!-- 左边部门选择 -->
      <Col v-bind="{ xs: 24, sm: 24, md: 24, lg: 4 }" class="h-[calc(100vh-80px)]">
        <MaintenanceRecordDeptTree
          v-model:selectDeptId="selectDeptId"
          v-model:searchValue="searchValue"
          @select="reload"
        />
      </Col>
      <Col v-bind="{ xs: 24, sm: 24, md: 24, lg: 20 }">
        <BasicTable @register="registerTable">
          <template #toolbar> </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'action'">
              <TableAction
                stopButtonPropagation
                :actions="[
                  {
                    label: '维保计划',

                    type: 'primary',
                    ghost: true,
                    auth: 'elevator:maintenancePlan:list',
                    onClick: handCheck.bind(null, record),
                  },
                  {
                    label: '年检计划',

                    type: 'primary',
                    ghost: true,
                    auth: 'elevator:maintenancePlan:annualPlan',
                    onClick: handleYear.bind(null, record),
                  },
                ]"
              />
            </template>
          </template>
        </BasicTable>
      </Col>
    </Row>

    <MaintenanceRecordModal @register="registerModal" @reload="reload" />
    <yearCheckModal @register="registerYearModal" @reload="reload" />
  </PageWrapper>
</template>

<script setup lang="ts">
import { Row, Col } from "ant-design-vue";
import { PageWrapper } from "@/components/Page";
import { BasicTable, useTable, TableAction } from "@/components/Table";
import {
  maintenanceRecordList,
  maintenanceRecordExport,
  maintenanceRecordRemove,
} from "@/api/basicInfo/maintenanceRecord";
import { downloadExcel } from "@/utils/file/download";
import { useModal } from "@/components/Modal";
import MaintenanceRecordModal from "./MaintenanceRecordModal.vue";
import { formSchemas, columns } from "./maintenanceRecord.data";
import { IconEnum } from "@/enums/appEnum";
import MaintenanceRecordDeptTree from "./MaintenanceRecordDeptTree.vue";
import yearCheckModal from "./yearCheckModal.vue";
import { ref, toValue } from "vue";
import { infoList } from "@/api/basicInfo/elevator";
defineOptions({ name: "MaintenanceRecord" });
// 左边部门用
const selectDeptId = ref<string[]>([]);
const searchValue = ref<string>("");
const [registerTable, { reload, multipleRemove, selected, getForm }] = useTable({
  rowSelection: {
    type: "checkbox",
  },
  title: "维保计划列表",
  api: infoList,
  showIndexColumn: true,
  rowKey: "id",
  useSearchForm: true,
  formConfig: {
    schemas: formSchemas,
    baseColProps: {
      xs: 24,
      sm: 24,
      md: 24,
      lg: 6,
    },
    resetFunc: async () => {
      selectDeptId.value = [];
      searchValue.value = "";
      await reload();
    },
  },
  columns: columns,
  // 需要添加上部门参数
  beforeFetch(params: Recordable) {
    if (toValue(selectDeptId).length === 1) {
      params.deptId = toValue(selectDeptId)[0];
    }
    return params;
  },
  actionColumn: {
    width: 200,
    title: "操作",
    key: "action",
    fixed: "right",
  },
});

const [registerModal, { openModal }] = useModal();
const [registerGivenModal, { openModal: openGivenModal }] = useModal();
const [registerYearModal, { openModal: openYearModal }] = useModal();

function handleEdit(record: Recordable) {
  openModal(true, { record, update: 1 });
}
function handleYear(record: Recordable) {
  openYearModal(true, { record, update: 3 });
}
function handleAdd() {
  openGivenModal(true, { update: 2 });
}
function handCheck(record: Recordable) {
  openModal(true, { record, update: 3 });
}
async function handleDelete(record: Recordable) {
  await maintenanceRecordRemove([record.id]);
  await reload();
}
</script>

<style scoped></style>
