<template>
  <div class="p-4">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button
          type="primary"
          @click="handleCreate"
          v-if="getCheckPerm('application-add')"
          >新增</a-button
        >
      </template>
      <!-- , -->
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '删除',
              ifShow: getCheckPerm('application-delete'),
              color: 'error',
              onClick:handDelconfirm.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <DetailsModal @register="registerDetail" @ok="reload" />
    <!--<DelListModal @register="registerDelList" @reload="reload" /> -->
  </div>
</template>
<script lang="ts">
  import { defineComponent, computed, onMounted, ref, } from 'vue';
  import { BasicTable, useTable, BasicColumn, FormProps, TableAction } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';
  import { uploadApi } from '/@/api/sys/upload';
  import DetailsModal from './detailsModal.vue';
  import { buryPointList, buryPointDlt } from '/@/api/statistics/index';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { RoleEnum } from '/@/enums/roleEnum';
  import { useGo } from '/@/hooks/web/usePage';
  import { Time } from '/@/components/Time';
  import { useUserStore } from '/@/store/modules/user';
  import { usePermissionStore } from '/@/store/modules/permission';
  export default defineComponent({
    components: {
      BasicTable,
      TableAction,
      Time,
      DetailsModal,
      // DelListModal,
    },
    setup() {
      const [register, { openModal }] = useModal();
      const surplusSubNum = ref({
        lookNum: 0,
        shotNum: 0,
      });
      const [registerDetail, { openModal: openDetaileModal }] = useModal();
      const [registerDelList, { openModal: openDelListeModal }] = useModal();
      const { createConfirm, createMessage } = useMessage();
      const permissionStore = usePermissionStore();
      const { getCheckPerm } = permissionStore;
      const userStore = useUserStore();
      // const { getCheckRole } = userStore;
      const roleList = computed(() => userStore.getRoleList);
      console.log('getRoleList', roleList);
      const go = useGo();
      const { t } = useI18n();
      onMounted(() => {
        // getNumByStaffData();
      });

      const columns: BasicColumn[] = [
        {
          title: '应用名称',
          dataIndex: 'applicationName',
        },
        {
          title: '应用key',
          dataIndex: 'applicationKey',
        },
        {
          title: '操作',
          align:  'center',
          dataIndex: '',
          flag:'ACTION',
          // ifShow: !getCheckRole('tourist'),
          slots: { customRender: 'action' },
          fixed: 'right',
          width: 150,
        },
      ];

      const searchForm: Partial<FormProps> = {
        labelWidth: 100,
        autoSubmitOnEnter:true,
        schemas: [
          {
            field: 'applicationName',
            label: '应用名称',
            component: 'Input',
            componentProps: {
              maxLength: 15,
            },
            colProps: {
              xl: 6,
              xxl: 6,
            },
          },
        ],
      };

      const [registerTable, { reload }] = useTable({
        title: '应用列表',
        api: buryPointList,
        columns: columns,
        useSearchForm: true,
        formConfig: searchForm,
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        showIndexColumn: false,
        rowKey: 'id',
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },
        pagination: { pageSize: 20 },
        afterFetch: (T) => {
          return T;
        },
        bordered: true,
        sortFn: (sortInfo) => {
          let order = sortInfo.order && sortInfo.order.replace('end', '');
          return { ...sortInfo, sidx: sortInfo.field, order: order };
        },
      });

      function renderRoleType(type: number): string {
        switch (type) {
          case 0:
            return t('routes.staff.roleType.0');
          case 1:
            return t('routes.staff.roleType.1');
          default:
            return '';
        }
      }
      function renderStatus(type: number): string {
        switch (type) {
          case 1:
            return t('common.normal');
          case 0:
            return t('common.unNormal');
          default:
            return '';
        }
      }
      function handleOpenModal(record: Recordable) {
        openModal(true, record);
      }
      function handleCreate() {
        // if (
        //   getCheckRole([RoleEnum.COMPANY_ADMIN]) &&
        //   surplusSubNum.value.lookNum == 0 &&
        //   surplusSubNum.value.shotNum == 0
        // ) {
        //   return createMessage.error('新增失败，可添加员工数量为0个');
        // }
        openDetaileModal(true, {});
      }
      function handleEdit(record: Recordable) {
        openDetaileModal(true, {
          ...record,
          phone:record.userName,
        });
      }
      function handDelconfirm(record) {
        createConfirm({
          iconType: 'warning',
          title: '警告',
          content: `此操作将对${record.applicationName}进行删除, 是否继续?`,
          onOk: async () => {
            await buryPointDlt({ id: record.id });
            reload();
            createMessage.success(t('common.optSuccess'));
          },
        });
      }
      return {
        registerTable,
        registerDetail,
        registerDelList,
        openDelListeModal,
        createMessage,
        handDelconfirm,
        t,
        reload,
        go,
        renderRoleType,
        renderStatus,
        handleCreate,
        handleOpenModal,
        register,
        handleEdit,
        uploadApi: uploadApi as any,
        RoleEnum,
        surplusSubNum,
        getCheckPerm,
      };
    },
  });
</script>
