<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="siteStationName">
            <el-input
              v-model="queryParams.siteStationName"
              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="siteInspectInfo">
          <el-table-column label="场站名称" align="center" prop="siteStationName" width="100":show-overflow-tooltip="true"/>
          <el-table-column label="场站类型" align="center" prop="siteStationType" :formatter="siteStationFormatte" :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="getSiteInspectInfo"
        />
      </el-col>

      <el-col :span="9" style="padding-left: 10px">
        <div id="site-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 { selectSiteStationInspectInfo} from "@/api/devicemanage/siteInspect";
  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: "site-inspect-index",
    components:{
      DangerDetailInfo,
      InspectDetailInfo
    },
    data(){
      return{
        total: 0,
        loading: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          taskId: null,
          siteStationName: null,
          inspector: null,
          inspectStatus: null
        },
        siteInspectInfo:[],
        stationType: [],
        dangerDetailOpen: false,
        title: ""
      }
    },
    mounted(){
      this.queryParams.taskId = this.$route.query.taskId;
      this.getStationType();
      this.getSiteInspectInfo();
      this.initMap();
    },
    methods:{
      getSiteInspectInfo(){
        selectSiteStationInspectInfo(this.queryParams).then(res =>{
          if(res.code == 200){
             this.total = res.total;
             this.siteInspectInfo = res.rows;
             if(this.siteInspectInfo && this.siteInspectInfo.length > 0 && this.siteInspectInfo[0].longitude){
                this.addLocation({longitude: this.siteInspectInfo[0].longitude, latitude: this.siteInspectInfo[0].latitude,nickName:this.siteInspectInfo[0].createTime,iconType:"siteInspect"});
             }
          }
        })
      },
      //地图初始化
      initMap(){
        const path = eval(this.systemSetting.map_center);
        this.gaoMap = new EditorMap(
          "site-inspect-map",
          {
            center: path,
            zoom: 12,
          },
          this
        );
      },
      //场站类型
      getStationType(){
        this.getDicts("t_station_type").then((response) => {
            this.stationType = response.data;
        });
      },
      //场站类型格式化
      siteStationFormatte(row){
        return this.selectDictLabel(
          this.stationType,
          row.siteStationType
        );
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getSiteInspectInfo();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      //详情
      handleDetail(row){
        this.title = row.siteStationName;
        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:"siteInspect"})
      },
      //隐患
      // handleDanger(row){
      //   this.dangerDetailOpen = true;
      //   this.$nextTick(() => {
      //     this.$refs.dangerDetail.queryParams.inspectId = row.id;
      //     this.$refs.dangerDetail.queryParams.type = 5;
      //     this.$refs.dangerDetail.getList();
      //   })
      // },
      //隐患
      handleDanger(row) {
          listDanger({pageNum: 1,pageSize: 99999,inspectId:row.id,type:5}).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();
         });*/
      },
      //位置上图
      addLocation(data){
        this.gaoMap.allDevice["siteInspect"]?.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">

  #site-inspect-map{
    width: 100%;
    height: 650px;
    border: 1px solid;
  }
</style>
