<template>
  <ResizePageWrapper>
    <BasicTable @register="registerTable" isMenuTable>
      <template #toolbar>
        <a-button type="primary" v-auth="'operationManagement:add'" @click="handleCreate">
          {{ t('新增') }}
        </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex == 'action'">
          <TableAction
            :actions="[
              {
                label: '编辑',
                auth: 'operationManagement:edit',
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '删除',
                auth: 'operationManagement:delete',
                color: 'error',
                ifShow: record.code !== 'chinese',
                popConfirm: {
                  title: t('是否确认删除？'),
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <OperationManagementDesign
      :id="state.id"
      v-if="state.show"
      @close="closeOperationManagementDesign"
    />
  </ResizePageWrapper>
</template>
<script lang="ts">
  import { defineComponent, reactive } from 'vue';
  import { ResizePageWrapper } from '/@/components/Page';
  import { BasicTable, useTable, TableAction, BasicColumn, FormSchema } from '/@/components/Table';
  import OperationManagementDesign from './components/Design.vue';

  import { useMessage } from '/@/hooks/web/useMessage';
  import { usePermission } from '/@/hooks/web/usePermission';

  import {
    getOperationManagementPage,
    deleteOperationManagement,
  } from '/@/api/operationManagement/index';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { OperationManagementListModel } from '/@/api/operationManagement/model';
  const { t } = useI18n();
  export const columns: BasicColumn[] = [
    {
      title: t('名称'),
      dataIndex: 'name',
      align: 'center',
      resizable: true,
    },
    {
      title: t('编码'),
      dataIndex: 'code',
      align: 'center',
      resizable: true,
    },
    {
      title: t('创建人'),
      dataIndex: 'createUserName',
      align: 'center',
      resizable: true,
    },
    {
      title: t('创建时间'),
      dataIndex: 'createDate',
      align: 'center',
      resizable: true,
    },
    {
      title: t('备注'),
      dataIndex: 'remark',
      align: 'center',
      resizable: true,
    },
  ];

  export const searchFormSchema: FormSchema[] = [
    {
      field: 'keyword',
      label: '',
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: t('请输入规则名称或应用名称'),
      },
    },
  ];

  export default defineComponent({
    name: 'OperationManagement',
    components: {
      BasicTable,
      TableAction,
      ResizePageWrapper,
      OperationManagementDesign,
    },
    setup() {
      const state = reactive({
        show: false,
        classifyId: '',
        id: '' as string | undefined,
      });
      const { notification } = useMessage();
      const { hasPermission } = usePermission();
      const [registerTable, { reload }] = useTable({
        title: t('运维管理列表'),
        api: getOperationManagementPage,
        columns,
        formConfig: {
          rowProps: {
            gutter: 16,
          },
          schemas: searchFormSchema,
          showResetButton: false,
        },
        useSearchForm: true,
        showTableSetting: true,
        striped: false,
        pagination: {
          hideOnSinglePage: true,
        },
        actionColumn: {
          width: 88,
          title: t('操作'),
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
        tableSetting: {
          size: false,
        },
        customRow: (record) => {
          return {
            ondblclick: () => {
              if (hasPermission('operationManagement:edit')) {
                handleEdit(record);
              }
            },
          };
        },
      });
      function handleSelect(selectIds: Array<string>) {
        state.classifyId = selectIds[0];
        reload();
      }

      function handleCreate() {
        state.id = '';
        state.show = true;
      }

      function handleEdit(record: OperationManagementListModel) {
        state.id = record.id;
        state.show = true;
      }

      function handleDelete(record: Recordable) {
        deleteOperationManagement([record.id]).then((_) => {
          reload();
          notification.success({
            message: t('提示'),
            description: t('删除成功'),
          }); //提示消息
        });
      }

      function handleSuccess() {
        reload();
      }

      function closeOperationManagementDesign() {
        state.show = false;
        reload();
      }
      return {
        state,
        registerTable,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        t,
        closeOperationManagementDesign,
        handleSelect,
      };
    },
  });
</script>
