<template>
  <div class="p-4">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">新增</a-button>
      </template>
      <template #img="{ record }">
        <TableImg :size="200" :simpleShow="true" :imgList="[record.image]" />
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              label: '删除',
              popConfirm: {
                title: '是否确认删除',
                confirm: () => {
                  createMessage.info(`暂未接入`);
                },
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <PadsDrawer @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 } from '/@/api/advertisement/list';
  import { useDrawer } from '/@/components/Drawer';
  import { Time } from '/@/components/Time';
  import { useRoute } from 'vue-router';
  // param type 2
  export default defineComponent({
    components: { BasicTable, TableAction, TableImg, PadsDrawer },
    setup() {
      const { createMessage } = useMessage();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const route = useRoute();
      console.log('searchForm', route, route.fullPath);
      const type = route.fullPath.charAt(route.fullPath.length - 1) || 1;
      const columns: BasicColumn[] = [
        {
          title: '序号',
          dataIndex: 'id',
          fixed: 'left',
          width: 100,
        },
        {
          title: '封面',
          dataIndex: 'image',
          slots: { customRender: 'img' },
          width: 230,
        },
        {
          title: '标题',
          dataIndex: 'title',
          width: 230,
        },
        {
          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: 150,
        },
      ];
      // const searchForm: Partial<FormProps> = {
      //   labelWidth: 100,
      //   schemas: [
      //     {
      //       field: 'type',
      //       label: '推荐类型',
      //       component: 'Select',
      //       defaultValue: 0,
      //       colProps: {
      //         xl: 5,
      //         xxl: 5,
      //       },
      //       componentProps: {
      //         onChange: function () {
      //           reload();
      //         },
      //         options: [
      //           {
      //             label: '新上房源',
      //             value: 0,
      //             key: '0',
      //           },
      //           {
      //             label: '热门房源',
      //             value: 1,
      //             key: '1',
      //           },
      //           {
      //             label: '热门企业',
      //             value: 2,
      //             key: '2',
      //           },
      //         ],
      //       },
      //     },
      //   ],
      // };reload

      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(),
        });
      }
      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          ...getForm().getFieldsValue(),
          isUpdate: true,
        });
      }
      return {
        registerTable,
        createMessage,
        registerDrawer,
        handleCreate,
        handleEdit,
        reload,
      };
    },
  });
</script>
