<template>
  <PageWrapper dense>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <!-- <a-button type="primary" @click="showFaultModal">故障上报</a-button> -->
        <a-button
          @click="downloadExcel(exportExcel, '工单信息', getForm().getFieldsValue())"
          >工单导出</a-button
        >
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column && record && column.key === 'action'">
          <a-button type="link" @click="showDrawer(record.id)">详情</a-button>
        </template>
      </template>
    </BasicTable>
    <faultModal ref="falutModalRef" @success="reload()" />
    <detailDrawer ref="detailDrawerRef" />
  </PageWrapper>
</template>

<script setup lang="ts">
  import { PageWrapper } from '@/components/Page';
  import { BasicTable, useTable } from '@/components/Table';
  import { queryPointNames } from '@/api/common/index';
  import { list, getProjectInfo, getSubcategoryType, exportExcel } from './api';
  import { formSchemas, columns } from './data';
  import faultModal from './faultModal.vue';
  import detailDrawer from './detailDrawer.vue';
  import { ref, onActivated } from 'vue';
  import { downloadExcel } from '@/utils/file/download';
  const [registerTable, { getForm, reload }] = useTable({
    title: '工单搜索',
    api: list,
    showIndexColumn: true,
    rowKey: 'id',
    useSearchForm: true,
    formConfig: {
      schemas: formSchemas,
      name: 'orderSearch',
      baseColProps: {
        xs: 24,
        sm: 24,
        md: 24,
        lg: 6,
      },
    },
    columns: columns,
    actionColumn: {
      width: 200,
      title: '操作',
      key: 'action',
      fixed: 'right',
    },
  });
  //弹窗内容
  const falutModalRef = ref();
  const showFaultModal = () => {
    falutModalRef.value.showModal();
  };
  //详情抽屉
  const detailDrawerRef = ref();
  const showDrawer = (id: any) => {
    detailDrawerRef.value.showDrawer(id);
  };
  const projectOptions = ref([]);
  const getProjectOptions = async () => {
    const res = await getProjectInfo();
    res.forEach((i: any) => {
      i.value = i.projectName;
      i.label = i.projectName;
    });
    projectOptions.value = res;
    formSchemas[0].componentProps.options = projectOptions.value;
  };
  const faultSubcategoryOptions = ref([]);
  const getFaultSubcategoryOptions = async () => {
    const res = await getSubcategoryType();
    faultSubcategoryOptions.value = res.rows;
    faultSubcategoryOptions.value.forEach((i: any) => {
      i.value = i.typeName;
      i.label = i.typeName;
    });
    formSchemas[4].componentProps.options = faultSubcategoryOptions.value;
  };
  // const faultLocationOptions = ref([]);
  // const getFaultLocationOptions = async () => {
  //   const res = await queryPointNames();
  //   faultLocationOptions.value = res;
  //   faultLocationOptions.value.forEach((i: any) => {
  //     i.value = i.pointName;
  //     i.label = i.pointName;
  //   });
  //   formSchemas[5].componentProps.options = faultLocationOptions.value;
  // };
  const getOptions = () => {
    getProjectOptions();
    getFaultSubcategoryOptions();
    // getFaultLocationOptions();
  };
  getOptions();
  onActivated(() => {
    reload();
  });
  // 前往审批记录页面
</script>

<style scoped></style>
