<template>
  <div class="app-container detail" style="background-color: rgb(238, 241, 245);">
    <div style="padding-top: 10px;background: #fff;height: 100%;">
      <el-row>
        <el-col :span="24" style="padding-left: 15px;margin-bottom: -10px;">
          <div style="height: 45px;" @click="$router.go(-1)">
            <el-button size="medium" type="text" style="font-size: 18px; color: rgb(7, 63, 112);float: left;">返回
            </el-button>
            <div style="float: left;margin-top: 8px;margin-left: 5px;"><img src="../../../assets/logo/fanhui.png"
                style="width: 25px;" alt=""></div>
          </div>
        </el-col>
      </el-row>
      <el-row style="width: 100%;height: 40px;">
        <el-col :span="24">
          <div style="">
            <ul>
              <li style="list-style: none;font-weight: 900;font-size: 20px;color: #053b6a;">设备信息管理详情</li>
            </ul>
          </div>
        </el-col>
      </el-row>
      <el-divider></el-divider>
      <el-row style="width: 100%;margin-top: -15px;margin-bottom: -5px;">
        <el-col :span="24">
          <div style="color: #31EAEA;height: 25px;">
            <ul>
              <li>详细信息</li>
            </ul>
          </div>
        </el-col>
      </el-row>

      <el-row style="width: 100%;padding: 10px;">
        <el-col :span="5">
          <el-form ref="form" v-model="form" label-width="100px" style="width: 100%;">
            <el-form-item label="设备名称:" prop="deviceName">
              <font>{{form.deviceName}}</font>
            </el-form-item>
            <el-form-item label="设备编号:" prop="deviceCode">
              <font>{{form.deviceCode}}</font>
            </el-form-item>
            <el-form-item label="设备类型:" prop="deviceType">
              <font v-if="form.deviceType == 1">调压阀</font>
              <font v-if="form.deviceType == 2">阀门井</font>
              <font v-if="form.deviceType == 3">流量计</font>
              <font v-if="form.deviceType == 4">压力表</font>
            </el-form-item>
            <el-form-item label="设备型号:" prop="deviceModel">
              <font>{{form.deviceModel}}</font>
            </el-form-item>
            <el-form-item label="所属管道:" prop="pipeName">
              <font>{{form.pipeName}}</font>
            </el-form-item>
            <el-form-item label="物联网编号:" prop="iotNo">
              <font>{{form.iotNo}}</font>
            </el-form-item>
            <el-form-item label="联系人:" prop="linkman">
              <font>{{form.linkman}}</font>
            </el-form-item>
            <el-form-item label="联系电话:" prop="phone">
              <font>{{form.phone}}</font>
            </el-form-item>
            <el-form-item label="备注信息:" prop="remarks" style="width: 170%;">
              <font>{{form.remarks}}</font>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6">
          <el-form ref="form" v-model="form" label-width="100px" style="width: 100%;">
            <el-form-item label="所在地址:" prop="deviceAddr">
              <font>{{form.deviceAddr}}</font>
            </el-form-item>
            <el-form-item label="经度:" prop="longitude">
              <font>{{form.longitude}}</font>
            </el-form-item>
            <el-form-item label="经度:" prop="latitude">
              <font>{{form.latitude}}</font>
            </el-form-item>
            <el-form-item label="安装时间:" prop="installationTime">
              <font>{{form.installationTime}}</font>
            </el-form-item>
            <el-form-item label="最后巡检时间:" prop="inspectionTime">
              <font>{{form.inspectionTime}}</font>
            </el-form-item>
            <el-form-item label="设备图片:" prop="iconUrl">
              <div style="width: 20%;">
                <el-image :src="form.iconUrl" :preview-src-list="[form.iconUrl]" style="width: auto;height: auto;"></el-image>
              </div>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="11">
          <div id="marbox"
            style="width: 100%;height: 390px;margin-top: -25px; border: 1px solid rgb(218, 213, 213);margin-bottom: 10px;">
            <div style="width: 100%;height: 100%" id="container"></div>
          </div>
        </el-col>
      </el-row>

      <div class="" style="width: 95%; height: 60px;margin-left: 50px;text-align: center;margin-top: 5px;">
        <el-button size="normal" type="primary" icon="el-icon-edit" @click="handleUpdate(form)"
          v-hasPermi="['device:deviceInfo:edit']">修改</el-button>
        <el-button size="normal" type="primary" icon="el-icon-delete" @click="handleDelete(form)"
          v-hasPermi="['device:deviceInfo:remove']">删除</el-button>

        <el-dialog :title="title" :visible.sync="open" append-to-body @close="cancel">
          <el-form ref="editForm" :model="editForm" :rules="rules" label-width="135px">
            <el-col :span="11">
              <el-form-item label="所属管道" prop="pipeId">
                <el-select v-model="editForm.pipeId" placeholder="请选择所属管道" filterable style="width: 100%">
                  <el-option v-for="item in options" :key="item.pipeId" :label="item.pipeName" :value="item.pipeId">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="设备编号" prop="deviceCode">
                <el-input v-model="editForm.deviceCode" placeholder="请输入设备编号" />
              </el-form-item>
            </el-col>

            <el-col :span="11">
              <el-form-item label="设备类型" prop="deviceType">
                <el-select v-model="editForm.deviceType" placeholder="请选择设备类型" filterable style="width: 100%">
                  <el-option v-for="dict in typeOptions" :key="dict.dictValue" :label="dict.dictLabel"
                    :value="dict.dictValue"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="设备名称" prop="deviceName">
                <el-input v-model="editForm.deviceName" placeholder="请输入设备名称" />
              </el-form-item>
            </el-col>

            <el-col :span="11">
              <el-form-item label="设备型号" prop="deviceModel">
                <el-input v-model="editForm.deviceModel" placeholder="请输入设备型号" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="物联网编号" prop="iotNo">
                <el-input v-model="editForm.iotNo" placeholder="请输入物联网编号" />
              </el-form-item>
            </el-col>

            <el-col :span="23">
              <el-form-item label="所在地址" prop="deviceAddr">
                <el-input v-model="editForm.deviceAddr" placeholder="请输入所在地址" />
              </el-form-item>
            </el-col>

            <el-col :span="23">
              <el-form-item label="经纬度坐标" prop="longitude">
                <el-row>
                  <el-col :span="8">
                    <el-input v-model.number="editForm.longitude" placeholder="请输入经度" />
                  </el-col>
                  <el-col :span="8" style="margin-left: 10px">
                    <el-input v-model.number="editForm.latitude" placeholder="请输入纬度" />
                  </el-col>
                  <el-col :span="5" style="margin-left: 10px">
                    <el-button type="primary" plain @click="MapdialogFun">选择经纬度</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>

            <el-col :span="11">
              <el-form-item label="联系人" prop="linkman">
                <el-input v-model="editForm.linkman" placeholder="请输入联系人" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="联系电话" prop="phone">
                <el-input type="number" maxlength="12" v-model="editForm.phone" placeholder="请输入联系电话" />
              </el-form-item>
            </el-col>

            <el-col>
              <el-form-item label="设备图片上传" prop="iconUrl">
                <MyFileUpload listType="picture-card" @resFun="getFileInfo" @remove="listRemove" :fileArr="fileList" />
                <el-input v-show="false" disabled v-model="editForm.iconUrl"></el-input>
              </el-form-item>
            </el-col>

            <el-col>
              <el-form-item label="安装日期" prop="installationTime">
                <el-col :span="11">
                  <el-date-picker clearable size="small" style="width: 100%" v-model="editForm.installationTime"
                    type="date" value-format="yyyy-MM-dd" placeholder="请选择安装日期">
                  </el-date-picker>
                </el-col>
              </el-form-item>
            </el-col>

            <el-col :span="23">
              <el-form-item label="备注信息" prop="remarks">
                <el-input type="textarea" v-model="editForm.remarks" placeholder="请输入备注信息" />
              </el-form-item>
            </el-col>
          </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>

        <Mapdialog v-if="loadmap" :dialogTableVisible="dialogTableVisible" @dialogcancelFun="dialogcancelFun"
          :slat="editForm.latitude" :slng="editForm.longitude" @confirmFun="confirmFun($event)"></Mapdialog>
      </div>
    </div>
  </div>
