<template>
  <PageWrapper dense>
    <Row>
      <!-- 左边部门选择 -->
      <Col v-bind="{ xs: 24, sm: 24, md: 24, lg: 4 }" class="h-[calc(100vh-80px)]">
        <VidiconInfoDeptTree
          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(
                  vidiconInfoExport,
                  '网络摄像机信息;数据',
                  getForm().getFieldsValue()
                )
              "
              v-auth="'elevator:vidiconInfo:export'"
              >导出</a-button
            >
            <a-button
              type="primary"
              danger
              @click="multipleRemove(vidiconInfoRemove)"
              :disabled="!selected"
              v-auth="'elevator:vidiconInfo:remove'"
              >删除</a-button
            >
            <a-button type="primary" @click="handleCheckDevice">同步设备</a-button>
            <a-button type="primary" @click="handleCheckStatus">同步状态</a-button>
            <a-button
              type="primary"
              @click="handleAddDevice"
              v-auth="'blackBox:blackBoxInfo:add'"
              >终端分配</a-button
            >
            <a-button type="primary" @click="handleAdd" v-auth="'elevator:vidiconInfo: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,
                    auth: 'vidicon:vidiconInfo:query',
                    onClick: handCheck.bind(null, record),
                  },
                  {
                    label: '修改',

                    type: 'primary',
                    ghost: true,
                    auth: 'elevator:vidiconInfo:edit',
                    onClick: handleEdit.bind(null, record),
                  },
                  {
                    label: '删除',

                    type: 'primary',
                    danger: true,
                    ghost: true,
                    auth: 'elevator:vidiconInfo:remove',
                    popConfirm: {
                      placement: 'left',
                      title: '是否删除网络摄像机信息;[' + record.id + ']?',
                      confirm: handleDelete.bind(null, record),
                    },
                  },
                ]"
                :dropDownActions="[
                  {
                    label: '开启控门',
                    icon: 'mingcute:department-line',
                    auth: '',
                    onClick: handleOpenAuth.bind(null, record),
                  },
                  {
                    label: '关闭控门',
                    icon: 'mingcute:department-line',
                    auth: '',
                    onClick: handleCloseAuth.bind(null, record),
                  },
                  {
                    label: '人体关键点参数',
                    icon: 'mingcute:department-line',
                    auth: '',
                    onClick: handleBodyAccess.bind(null, record),
                  },
                  {
                    label: '拍照',
                    icon: 'mingcute:department-line',
                    auth: '',
                    onClick: handlePhotoModal.bind(null, record),
                  },
                  {
                    label: '设置检测区域',
                    icon: 'mingcute:department-line',
                    auth: '',
                    onClick: handleSetAreaModal.bind(null, record),
                  },
                  {
                    label: '查看检测区域',
                    icon: 'mingcute:department-line',
                    auth: '',
                  },
                  {
                    label: '清空检测区域',
                    icon: 'mingcute:department-line',
                    auth: '',
                  },
                ]"
              />
            </template>
          </template>
        </BasicTable>
      </Col>
    </Row>

    <VidiconInfoModal @register="registerModal" @reload="reload" />
    <commonDeviceDistrbuteModal @register="registerDeviceModal" />
    <bodyPointAccessModal @register="registerBodyModal" />
    <photoModal @register="registerPhotoModal" />
    <setCheckAreaModal @register="registerSetAreaModal" />
  </PageWrapper>
</template>

<script setup lang="ts">
import { Row, Col, Modal } from "ant-design-vue";
import { PageWrapper } from "@/components/Page";
import { BasicTable, useTable, TableAction } from "@/components/Table";
import commonDeviceDistrbuteModal from "@/components/CommonDeviceDistrbuteModal/UserManageModal.vue";
import bodyPointAccessModal from "./bodyPointAccessModal.vue";
import setCheckAreaModal from "./setCheckAreaModal.vue";
import photoModal from "./photoModal.vue";
import {
  vidiconInfoList,
  vidiconInfoExport,
  vidiconInfoRemove,
} from "@/api/netCamera/netCameraManage";
import { downloadExcel } from "@/utils/file/download";
import { useModal } from "@/components/Modal";
import VidiconInfoModal from "./VidiconInfoModal.vue";
import { formSchemas, columns } from "./vidiconInfo.data";
import { IconEnum } from "@/enums/appEnum";
import VidiconInfoDeptTree from "./VidiconInfoDeptTree.vue";
import { ref, toValue } from "vue";

defineOptions({ name: "VidiconInfo" });
// 左边部门用
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: 250,
    title: "操作",
    key: "action",
    fixed: "right",
  },
});

const [registerModal, { openModal }] = useModal();
const [registerDeviceModal, { openModal: openDeviceModal }] = useModal();
const [registerBodyModal, { openModal: openBodyModal }] = useModal();
const [registerPhotoModal, { openModal: openPhotoModal }] = useModal();
const [registerSetAreaModal, { openModal: openSetAreaModal }] = useModal();
function handleSetAreaModal() {
  openSetAreaModal(true);
}
function handleAddDevice() {
  openDeviceModal(true, { update: 2 });
}
function handlePhotoModal() {
  openPhotoModal(true);
}
function handleBodyAccess() {
  openBodyModal(true);
}
function handleCheckDevice() {
  Modal.confirm({
    title: "同步设备需要一些时间,您确定要进行该操作？",
    content: "",
    okText: "确定",
    okType: "danger",
    cancelText: "取消",
    onOk() {
      console.log("OK");
    },
    onCancel() {
      console.log("Cancel");
    },
  });
}
function handleCheckStatus() {
  Modal.confirm({
    title: "同步状态需要一些时间,您确定要进行该操作？",
    content: "",
    okText: "确定",
    okType: "danger",
    cancelText: "取消",
    onOk() {
      console.log("OK");
    },
    onCancel() {
      console.log("Cancel");
    },
  });
}
function handleOpenAuth() {
  Modal.confirm({
    title: "开启控门需要一些时间,您确定要进行该操作？",
    content: "",
    okText: "确定",
    okType: "danger",
    cancelText: "取消",
    onOk() {
      console.log("OK");
    },
    onCancel() {
      console.log("Cancel");
    },
  });
}
function handleCloseAuth() {
  Modal.confirm({
    title: "关闭控门需要一些时间,您确定要进行该操作？",
    content: "",
    okText: "确定",
    okType: "danger",
    cancelText: "取消",
    onOk() {
      console.log("OK");
    },
    onCancel() {
      console.log("Cancel");
    },
  });
}
function handleEdit(record: Recordable) {
  openModal(true, { record, update: 1 });
}

function handleAdd() {
  openModal(true, { update: 2 });
}
function handCheck(record: Recordable) {
  openModal(true, { record, update: 3 });
}
async function handleDelete(record: Recordable) {
  await vidiconInfoRemove([record.id]);
  await reload();
}
</script>

<style scoped></style>
