<template>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" v-if="getCheckPerm('app-add')" @click="()=>{openAddModal(true)}"> 新建</a-button>
      </template>
        <template #action="{ record }">
          <TableAction
            :actions="[
              {
                label: '删除',
                icon: 'ic:outline-delete-outline',
                ifShow: getCheckPerm('app-delete'),
                onClick: handleDelete.bind(null, record),
              },
            ]"
          />
      </template>
    </BasicTable>
    <AddModal @update="reload" @register="registerAddModal" />
</template>
<script lang="ts">
  import { defineComponent, h } 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 { AppFileList,AppFileDelete } from '/@/api/product'
  import { useModal } from '/@/components/Modal';
  import AddModal from './AddModal.vue';
  import { usePermissionStore } from '/@/store/modules/permission';

  export default defineComponent({
    components: { 
      BasicTable, 
      TableAction, 
      PageWrapper,
      AddModal,
      TableImg,
      [Descriptions.name]: Descriptions,
      [Descriptions.Item.name]: Descriptions.Item,
    },
    setup() {
      const { t } = useI18n();
      const permissionStore = usePermissionStore();
      const { getCheckPerm } = permissionStore;
      const { createMessage,createConfirm } = useMessage();
      const [registerAddModal, { openModal: openAddModal }] = useModal();
      const columns: BasicColumn[] = [
        {
          title: '上传服务器',
          dataIndex: 'fileServerType',
          ellipsis: false,
          width: 80,
        },
        {
          title: '代理商标识',
          dataIndex: 'agent',
          width: 80,
        },{
          title: 'APP类型',
          dataIndex: 'appType',
          ellipsis: false,
          width: 100,
        },
        {
          title: 'APP包',
          dataIndex: 'name',
          width: 150,
        },{
          title: 'MD5',
          dataIndex: 'md5',
          ellipsis: false,
          width: 250,
        },
        {
          title: '时间',
          dataIndex: 'createTime',
          width: 150,
          customRender: ({ record }) => {
            return (
              record.createTime &&
              h(Time, {
                value: record.createTime,
                mode: 'datetime',
              })
            );
          },
        },
      ];
      const searchForm: Partial<FormProps> = {
        labelWidth: 100,
        schemas: [
          {
            field: 'sceneName',
            label: '开票申请时间',
            component: 'RangePicker',
            componentProps: {
              maxLength: 100,
            },
            colProps: {
              xl: 7,
              xxl: 7,
            },
          },
          {
            field: 'sceneName',
            label: '支付时间',
            component: 'RangePicker',
            componentProps: {
              maxLength: 100,
            },
            colProps: {
              xl: 7,
              xxl: 7,
            },
          },{
            field: 'sceneName',
            label: '订单号',
            component: 'Input',
            componentProps: {
              maxLength: 100,
            },
            colProps: {
              xl: 6,
              xxl: 6,
            },
          }
        ],
      };
      const [registerTable,{reload}] = useTable({
        api: AppFileList,
        title: 'App列表',
        // titleHelpMessage: ['已启用expandRowByClick', '已启用stopButtonPropagation'],
        columns: columns,
        useSearchForm: true,
        formConfig: searchForm,
        showTableSetting: true,
        showIndexColumn:false,
        rowKey: 'id',
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },
        actionColumn: {
          width: 100,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
        canResize: false,
      });
      function handleDelete(record: Recordable) {
        createConfirm({
          iconType: 'warning',
          title: () => h('span', '温馨提示'),
          content: () => h('span', '确定当删除当前记录?'),
          onOk: async () => {
            await AppFileDelete({ id: record.id });
            createMessage.success(t('common.optSuccess'));
            reload();
          },
        });
      }
      function handleOpen(record: Recordable) {
        console.log('点击了启用', record);
      }
      function exportExcel() {
        createConfirm({
          iconType: 'warning',
          title: () => h('span', '温馨提示'),
          content: () => h('span', '确定当前标签下的订单记录?'),
          onOk: async () => {
            await DownExport();
          },
        });
      }
      return {
        registerTable,
        handleDelete,
        handleOpen,
        registerAddModal,
        openAddModal,
        exportExcel,
        reload,
        getCheckPerm,
      };
    },
  });
</script>