<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <!-- RoleEnum.COMPANY_ADMIN, -->
        <a-button v-power="[RoleEnum.PLAT_ADMIN]" type="primary" @click="handleOpenModal">
          新增</a-button
        >
      </template>
      <template #status="{ record }">
        {{ renderStatus(record.state) }}
      </template>
      <template #createTime="{ record }">
        <Time v-show="record.createTime" :value="record.createTime" mode="datetime" />
      </template>
      <template #incrementEndTime="{ record }">
        <span v-if="record.status" style="color: #ed6f6f">已过期</span>
        <Time v-else :value="record.incrementEndTime" mode="datetime" />
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '授权',
              ifShow: !record.snCode && !record.status,
              color: 'success',
              onClick: handleDelete.bind(null, record),
            },
            {
              label: '解绑',
              color: 'error',
              ifShow: !!record.snCode,
              onClick: handleUpBind.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <addCameraModal @register="register" @update="reload" />
    <BindModal @register="registerDelList" @update="reload" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, computed, onMounted, ref, watch, reactive } 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 BindModal from './cameraBind.vue';
  import addCameraModal from './addCameraModal.vue';
  import { cameraDetailList, unbindCamera } from '/@/api/rightsEnterprises/list';
  import { useI18n } from '/@/hooks/web/useI18n';
  // import { useCopyToClipboard } from '/@/hooks/web/useCopyToClipboard';
  import dayjs from 'dayjs';
  import { RoleEnum } from '/@/enums/roleEnum';
  import { useGo } from '/@/hooks/web/usePage';
  import { Time } from '/@/components/Time';
  import { useUserStore } from '/@/store/modules/user';
  export default defineComponent({
    name: 'StaffList',
    components: {
      BasicTable,
      TableAction,
      Time,
      BindModal,
      addCameraModal,
    },
    props: {
      companyInfo: {
        type: Object,
        default: function () {
          return {};
        },
      },
    },
    emits: ['register'],
    setup(props) {
      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 userStore = useUserStore();
      const { getCheckRole } = userStore;
      const userinfo = computed(() => userStore.getUserInfo);
      const modelRef = reactive({
        companyId: userinfo.value.companyId,
        companyName: '',
        companyPhone: '',
      });
      const go = useGo();
      const { t } = useI18n();
      watch(
        () => props.companyInfo,
        (newProps) => {
          modelRef.companyId = newProps.companyId;
          modelRef.companyName = newProps.companyName;
          modelRef.companyPhone = newProps.companyPhone;
          console.log('companyInfo', modelRef);
          reload();
        },
        {
          deep: true,
        },
      );
      onMounted(() => {});

      const columns: BasicColumn[] = [
        {
          title: '相机权益ID',
          dataIndex: 'id',
          fixed: 'left',
          width: 100,
        },
        {
          title: t('routes.rightsEnterprises.snCode'),
          dataIndex: 'snCode',
          width: 160,
        },
        {
          title: t('routes.rightsEnterprises.cameraIncrementTypeName'),
          dataIndex: 'cameraIncrementTypeName',
          width: 160,
        },
        {
          title: t('routes.rightsEnterprises.companyName'),
          dataIndex: 'companyName',
          width: 160,
        },
        {
          title: t('routes.rightsEnterprises.companyPhone'),
          dataIndex: 'companyPhone',
          width: 160,
        },
        {
          title: t('routes.rightsEnterprises.purchaseTime'),
          dataIndex: 'createTime',
          slots: { customRender: 'createTime' },
          width: 160,
        },
        {
          title: t('routes.rightsEnterprises.expirationTime'),
          dataIndex: 'incrementEndTime',
          slots: { customRender: 'incrementEndTime' },
          width: 160,
        },
        {
          title: '操作',
          dataIndex: '',
          ifShow: !getCheckRole('tourist'),
          slots: { customRender: 'action' },
          fixed: 'right',
          width: 60,
        },
      ];

      const searchForm: Partial<FormProps> = {
        labelWidth: 100,
        schemas: [
          // {
          //   field: 'staffName',
          //   label: t('routes.rightsEnterprises.companyName'),
          //   component: 'Input',
          //   componentProps: {
          //     maxLength: 15,
          //   },
          //   colProps: {
          //     span: 6,
          //   },
          // },
          // {
          //   field: 'staffPhone',
          //   label: t('routes.corporation.enterpriseId'),
          //   component: 'Input',
          //   componentProps: {
          //     maxLength: 15,
          //   },
          //   colProps: {
          //     span: 6,
          //   },
          // },
          {
            field: 'snCode',
            label: t('routes.rightsEnterprises.snCodeText'),
            component: 'Input',
            componentProps: {
              maxLength: 15,
            },
            colProps: {
              span: 6,
            },
          },
        ],
      };

      const [registerTable, { reload }] = useTable({
        title: t('routes.rightsEnterprises.cameraTitle'),
        api: cameraDetailList,
        columns: columns,
        useSearchForm: true,
        formConfig: searchForm,
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        showIndexColumn: true,
        rowKey: 'id',
        immediate: userinfo.value.companyId ? true : false,
        pagination: { pageSize: 20 },
        searchInfo: modelRef,
        afterFetch: (T) => {
          let newDate = dayjs();
          return T.map((ele) => {
            if (ele.incrementEndTime) {
              ele.status = dayjs(ele.incrementEndTime).isAfter(newDate) ? 0 : 1;
            }
            console.log('newDate', ele);
            return ele;
          });
        },
        bordered: true,
      });

      function renderStatus(type: number): string {
        switch (type) {
          // case 0:
          //   return t('routes.rightsEnterprises.state.0');
          // case 1:
          //   return t('routes.rightsEnterprises.state.1');
          case 2:
            return t('routes.rightsEnterprises.state.2');
          default:
            return t('routes.rightsEnterprises.state.9');
        }
      }
      function handleOpenModal() {
        openModal(true, modelRef);
      }
      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);
      }
      async function handleDelete(record) {
        return openDelListeModal(true, {
          ...record,
        });
      }
      function handleUpBind(record) {
        createConfirm({
          iconType: 'warning',
          title: '温馨提醒',
          content: `是否确定解绑相机？`,
          onOk: async () => {
            await unbindCamera({ id: record.id });
            reload();
          },
        });
      }
      return {
        registerTable,
        registerDetail,
        registerDelList,
        openDelListeModal,
        createMessage,
        modelRef,
        handleUpBind,
        t,
        reload,
        go,
        renderStatus,
        handleCreate,
        handleOpenModal,
        register,
        handleEdit,
        handleDelete,
        uploadApi: uploadApi as any,
        RoleEnum,
        surplusSubNum,
        getCheckRole,
      };
    },
  });
</script>
