<template>
  <div style="background-color: #f0f2f5">
    <BasicTable @register="registerTable">
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '详情',
              icon: 'ant-design:eye-outlined',
              onClick: handleDetail.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <AlarmDetailModal @register="registerDetailModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { alarmColumns, alarmSearchSchemas } from '../../config/detail.config';
  import { getDeviceAlarm } from '/@/api/device/deviceManager';
  import AlarmDetailModal from '../modal/AlarmDetailModal.vue';
  import { useModal } from '/@/components/Modal';
  export default defineComponent({
    name: 'DeviceManagement',
    components: {
      BasicTable,
      TableAction,
      AlarmDetailModal,
    },
    props: {
      id: {
        type: String,
        required: true,
      },
    },
    setup(props) {
      const [registerTable, { reload }] = useTable({
        api: getDeviceAlarm,
        columns: alarmColumns,
        useSearchForm: true,
        formConfig: {
          labelWidth: 100,
          schemas: alarmSearchSchemas,
          fieldMapToTime: [['alarmTime', ['startTime', 'endTime'], 'x']],
        },
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        beforeFetch: (data) => {
          Reflect.set(data, 'deviceId', props.id);
        },
        actionColumn: {
          width: 200,
          title: '操作',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });
      const [registerDetailModal, { openModal }] = useModal();
      const handleDetail = (record: Recordable) => {
        openModal(true, record);
      };
      const handleSuccess = () => {
        reload();
      };
      return {
        registerTable,
        registerDetailModal,
        handleDetail,
        handleSuccess,
      };
    },
  });
</script>
