<template>
  <div class="app-container">

    <el-row>
      <el-col :span="15">

        <el-form :model="queryParams" :inline="true" ref="queryForm" label-width="68px">
          <el-form-item label="设备名称" prop="deviceName">
            <el-input
              v-model="queryParams.deviceName"
              placeholder="请输入设备名称"
              clearable
              size="small"
              style="width: 180px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="巡检人" prop="inspector">
            <el-input
              v-model="queryParams.inspector"
              placeholder="请输入巡检人"
              clearable
              size="small"
              style="width: 180px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="巡检状态" prop="inspectStatus">
            <el-select
              v-model="queryParams.inspectStatus"
              palceholder="请选择巡检状态"
              clearable
              size="small"
              style="width: 180px"
              @keyup.enter.native="handleQuery"
            >
              <el-option label="未巡检" value= "0" />
              <el-option label="已巡检" value= "1"/>
            </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="inspectList">
          <el-table-column label="设备名称" align="center" prop="deviceName" width="100" :show-overflow-tooltip="true"/>
          <!--<el-table-column label="设备类型" align="center" prop="deviceType" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span v-if="scope.row.deviceType == 1">调压箱</span>
              <span v-else>阀门井</span>
            </template>
          </el-table-column>-->
          <el-table-column label="设备编号" align="center" prop="deviceCode" width="100" :show-overflow-tooltip="true"/>
          <!--<el-table-column label="巡检图片" align="center" prop="meterImg" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <el-image v-if="scope.row.meterImg" :src="scope.row.meterImg" :preview-src-list="[scope.row.meterImg]" :z-index="9999" style="width: 30px;height: 30px;"></el-image>
              <span v-else>-</span>
            </template>
          </el-table-column>-->
          <el-table-column label="有无隐患" align="center" prop="danger">
            <template slot-scope="scope">
              <span v-if="scope.row.danger == 0">无</span>
              <span v-else-if="scope.row.danger == 1">有</span>
              <span v-else>-</span>
            </template>
          </el-table-column>
          <el-table-column label="巡检状态" align="center" prop="inspectStatus">
            <template slot-scope="scope">
              <span v-if="scope.row.inspectStatus == 0" style="color: red">未巡检</span>
              <span v-else-if="scope.row.inspectStatus == 1" style="color: green">已巡检</span>
              <span v-else>-</span>
            </template>
          </el-table-column>
          <el-table-column label="巡检人" align="center" prop="inspector" :show-overflow-tooltip="true"/>
          <el-table-column label="巡检时间" align="center" prop="createTime" width="150" :show-overflow-tooltip="true"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleDetail(scope.row)"
              >检查单</el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleLocation(scope.row)"
              >位置</el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleDanger(scope.row)"
                v-if="scope.row.danger == 1"
              >隐患</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getInspectInfo"
        />
      </el-col>

      <el-col :span="9" style="padding-left: 10px">
        <div id="device-inspect-map"></div>
      </el-col>
    </el-row>

    <!-- 隐患信息 -->
    <el-dialog title="隐患信息" :visible.sync="dangerDetailOpen" width="1000px" append-to-body destroy-on-close>
      <!-- 隐患信息 -->
      <DangerDetailInfo ref="dangerDetail"/>
    </el-dialog>

    <!-- 详情 -->
    <InspectDetailInfo
      ref="inspectDetail"
      :type="queryParams.deviceType"
      :title="title"
    />

  </div>

</template>

<script>
  import { selectTDeviceTaskInspectInfoList } from "@/api/devicemanage/inspect";
  import { EditorMap } from "@/utils/mapClass/map";
  import { mapGetters } from "vuex";
  import DangerDetailInfo from "@/views/checktask/task/components/DangerDetailInfo";
  import InspectDetailInfo from "./components/InspectDetailInfo";
  import { listDanger } from "@/api/checktask/danger";
  export default {
    name: "device-inspect-index",
    components:{
      DangerDetailInfo,
      InspectDetailInfo
    },
    data(){
        return{
          total: 0,
          loading: false,
          // 查询参数
          queryParams: {
            pageNum: 1,
            pageSize: 10,
            taskId: null,
            deviceName: null,
            deviceType: null,
            inspector: null,
            inspectStatus: null
          },
          inspectList: [],
          gaoMap: null,
          dangerDetailOpen: false,
          title: ""
        }
    },
    mounted(){
      this.queryParams.taskId = this.$route.query.taskId;
      this.queryParams.deviceType = eval(this.$route.query.type);
      this.getInspectInfo();
      this.initMap();
    },
    methods:{
      getInspectInfo(){
        this.loading = true;
        selectTDeviceTaskInspectInfoList(this.queryParams).then(res =>{
          this.loading = false;
          if(res.code == 200){
            this.total = res.total;
            this.inspectList = res.rows;
            if(this.inspectList && this.inspectList.length > 0 && this.inspectList[0].longitude){
              this.addLocation({longitude: this.inspectList[0].longitude, latitude: this.inspectList[0].latitude,nickName:this.inspectList[0].createTime,iconType:"deviceInspect"});
            }
          }
        })
      },
      //地图初始化
      initMap(){
        const path = eval(this.systemSetting.map_center);
        this.gaoMap = new EditorMap(
          "device-inspect-map",
          {
            center: path,
            zoom: 12,
          },
          this
        );
      },
      //详情
      handleDetail(row){
        this.title = row.deviceName;
        this.$refs.inspectDetail.openDetail(row);
      },
      //位置
      handleLocation(row){
        if(!row.longitude) return this.$message.error("未查到位置信息！")
        this.addLocation({longitude: row.longitude, latitude: row.latitude,nickName:row.createTime,iconType:"deviceInspect"})
      },
      //隐患
      // handleDanger(row){
      //   this.dangerDetailOpen = true;
      //   this.$nextTick(() => {
      //     this.$refs.dangerDetail.queryParams.inspectId = row.id;
      //     this.$refs.dangerDetail.queryParams.type = 6;
      //     this.$refs.dangerDetail.getList();
      //   })
      // },
      //隐患
      handleDanger(row) {
        listDanger({pageNum: 1,pageSize: 99999,inspectId:row.id,type:6}).then((response) => {
          if(response.code != 200 || response.rows == null || response.rows.length == 0){
          this.$message.error("未查到隐患信息！");
          return;
        }
        this.dangerDetailOpen = true;
        this.$nextTick(() => {
          this.$refs.dangerDetail.getList(response);
      });
      })
        /* this.dangerDetailOpen = true;
         this.$nextTick(() => {
           this.$refs.dangerDetail.queryParams.inspectId = row.id;
           this.$refs.dangerDetail.getList();
         });*/
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getInspectInfo();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      //位置上图
      addLocation(data){
          this.gaoMap.allDevice["deviceInspect"]?.forEach((item) =>{
            this.gaoMap.map.remove(item);
        })
        this.gaoMap.control= 2;
        this.gaoMap.addDevice(data,null);
        this.gaoMap.map.panTo([data.longitude, data.latitude]);
      },
    },
    computed: {
        ...mapGetters(["systemSetting"])
    },
  }
</script>

<style scoped lang="scss">
  #device-inspect-map{
    width: 100%;
    height: 650px;
    border: 1px solid;
  }

</style>
