<template>
  <div>
    <BasicTable :clickToRowSelect="false" @register="registerTable">
      <template #toolbar>
        <Authority value="api:yt:device:state:log::delete">
          <Popconfirm
            title="您确定要批量删除数据"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleDeleteOrBatchDelete(null)"
          >
            <a-button color="error" :disabled="hasBatchDelete"> 批量删除 </a-button>
          </Popconfirm>
        </Authority>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '处理',
              auth: 'api:yt:device:state:log::post',
              icon: 'clarity:note-edit-line',
              onClick: handleView.bind(null, record),
            },
            {
              label: '详情',
              auth: 'api:yt:device:state:log::detail',
              icon: 'clarity:note-edit-line',
              onClick: handleViewDetail.bind(null, record),
            },
            {
              label: '删除',
              auth: 'api:yt:device:state:log::delete',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDeleteOrBatchDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <OnlineRecordDrawer @register="registerDrawer" @success="handleSuccess" />
    <OnlineHandleDrawer @register="registerHandleDrawer" @success="handleHandleSuccess" />
  </div>
</template>

<script lang="ts" setup>
  import { nextTick, onMounted } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import OnlineRecordDrawer from './OnlineRecordDrawer.vue';
  import OnlineHandleDrawer from './OnlineHandleDrawer.vue';
  import { deviceStateLogPage, deleteStateLogDevice } from '/@/api/device/deviceManager';
  import { searchFormSchema, columns } from './config.data';
  import { useBatchDelete } from '/@/hooks/web/useBatchDelete';
  import { Popconfirm } from 'ant-design-vue';
  import { Authority } from '/@/components/Authority';
  import { useRoute } from 'vue-router';

  const [registerTable, { setProps, reload, getForm }] = useTable({
    title: '上下线记录',
    immediate: false,
    api: deviceStateLogPage,
    columns,
    showIndexColumn: false,
    clickToRowSelect: false,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      fieldMapToTime: [['queryTime', ['startTime', 'endTime'], 'x']],
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    rowKey: 'id',
    actionColumn: {
      width: 200,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  });

  const handleSuccess = () => {
    reload();
    resetSelectedRowKeys();
  };

  const handleHandleSuccess = () => {
    reload();
    resetSelectedRowKeys();
  };

  const { hasBatchDelete, handleDeleteOrBatchDelete, selectionOptions, resetSelectedRowKeys } =
    useBatchDelete(deleteStateLogDevice, handleSuccess, setProps);

  nextTick(() => {
    setProps(selectionOptions);
  });

  // 弹框
  const [registerDrawer, { openDrawer }] = useDrawer();

  const [registerHandleDrawer, { openDrawer: openHandleDrawer }] = useDrawer();

  const handleViewDetail = (record) => {
    openDrawer(true, {
      isUpdate: true,
      record,
    });
  };
  const handleView = (record) => {
    openHandleDrawer(true, {
      isUpdate: true,
      record,
    });
  };
  const ROUTE = useRoute();

  onMounted(async () => {
    const formActionType = getForm();
    const params = ROUTE.query;
    if (params) {
      await formActionType.setFieldsValue(params);
    }
    await reload();
  });
</script>
