<template>
  <div class="p-4">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button v-if="getCheckPerm('platformList-add')" type="primary" @click="handleCreate">创建平台</a-button>
      </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="[
            {
              ifShow: getCheckPerm('platformList-disable'),
              label: '禁用',
              disabled: record.status != 0,
              color: 'error',
              onClick: handleOpenModal.bind(null, record),
            },
            //{
            //  label: '启用',
            //  ifShow: getCheckPerm('platformList-enable') && record.status != 0,
            //  onClick: handleEdit.bind(null, record),
            //},
            {
              label: '编辑',
              ifShow: getCheckPerm('platformList-update'),
              disabled: record.status != 0,
              onClick: handleEdit.bind(null, record), //handleEdit.bind(null, record),
            },
            //{
            //  label: '删除',
            //  color: 'error',
            //  //ifShow:getCheckPerm('lanuser-delete'),
            //  onClick: handDelconfirm.bind(null, record),
            //},
          ]"
        />
      </template>
    </BasicTable>
    <DetailsModal @register="registerDetail" @ok="reload" />
    <SetpaswordModal @register="register" @ok="reload" />
    <addDetailsModal @register="registerAddDetail" @ok="reload" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, computed, onMounted, ref, h } 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 SetpaswordModal from './setpaswordModal.vue';
  import DetailsModal from './detailsModal.vue';
  import addDetailsModal from './adddetailsModal.vue';
  import { Switch } from 'ant-design-vue';
  // import DelListModal from './delListModal.vue';
  import { Alert } from 'ant-design-vue';
  // import { h } from 'vue';
  import { delUcenterUser, preDelApi, updateApi, getNumByStaff } from '/@/api/staff/list';
  import { ListApi } from '/@/api/account';
  import { useI18n } from '/@/hooks/web/useI18n';
  // import { useCopyToClipboard } from '/@/hooks/web/useCopyToClipboard';
  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';
  import { jyPlatformlist, delplatformList,enable } from '/@/api/jyUserPlatform/index'; //roleLIstApi
  export default defineComponent({
    components: {
      BasicTable,
      TableAction,
      Time,
      SetpaswordModal,
      DetailsModal,
      addDetailsModal,
      Alert,
    },
    setup() {
      const [register, { openModal }] = useModal();
      const surplusSubNum = ref({
        lookNum: 0,
        shotNum: 0,
      });
      const [registerDetail, { openModal: openDetaileModal }] = useModal();
      const [registerAddDetail, { openModal: openAddDetaileModal }] = useModal();
      const [registerDelList, { openModal: openDelListeModal }] = useModal();
      const { createConfirm, createMessage } = useMessage();
      const userStore = useUserStore();
      const permissionStore = usePermissionStore();
      const { getCheckPerm } = permissionStore;
      const roleList = computed(() => userStore.getRoleList);
      console.log('getRoleList', roleList);
      const go = useGo();
      const { t } = useI18n();
      onMounted(() => {
        // getNumByStaffData();
      });
      const deseHandler = (str, frontLen, endLen, wildcard) => {
        if (!str) {
          return '--';
        } else if (str.length < 9) {
          return str;
        } else {
          var len = str.length - frontLen - endLen;
          var xing = '';
          for (var i = 0; i < len; i++) {
            xing += wildcard;
          }
          return str.substring(0, frontLen) + xing + str.substring(str.length - endLen);
        }
      };
      const columns: BasicColumn[] = [
        {
          title: '平台名称',
          dataIndex: 'platformName',
          width: 150,
          customRender: ({ record }) => {
            return record.platformName || '-';
          },
        },
        {
          title: '平台地址',
          dataIndex: 'platformAddress',
          width: 200,
          customRender: ({ record }) => {
            let url = window.location.origin + window.location.pathname + '?key=' + record.platformAddress;
            return url || '-';

          },
        },
        {
          title: '平台管理员姓名',
          dataIndex: 'name',
          width: 100,
          customRender: ({ record }) => {
            return record.name || '-';
          },
        },
        // {
        //   title: '平台管理员手机号码',
        //   dataIndex: 'phone',
        //   width: 150,
        // },
        {
          title: '添加时间',
          dataIndex: 'createTime',
          width: 100,
        },
        // {
        //   title: '状态',
        //   dataIndex: 'status',
        //   // ifShow:getCheckPerm('sysuser-enable'),
        //   width: 80,
        //   customRender: ({ record }) => {
        //     // return record.status == 1 ? '启用' : '禁用';
        //     if (!Reflect.has(record, 'status')) {
        //       record.pendingStatus = false;
        //     }
        //     return h(Switch, {
        //       checked: record.status == 1 ? true : false,
        //       checkedChildren: '启用',
        //       unCheckedChildren: '禁用',
        //       loading: false,
        //       onChange: async (checked: boolean) => {
        //         record.pendingStatus = true;
        //         const newStatus = checked?1:0;
        //         await updateApi({...record,status:newStatus});
        //         Reflect.set(record, 'status', checked);
        //         createMessage.success(t('common.optSuccess'));
        //         reload()
        //       },
        //     });
        //   },
        // },
        {
          title: '操作',
          dataIndex: '',
          // ifShow: !getCheckRole('tourist'),
          slots: { customRender: 'action' },
          // fixed: 'right',
          flag: 'ACTION',
          width: 80,
        },
      ];

      const searchForm: Partial<FormProps> = {
        labelWidth: 90,
        schemas: [
          {
            field: 'platformName',
            label: '平台名称',
            component: 'Input',
            componentProps: {
              maxLength: 15,
            },
            colProps: {
              xl: 6,
              xxl: 6,
            },
          },
          {
            field: 'name',
            labelWidth: 120,
            label: '平台管理员姓名',
            component: 'Input',
            componentProps: {
              maxLength: 15,
            },
            colProps: {
              xl: 6,
              xxl: 6,
            },
          },
          // {
          //   field: 'phone',
          //   label: '平台管理员手机号码',
          //   labelWidth: 140,
          //   component: 'Input',
          //   componentProps: {
          //     maxLength: 15,
          //   },
          //   colProps: {
          //     xl: 6,
          //     xxl: 6,
          //   },
          // },
        ],
      };

      const [registerTable, { reload }] = useTable({
        // title: t('routes.staff.staffList'),
        api: jyPlatformlist,
        columns: columns,
        useSearchForm: true,
        formConfig: searchForm,
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        showIndexColumn: false,
        canResize: true,
        rowKey: 'id',
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },
        pagination: { pageSize: 10 },
        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() {
        openDetaileModal(true, {});
      }
      function handleEdit(record: Recordable) {
        openDetaileModal(true, {
          ...record,
          phone: record.userName,
        });
      }
      function getNumByStaffData() {
        getNumByStaff({}).then((res) => {
          surplusSubNum.value.lookNum = res.lookNum;
          surplusSubNum.value.shotNum = res.shotNum;
        });
      }
      async function handleDelete(record) {
        let check = await preDelApi(record.id); //
        if (Array.isArray(check)) {
          return openDelListeModal(true, {
            ...record,
            option: check,
          });
        }
        createMessage.success(t('common.optSuccess'));
        reload();
        // handDelconfirm(record);
      }
      function handDelconfirm(record) {
        createConfirm({
          iconType: 'warning',
          title: '警告',
          content: `此操作将对${record.userName}进行删除, 是否继续?`,
          onOk: async () => {
            await delplatformList({ id: record.id });
            createMessage.success(t('common.optSuccess'));
            reload();
          },
        });
      }
      async function handEnable(id) {
        createConfirm({
          iconType: 'warning',
          title: () => h('span', '温馨提示'),
          content: '是否确认启用？',
          onOk: async () => {
            await enable({ id });
            createMessage.success(t('common.optSuccess'));
            reload();
          },
        });
      }
      return {
        registerTable,
        registerDetail,
        registerDelList,
        registerAddDetail,
        openDelListeModal,
        createMessage,
        handDelconfirm,
        t,
        reload,
        go,
        renderRoleType,
        renderStatus,
        handleCreate,
        handleOpenModal,
        register,
        handleEdit,
        handleDelete,
        uploadApi: uploadApi as any,
        RoleEnum,
        surplusSubNum,
        getCheckPerm,
        getNumByStaffData,
        handEnable,
      };
    },
  });
</script>
