<template>
  <div class="p-4">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">新增</a-button>
      </template>
      <template #avatar="{ record }">
        <Avatar :size="80" :src="record.avatar" />
      </template>

      <template #content="{ record }">
        <div v-if="record.type === 0" v-html="record.content"></div>
        <!-- <img v-if="record.type === 1" :src="record.content" /> -->
        <TableImg
          v-if="record.type === 1"
          :size="200"
          :simpleShow="true"
          :imgList="[record.content]"
        />
      </template>

      <template #periodTime="{ record }">
        <span v-if="record.startTime?.length && record.endTime?.length">
          <Time :value="record.startTime" mode="datetime" /> 到
          <Time :value="record.endTime" mode="datetime" />
        </span>
      </template>

      <template #createTime="{ record }">
        <Time :value="record.createTime" mode="datetime" />
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '查看',
              onClick: () => {
                handleView(record);
              },
            },
            {
              color: 'error',
              label: '删除',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <AddModal @register="registerAddModal" @ok="reload" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import {
    BasicTable,
    useTable,
    BasicColumn,
    FormProps,
    TableAction,
    TableImg,
  } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { uploadApi } from '/@/api/sys/upload';
  import { useModal } from '/@/components/Modal';
  import { listApi, deleteNoticeApi } from '/@/api/notification/list';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useGo } from '/@/hooks/web/usePage';
  import { Avatar } from 'ant-design-vue';
  import { Time } from '/@/components/Time';
  import AddModal from './addModal.vue';

  export default defineComponent({
    components: { BasicTable, TableAction, Avatar, Time, AddModal, TableImg },
    setup() {
      const { createMessage } = useMessage();
      const go = useGo();
      const { t } = useI18n();
      const [registerAddModal, { openModal: openAddModal }] = useModal();
      const columns: BasicColumn[] = [
        {
          title: 'ID',
          dataIndex: 'id',
          fixed: 'left',
          width: 60,
        },
        {
          title: '名称',
          dataIndex: 'title',
          // sorter: true,
          width: 160,
        },
        {
          title: '内容',
          dataIndex: 'content',
          slots: { customRender: 'content' },
          // sorter: true,
          width: 160,
        },
        {
          title: '持续时间',
          dataIndex: 'periodTime',
          slots: { customRender: 'periodTime' },
          // sorter: true,
          width: 120,
        },
        {
          title: '发布时间',
          dataIndex: 'createTime',
          slots: { customRender: 'createTime' },
          // sorter: true,
          width: 120,
        },
        {
          title: '操作',
          dataIndex: '',
          slots: { customRender: 'action' },
          fixed: 'right',
          width: 80,
        },
      ];

      const searchForm: Partial<FormProps> = {
        labelWidth: 100,
        schemas: [
          {
            field: 'title',
            label: '名称',
            component: 'Input',
            componentProps: {
              maxLength: 100,
            },
            colProps: {
              xl: 5,
              xxl: 5,
            },
          },
        ],
      };

      const [registerTable, { reload }] = useTable({
        title: '消息列表',
        api: listApi,
        columns: columns,
        useSearchForm: true,
        formConfig: searchForm,
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        showIndexColumn: false,
        rowKey: 'id',
        pagination: { pageSize: 20 },
        bordered: true,
        fetchSetting: {
          pageField: 'page',
          sizeField: 'limit',
          listField: 'list',
          totalField: 'totalCount',
        },
      });

      function handleCreate(): void {
        // openAddModal();
        openAddModal(true, { isView: false });
      }
      function handleView(record: Recordable): void {
        openAddModal(true, { ...record, isView: true });
      }
      async function handleDelete(record: Recordable) {
        const data = await deleteNoticeApi({
          id: record.id,
        });
        createMessage.success(t('common.optSuccess'));
        reload();
        console.log('data', data);
      }

      return {
        registerTable,
        createMessage,
        t,
        go,
        uploadApi: uploadApi as any,
        handleCreate,
        reload,
        registerAddModal,
        openAddModal,
        handleDelete,
        handleView,
      };
    },
  });
</script>
