<template>
  <div class="app-container home">
    <TabTitle :text="nowText" />
    <el-row :gutter="10">
      <el-col :xs="0" :sm="2" :md="3" :lg="4">
        <el-card class="left-list" :style="{height:globalScreenHeight+'px'}">
          <div class="head-container">
            <el-input
              v-model="deptName"
              placeholder="请输入部门名称"
              clearable
              prefix-icon="Search"
              style="margin-bottom: 20px"
            />
          </div>
          <div class="head-container">
            <el-tree
              :data="deptOptions"
              :props="{ label: 'label', children: 'children' }"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="deptTreeRef"
              node-key="id"
              highlight-current
              default-expand-all
              @node-click="handleNodeClick"
            >
            <template #default="{ node, data }">
              <span class="custom-tree-node">
                <el-icon v-if="handjudge(data)"><MapLocation /></el-icon> 
                <el-icon v-else><VideoCamera /></el-icon>
                &nbsp;
                <span>{{ node.label }}</span>
              </span>
            </template>
            </el-tree>
          </div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="16" :md="16" :lg="14">
          <el-card class="right-list" :style="{height:globalScreenHeight+'px'}">
            <template v-slot:header>
              <div class="cleartitle" style="justify-content: flex-start">
                <img src="@/assets/images/logo_video.png" width="25px" alt />
                <span>视频预览</span>
              </div>
            </template>
            <!-- <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="primary" plain @click="handleUpdate">一分屏</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button type="primary" plain @click="handleUpdate">四分屏</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button type="primary" plain @click="handleDelete">六分屏</el-button>
              </el-col>
            </el-row> -->
            <div class="alg-list">
              <el-card
                class="alg-item"
                shadow="hover"
              >
                <template v-slot:default>
                  <!-- <img src="@/assets/images/picStream.png" style="width: 100%" /> -->
                  <canvas id="video" :style="{width: '100%', maxHeight: (globalScreenHeight-120)+'px'}" ></canvas>
                </template>
              </el-card>
            </div>
          </el-card>
      </el-col>
      <el-col :xs="0" :sm="2" :md="3" :lg="6">
        <el-scrollbar :height="globalScreenHeight+'px'">
        <el-card class="danger-list" :style="{minHeight:globalScreenHeight+'px'}">
          <template v-slot:header>
            <div class="cleartitle" style="justify-content: flex-start">
              <img src="@/assets/images/logo_video.png" width="25px" alt />
              <span>实时预警</span>
            </div>
          </template>
          <div class="alg-list">
              <el-card
                v-for="item in algorithmList"
                :key="item.name"
                class="alg-item"
                shadow="hover"
              >
              <template v-slot:default>
                <ImagePreview :src="item.alarmImg" style="width: 100%" />
                <el-form :model="form" label-width="100px">
                  <!-- <el-form-item label="预警设备：">{{ form.title }} </el-form-item>
                  <el-form-item label="登录信息：">{{ form.title }}</el-form-item>
                  <el-form-item label="预警区域：">{{ form.title }}</el-form-item> -->
                  <el-form-item label="预警类型：">{{ item.alarmType }}</el-form-item>
                  <el-form-item label="预警时间：">{{  parseTime(item.alarmTime)  }}</el-form-item>
                  <el-form-item label="预警等级："><el-tag :color="item.alarmLevelColor"  type="info" effect="dark">{{ item.alarmLevelName+'预警'}} </el-tag></el-form-item>
               </el-form>
              </template>
              </el-card>
            </div>
        </el-card>
        </el-scrollbar>
      </el-col>
    </el-row>
  </div>
</template>
  
