<template>
  <PageWrapper dense fixedHeight contentFullHeight>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" v-auth="'deviceInspect:add'" @click="handleCreate">
          {{ t('新增') }}
        </a-button>
        <a-button type="primary" v-auth="'deviceInspect:export'" @click="handleExport" class="ml-4">
          {{ t('导出') }}
        </a-button></template
      >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex == 'state'">
          <span :style="`color:${record.state === 1 ? '#67c23a' : '#F56C6C'}`">
            {{ record.state === 1 ? '已完成' : '待维修' }}
          </span>
        </template>
        <template v-if="column.dataIndex == 'action'">
          <TableAction
            :actions="[
              {
                label: '查看',
                auth: 'deviceInspect:view',
                onClick: handleView.bind(null, record),
              },
              {
                label: '编辑',
                auth: 'deviceInspect:edit',
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '删除',
                auth: 'deviceInspect:delete',
                color: 'error',
                onClick: handleDelete.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <InspectModal @register="registerModal" @success="reload" />
    <InspectInfomation @register="registerCheckModal" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { createVNode } from 'vue';
  import { BasicTable, useTable, TableAction, BasicColumn, FormSchema } from '/@/components/Table';

  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { PageWrapper } from '/@/components/Page';
  import {
    getDeviceInspectPageList,
    deleteDeviceInspect,
    exportInfo,
  } from '/@/api/erp/device/inspect';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import { downloadByData } from '/@/utils/file/download';
  import InspectModal from './components/InspectModal.vue';
  import InspectInfomation from './components/InspectInfomation.vue';
  import { usePermission } from '/@/hooks/web/usePermission';

  const { t } = useI18n();
  const columns: BasicColumn[] = [
    {
      title: '设备编号',
      dataIndex: 'number',
      resizable: true,
    },
    {
      title: '设备名称',
      dataIndex: 'name',
      resizable: true,
    },
    {
      title: '设备类型',
      dataIndex: 'typeName',
      width: 100,
      resizable: true,
    },
    {
      title: '规格型号',
      dataIndex: 'model',
      width: 100,
      resizable: true,
    },
    {
      title: '设备位置',
      dataIndex: 'address',
      resizable: true,
    },
    {
      title: '巡检结果',
      dataIndex: 'result',
      resizable: true,
    },
    {
      title: '巡检人',
      dataIndex: 'checkedUserNames',
      resizable: true,
    },
    {
      title: '巡检时间',
      dataIndex: 'date',
      width: 100,
      resizable: true,
    },
    {
      title: '巡检状态',
      dataIndex: 'state',
      width: 100,
      resizable: true,
    },
  ];

  const searchFormSchema: FormSchema[] = [
    {
      field: 'typeId',
      label: '设备类型',
      component: 'XjrSelect',
      colProps: { span: 8 },
      componentProps: {
        datasourceType: 'dic',
        params: { itemId: '1671412696707850241' },
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择设备类型',
        getPopupContainer: () => document.body,
      },
    },
    {
      field: 'name',
      label: '设备名称',
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: '请输入设备类型',
      },
    },
    {
      field: 'state',
      label: '状态',
      component: 'Select',
      colProps: { span: 8 },
      componentProps: {
        placeholder: '请选择状态',
        options: [
          { label: '已完成', value: 1 },
          { label: '未维修', value: 0 },
        ],
      },
    },
  ];

  const { notification } = useMessage();
  const { hasPermission } = usePermission();

  const [registerModal, { openModal }] = useModal();
  const [registerCheckModal, { openModal: openCheckModal }] = useModal();

  const [registerTable, { reload }] = useTable({
    title: '设备巡检',
    api: getDeviceInspectPageList,
    rowKey: 'id',
    columns,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
      showResetButton: false,
    },
    useSearchForm: true,
    showTableSetting: true,
    striped: false,
    actionColumn: {
      width: 132,
      title: t('操作'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
    customRow: (record) => {
      return {
        ondblclick: () => {
          if (hasPermission('deviceInspect:edit')) {
            handleEdit(record);
          }
        },
      };
    },
    tableSetting: {
      size: false,
    },
  });

  const handleCreate = () => {
    openModal(true, {
      isUpdate: false,
    });
  };

  const handleEdit = (record) => {
    openModal(true, {
      id: record.id,
      isUpdate: true,
    });
  };

  const handleView = (record) => {
    openCheckModal(true, {
      id: record.id,
    });
  };

  const handleDelete = (record) => {
    Modal.confirm({
      title: t('提示信息'),
      icon: createVNode(ExclamationCircleOutlined),
      content: t('是否确认删除？'),
      okText: t('确认'),
      cancelText: t('取消'),
      async onOk() {
        await deleteDeviceInspect(record.id);
        notification.success({
          message: t('提示'),
          description: t('删除成功'),
        });
        reload();
      },
      onCancel() {},
    });
  };
  const handleExport = async () => {
    const res = await exportInfo();
    downloadByData(
      res.data,
      '设备巡检.xlsx',
      'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    );
  };
</script>
