<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="巡检点名称" prop="patrolName">
        <el-input
          v-model="queryParams.patrolName"
          placeholder="请输入巡检点名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="巡检点类型" prop="patrolType">-->
        <!--<el-select v-model="queryParams.patrolType" placeholder="请选择巡检点类型" clearable size="small">-->
          <!--<el-option label="请选择字典生成" 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-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"
        >修改</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"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="positionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="巡检点id" align="center" prop="patrolId" />-->
      <el-table-column label="巡检点名称" align="center" prop="patrolName" />
      <!--<el-table-column label="巡检点类型" align="center" prop="patrolType" />-->
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="位置描述" align="center" prop="position" />
      <el-table-column label="巡检点参数" align="center" prop="patrolDetail" >
        <template slot-scope="scope">
          <div  v-for="item in JSON.parse(scope.row.patrolDetail)">
            <span v-if="item.paramType==1">{{item.paramName}}：{{item.paramNum1}} ~ {{item.paramNum2}}</span>
            <span v-if="item.paramType==2">{{item.paramName}}：{{item.paramNum1==1?'是':'否'}}</span>
          </div>
        </template>
      </el-table-column>
      <!--<el-table-column label="备注" align="center" prop="remarks" />-->
      <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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="95px">
        <el-form-item label="巡检点名称" prop="patrolName">
          <el-input v-model="form.patrolName" placeholder="请输入巡检点名称" />
        </el-form-item>
        <!--<el-form-item label="巡检点类型" prop="patrolType">-->
          <!--<el-select v-model="form.patrolType" placeholder="请选择巡检点类型">-->
            <!--<el-option label="请选择字典生成" value="" />-->
          <!--</el-select>-->
        <!--</el-form-item>-->
        <el-form-item label="经纬度" prop="longitude">
          <el-row>
            <el-col :span="10">
              <el-input v-model="form.longitude" placeholder="请选择经度" :disabled="true"/>
            </el-col>
            <el-col :span="10" style="margin-left: 6px">
              <el-input v-model="form.latitude" placeholder="请选择纬度" :disabled="true"/>
            </el-col>
            <el-col :span="3" style="margin-left: 6px">
              <el-button type="primary" @click="MapdialogFun" >选择位置</el-button>
            </el-col>
          </el-row>

        </el-form-item>
        <el-form-item label="详细位置" prop="position">
          <el-input v-model="form.position" placeholder="请输入位置描述" />
        </el-form-item>
        <el-form-item label="巡检点参数" prop="patrolDetail">
          <div class="detail-div">
            <div class="button-div">
              <div class="button" @click="addParam()">新增</div>
            </div>
            <div class="roll-div">
              <div class="one-detail-div"  v-for="(item,index) in paramList" :key="index" >
                <el-row :gutter="24" class="mb8">
                  <el-col :span="22">
                    <el-form-item label="参数名称" >
                      <input class="" v-model="item.paramName"/>
                    </el-form-item>
                    <el-form-item label="参数类型" >
                      <select style="width: 83px" v-model="item.paramType" @change="tplSelectChange($event.target.value,index)">
                        <option
                          v-for="dict in types"
                          :key="dict.dictValue"
                          :label="dict.dictLabel"
                          :value="parseInt(dict.dictValue)"
                        ></option>
                      </select>
                      <!--<input class="" v-model="item.paramType"/>-->
                    </el-form-item>
                    <el-form-item label="参数值" v-if="item.paramType==1">
                      <input v-model="item.paramNum1" class="inputstyle" /> - <input v-model="item.paramNum2" class="inputstyle"/>
                      <span class="tixing">（ 当超过最大值，或低于最小值时报警 ）</span>
                    </el-form-item>
                    <el-form-item label="参数值" v-if="item.paramType==2">
                      <el-radio v-model="item.paramNum1" :label="1">是</el-radio>
                      <el-radio v-model="item.paramNum1" :label="0">否</el-radio>
                    </el-form-item>
                  </el-col>
                  <el-col :span="2">
                    <img class="delete" src="@/assets/images/delete.png" @click="deleteParam(index)"/>
                  </el-col>
                </el-row>
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </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="dialogTableVisibles"
      device=""
      :devicePos="devicePos"
      @close="dialogcancelFun"
      @getPath="getPath"
    />
  </div>
