<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-auth="'formTemplate:add'">{{
          t('新增')
        }}</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              auth: 'formTemplate:edit',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              auth: 'formTemplate:delete',
              color: 'error',
              popConfirm: {
                title: t('是否确认删除'),
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <TemplateModal
      @register="registerModal"
      @success="handleSuccess"
      @close="handleClose"
      v-if="isShowModal"
    />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { h, ref } from 'vue';
  import { BasicTable, useTable, TableAction, FormSchema, BasicColumn } from '/@/components/Table';
  import { getFormTemplateList, deleteFormTemplate } from '/@/api/form/formTemplate';
  import { PageWrapper } from '/@/components/Page';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useModal } from '/@/components/Modal';
  import TemplateModal from './components/TemplateModal.vue';
  import { usePermission } from '/@/hooks/web/usePermission';

  const { t } = useI18n();
  const searchFormSchema: FormSchema[] = [
    {
      field: 'keyword',
      label: '',
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: '请输入编号或名称',
      },
    },
  ];

  const columns: BasicColumn[] = [
    {
      title: '模板编号',
      dataIndex: 'code',
      align: 'center',
      resizable: true,
    },
    {
      title: '模板名称',
      dataIndex: 'name',
      align: 'center',
      resizable: true,
    },
    {
      title: '模板来源',
      dataIndex: 'source',
      align: 'center',
      resizable: true,
      customRender: ({ record }) => {
        let color = '#1c8dff';
        let text = '新增';
        switch (record.source) {
          case 1:
            color = '#f59a23';
            text = '表单设计';
            break;
          case 2:
            color = '#d6c652';
            text = '代码生成器';
            break;
        }
        return h('span', { style: { color } }, text);
      },
    },
    {
      title: '创建用户',
      dataIndex: 'createUserName',
      align: 'center',
      resizable: true,
    },
    {
      title: '创建时间',
      dataIndex: 'createDate',
      align: 'center',
      resizable: true,
    },
    {
      title: '备注',
      dataIndex: 'remark',
      align: 'center',
      resizable: true,
    },
  ];

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

  const isShowModal = ref<boolean>(true);

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

  function handleClose() {
    isShowModal.value = !isShowModal.value;
    setTimeout(() => {
      isShowModal.value = !isShowModal.value;
    }, 100);
  }

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    openModal(true, {
      id: record.id,
      isUpdate: true,
    });
  }

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

  function handleSuccess() {
    reload();
  }
</script>
