<template>
  <div class="p-4">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">新增</a-button>
      </template>
      <template #img="{ record }">
        <TableImg :size="90" :simpleShow="true" :imgList="[record.image]" />
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              color: 'error',
              label: '删除',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <PadsDrawer :type="type" @register="registerDrawer" @reload="reload" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, h } from 'vue';
  import { BasicTable, useTable, BasicColumn, TableAction, TableImg } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  // import { Tag } from 'ant-design-vue';
  import PadsDrawer from './padsDrawer.vue';
  import { RecommendListApi, RecommendDeleteApi } from '/@/api/advertisement/list';
  import { useDrawer } from '/@/components/Drawer';
  import { Time } from '/@/components/Time';
  import { useRoute } from 'vue-router';
  import { useI18n } from '/@/hooks/web/useI18n';
  // param type 2
  export default defineComponent({
    components: { BasicTable, TableAction, TableImg, PadsDrawer },
    setup() {
      const { t } = useI18n();
      const { createMessage } = useMessage();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const route = useRoute();
      console.log('searchForm', route, route.fullPath);
      const type = route.fullPath.split('/').pop(); //charAt(route.fullPath.length - 2) || '1';
      const columns: BasicColumn[] = [
        {
          title: '排序',
          dataIndex: 'orderNum',
          fixed: 'left',
          width: 100,
        },
        {
          title: '封面',
          dataIndex: 'image',
          ellipsis: true,
          slots: { customRender: 'img' },
          width: 230,
        },
        {
          title: '标题',
          dataIndex: 'title',
          width: 230,
        },
        {
          title: '直播间链接',
          dataIndex: 'sceneUrl',
          width: 300,
        },
        {
          title: '编辑时间',
          dataIndex: 'createTime',
          width: 150,
          customRender: ({ record }) => {
            return (
              record.createTime &&
              h(Time, {
                value: record.createTime,
                mode: 'datetime',
              })
            );
          },
        },

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

      const [registerTable, { getForm, reload }] = useTable({
        title: '推荐位',
        api: RecommendListApi,
        columns: columns,
        // useSearchForm: true,
        // formConfig: searchForm,
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        showIndexColumn: false,
        rowKey: 'id',
        searchInfo: {
          type,
        },
      });
      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
          ...getForm().getFieldsValue(),
        });
      }
      async function handleDelete(record: Recordable) {
        let res = await RecommendDeleteApi({ id: record.id });
        console.log(res);
        createMessage.success(t('common.optSuccess'));
        reload();
      }
      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          ...getForm().getFieldsValue(),
          isUpdate: true,
        });
      }
      return {
        registerTable,
        createMessage,
        registerDrawer,
        handleCreate,
        handleEdit,
        reload,
        type,
        handleDelete,
      };
    },
  });
</script>
