<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="
                downloadExcel(blackBoxInfoExport, '黑匣子信息;数据', getForm().getFieldsValue())
              "
              v-auth="'elevator:blackBoxInfo:export'"
              >导出</a-button
            >
            <!-- <a-button
              type="primary"
              danger
              @click="multipleRemove(blackBoxInfoRemove)"
              :disabled="!selected"
              v-auth="'blackBox:blackBoxInfo:remove'"
              >删除</a-button
            > -->
            <a-button type="primary" @click="handleAdd" v-auth="'blackBox:blackBoxInfo: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 ? '在线' : '离线' }}
              </div>
            </template>
            <template v-if="column.dataIndex === 'isElevator'">
              <div>
                {{ record.isElevator === 1 ? '是' : '否' }}
              </div>
            </template>
            <template v-if="column.dataIndex === 'energyType'">
              <div>
                {{ record.energyType === 1 ? '节能' : '非节能' }}
              </div>
            </template>
            <template v-if="column.dataIndex === 'startType'">
              <div>
                {{
                  record.startType === 1 ? '正常' : record.startType === 2 ? '启动慢' : '启动特慢'
                }}
              </div>
            </template>
            <template v-if="column.key === 'action'">
              <TableAction
                stopButtonPropagation
                :actions="[
                  {
                    label: '查看',

                    type: 'primary',
                    ghost: true,
                    auth: 'elevator:blackBoxInfo:query',
                    onClick: handCheck.bind(null, record),
                  },
                  {
                    label: '修改',

                    type: 'primary',
                    ghost: true,
                    auth: 'elevator:blackBoxInfo:edit',
                    onClick: handleEdit.bind(null, record),
                  },
                ]"
                :dropDownActions="[
                  {
                    label: '同步',
                    icon: 'elevator:blackBoxInfo:asnyc',
                    auth: '',

                    onClick: handleOpenAuth.bind(1, record),
                  },
                  {
                    label: '同步状态',
                    icon: 'elevator:blackBoxInfo:asnycStatus',
                    auth: '',

                    onClick: handleOpenStatus.bind(2, record),
                  },

                  {
                    label: '实时数据',
                    icon: 'mingcute:department-line',
                    auth: '',

                    onClick: handleOpenData.bind(4, record),
                  },
                  {
                    label: '运行信息',
                    icon: 'mingcute:department-line',
                    auth: '',

                    onClick: handleOpenRun.bind(5, record),
                  },
                ]"
              />
            </template>
          </template>
        </BasicTable> </Col
    ></Row>

    <BlackBoxInfoModal @register="registerModal" @reload="reload" />
    <runInfoModal @register="registerRunDataModal" />
    <realTimeData @register="registerRealTimeModal" />
    <commonDeviceDistrbuteModal @register="registerDeviceModal" />
  </PageWrapper>
</template>

<script setup lang="ts">
import { PageWrapper } from '@/components/Page';
import { BasicTable, useTable, TableAction } from '@/components/Table';
import {
  blackBoxInfoList,
  blackBoxInfoExport,
  blackBoxInfoRemove,
} from '@/api/blackBoxManage/blackBoxTim';
import DeptTree from './DeptTree.vue';
import { downloadExcel } from '@/utils/file/download';
import { useModal } from '@/components/Modal';
import BlackBoxInfoModal from './BlackBoxInfoModal.vue';
import { formSchemas, columns } from './blackBoxInfo.data';
import { Modal, Row, Col } from 'ant-design-vue';
import runInfoModal from './runInfoModal.vue';
import realTimeData from './realTimeData.vue';
import deviceDistrubuteModal from './deviceDistrubuteModal.vue';
import commonDeviceDistrbuteModal from '@/components/CommonDeviceDistrbuteModal/UserManageModal.vue';
import { ref, toValue } from 'vue';
defineOptions({ name: 'BlackBoxInfo' });
const selectDeptId = ref<string[]>([]);
const searchValue = ref<string>('');
const [registerTable, { reload, multipleRemove, selected, getForm }] = useTable({
  rowSelection: {
    type: 'checkbox',
  },
  title: '黑匣子信息列表',
  api: blackBoxInfoList,
  showIndexColumn: false,
  rowKey: 'id',
  useSearchForm: true,
  formConfig: {
    schemas: formSchemas,
    labelWidth: 120,
    baseColProps: {
      xs: 24,
      sm: 24,
      md: 24,
      lg: 8,
    },
    resetFunc: async () => {
      selectDeptId.value = [];
      searchValue.value = '';
      await reload();
    },
  },
  // 需要添加上部门参数
  beforeFetch(params: Recordable) {
    if (toValue(selectDeptId).length === 1) {
      params.deptId = toValue(selectDeptId)[0];
    }
    return params;
  },
  columns: columns,
  actionColumn: {
    width: 250,
    title: '操作',
    key: 'action',
    fixed: 'right',
  },
});

const [registerModal, { openModal }] = useModal();
const [registerRunDataModal, { openModal: openRunDataModal }] = useModal();
const [registerRealTimeModal, { openModal: openRealTimeModal }] = useModal();
const [registerDeviceModal, { openModal: openDeviceModal }] = useModal();

function handleEdit(record: Recordable) {
  openModal(true, { record, update: 1 });
}
function handleOpenAuth(record: Recordable) {
  Modal.confirm({
    title: `同步数据需要一些时间,您确定要进行该操作？`,
    content: '',
    okText: '确定',
    okType: 'danger',
    cancelText: '取消',
    onOk() {
      console.log('OK');
    },
    onCancel() {
      console.log('Cancel');
    },
  });
}
function handleOpenStatus() {
  Modal.confirm({
    title: `同步状态需要一些时间,您确定要进行该操作？`,
    content: '',
    okText: '确定',
    okType: 'danger',
    cancelText: '取消',
    onOk() {
      console.log('OK');
    },
    onCancel() {
      console.log('Cancel');
    },
  });
}
function handleOpenDevice() {}
function handleOpenData() {
  openRealTimeModal(true);
}
function handleOpenRun(record: Recordable) {
  openRunDataModal(true, { record, update: 1 });
}
function handleAdd() {
  openDeviceModal(true, { update: 2 });
}
function handCheck(record: Recordable) {
  openModal(true, { record, update: 3 });
}
async function handleDelete(record: Recordable) {
  await blackBoxInfoRemove([record.id]);
  await reload();
}
</script>

<style scoped></style>
