<template>
  <div class="app-container">
    <el-card class="top-leader" shadow="never">
      <div class="now-leader">
        <div>带班领导:{{ leaderList.leader | leaderFilter }}</div>
        <div>系统监控总人数:{{ leaderList.systemMonitorAllCount }} 人</div>
        <div>下井总人数:{{ total }} 人</div>
        <div>地面井口人数:{{ leaderList.groundWellCount }} 人</div>
        <div>重点区域人数:{{ leaderList.keyAreaPopulation }} 人</div>
        <!-- <div>部门:{{ findLabelByValue(deptList,'deptId',leaderList.department,'deptName')  }}</div>
          <div>是否下井:{{ leaderList.whether==0?'是':'否' }}</div>
          <div>下井时间:{{ parseTime(leaderList.downholeTime, '{y}-{m}-{d} {h}:{i}:{s}')}}</div>
          <div>上井时间:{{ parseTime(leaderList.upholeTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</div> -->
      </div>
    </el-card>
    <el-divider></el-divider>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="" prop="perName">
        <el-input
          v-model="queryParams.perName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="" prop="departId">
        <el-select
          v-model="queryParams.departId"
          placeholder="请选择部门"
          clearable
        >
          <el-option
            v-for="dict in DepartmentList"
            :key="dict.value"
            :label="dict.name"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="addressId">
        <el-select
          v-model="queryParams.addressId"
          placeholder="请选择分站"
          clearable
        >
          <el-option
            v-for="dict in StationList"
            :key="dict.value"
            :label="dict.name"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="areaId">
        <el-select
          v-model="queryParams.areaId"
          placeholder="请选择区域"
          clearable
        >
          <el-option
            v-for="dict in AreaList"
            :key="dict.value"
            :label="dict.name"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="perList">
      <el-table-column label="姓名" align="center" prop="perName" />
      <el-table-column label="卡号" align="center" prop="cardNumber" />
      <el-table-column label="部门" align="center" prop="departName" />
      <el-table-column label="分站名称" align="center" prop="locationName" />
      <!-- <el-table-column label="到达位置" align="center" prop="localtion" />
      <el-table-column label="来向" align="center" prop="" /> -->
      <el-table-column label="时间" align="center" prop="lastTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="进入区域" align="center" prop="areaName" />

      <el-table-column label="入井时间" align="center" prop="entryTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="距离分站（m）" align="center" prop="distance" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-divider></el-divider>

    <div>
      <sec></sec>
    </div>
  </div>
</template>

<script>
import { listPer } from "@/api/tyler/perReal";
import {
  baseInfoDepartment,
  baseInfoArea,
  baseInfoStation,
  baseInfoWorkType,
  baseInfoPosition,
  baseInfoCardNumber,
} from "@/api/tyler/common";
import sec from "./sec.vue";
export default {
  name: "Per",
  components: { sec },
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //带班领导
      leaderList: [],
      // 人员定位实时数据表格数据
      perList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        perName: null,
        cardNumber: null,
        monitoringStation1: null,
        monitoringStation2: null,
        entryTime: null,
        latestTime: null,
        entryFlag: null,
        timeoutFlag: null,
        area: null,
        abnormalAlarmTime: null,
        abnormalFlag: null,
        abnormalAreaId: null,
        oldStationTime: null,
        personnelAlarmId: null,
        personnelAlarmTime: null,
        personnelAlarmLocationId: null,
        arrivalStationTime: null,
        entryStation: null,
        trajectoryTime: null,
        timeoutAlarmTime: null,
        distance: null,
        xCoord: null,
        yCoord: null,
        zCoord: null,
      },
      // 表单参数
      form: {},
      // common
      //卡号列表
      cardNoList: [],
      //部门列表
      DepartmentList: [],
      //分站列表
      StationList: [],
      //区域列表
      AreaList: [],
    };
  },
  created() {
    this.getCommon();
    this.getList();
    // 启动定时器，每隔 20秒调用一次 getList
    this.timerId = setInterval(() => {
      this.getList();
    }, 20000); // 20000 毫秒
  },
  filters: {
    leaderFilter(value) {
      if (value === null || value === undefined) return "--";
      if (value === "开" || value === "关") {
        return value;
      }
      const parsedValue = Number(value);
      if (isNaN(parsedValue)) return "--";

      // 整数判断
      if (Number.isInteger(parsedValue)) {
        return parsedValue;
      }
      return value;
    },
  },
  filters: {
    leaderFilter(value) {
      if (value == "暂无排班") {
        return "暂无排班";
      }
      return (value || []).join("、");
    },
  },
  methods: {
    // common

    getCommon() {
      //卡号
      baseInfoCardNumber().then((res) => {
        this.cardNoList = res.data;
      });
      //部门
      baseInfoDepartment().then((res) => {
        this.DepartmentList = res.data;
      });
      //分站
      baseInfoStation().then((res) => {
        this.StationList = res.data;
      });
      //区域
      baseInfoArea().then((res) => {
        this.AreaList = res.data;
      });
    },
    /** 查询人员定位实时数据列表 */
    getList() {
      this.loading = true;
      listPer(this.queryParams).then((response) => {
        this.perList = response.rows;
        this.leaderList = response.remark;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        perName: null,
        departId: null,
        addressId: null,
        areaId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/per/export",
        {
          ...this.queryParams,
        },
        `per_${new Date().getTime()}.xlsx`
      );
    },
  },
  beforeDestroy() {
    // 清除定时器，避免内存泄漏
    if (this.timerId) {
      clearInterval(this.timerId);
      this.timerId = null;
    }
  },
};
</script>
<style lang="scss" scoped>
.now-leader {
  display: flex;
  justify-content: space-between;
}
</style>