</template>

<script>
import { listPosition, getPosition, delPosition, addPosition, updatePosition, exportPosition } from "@/api/system/position";
import GetPos from '@/components/GetPos';
export default {
  name: "Position",
  components: {
    GetPos
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      num:0,
      /**--------------地图使用数据---------------*/
      dialogTableVisibles: false,
      devicePos: [],
      /**--------------地图使用数据---------------*/
      // 巡检点表格数据
      positionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patrolName: null,
        patrolType: null,
        longitude: null,
        latitude: null,
        position: null,
        patrolDetail: null,
        deptId: null,
        remarks: null,
        isDel: null
      },
      paramList:[

      ],
      types:[
        {
          dictValue:1,
          dictLabel:"数值范围"
        },
        {
          dictValue:2,
          dictLabel:"是否"
        }
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        patrolName: [
          { required: true, message: "巡检点名称不能为空", trigger: "change" }
        ],
        // longitude: [
        //   { required: true, message: "经纬度不能为空", trigger: "change" }
        // ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询巡检点列表 */
    getList() {
      this.loading = true;
      listPosition(this.queryParams).then(response => {
        this.positionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        patrolId: null,
        patrolName: null,
        patrolType: null,
        longitude: null,
        latitude: null,
        position: null,
        patrolDetail: null,
        deptId: null,
        remarks: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        isDel: 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.patrolId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    //添加参数
    addParam(){
      var param =  {
        paramName:"",
        paramType:1,
        paramNum1:"",
        paramNum2:"",
        testingNum:""
      }
      this.paramList.push(param);
    },
    //删除参数
    deleteParam(index){
      this.paramList.splice(index,1);
      //this.paramList.splice(index,1);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加巡检点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const patrolId = row.patrolId || this.ids
      getPosition(patrolId).then(response => {
        this.form = response.data;
        if(this.form.patrolDetail!=null && this.form.patrolDetail!=""){
          this.paramList = JSON.parse(this.form.patrolDetail);
        }
        this.devicePos = [response.data.longitude, response.data.latitude];
        this.open = true;
        this.title = "修改巡检点";
      });
    },
    /**
     * 经纬度坐标方法
     */
    MapdialogFun() {
      this.dialogTableVisibles = true;
    },
    /**
     * 地图关闭方法
     */
    dialogcancelFun() {
      this.dialogTableVisibles = false;
    },
    /**
     * 经纬度 选择
     * @param res
     */
    getPath(res){
      console.log("res", res);
      console.log(this.form.longitude, this.form.latitude);
      //确认选择经纬度
      this.form.longitude = res[0];
      this.form.latitude = res[1];
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.patrolDetail = JSON.stringify(this.paramList);
          if (this.form.patrolId != null) {
            updatePosition(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPosition(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const patrolIds = row.patrolId || this.ids;
      this.$confirm('是否确认删除巡检点编号为"' + patrolIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPosition(patrolIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有巡检点数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportPosition(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style>
  .detail-div{
    width: 100%;
    height: 300px;
    border: 1px solid #adafb7;
    border-radius: 5px;
  }
  .one-detail-div{
    width: 600px;
    height: 110px;
    border: 1px solid #adafb7;
    border-radius: 5px;
    margin-left: 5%;
    margin-top: 5px;
  }
  .button-div{
    height: 25px;
    border-radius: 0;
    width: 100%;
  }
  .button{
    width: 90px;
    background: #1c84c6;
    color: #fff;
    margin-left: 81%;
    border: 0;
    border-radius: 5px;
    text-align: center;
    height: 25px;
    line-height: 25px;
    margin-top: 5px;
  }
  .inputstyle{
    width: 83px;
  }
  .delete{
    width: 25px;
    margin-left: 3px;
  }
  .roll-div{
    width: 100%;
    height: 270px;
    overflow:auto;
    overflow-x: hidden;
  }
  .tixing{
    font-size: 12px;
    margin-left: 0px;
  }
</style>
<style lang="scss" scoped>
  ::v-deep .el-select{
    width: 100%;
  }
</style>
