<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="管道名称" prop="pipeName">
        <el-input
          v-model="queryParams.pipeName"
          placeholder="请输入管道名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管线类型" prop="pipeType">
        <el-select v-model="queryParams.pipeType" placeholder="请选择管线类型" clearable size="small">
          <el-option
            v-for="dict in pipeTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['baseInfo:pipe:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		      :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['baseInfo:pipe:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="管道名称" align="center" prop="pipeName" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="管线类型" align="center" prop="pipeType" :formatter="pipeTypeFormat" />
      <el-table-column label="管道长度(m)" align="center" prop="pipeLength" width="100"/>
      <el-table-column label="管径(mm)" align="center" prop="pipeDiameter" />
      <el-table-column label="管道壁厚(mm)" align="center" prop="pipeThickness" width="120"/>
      <el-table-column label="压力(Mpa)" align="center" prop="pipePressure" />
      <el-table-column label="材质" align="center" prop="pipeMaterial" :formatter="materialFormatte"/>
      <el-table-column label="管道地址" align="center" prop="pipeAddr" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="敷设方式" align="center" prop="layType" :formatter="layTypeFormatte" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="埋深(m)" align="center" prop="buriedDepth" />
      <el-table-column label="架空高度(m)" align="center" prop="overhead" width="100"/>
      <el-table-column label="打卡点数" align="center" prop="pointNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
            v-hasPermi="['baseInfo:pipe:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['baseInfo:pipe:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['baseInfo:pipe:remove']"
          >删除</el-button>

          <el-button
            size="mini"
            type="text"
            @click="handleInspectPoint(scope.row)"
          >
            巡线打卡点
          </el-button>

        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改管道信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body destroy-on-close :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="管道名称" prop="pipeName">
              <el-input v-model="form.pipeName" placeholder="请输入管道名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="管线类型" prop="pipeType">
              <el-select style="width: 100%" v-model="form.pipeType" placeholder="请选择管线类型">
                <el-option
                  v-for="dict in pipeTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="管道长度(m)" prop="pipeLength">
              <el-input v-model="form.pipeLength" placeholder="请输入管道长度"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="管径(mm)" prop="pipeDiameter">
              <el-input v-model="form.pipeDiameter" placeholder="请输入管径"/>
            </el-form-item>
          </el-col>

        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="管道壁厚(mm)" prop="pipeThickness">
              <el-input v-model="form.pipeThickness" placeholder="请输入管道壁厚"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="压力(Mpa)" prop="pipePressure">
              <el-input v-model="form.pipePressure" placeholder="请输入压力"/>
            </el-form-item>
          </el-col>

        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="材质" prop="pipeMaterial">
              <el-select v-model="form.pipeMaterial" palceholder="请选择材质" style="width: 100%" >
                <el-option
                  v-for="item in materialType"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="管道地址" prop="pipeAddr">
              <el-input v-model="form.pipeAddr" placeholder="请输入管道地址" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="敷设方式" prop="layType">
              <el-select v-model="form.layType" placeholder="请选择敷设方式" style="width: 100%" >
                <el-option
                  v-for="item in layType"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="埋深(m)" prop="buriedDepth">
              <el-input v-model="form.buriedDepth" placeholder="请输入埋深" type="number"/>
            </el-form-item>
          </el-col>

        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="架空高度(m)" prop="overhead">
              <el-input v-model="form.overhead" placeholder="请输入架空高度" type="number"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="19">
            <el-form-item label="坐标" prop="coordinates">
              <el-input v-model="form.coordinates"   type="textarea" placeholder="请输入坐标" />
            </el-form-item>
          </el-col>
          <el-col :span="3" style="margin-left: 30px">
            <el-button type="primary" plain @click="MapdialogFun">选择坐标</el-button>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <GetPos
      :dialogVisible.sync="dialogVisible"
      device="pipe"
      @close="dialogVisible=false"
      @getPath="getPath"
      :pipePath="this.coordinateslist"
      ref="getPos"
    />

    <!-- 详情 -->
    <DetailInfo ref="detail"/>

    <!-- 巡线打卡点 -->
    <PipeInspectPoint ref="pipeInspectPoint"/>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo } from "@/api/baseinfo/pipeInfo";
