<template>
  <PageWrapper dense>
    <Row>
      <!-- 左边部门选择 -->
      <Col v-bind="{ xs: 24, sm: 24, md: 24, lg: 4 }" class="h-[calc(100vh-80px)]">
        <DeptTree
          v-model:selectDeptId="selectDeptId"
          v-model:searchValue="searchValue"
          @select="reload"
        />
      </Col>
      <Col v-bind="{ xs: 24, sm: 24, md: 24, lg: 20 }">
        <BasicTable @register="registerTable">
          <!-- <template #toolbar>
            <a-button @click="handleImport" v-auth="'system:user:import'">导入</a-button>
            <a-button
              @click="
                downloadExcel(infoExport, '电梯基础信息;数据', getForm().getFieldsValue())
              "
              v-auth="'elevator:info:export'"
              >导出</a-button
            >
            <a-button
              type="primary"
              danger
              @click="multipleRemove(infoRemove)"
              :disabled="!selected"
              v-auth="'elevator:info:remove'"
              >删除</a-button
            >
            <a-button type="primary" @click="handleAdd" v-auth="'elevator:info:add'"
              >新增</a-button
            >
          </template> -->
          <template #bodyCell="{ column, record }">
            <template v-if="column.dataIndex === 'isLine'">
              <div :style="{ color: record.isLine === 1 ? 'green' : 'red' }">
                {{ record.isLine === 1 ? '在线' : record.isLine === 0 ? '离线' : '未知' }}
              </div>
            </template>
            <template v-if="column.key === 'action'">
              <TableAction
                stopButtonPropagation
                :actions="[
                  {
                    label: '进入监控',

                    type: 'primary',
                    ghost: true,
                    
                    onClick: handleJoinVideo.bind(null, record),
                  },
                ]"
              />
            </template>
          </template>
        </BasicTable>
      </Col>
    </Row>
    <InfoModal @register="registerModal" @reload="reload" />
    <UserImportModal @register="registerImportModal" @reload="reload" />
  </PageWrapper>
</template>

<script setup lang="ts">
import { Row, Col, Space, Avatar } from 'ant-design-vue';
import { PageWrapper } from '@/components/Page';
import { BasicTable, useTable, TableAction } from '@/components/Table';
import { infoList, infoExport, infoRemove } from '@/api/basicInfo/elevator';
import { downloadExcel } from '@/utils/file/download';
import { useModal } from '@/components/Modal';
import InfoModal from './InfoModal.vue';
import { formSchemas, columns } from './info.data';
import { IconEnum } from '@/enums/appEnum';
import { ref, toValue } from 'vue';
import DeptTree from './DeptTree.vue';
import UserImportModal from './UserImportModal.vue';
import { vidiconInfoList } from '@/api/netCamera/netCameraManage';
import { getElevatorToken, getJingMenElevatorToken } from '@/api/auth';

defineOptions({ name: 'Info' });
// 左边部门用
const selectDeptId = ref<string[]>([]);
const searchValue = ref<string>('');
const [registerTable, { reload, multipleRemove, selected, getForm }] = useTable({
  rowSelection: {
    type: 'checkbox',
  },
  title: '轿厢视屏列表',
  api: vidiconInfoList,
  showIndexColumn: true,
  rowKey: 'id',
  useSearchForm: true,
  formConfig: {
    schemas: formSchemas,
    baseColProps: {
      xs: 24,
      sm: 24,
      md: 24,
      lg: 6,
    },
    resetFunc: async () => {
      selectDeptId.value = [];
      searchValue.value = '';
      await reload();
    },
  },
  columns: columns,
  // 需要添加上部门参数
  beforeFetch(params: Recordable) {
    if (toValue(selectDeptId).length === 1) {
      params.deptId = toValue(selectDeptId)[0];
    }
    return params;
  },
  actionColumn: {
    width: 200,
    title: '操作',
    key: 'action',
    fixed: 'right',
  },
});

const [registerModal, { openModal }] = useModal();
const [registerImportModal, { openModal: openImportModal }] = useModal();
function handleEdit(record: Recordable) {
  openModal(true, { record, update: true });
}

function handleAdd() {
  openModal(true, { update: false });
}
function handleImport() {
  openImportModal(true);
}
async function handleDelete(record: Recordable) {
  await infoRemove([record.id]);
  await reload();
}
async function handleJoinVideo(data) {
  const res = data.areaCode === '1' ? await getElevatorToken() : await getJingMenElevatorToken();

  const accessToken = res.obj.accessToken;
  const url1 = `https://ele.secp.com.cn/androidui/CameraInfo.html?accessToken=${accessToken}&eleId=${data.eleId}&ruantongFlag=1&ptzControl=1&status=1#index_section`;
  const url2 = `https://dt.hbyuan.com.cn//androidui/CameraInfo.html?accessToken=${accessToken}&eleId=${data.eleId}&ruantongFlag=1#index_section`;

  console.log(url1,url2);
  window.open(data.areaCode === '1' ?url1:url2);
}
</script>

<style scoped></style>