<script setup name="Index">
import { ArrowDown } from "@element-plus/icons-vue";
import { Search } from "@element-plus/icons-vue";
import '@/utils/jsmpeg.min'
import { listAiRegionManage,videoTreeListManage,idDevice } from "@/api/videoControl/videoPreview";
import { listAlarm } from "@/api/alarmControl/index.js";
import { onMounted, reactive, ref, } from "vue";
import { el } from "element-plus/es/locales.mjs";
const { proxy } = getCurrentInstance();
const globalScreenHeight = inject('globalScreenHeight');
const nowText = ref("视频预览");
const deptTreeRef = ref(null);
const deptName = ref("");
const deptOptions = ref(undefined);
const algorithmList = ref([]);
const player=ref(null);
const data = reactive({
  form: {
    title:'测试测试测试'
  },
  queryParams: {
    postCode: undefined,
    searchValue: "",
    status: undefined,
  },
  rules: {
    postName: [
      { required: true, message: "岗位名称不能为空", trigger: "blur" },
    ],
    postCode: [
      { required: true, message: "岗位编码不能为空", trigger: "blur" },
    ],
    postSort: [
      { required: true, message: "岗位顺序不能为空", trigger: "blur" },
    ],
  },
});
const { queryParams, form, rules } = toRefs(data);
/** 通过条件过滤节点  */

onMounted(() => {
  // player.value.play()//播放
  getTreeData()
  getWaringData()
  getVideos()
})

onDeactivated(() => {
  player.value.destroy();
  player.value = null;
})

watch(deptName, (val) => {
  deptTreeRef.value.filter(val)
})

const filterNode = (value, data) => {
  if (!value) return true;
  return data.label.indexOf(value) !== -1;
};
function getTreeData() {
  videoTreeListManage().then((res) => {
      deptOptions.value=res.data
  })
}

function getVideos(str){
    let canvas = document.getElementById('video')
    let url = str?str:'rtsp://192.168.20.211:554/av0_0'
    if (player.value) {
      player.value.destroy();
      player.value = null;
    }
    player.value = new JSMpeg.Player("ws://192.168.4.232:9999/rtsp?url="+btoa(url), {canvas: canvas})
    player.value.play()
 
}

function handleNodeClick(row) {
  const treeType = row.id.split("-")[0];
  const treeID = row.id.split("-")[1];
  if(treeType=='点位'){
    handlvideoPath(treeID)
    queryParams.value.locationId = treeID;
    queryParams.value.regionId = null;
  }else{
    queryParams.value.regionId = treeID;
    queryParams.value.locationId = null;
  }
  clearTimeout(timeoutId);
  getWaringData()
}
function handjudge(row) {
  const treeType = row.id.split("-")[0];
  if(treeType=='区域'){
    return true 
  }else{
    return false
  }
  

}

function handlvideoPath(id) {
  idDevice(id).then((res) => {
    getVideos(res.data.videoUrl)
  })
}

let timeoutId = null;
function getWaringData() {
  listAlarm(queryParams.value).then((res) => {
    algorithmList.value=res.rows
    timeoutId = setTimeout(getWaringData, 30000);
  }).catch((error) => {
    // 请求失败，重新尝试
    timeoutId = setTimeout(getWaringData, 30000);
  });
}
onUnmounted(() => {
  // 清除定时器
  if (timeoutId) {
    clearTimeout(timeoutId);
  }
});
</script>
  
<style scoped lang="scss">
.left-list {
  min-height: 700px;
}
.right-list {
  min-height: 700px;
  .alg-list {
    display: grid;
    // grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 16px;
    .el-card {
      border: 1px soild #ff6700;
    }
    .alg-item {
      width: 100%;
      // min-height: 350px;
      cursor: pointer;
      position: relative;
    }
  }
}
.danger-list{
  .alg-list {
    display: grid;
    grid-template-columns: repeat(minmax(180px, 1fr));
    gap: 16px;
    .el-card {
      border: 1px soild #ff6700;
    }
    .alg-item {
      width: 100%;
      // min-height: 350px;
      cursor: pointer;
      position: relative;
    }
    .el-form-item--default,.el-form-item {
      font-size: 12px;
      margin-bottom: 0px;
    }
  }
}

.cleartitle {
  font-size: 16px;
  font-weight: 600;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
  
  