import GetPos from '@/components/GetPos';
import DetailInfo from "./components/DetailInfo";
import PipeInspectPoint from './components/PipeInspectPoint';
import { mapDotList } from "@/api/devicemanage/mapdot";
import ReferPointInfo from "./components/ReferPointInfo";
import gcoord from "gcoord";
import axios from 'axios';
export default {
  name: "Info",
  components: {
    GetPos,
    DetailInfo,
    PipeInspectPoint,
    ReferPointInfo
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      pipeNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 管道信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pipeName: null,
        pipeType: null,
        pipeLength: null,
        pipeDiameter: null,
        pipeThickness: null,
        pipePressure: null,
        pipeMaterial: null,
        pipeAddr: null,
        layType: null,
        buriedDepth: null,
        overhead: null,
        beyondEnterpriseId: null,
        beyondEnterpriseName: null,
        coordinates: null,
        isDel: null,
        remarks: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pipeName: [
          { required: true, message: "管道名称不能为空", trigger: "blur" },
        ],
        pipeType: [
          { required: true, message: "请选择管线类型", trigger: "change" },
        ],
        pipeLength: [
          { required: true, message: "管道长度不能为空", trigger: "blur" },
        ],
        pipeMaterial: [
          { required: true, message: "请选择管道材质", trigger: "change" },
        ],
        pipeAddr: [
          { required: true, message: "管道地址不能为空", trigger: "blur" },
        ],
        layType: [
          { required: true, message: "请选择敷设方式", trigger: "change" },
        ],
        coordinates: [
          { required: true, message: "坐标不能为空", trigger: "change" },
        ],
      },
      materialType: [],
      layType: [],
      dialogVisible: false,
      coordinateslist: [],
      referPointData: [],
      referMarkerList: [],
      pipeTypeOptions: [],
      deviceType:{
        1 : "调压箱",
        2 : "阀门井",
        3 : "管线",
        4 : "管线定向钻",
        5 : "管线大开挖",
        6 : "管线直埋",
        7 : "阴保测试桩",
        8 : "光缆测试桩",
        9 : "排流测试桩",
        10 : "计量柜",
        11 : "交叉测试桩",
      },
      lineStyle:{
        "medium": "#FF0000",
        "height1": "#00FF00",
        "height2": "#FFFF00",
        "newLine": "#00FFFF"
      },
    };
  },
  created() {
    this.getMaterialType();
    this.getLayType();
    this.getList();
    this.getPipeType();
  },
  methods: {
    /** 查询管道信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pipeId: null,
        pipeName: null,
        pipeType: null,
        pipeLength: null,
        pipeDiameter: null,
        pipeThickness: null,
        pipePressure: null,
        pipeMaterial: null,
        pipeAddr: null,
        layType: null,
        buriedDepth: null,
        overhead: null,
        beyondEnterpriseId: null,
        beyondEnterpriseName: null,
        coordinates: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: null
      };
      this.resetForm("form");
      this.coordinateslist = [];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pipeId);
      this.pipeNames = selection.map(item => item.pipeName);
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加管道信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const pipeId = row.pipeId || this.ids
      getInfo(pipeId).then(response => {
        this.form = response.data;
        if(this.form.coordinates){
          this.coordinateslist=JSON.parse(this.form.coordinates);
        }
        this.open = true;
        this.title = "修改管道信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pipeId != null) {
            updateInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pipeIds = row.pipeId || this.ids;
      const pipeNames = row.pipeName || this.pipeNames;
      this.$confirm('是否确认删除管道名称为"' + pipeNames + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfo(pipeIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有管道信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    //材质类型
    getMaterialType(){
      this.getDicts("t_pipe_material").then((response) => {
          this.materialType = response.data;
      });
    },
    //敷设方式类型
    getLayType(){
      this.getDicts("t_lay_type").then((response) => {
        this.layType = response.data;
      });
    },
    //管线类型
    getPipeType(){
      this.getDicts("t_pipe_type").then(response => {
          this.pipeTypeOptions = response.data;
      });
    },
    //材质格式化
    materialFormatte(row){
      return this.selectDictLabel(
        this.materialType,
        row.pipeMaterial
      );
    },
    //敷设方式格式化
    layTypeFormatte(row){
      return this.selectDictLabel(
        this.layType,
        row.layType
      );
    },
    // 管线类型字典翻译
    pipeTypeFormat(row, column) {
      return this.selectDictLabel(this.pipeTypeOptions, row.pipeType);
    },
    /** 管道打开方法*/
    MapdialogFun(){
      this.dialogVisible=true;
      this.$nextTick(() => {
          this.clearReferMarker();
          this.getReferPointInfo();
          this.getPipeByWgsArr();
      })
    },
    //初始化参照点
    initReferMarker(){
      this.referPointData.forEach(item =>{
        if(item.type == '3'){
          this.addReferMarker("https://yhq.zhkjgf.com:8898/profile/upload/2024/01/18/ee801330-c287-436c-81ed-589a1bddf007.png",item);
        }
        if(item.type == '4'){
          this.addReferMarker("https://yhq.zhkjgf.com:8898/profile/upload/2024/01/18/e116c7df-3727-4260-b068-f2e75efea724.png",item);
        }
        if(item.type == '5'){
          this.addReferMarker("https://yhq.zhkjgf.com:8898/profile/upload/2024/01/18/e3248213-088f-4a5e-ba3b-a50856f3199c.png",item);
        }
        if(item.type == '6'){
          this.addReferMarker("https://yhq.zhkjgf.com:8898/profile/upload/2024/01/18/77d0e991-acfb-46ac-876e-65a4555db9da.png",item);
        }
        if(item.type == '7' || item.type == '8' || item.type == '9' || item.type == '11'){
          this.addReferMarker("https://yhq.zhkjgf.com:8898/profile/upload/2024/03/27/279d3d21-a859-4e00-b07d-a04bc3da7d4c.png",item);
        }
        if(item.type == '10'){
          this.addReferMarker("https://yhq.zhkjgf.com:8898/profile/upload/2024/03/27/be257f4c-3917-4357-882b-fa9e9307aef9.png",item);
        }
      })
    },
    //获取参照点
    getReferPointInfo(){
      mapDotList().then(res =>{
        if(res.code == 200){
           this.referPointData = res.data;
           this.initReferMarker();
        }
      })
    },
    getPipeByWgsArr(){
      this.getMediumLineInfo();
      this.getNewLineInfo();
      this.getHeight2Info();
      this.getHeight2Info2();
    },
    //中压
    getMediumLineInfo(){
      axios.get('../geo_json/mediumLine.json').then(res =>{
          res.data.forEach(line =>{
              const linePath = [];
              line.forEach(ponit =>{
                  let gaoPoint = gcoord.transform(
                    ponit, // 经纬度坐标
                    gcoord.WGS84, // 当前坐标系
                    gcoord.AMap // 目标坐标系
                    // gcoord.GCJ02,
                    // gcoord.WGS84
                  );
                linePath.push(gaoPoint);
              })
            this.pipeOnMap("medium",linePath);
          })
      })
    },
    //新管线
    getNewLineInfo(){
      axios.get('../geo_json/newLine.json').then(res =>{
          res.data.forEach(line =>{
              const linePath = [];
              line.forEach(ponit =>{
                  let gaoPoint = gcoord.transform(
                    ponit, // 经纬度坐标
                    gcoord.WGS84, // 当前坐标系
                    gcoord.AMap // 目标坐标系
                    // gcoord.GCJ02,
                    // gcoord.WGS84
                  );
                linePath.push(gaoPoint);
              })
            this.pipeOnMap("newLine",linePath);
          })
      })
    },
    //高压管线
    getHeight2Info(){
      axios.get('../geo_json/height2.json').then(res =>{
          res.data.forEach(line =>{
              const linePath = [];
              line.forEach(ponit =>{
                let gaoPoint = gcoord.transform(
                  ponit, // 经纬度坐标
                  gcoord.WGS84, // 当前坐标系
                  gcoord.AMap // 目标坐标系
                  // gcoord.GCJ02,
                  // gcoord.WGS84
                );
              linePath.push(gaoPoint);
            })
            this.pipeOnMap("height2",linePath);
          })
      })
    },
    getHeight2Info2() {
      axios.get("../geo_json/height3.json").then((res) => {
          res.data.forEach((line) => {
            const linePath = [];
            line.forEach((ponit) => {
                let gaoPoint = gcoord.transform(
                  ponit, // 经纬度坐标
                  gcoord.AMap, // 当前坐标系
                  gcoord.AMap // 目标坐标系
                  // gcoord.GCJ02,
                  // gcoord.WGS84
                );
              linePath.push(gaoPoint);
            });
          this.pipeOnMap("height1", linePath);
        });
      });
    },
    pipeOnMap(type,linePath){
      //线上图
      const polyLine = new AMap.Polyline({
        path: linePath,
        strokeWeight: 4, // 线条宽度
        strokeColor: this.lineStyle[type], // 线条颜色
      });
      this.$refs.getPos.map.map.add(polyLine);
    },
    addReferMarker(icon,data){
      let marker = new AMap.Marker({
        map: this.$refs.getPos.map.map,
        anchor: "bottom-center",
        icon,
        position: [data.longitude, data.latitude],
        label: {
          content: "<div style='margin-bottom: 3px'>设备类型:&nbsp;" + this.deviceType[data.type] + "</div><div>设备名称:&nbsp;" + data.title + "</div>",
          direction: 'top',
          //offset:[0,-2]
        },
        extData: data,
      });
      this.referMarkerList.push(marker);
      this.clickEvent(marker,data);
    },
    clickEvent(marker,data){
      marker.on("mouseover", (e) => {
        let referPoint = this.createReferPointInfoWindow(data);
        this.infowindow = new AMap.InfoWindow({
          isCustom: true,
          content: referPoint.$el,
          position: [data.longitude, data.latitude],
          // anchor: "top-left",
          // offset: [20, -15],
          anchor: "middle-left",
          offset: [20, 0],
        });
        this.infowindow.open(this.$refs.getPos.map.map);
      });
      marker.on("mouseout", (e) => {this.infowindow.close()});
    },
    //创建点弹框
    createReferPointInfoWindow(data){
      let that = this;
      let ReferPoint = this.$Vue.extend(ReferPointInfo,{});
      return new ReferPoint({
        data(){
          return{
            parent: that,
            pointInfo: data
          }
        }
      }).$mount();
    },
    clearReferMarker(){
      this.referMarkerList?.forEach((iten) => {
         this.$refs.getPos.map.map.remove(iten);
      });
      this.referMarkerList = [];
    },
    /**管道信息 选择管道提交数据方法*/
    getPath(e){
      var a = '['
      for (var i in e) {
        if (i == e.length - 1) {
          a += '[' + e[i].toString()+']'
        } else {
          a += '[' + e[i].toString()+'],'
        }
      }
      a += ']'
      this.form.coordinates=a;
    },
    handleDetail(row){
      this.$refs.detail.getDetailInfo(row.pipeId);
    },
    //巡检打卡点
    handleInspectPoint(row){
      this.$refs.pipeInspectPoint.title = row.pipeName;
      this.$refs.pipeInspectPoint.pipeInspectopen = true;
      this.$refs.pipeInspectPoint.pipeInfo = row;
    }
  }
};
</script>
