<template>
  <PageWrapper dense fixed-height contentFullHeight>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-auth="'bi-menu:add'">{{
          t('新增')
        }}</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              auth: 'bi-menu:edit',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              auth: 'bi-menu:delete',
              color: 'error',
              custom: true,
              onClick: handleDel.bind(null, record),
              popConfirm: {
                visible: curID == record.id,
                confirm: handleDelete.bind(null, record),
                deleteMenu: handleDeleteMenu.bind(null, record),
                cancel: () => {
                  curID = '';
                },
              },
            },
          ]"
        />
      </template>
    </BasicTable>

    <ReleaseModal
      v-if="modalClose"
      @register="registerModal"
      @success="handleSuccess"
      @close="handleClose"
    />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicTable, useTable, TableAction, FormSchema, BasicColumn } from '/@/components/Table';
  import { getBiReleasePage, deleteBiRelease, deleteBiAndMenu } from '/@/api/bi/index';
  import { PageWrapper } from '/@/components/Page';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useModal } from '/@/components/Modal';
  import ReleaseModal from './components/ReleaseModal.vue';
  import { usePermission } from '/@/hooks/web/usePermission';

  const { t } = useI18n();
  const searchFormSchema: FormSchema[] = [
    {
      field: 'keyword',
      label: '',
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: t('请输入关键字'),
      },
    },
  ];

  const columns: BasicColumn[] = [
    {
      dataIndex: 'visualName',
      title: t('BI名称'),
      align: 'center',
    },
    {
      dataIndex: 'menuName',
      title: t('菜单名称'),
      align: 'center',
    },
    {
      dataIndex: 'remark',
      title: t('备注'),
      align: 'center',
    },
  ];
  const { notification } = useMessage();
  const { hasPermission } = usePermission();
  const curID = ref('');
  const modalClose = ref(true);
  defineEmits(['register']);
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload }] = useTable({
    title: t('BI发布管理'),
    api: getBiReleasePage,
    rowKey: 'id',
    columns,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
      showResetButton: false,
    },
    useSearchForm: true,
    showTableSetting: true,
    striped: false,
    actionColumn: {
      width: 88,
      title: t('操作'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
    tableSetting: {
      size: false,
    },
    customRow: (record) => {
      return {
        ondblclick: () => {
          if (hasPermission('bi-menu:edit')) {
            handleEdit(record);
          }
        },
      };
    },
  });

  function handleCreate() {
    openModal(true, {
      title: t('新增BI发布'),
    });
  }

  function handleEdit(record: Recordable) {
    openModal(true, {
      id: record.id,
      isUpdate: true,
      title: t('编辑BI发布'),
    });
  }

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

  function handleSuccess(isEdit) {
    notification.success({
      message: t('提示'),
      description: isEdit ? t('修改成功！') : t('发布成功！'),
    }); //提示消息
    reload();
  }

  function handleClose() {
    modalClose.value = !modalClose.value;
    setTimeout(() => {
      modalClose.value = !modalClose.value;
    }, 100);
  }
  function handleDeleteMenu(record: Recordable) {
    deleteBiAndMenu([record.id]).then((_) => {
      reload();
      notification.success({
        message: t('提示'),
        description: t('删除成功'),
      }); //提示消息
    });
  }
  function handleDel(record: Recordable) {
    curID.value = record.id;
  }
</script>
