<template>
  <el-dialog :title="title" :visible.sync="pipeInspectopen" width="900px" append-to-body destroy-on-close :close-on-click-modal="false">
    <!-- 巡线打卡点 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="打卡点名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入打卡点名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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"
        >新增</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:point:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:point:remove']"
        >删除</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="['system:point:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="pointList">
      <el-table-column label="打卡点名称" align="center" prop="title" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="上传照片" align="center" prop="hasImg">
        <template slot-scope="scope">
          <span v-if="scope.row.hasImg == 1">必传</span>
          <span v-if="scope.row.hasImg == 0">选传</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="adminName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(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="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="打卡点名称" prop="title">
              <el-input v-model="form.title" placeholder="请输入打卡点名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="上传照片" prop="hasImg">
              <el-select v-model="form.hasImg" style="width: 100%">
                <el-option label="必传" :value="1"/>
                <el-option label="选传" :value="0"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="经度" prop="longitude">
              <el-input v-model="form.longitude" placeholder="请输入经度" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="纬度" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请输入纬度" />
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 地图 -->
        <div>
          <div class="search-wrapper pos">
            <el-input
              v-model="searchinput"
              class="searchinput"
              placeholder="请输入内容"
              size="mini"
              style="width: 150px"
              ref="input"
            ></el-input>
          </div>
          <div id="pipeMap" style="width: 100%;height: 300px;border:1px solid"></div>
        </div>


      </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>
  </el-dialog>
</template>

<script>
  import { listPoint, getPoint, delPoint, addPoint, updatePoint, exportPoint } from "@/api/baseinfo/pipePoint";
  import { EditorMap } from "@/utils/mapClass/getPath.js";
  import { pointInfoList } from "@/api/baseinfo/pipePoint";
  export default {
    name: "Point",
    components: {
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 管线打卡点表格数据
        pointList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          pipeId: null,
          title: null,
          longitude: null,
          latitude: null,
          adminId: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          title: [
            { required: true, message: "打卡点名称不能为空", trigger: "blur" },
          ],
          hasImg: [
            { required: true, message: "选择上传照片", trigger: "change" },
          ],
          longitude: [
            { required: true, message: "经度不能为空", trigger: "blur" },
          ],
          latitude: [
            { required: true, message: "纬度不能为空", trigger: "blur" },
          ]
        },
        searchinput: "",
        pipeInspectopen: false,
        pipeInfo: null,
        title: null
      };
    },
    watch: {
      pipeInspectopen(newVal){
        if(newVal){
          this.queryParams.pipeId = this.pipeInfo.pipeId;
          this.getList();
        }
      }
    },
    methods: {
      /** 查询管线打卡点列表 */
      getList() {
        this.loading = true;
        listPoint(this.queryParams).then(response => {
          this.pointList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          pipeId: null,
          title: null,
          longitude: null,
          latitude: null,
          hasImg: null,
          adminId: null,
          createTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加管线打卡点";
        this.initMap();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getPoint(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改管线打卡点";
          this.initMap();
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updatePoint(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              this.form.pipeId = this.pipeInfo.pipeId;
              addPoint(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除管线打卡点名称为"' + row.title + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPoint(ids);
        }).then(() => {
          this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有管线打卡点数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
        return exportPoint(queryParams);
      }).then(response => {
          this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
      },
      initMap(){
        this.$nextTick(() => {
          const path = eval(this.$store.state.user.systemSetting.map_center);
          this.map = new EditorMap("pipeMap", {center:path,zoom:13.5}, this);
          //初始化管道信息
          if(this.pipeInfo.coordinates){
            this.addPipeLine(JSON.parse(this.pipeInfo.coordinates));
          }
          if (this.form.longitude && this.form.latitude) {
            this.map.addDevice({ path: [eval(this.form.longitude),eval(this.form.latitude)]});
          } else {
            this.map.mouseAddMarker();
          }
          this.mouseEvent();
          this.searchPlace();
          this.getPipePointList();
        });
      },
      //添加管线信息
      addPipeLine(path){
        const pipe = new AMap.Polyline({
          path,
          strokeColor: "#3993f8",
          strokeWeight: 5,
          strokeOpacity: 0.9,
        });
        this.map.map.add(pipe);
        this.map.map.setCenter(path[0]);
      },
      //鼠标事件监听
      mouseEvent(){
        this.map.mousetool.on("draw", (e) => {
          this.form.longitude = e.obj._position.lng;
          this.form.latitude = e.obj._position.lat;
        })
      },
      //地图搜索
      searchPlace(){
        const input = this.$refs.input.$refs.input;
        this.map.positionSearch(input);
      },
      //获取管道打卡点
      getPipePointList(){
        pointInfoList({pipeId:this.pipeInfo.pipeId}).then(res =>{
          if(res.code == 200 && res.data){
            let points = res.data.filter(item => item.id != this.form.id);
            this.addMarker(points);
          }
        })
      },
      //添加标点
      addMarker(points){
        console.log("fdsfdsfdsfd====",points)
        points.forEach(item =>{
          let marker = new AMap.Marker({
            map: this.map.map,
            anchor: "bottom-center",
            icon: require("@/assets/mapImages/coordinate-grey.svg"),
            position: [item.longitude, item.latitude],
            extData: item,
          });
          this.addMarkerEvent(marker,item.title);
        })
      },
      //标点事件
      addMarkerEvent(marker,title){
        marker.on("mouseover", (e) => {
            const target = e.target;
          target.setLabel({ content: title, direction: "top" });
        });
        marker.on("mouseout", (e) => {
          const target = e.target;
          target.setLabel({ content: "" });
        });
      }
    }
  };
</script>

<style scoped lang="scss">
  .search-wrapper {
    left: 30px;
  }

  .pos {
    position: relative;
    top: 40px;
    left: 16px;
    z-index: 20;
  }
</style>
