<template>
  <div class="p-4">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">新增</a-button>
      </template>
      <template #time="{ record }">
        <Time v-if="record.createTime" :value="record.createTime" mode="date" />
      </template>
      <template #img="{ record }">
        <TableImg :size="200" :simpleShow="true" :imgList="[record.image]" />
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              color: 'error',
              label: '删除',
              popConfirm: {
                title: '是否确认删除',
                confirm: deleteConfirm.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <listDrawer @register="registerDrawer" @reload="reload" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicTable, useTable, BasicColumn, TableAction, TableImg } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useDrawer } from '/@/components/Drawer';
  import { Time } from '/@/components/Time';
  import listDrawer from './listDrawer.vue';
  import { ListApi, itemDeletelApi } from '/@/api/advertisement/list';
  // param type 2
  export default defineComponent({
    components: { BasicTable, TableAction, TableImg, listDrawer, Time },
    setup() {
      const { createMessage } = useMessage();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const columns: BasicColumn[] = [
        {
          title: '序号',
          dataIndex: 'id',
          fixed: 'left',
          width: 100,
        },
        {
          title: '封面',
          dataIndex: 'image',
          ellipsis: true,
          slots: { customRender: 'img' },
          width: 230,
        },
        {
          title: '标题',
          dataIndex: 'title',
          width: 230,
        },
        {
          title: '排序',
          dataIndex: 'orderNum',
          width: 100,
        },
        {
          title: '编辑时间',
          dataIndex: 'createTime',
          slots: { customRender: 'time' },
          width: 150,
        },

        {
          title: '操作',
          dataIndex: '',
          fixed: 'right',
          slots: { customRender: 'action' },
          width: 130,
        },
      ];

      // { getForm }
      const [registerTable, { reload }] = useTable({
        title: '轮播图',
        api: ListApi,
        columns: columns,
        useSearchForm: false,

        showTableSetting: true,
        tableSetting: { fullScreen: true },
        showIndexColumn: false,
        rowKey: 'id',
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }
      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }
      function deleteConfirm(val) {
        itemDeletelApi({ id: val.id }).then((res) => {
          createMessage.success(res);
          reload();
        });
      }
      return {
        deleteConfirm,
        reload,
        registerTable,
        createMessage,
        registerDrawer,
        handleCreate,
        handleEdit,
      };
    },
  });
</script>
