<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <!-- <a-button
          v-power="[RoleEnum.COMPANY_ADMIN, RoleEnum.PLAT_ADMIN]"
          type="primary"
          @click="handleCreate"
          >新增</a-button
        > -->
      </template>
      <template #headerTop v-if="getCheckRole([RoleEnum.COMPANY_ADMIN])">
        <Alert
          :message="`带看权益为 ${surplusSubNum.lookNum} 个、拍摄权益为 ${surplusSubNum.shotNum} 个`"
          type="info"
        />
      </template>
      <template #role="{ record }">
        {{ renderRoleType(record.role) }}
      </template>
      <template #status="{ record }">
        {{ renderStatus(record.status) }}
      </template>
      <template #createTime="{ record }">
        <Time :value="record.createTime" mode="datetime" />
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '查看',
              onClick: handleEdit.bind(null, record),
            },
            {
              color: 'warning',
              label: t('routes.rightsEnterprises.add'),
              onClick: handleOpenModal.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <addModal @register="register" @update="reload" />
    <ProductDrawer @register="registerDrawer" type="rights" @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 { Alert } from 'ant-design-vue';
  import { ListApi, delApi, getNumByStaff } from '/@/api/rightsEnterprises/list';
  import { useDrawer } from '/@/components/Drawer';
  import { useI18n } from '/@/hooks/web/useI18n';
  import ProductDrawer from './productDrawer.vue';
  import { RoleEnum } from '/@/enums/roleEnum';
  import { useGo } from '/@/hooks/web/usePage';
  import addModal from './addModal.vue';
  import { Time } from '/@/components/Time';
  import { useUserStore } from '/@/store/modules/user';
  import { useRouter } from 'vue-router';
  export default defineComponent({
    components: {
      BasicTable,
      TableAction,
      Time,
      Alert,
      addModal,
      ProductDrawer,
    },
    setup() {
      const router = useRouter();
      const [register, { openModal }] = useModal();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const surplusSubNum = ref({
        lookNum: 0,
        shotNum: 0,
      });
      const { createConfirm, createMessage } = useMessage();
      const userStore = useUserStore();
      const { getCheckRole } = userStore;
      const userinfo = computed(() => userStore.getUserInfo);
      const companyId = ref(userinfo.value.companyId);
      const go = useGo();
      const { t } = useI18n();
      console.log('companyId', companyId);
      if (companyId.value) {
        router.replace(`detail/${companyId.value}`);
      }
      onMounted(() => {
        getNumByStaffData();
      });

      const columns: BasicColumn[] = [
        {
          title: 'ID',
          dataIndex: 'companyId',
          fixed: 'left',
          width: 60,
        },
        {
          title: t('routes.staff.deptName'),
          dataIndex: 'companyName',
          width: 160,
        },
        {
          title: t('routes.rightsEnterprises.companyPhone'),
          dataIndex: 'companyPhone',
          width: 80,
        },
        {
          title: t('routes.rightsEnterprises.userPermNum'),
          dataIndex: 'userPermNum',
          width: 130,
        },

        {
          title: t('routes.rightsEnterprises.exUserPermNum'),
          dataIndex: 'exUserPermNum',
          sorter: true,
          width: 130,
        },
        {
          title: t('routes.staff.createTime'),
          dataIndex: 'createTime',
          slots: { customRender: 'createTime' },
          width: 130,
        },
        {
          title: '操作',
          dataIndex: '',
          ifShow: !getCheckRole('tourist'),
          slots: { customRender: 'action' },
          fixed: 'right',
          width: 120,
        },
      ];

      const searchForm: Partial<FormProps> = {
        labelWidth: 100,
        schemas: [
          {
            field: 'fieldTime',
            component: 'RangePicker',
            label: t('routes.rightsEnterprises.timeField'),
            colProps: {
              span: 8,
            },
          },
        ],
      };

      const [registerTable, { reload }] = useTable({
        title: t('routes.rightsEnterprises.menuList'),
        api: ListApi,
        columns: columns,
        useSearchForm: true,
        formConfig: searchForm,
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        showIndexColumn: true,
        rowKey: 'id',
        pagination: { pageSize: 20 },
        beforeFetch: (T) => {
          return {
            limit: T.limit,
            page: T.page,
            startTime: T.fieldTime?.[0],
            endTime: T.fieldTime?.[1],
          };
        },
        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) {
        console.log('Received', record);
        openModal(true, record);
      }
      function getNumByStaffData() {
        getNumByStaff({}).then((res) => {
          surplusSubNum.value.lookNum = res.lookNum;
          surplusSubNum.value.shotNum = res.shotNum;
        });
      }
      function handleEdit(record) {
        // router.push(`detail/${record.companyId}`);
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }
      function handDelconfirm(record) {
        createConfirm({
          iconType: 'warning',
          title: '警告',
          content: `此操作将对${record.userName}进行删除, 是否继续?`,
          onOk: async () => {
            await delApi({ userId: record.id });
            reload();
          },
        });
      }
      return {
        registerTable,
        createMessage,
        handDelconfirm,
        t,
        reload,
        openModal,
        go,
        renderRoleType,
        renderStatus,
        handleOpenModal,
        handleEdit,
        register,
        uploadApi: uploadApi as any,
        RoleEnum,
        surplusSubNum,
        getCheckRole,
        getNumByStaffData,
        registerDrawer,
      };
    },
  });
</script>
