<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" v-if="getCheckPerm('App-add')" @click="()=>{openAddModal(true)}"> 新增 App</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" />
  </div>
</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';
  import { AppFileAgentList } from '/@/api/product'
  import { TreeNode } from '/@/utils/treeUtils';

  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: 100,
        },
        {
          title: '代理商标识',
          dataIndex: 'agent',
          width: 160,
        },{
          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: 'agentName',
            label: '代理商标识',
            component: 'ApiSelect',
            componentProps: {
            api: async function (params) {
              const res = (await AppFileAgentList(params)) as any as TreeNode[];
              return res.map(ele => {
                return {name:ele,value:ele}
              });
            },
            numberToString: true,
            labelField: 'name',
            valueField: 'value',
            immediate: true,
            params: {
              agentName: '',
            },
          },
            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);
      }
      return {
        registerTable,
        handleDelete,
        handleOpen,
        registerAddModal,
        openAddModal,
        reload,
        getCheckPerm,
      };
    },
  });
</script>