<template>
  <ResizePageWrapper>
    <template #resizeLeft>
      <DeptTree @select="handleSelect" />
    </template>

    <template #resizeRight>
      <BasicTable @register="registerTable">
        <template #toolbar>
          <a-button type="error" v-auth="'log:empty'" @click="handleClear" class="mr-4">{{
            t('清空')
          }}</a-button>
          <a-button type="primary" v-auth="'log:export'" @click="handleExport" class="mr-4">{{
            t('导出')
          }}</a-button>
          <a-button type="primary" v-auth="'log:batchDelete'" @click="handleDelete">{{
            t('批量删除')
          }}</a-button>
        </template>
      </BasicTable>
    </template>
    <AccountModal @register="registerModal" @success="handleSuccess" />
  </ResizePageWrapper>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicTable, useTable, FormSchema, BasicColumn } from '/@/components/Table';

  // import { PageWrapper } from '/@/components/Page';
  import { ResizePageWrapper } from '/@/components/Page';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { downloadByData } from '/@/utils/file/download';
  import { useModal } from '/@/components/Modal';
  import AccountModal from './components/logModal.vue';
  import DeptTree from './components/DeptTree.vue';
  import { deleteLog, exportLog, getLogPageList } from '/@/api/system/log';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  export const searchFormSchema: FormSchema[] = [
    {
      field: 'keyword',
      label: t('关键字'),
      component: 'Input',
      componentProps: {
        placeholder: t('请输入要查询的关键字'),
      },
      colProps: { lg: 8, md: 24 },
    },
    {
      field: 'createTime',
      label: t('日期'),
      component: 'RangePicker',
      colProps: { lg: 8, md: 24 },
    },
  ];

  export const columns: BasicColumn[] = [
    {
      title: t('操作时间'),
      dataIndex: 'createTime',
      width: 180,
      //sorter: true,
      align: 'center',
      resizable: true,
    },
    {
      title: t('操作用户'),
      dataIndex: 'username',
      width: 120,
      //sorter: true,
      align: 'center',
      resizable: true,
    },
    {
      title: t('操作类型'),
      dataIndex: 'operation',
      // sorter: true,
      align: 'center',
      resizable: true,
    },
  ];

  export const formColumns: BasicColumn[] = [
    {
      title: t('操作时间'),
      dataIndex: 'createTime',
      // sorter: true,
      align: 'center',
      resizable: true,
    },
    {
      title: t('操作用户'),
      dataIndex: 'username',
      // sorter: true,
      align: 'center',
      resizable: true,
    },
    {
      title: t('IP地址'),
      dataIndex: 'ip',
      // sorter: true,
      align: 'center',
      resizable: true,
    },
    {
      title: t('功能名称'),
      dataIndex: 'method',
      // sorter: true,
      align: 'center',
      resizable: true,
    },
    {
      title: t('操作类型'),
      dataIndex: 'operation',
      width: 400,
      // sorter: true,
      align: 'center',
      resizable: true,
    },
  ];
  export default defineComponent({
    name: 'LogManagement',
    components: { BasicTable, ResizePageWrapper, DeptTree, AccountModal },
    setup() {
      const { notification } = useMessage();

      const selectDeptId = ref(1);

      const [registerModal, { openModal }] = useModal();
      const [registerTable, { reload, getSelectRowKeys, setProps }] = useTable({
        title: t('登录日志列表'),
        api: getLogPageList,
        rowKey: 'id',
        columns,
        formConfig: {
          rowProps: {
            gutter: 16,
          },
          schemas: searchFormSchema,
          fieldMapToTime: [
            ['createTime', ['createTimeStart', 'createTimeEnd'], 'YYYY-MM-DD', false],
          ],
          actionColOptions: { span: 24 },
          showResetButton: false,
        },
        rowSelection: {
          type: 'checkbox',
        },
        beforeFetch: (params) => {
          //发送请求默认新增  左边树结构所选机构id
          return { ...params, category: selectDeptId.value };
        },
        useSearchForm: true,
        showTableSetting: true,
        striped: false,
        tableSetting: {
          size: false,
        },
      });
      function handleClear() {
        openModal(true);
      }
      async function handleExport() {
        let rows = warning();
        if (!rows) {
          return;
        }
        try {
          let res = await exportLog(rows);

          downloadByData(
            res.data,
            t('日志列表.xlsx'),
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
          );
        } catch (error) {}
      }

      function handleDelete() {
        let rows = warning();
        if (!rows) {
          return;
        }
        deleteLog(rows).then(() => {
          notification.success({
            message: t('批量删除日志成功'),
            description: t('成功'),
          });
          handleSuccess();
        });
      }
      function warning() {
        const selectRows = getSelectRowKeys();
        if (selectRows.length === 0) {
          notification.warning({
            message: t('警告'),
            description: t('请选择一条数据！'),
          }); //提示消息
          return false;
        } else {
          return selectRows;
        }
      }
      function handleSuccess() {
        reload({ searchInfo: { category: selectDeptId.value } });
      }

      function handleSelect(dept) {
        selectDeptId.value = dept.key;
        setProps({
          title: dept.title + t('列表'),
          columns: dept.key === '6' ? formColumns : columns,
        });
        handleSuccess();
      }

      return {
        registerTable,
        registerModal,
        handleExport,

        handleDelete,
        handleSuccess,
        handleSelect,

        handleClear,
        t,
      };
    },
  });
</script>
