<template>
  <div class="device">
    <div class="device-head">
      <div class="title">设备列表</div>
      <el-select class="select" v-model="mode" @change="handelMode">
        <el-option label="全部" value="" />
        <el-option label="常规模式" value="0" />
        <el-option label="审讯模式" value="1" />
        <el-option label="户外押送" value="2" />
      </el-select>
    </div>
    <template v-if="deviceData.length">
      <div v-infinite-scroll="handelLoad" :infinite-scroll-immediate="false" class="device-list noScrollbar infinite-list" style="overflow: auto">
        <el-popover :width="350" class="box-item" placement="right" v-for="item in deviceData" :key="item.id">
          <template #reference>
            <div class="item" :class="{ active: deviceId === item.deviceId }" @click="handelItem(item)">
              <div class="item-img">
                <img src="@/assets/img/handcuffs.png" alt="" srcset="" />
              </div>
              <div class="item-content">
                <div class="item-content-name">手铐-{{ item.id }}</div>
                <div class="item-content-num">{{ item.deviceId }}</div>
              </div>
              <div class="item-right">
                <div class="battery">
                  <div class="battery-icon">
                    <img src="@/assets/img/battery.png" alt="" srcset="" />
                  </div>
                  <div class="battery-num">{{ item.battery }}%</div>
                </div>
                <div class="user">{{ item.userNumber || "--" }}</div>
              </div>
            </div>
          </template>
          <template #default>
            <div class="demo-rich-conent">
              <div>
                <span class="lable">类型：</span>
                {{ item.adminType }}
              </div>
              <div>
                <span class="lable">用户名称：</span>
                {{ item.adminName }}
              </div>
              <div>
                <span class="lable">警员号：</span>
                {{ item.adminUsername }}
              </div>
              <div>
                <span class="lable">联系电话：</span>
                {{ item.adminPhone }}
              </div>
              <div>
                <span class="lable">状态：</span>
                <el-tag :type="statusColor[item.status]">{{ statusEnum[item.status] }}</el-tag>
              </div>
              <div>
                <span class="lable">模式：</span>
                <el-tag :type="modeColor[item.mode]">{{ modeEnum[item.mode] }}</el-tag>
              </div>
              <div>
                <span class="lable">佩戴者：</span>
                {{ item.userNumber || "--" }}
              </div>
            </div>
          </template>
        </el-popover>
      </div>
    </template>
    <template v-else>
      <el-empty description="暂无数据" />
    </template>
  </div>
</template>
<script setup lang="ts">
import { TDevice } from "@/api/index.d";
import { ref, watch, reactive, onMounted } from "vue";
import { deviceList } from "@/api/index";

const statusColor = ["danger", "success", "warning"];
const modeColor = ["primary", "danger", "warning"];
enum statusEnum {
  "离线" = 0,
  "在线",
  "充电中",
}
enum modeEnum {
  "常规",
  "审讯模式",
  "户外押送",
}
const emit = defineEmits(["click"]);
const mode = ref("");

const devicePaging = reactive({
  page: 1,
  size: 10,
  mode: undefined,
  useStatus: 1,
  monitor: 1,
});
const deviceData = ref<TDevice.IListRes[]>([]);
const loadFlag = ref(false);
const deviceId = ref(0);

const getdeviceList = async () => {
  const res = await deviceList(devicePaging);
  deviceData.value = [...deviceData.value, ...res.records];
  if (deviceData.value.length >= res.total) {
    loadFlag.value = true;
  }
  if (res.records.length > 0) {
    deviceId.value = res.records[0].deviceId;
    emit("click", res.records[0]);
  }
};

const handelLoad = () => {
  if (loadFlag.value) return;
  devicePaging.page += 1;
  getdeviceList();
};
const handelMode = (e) => {
  devicePaging.mode = e;
  devicePaging.page = 1;
  deviceData.value = [];
  getdeviceList();
};
const handelItem = (item) => {
  deviceId.value = item.deviceId;
  emit("click", item);
};
onMounted(() => {
  getdeviceList();
});
</script>
<style scoped lang="less">
.device {
  width: 100%;
  flex-shrink: 0;
  overflow: hidden;
  background: #ffffff;
  display: flex;
  flex-direction: column;
  height: 100%;

  .device-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    margin: 14px 0;
    .title {
      color: #061451;
      font-size: 18px;
      &::before {
        display: inline-block;
        margin-right: 10px;
        content: "";
        width: 2px;
        height: 14px;
        border-radius: 20px;
        background: #061451;
      }
    }
    .select {
      width: 120px;
      margin-right: 20px;
    }
  }
  .device-list {
    flex: 1;
    overflow: auto;
    list-style: none;
    .item {
      height: 89px;
      background: #ffffff;
      display: flex;
      align-items: center;
      box-sizing: border-box;
      border-bottom: 1px solid #dedede;
      cursor: pointer;
      &.active {
        background: #f5f5f5;
      }
      &:hover {
        background: #f5f5f5;
      }
      .item-img {
        width: 50px;
        height: 50px;
        margin-left: 20px;
        flex-shrink: 0;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .item-content {
        flex: 1;
        margin: 0 15px;
        .item-content-name {
          color: #061451;
          font-size: 18px;
        }
        .item-content-num {
          color: #787878;
          font-size: 16px;
        }
      }
      .item-right {
        margin-right: 20px;
        .battery {
          display: flex;
          align-items: center;
          .battery-icon {
            width: 24px;
            height: 24px;
            margin-right: 5px;
            img {
              width: 100%;
              height: 100%;
            }
          }
          .battery-num {
            color: #787878;
            font-size: 16px;
          }
        }
        .user {
          color: #787878;
          font-size: 16px;
          text-align: right;
          width: 80px;
          white-space: nowrap; // 强制一行显示
          overflow: hidden; // 超出隐藏
          text-overflow: ellipsis;
        }
      }
    }
  }
}
.demo-rich-conent {
  display: flex;
  flex-direction: column;

  .lable {
    display: inline-block;
    width: 90px;
    text-align: right;
    margin-top: 10px;
  }
}
</style>
