<template>
  <div>
    <BasicTable @register="registerTable">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'noticeType'">
          <Tag :color="formatDictColor(sysNoticeTypeOptions, record.noticeType)">
            {{ formatDictValue(sysNoticeTypeOptions, record.noticeType) }}
          </Tag>
        </template>
        <template v-if="column.key === 'disable'">
          <Tag :color="record.disable ? 'error' : 'success'">
            {{ commonDict(record.disable, 1) }}
          </Tag>
        </template>
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'icon-eye|iconfont',
                tooltip: '查看',
                onClick: handleView.bind(null, record),
              },
              {
                icon: 'icon-edit|iconfont',
                tooltip: '编辑',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'icon-delete|iconfont',
                tooltip: '删除',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" preIcon="ant-design:plus-outlined"
          >新增</a-button
        >
      </template>
    </BasicTable>
    <FormModal @register="registerModal" @success="handleSuccess" />
    <DescDrawer @register="registerDrawerDesc" />
  </div>
</template>
<script lang="ts" setup>
  import { onBeforeMount, ref } from 'vue';
  import { Tag } from 'ant-design-vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import FormModal from './FormModal.vue';
  import DescDrawer from './DescDrawer.vue';
  import { columns, searchFormSchema } from './data';

  import { sysNoticeQueryList, sysNoticeRemove } from '/@/api/sys/sysNoticeApi';
  import { listDictModel } from '/@/api/common';
  import { formatDictColor, formatDictValue, commonDict } from '/@/utils';
  import { useDrawer } from '/@/components/Drawer';

  const sysNoticeTypeOptions = ref([]);
  onBeforeMount(async () => {
    sysNoticeTypeOptions.value = await listDictModel({ dictCode: 'sys_notice_type' });
  });

  const { createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload }] = useTable({
    title: '通知公告列表 ',
    api: sysNoticeQueryList,
    rowKey: 'noticeId',
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      baseColProps: { xs: 24, sm: 12, md: 12, lg: 8 },
      resetButtonOptions: {
        preIcon: 'icon-delete|iconfont',
      },
      submitButtonOptions: {
        preIcon: 'icon-search|iconfont',
      },
    },
    showIndexColumn: false,
    useSearchForm: true,
    bordered: true,
    actionColumn: {
      // auth: ['system:sysNotice:edit', 'system:sysNotice:remove'],
      width: 200,
      title: '操作',
      dataIndex: 'action',
    },
  });
  const [registerDrawerDesc, { openDrawer: openDrawerDesc }] = useDrawer();
  // 详情按钮事件
  function handleView(record: Recordable) {
    console.log(record);
    openDrawerDesc(true, {
      record,
    });
  }

  // 新增按钮事件
  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  // 编辑按钮事件
  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  // 删除按钮事件
  async function handleDelete(record: Recordable) {
    await sysNoticeRemove([record.noticeId]);
    createMessage.success('删除成功！');
    await reload();
  }

  // 弹窗回调事件
  async function handleSuccess({ isUpdate, values }) {
    console.log(isUpdate);
    console.log(values);
    await reload();
  }
</script>
