<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.addText'),
              onClick: handleOpenModal.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <addModal @register="register" @update="reload" />
    <ProductDrawer @register="registerDrawer" type="camera" @reload="reload" />
    <addCameraModal @register="register" @update="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 { cameraListApi, 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 addCameraModal from './addCameraModal.vue';
  import addModal from './addModal.vue';
  import { Time } from '/@/components/Time';
  import { useUserStore } from '/@/store/modules/user';
  import { useTabs } from '/@/hooks/web/useTabs';
  import { useRouter } from 'vue-router';
  export default defineComponent({
    components: {
      BasicTable,
      TableAction,
      Time,
      Alert,
      addModal,
      ProductDrawer,
      addCameraModal,
    },
    emits: ['register', 'update'],
    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 { closeCurrent } = useTabs();
      const { t } = useI18n();
      console.log('companyId', companyId);
      if (companyId.value) {
        closeCurrent();
        router.push(`detail/${companyId.value}`);
      }
      onMounted(() => {
        getNumByStaffData();
      });

      const columns: BasicColumn[] = [
        {
          title: 'ID',
          dataIndex: 'id',
          fixed: 'left',
          width: 60,
        },
        {
          title: t('routes.rightsEnterprises.companyName'),
          dataIndex: 'name',
          width: 180,
        },
        {
          title: t('routes.rightsEnterprises.companyPhone'),
          dataIndex: 'phone',
          width: 150,
        },
        {
          title: t('routes.rightsEnterprises.kk'),
          dataIndex: 'proIncrementNum',
          width: 80,
        },

        {
          title: t('routes.rightsEnterprises.kj'),
          dataIndex: 'seeIncrementNum',
          // sorter: true,
          width: 80,
        },
        {
          title: t('routes.rightsEnterprises.ss'),
          dataIndex: 'sensorIncrementNum',
          // slots: { customRender: 'createTime' },
          width: 80,
        },
        {
          title: '操作',
          dataIndex: '',
          ifShow: !getCheckRole('tourist'),
          slots: { customRender: 'action' },
          fixed: 'right',
          width: 80,
        },
      ];

      const searchForm: Partial<FormProps> = {
        labelWidth: 100,
        schemas: [
          {
            field: 'companyName',
            label: t('routes.rightsEnterprises.companyName'),
            component: 'Input',
            componentProps: {
              maxLength: 15,
            },
            colProps: {
              span: 8,
            },
          },
          // {
          //   field: 'companyPhone',
          //   label: t('routes.rightsEnterprises.companyPhone'),
          //   component: 'Input',
          //   componentProps: {
          //     maxLength: 15,
          //   },
          //   colProps: {
          //     span: 8,
          //   },
          // },
        ],
      };

      const [registerTable, { reload }] = useTable({
        title: t('routes.rightsEnterprises.menuTableList'),
        api: cameraListApi,
        columns: columns,
        useSearchForm: true,
        formConfig: searchForm,
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        showIndexColumn: true,
        // searchInfo:'',
        rowKey: 'id',
        pagination: { pageSize: 20 },
        beforeFetch: (T) => {
          return {
            ...T,
            limit: T.limit,
            page: T.page,
            startTime: T.fieldTime?.[0],
            endTime: T.fieldTime?.[1],
          };
        },
        bordered: true,
      });

      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,
          modify: true,
          companyId: record.id,
          companyPhone: record.phone,
          companyName: record.name,
        });
      }
      function handleCreate() {
        openModal(true, {
          modify: true,
        });
      }
      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: {
            ...record,
            companyId: record.id,
            companyName: record.name,
            companyPhone: record.phone,
            userPermNum:
              record.proIncrementNum + record.seeIncrementNum + record.sensorIncrementNum,
          },
          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,
        handleCreate,
        handleEdit,
        register,
        uploadApi: uploadApi as any,
        RoleEnum,
        surplusSubNum,
        getCheckRole,
        getNumByStaffData,
        registerDrawer,
      };
    },
  });
</script>