</template>

<script>

  import { updateDeviceInfo, getDeviceInfo } from "@/api/device/deviceInfo";
  import { pipeAllInfoList } from "@/api/device/pipe";
  import MyFileUpload from '@/components/MyFileUpload';
  import Mapdialog from "@/components/mapDialog/checkDeviceLoaction.vue";
  import gaodeMap from "utils/gaodeMap.js";
  import { DEVICE_TYPE } from "utils/gaodeMap.js";

  export default {
    name: "DeviceInfoDetail",
    components: {
      MyFileUpload,
      Mapdialog
    },
    data() {
      return {
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 管道列表
        options: [],
        // 上传文件列表
        fileList: [],
        // 地图
        loadmap: false,
        dialogTableVisible: false,
        // 设备类型字典
        typeOptions: [],
        // 表单参数
        form: {},
        editForm: {},
        // 表单校验
        rules: {
          pipeId: [
            { required: true, message: "请选择所属管道", trigger: "blur" },
          ],
          deviceType: [
            { required: true, message: "请选择设备类型", trigger: "blur" },
          ],
          deviceName: [
            { required: true, message: "请输入设备名称", trigger: "blur" },
          ],
          deviceCode: [
            { required: true, message: "请输入设备编号", trigger: "blur" },
          ],
          deviceModel: [
            { required: true, message: "请输入设备型号", trigger: "blur" },
          ],
          deviceAddr: [
            { required: true, message: "请输入所在地址", trigger: "blur" },
          ],
          longitude: [
            { required: true, message: "请输入经纬度", trigger: "blur" },
          ],
          iotNo: [
            { required: true, message: "请输入物联网编号", trigger: "blur" },
          ],
          linkman: [
            { required: true, message: "请输入联系人", trigger: "blur" }
          ],
          phone: [
            { required: true, message: "请输入联系电话", trigger: "blur" },
            { min: 11, max: 11, message: "长度11个数字", trigger: "blur" },
          ],
          iconUrl: [
            { required: true, message: "请上传图片", trigger: "change" }
          ],
          installationTime: [
            { required: true, message: "请选择日期", trigger: "change" },
          ],
        }
      };
    },
    created() {
      // 如果是跳转来的，则接受初始化参数
      this.deviceId = this.$route.query.deviceId;

      this.getDicts("t_device_type").then(response => {
        this.typeOptions = response.data;
      });
    },
    mounted() {
      this.getDetail();
      // let gaoMap = new gaodeMap("石家庄");
      // this.gaoMap = gaoMap;
    },
    methods: {
      getDetail() {
        getDeviceInfo(this.deviceId).then(response => {
          this.form = response.data;
          console.log(this.form, "this.form")
          const obj =this.form;
          // this.gaoMap.resetMapCenter([this.form.longitude, this.form.latitude]);let obj = this.form.deviceList[0];
          let gaoMap = new gaodeMap("石家庄", [obj.longitude, obj.latitude]);
          this.gaoMap = gaoMap;
          this.gaoMap.addMarker(DEVICE_TYPE.WORKORDER, this.form)
        });
      },
      getFileInfo(res) {
        this.editForm.iconUrl = res.url;
      },
      listRemove(e) {
        this.editForm.iconUrl = "";
        this.fileList = [];
      },
      confirmFun(res) {
        //确认选择经纬度
        this.editForm.longitude = res.lng;
        this.editForm.latitude = res.lat;
      },
      MapdialogFun() {
        this.loadmap = true;
        this.dialogTableVisible = true;
      },
      dialogcancelFun() {
        this.loadmap = false;
        this.dialogTableVisible = false;
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.fileList = [];
      },
      /** 修改按钮操作 */
      handleUpdate(res) {
        pipeAllInfoList().then(response => {
          this.options = response.data;
        });
        getDeviceInfo(res.deviceId).then(response => {
          this.editForm = response.data;
          if (this.editForm.iconUrl) {
            this.fileList.push({
              url: this.editForm.iconUrl,
            });
          }
          this.open = true;
          this.title = "修改设备信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["editForm"].validate(valid => {
          if (valid) {
            console.log(this.editForm);
            updateDeviceInfo(this.editForm).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              location.reload();
            });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(res) {
        res.isDel = "1";
        this.$confirm('是否确认删除设备名称为"' + res.deviceName + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return updateDeviceInfo(res);
        }).then(() => {
          this.$router.go(-1);
          this.msgSuccess("已删除");
        }).catch(() => {
        });
      },
    }
  }
</script>
<style>
  .el-divider--horizontal {
    display: block;
    height: 1px;
    width: 100%;
    margin: 20px 0;
  }

  .feedbackTime-div {
    float: left;
    margin-left: 150px;
    margin-top: 10px;
  }

  .feedbackTime {
    height: 120px;
    width: 120px;
    float: left;
    margin-left: 15px;
    margin-top: 5px;
    margin-bottom: 15px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .el-card__body {
    padding: 5px 20px 20px 20px;
  }

  .detail .el-form {
    width: 20%;
  }

  .detail .el-form-item {
    margin-bottom: 0px;
  }

  .avatar-uploader {
    width: 25%;
    float: left;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

  li {
    font-size: 15px;
    font-weight: 900;
  }
</style>
