<template>
  <BasicTable @register="registerTable">
    <template #toolbar>
      <!-- <a-button type="primary" @click="exportExcel"> 导出1</a-button> -->
    </template>
    <template #href="{ record }">
          <a v-if="record.name" target="_blank" :href="record.share">{{record.name}}</a>
          <span v-else>-</span>
    </template>
    <template #action="{ record }">
      <TableAction
        stopButtonPropagation
        :actions="[
          {
            label: '删除',
            color: 'error',
            ifShow:getCheckPerm('projects-delete'),
            popConfirm: {
              title: '是否删除？',
              confirm: handleDelete.bind(null, record),
              placement: 'topLeft',
            },
          },
        ]"
      />
    </template>
  </BasicTable>
</template>
<script lang="ts">
  import { defineComponent, h, reactive, toRefs } from 'vue';
  import {
    BasicTable,
    useTable,
    TableAction,
    BasicColumn,
    TableImg,
    FormProps,
  } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { Time } from '/@/components/Time';
  import { Descriptions } from 'ant-design-vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Switch } from 'ant-design-vue';
  import { overallList, overallDelete } from '/@/api/operate';
  import { message } from 'ant-design-vue';
  import { usePermissionStore } from '/@/store/modules/permission';
  export default defineComponent({
    components: {
      BasicTable,
      TableAction,
      PageWrapper,
      TableImg,
      [Descriptions.name]: Descriptions,
      [Descriptions.Item.name]: Descriptions.Item,
    },
    setup() {
      const { t } = useI18n();
      const { createMessage, createConfirm } = useMessage();
      const permissionStore = usePermissionStore();
      const { getCheckPerm } = permissionStore;
      const columns: BasicColumn[] = [
        {
          title: '作品标题',
          dataIndex: 'name',
          slots: { customRender: 'href' },
          width: 150,
          // customRender: ({ record }) => {
          //   return record.name ? h('span', record.name) : '-';
          // },
        },
        {
          title: '作品码',
          dataIndex: 'sceneCodes',
          ellipsis: true,
          width: 180,
        },
        {
          title: '用户账号',
          dataIndex: 'userId',
          width: 100,
        },

        {
          title: '创建时间',
          dataIndex: 'createTime',
          sorter: true,
          width: 180,
          customRender: ({ record }) => {
            return record.createTime
              ? h(Time, {
                  value: record.createTime,
                  mode: 'datetime',
                })
              : '-';
          },
        },
        {
          title: '最新编辑时间',
          dataIndex: 'updateTime',
          width: 180,
          customRender: ({ record }) => {
            return record.updateTime
              ? h(Time, {
                  value: record.updateTime,
                  mode: 'datetime',
                })
              : '-';
          },
        },
        {
          title: '是否加密',
          dataIndex: 'status',
          width: 80,
          customRender: ({ record }) => {
            return record.isPassword == 1 ? '是' : '否';
          },
        },
        {
          title: '浏览量',
          dataIndex: 'visit',
          sorter: true,
          width: 80,
        },
        {
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          ifShow: true,
          fixed: 'right',
          flag: 'ACTION',
          width: 50,
        },
      ];
      const searchForm: Partial<FormProps> = {
        labelWidth: 100,
        autoSubmitOnEnter:true,
        autoAdvancedLine:1,
        actionColOptions: {
          span: 24,
        },
        schemas: [
          {
            field: 'sceneName',
            label: '作品标题',
            component: 'Input',
            componentProps: {
              maxLength: 100,
            },
            colProps: {
              xl: 7,
              xxl: 7,
            },
          },{
            field: 'sceneCodes',
            label: '作品码',
            component: 'Input',
            componentProps: {
              maxLength: 100,
            },
            colProps: {
              xl: 7,
              xxl: 7,
            },
          },
          {
            field: 'userName',
            label: '用户账号',
            component: 'Input',
            componentProps: {
              maxLength: 100,
            },
            colProps: {
              xl: 6,
              xxl: 6,
            },
          },{
            field: 'timeList',
            label: '创建时间',
            component: 'RangePicker',
            componentProps: {
              maxLength: 100,
              minWidth:'385px',
              format: 'YYYY-MM-DD',
              valueFormat: 'YYYY-MM-DD',
              showTime: true,
            },
            colProps: {
              xl: 8,
              xxl: 8,
            },
          },
        ],
      };
      const [registerTable, { reload }] = useTable({
        api: overallList,
        title: '作品列表',
        // titleHelpMessage: ['已启用expandRowByClick', '已启用stopButtonPropagation'],
        columns: columns,
        searchInfo: { type: 1 },
        useSearchForm: true,
        formConfig: searchForm,
        showTableSetting: true,
        rowKey: 'num',
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },
        canResize: true,
      });
      async function handleDelete(record: Recordable) {
        console.log('handleDelete', record);

        overallDelete({ id: record.id })
          .then(() => {
            message.success({
              content: '删除成功',
            });

            reload();
          })
      }
      async function handleMove(record: Recordable) {
        sceneMove({ snCode: record.snCode, num: record.num })
          .then(() => {
            message.success({
              content: '迁移成功',
            });
          })
      }
      function handleDownload(record: Recordable) {
        console.log('handleDownload', record);
      }
      function handleReset(record: Recordable) {
        console.log('handleReset', record);
      }
      // function exportExcel() {
      //   createConfirm({
      //     iconType: 'warning',
      //     title: () => h('span', '温馨提示'),
      //     content: () => h('span', '确定当前标签下的订单记录?'),
      //     onOk: async () => {
      //       // await DownExport();
      //     },
      //   });
      // }
      return {
        registerTable,
        handleDelete,
        handleMove,
        handleDownload,
        handleReset,
        getCheckPerm,
      };
    },
  });
</script>
