<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备类型" prop="type">
        <el-radio-group type="button" v-model="queryParams.type" @change="handleQuery">
          <el-radio-button :label="undefined">全部</el-radio-button>
          <el-radio-button v-for="dict in device_type" :key="dict.value" :label="dict.value">{{ dict.label
          }}</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item style="float: right;">
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-form-item>
    </el-form>


    <div style="display: flex;">
      <div class="card" @click="handleVideoCameraFilled(item)" v-for="item in deviceList" :key="item.deviceId">
        <div class="card-image">
          <img :src="item.url" :alt="item.url" style="width: 100%; height: 100%;" />
          <!-- <div style="width: 100%; height: 100%;">
              {{ item.url }}
            </div> -->
        </div>
        <div class="category"><dict-tag :options="device_type" :value="item.type" /></div>
        <div class="heading">
          {{ item.ip }}
          <!-- <div class="author"> By <span class="name">Abi</span> 4 days ago</div> -->
        </div>
      </div>
    </div>

    <el-row :gutter="20">
      <el-col :span="12">
        <div style="width: 100%; height: 100%;">
          <div style="display: flex; justify-content: space-between; align-items: center;">
            <h3>告警记录 {{ alarmRecordList.length }}条</h3>
            <el-button type="primary" @click="handleAlarmRecord">查看更多</el-button>
          </div>
          <el-table :data="alarmRecordList" style="width: 100%">
            <el-table-column label="任务名称" align="center" prop="taskName" />
            <el-table-column label="设备名称" align="center" prop="deviceName" />
            <el-table-column label="报警类型" align="center" prop="alarmType" />
            <!-- <el-table-column label="报警级别" align="center" prop="alarmLevel">
              <template #default="scope">
                <el-tag v-if="scope.row.alarmLevel === '1'" type="info">低</el-tag>
                <el-tag v-else-if="scope.row.alarmLevel === '2'" type="warning">中</el-tag>
                <el-tag v-else-if="scope.row.alarmLevel === '3'" type="danger">高</el-tag>
              </template>
            </el-table-column> -->
            <el-table-column label="置信度" align="center" prop="confidence">
              <template #default="scope">
                {{ (scope.row.confidence * 100).toFixed(1) }}%
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
      <el-col :span="12">
        <div style="width: 100%; height: 100%;">
          <div style="display: flex; justify-content: space-between; align-items: center;">
            <h3>巡检记录 {{ inspectionRecordList.length }}条</h3>
            <el-button type="primary" @click="handleInspectionRecord">查看更多</el-button>
          </div>
          <el-table :data="inspectionRecordList" style="width: 100%">
            <el-table-column label="巡检任务" prop="taskName">
              <template #default="scope">
                {{ findTaskName(scope.row.taskId) }}
              </template>
            </el-table-column>
            <el-table-column label="执行时间" prop="executeTime" />
            <el-table-column label="执行时长" prop="duration" />
            <el-table-column label="执行状态" prop="status">
              <template #default="scope">
                <dict-tag :options="ins_record_status" :value="scope.row.status" />
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup name="Device">
import router from '@/router'
import { listDevice } from "@/api/video/device";
import { listInspectionRecord } from "@/api/video/insRecord";
import { listInspection } from "@/api/video/inspection";
import { listAlarm } from "@/api/video/alarm";

const { proxy } = getCurrentInstance();
const { device_on_status, device_type, ins_record_status } = proxy.useDict('device_on_status', 'device_type', 'ins_record_status');

const inspectionRecordList = ref([]);
const inspectionList = ref([]);

const findTaskName = (taskId) => {
  return inspectionList.value.find(item => item.taskId === taskId)?.taskName;
}
function getInspectionList() {
  listInspection({
    pageNum: 1,
    pageSize: 10,
  }).then(response => {
    inspectionList.value = response.rows;
  });
}
function getInspectionRecordList() {
  listInspectionRecord({
    pageNum: 1,
    pageSize: 10,
  }).then(response => {
    inspectionRecordList.value = response.rows;
  });
}
function handleInspectionRecord() {
  router.push({ path: "/insrecord" });
}
getInspectionList();
getInspectionRecordList();

function handleAlarmRecord() {
  router.push({ path: "/alarm" });
}
const alarmRecordList = ref([]);
function getAlarmRecordList() {
  listAlarm({
    pageNum: 1,
    pageSize: 10,
  }).then(response => {
    alarmRecordList.value = response.rows;
  });
}
getAlarmRecordList();

const deviceList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    type: undefined,
  },
});

const { queryParams } = toRefs(data);

const handleVideoCameraFilled = (row) => {
  router.push({ path: "/video/flv", query: { deviceId: row.deviceId } });
}

/** 查询设备列表 */
function getList() {
  loading.value = true;
  listDevice(queryParams.value).then(response => {
    deviceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.deviceId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

getList();
</script>

<style scoped>
/* From Uiverse.io by alexmaracinaru */
.card {
  width: 190px;
  background: white;
  padding: .4em;
  border-radius: 6px;
}

.card-image {
  background-color: rgb(236, 236, 236);
  width: 100%;
  height: 130px;
  text-align: center;
  word-break: break-all;
  border-radius: 6px 6px 0 0;
}

.card-image:hover {
  transform: scale(0.98);
}

.category {
  text-transform: uppercase;
  font-size: 0.7em;
  font-weight: 600;
  color: rgb(63, 121, 230);
  padding: 10px 7px 0;
}

.category:hover {
  cursor: pointer;
}

.heading {
  font-weight: 600;
  color: rgb(88, 87, 87);
  padding: 7px;
}

.heading:hover {
  cursor: pointer;
}

.author {
  color: gray;
  font-weight: 400;
  font-size: 11px;
  padding-top: 20px;
}

.name {
  font-weight: 600;
}

.name:hover {
  cursor: pointer;
}
</style>
