<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="pipeName">
              <font>{{form.pipeName}}</font>
            </el-form-item>
            <el-form-item label="管道编号:" prop="pipeCode">
              <font>{{form.pipeCode}}</font>
            </el-form-item>
            <el-form-item label="管道类型:" prop="pipeType">
              <font v-if="form.pipeType == 1">地埋管线</font>
              <font v-if="form.pipeType == 2">地表管线</font>
            </el-form-item>
            <el-form-item label="管道压力:" prop="pipePressure">
              <font v-if="form.pipePressure == 1">低压</font>
              <font v-if="form.pipePressure == 2">中压</font>
              <font v-if="form.pipePressure == 3">次高压</font>
              <font v-if="form.pipePressure == 4">高压</font>
            </el-form-item>
            <el-form-item label="管道长度:" prop="pipeLength">
              <font>{{form.pipeLength}}</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="pipeAddr">
              <font>{{form.pipeAddr}}</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:pipe:edit']">修改</el-button>
        <el-button size="normal" type="primary" icon="el-icon-delete" @click="handleDelete(form)"
          v-hasPermi="['device:pipe: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="23">
              <el-form-item label="管道名称" prop="pipeName">
                <el-input v-model="editForm.pipeName" placeholder="请输入管道名称" />
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="管道编号" prop="pipeCode">
                <el-input v-model="editForm.pipeCode" placeholder="请输入管道编号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="管道长度" prop="pipeLength">
                <el-input type="number" min="0" v-model="editForm.pipeLength" placeholder="请输入管道长度">
                  <i slot="suffix" style="color: #000; font-style: normal; margin-right: 10px">米</i>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="23">
              <el-form-item label="管道所在地址" prop="pipeAddr">
                <el-input v-model="editForm.pipeAddr" placeholder="请输入管道所在地址" />
              </el-form-item>
            </el-col>
            <el-col :span="23">
              <el-form-item label="管道坐标" prop="coordinates">
                <el-col :span="18">
                  <el-input type="textarea" v-model="editForm.coordinates" placeholder="请输入管道坐标" />
                </el-col>
                <el-col :span="5" style="margin-left: 10px">
                  <el-button type="primary" plain @click="MapdialogFun">选择管道坐标</el-button>
                </el-col>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="管道类型" prop="pipeType">
                <el-radio-group v-model="editForm.pipeType">
                  <el-radio label="1">地埋管线</el-radio>
                  <el-radio label="2">地表管线</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="管道压力" prop="pipePressure">
                <!-- select -->
                <el-radio-group v-model="editForm.pipePressure">
                  <!-- 1低压，2中压，3次高压，4高压 -->
                  <el-radio label="1">低压</el-radio>
                  <el-radio label="2">中压</el-radio>
                  <el-radio label="3">次高压</el-radio>
                  <el-radio label="4">高压</el-radio>
                </el-radio-group>
              </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 v-model="editForm.remarks" type="textarea" 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"
          @confirmFun="confirmFun($event)" :str="str"></Mapdialog>

      </div>
    </div>
  </div>
</template>

<script>

  import { updatePipe, getPipe } from "@/api/device/pipe";
  import gaodeMap from "utils/gaodeMap.js";
  import MyFileUpload from '@/components/MyFileUpload';
  import Mapdialog from "@/components/mapDialog/checkPipeLineLocation.vue";

  export default {
    name: "PipeDetail",
    components: {
      MyFileUpload,
      Mapdialog
    },
    data() {
      return {
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 上传文件列表
        fileList: [],
        // 地图
        loadmap: false,
        dialogTableVisible: false,
        // 管道类型字典
        typeOptions: [],
        str: "",
        // 管道压力数据字典
        pressureOptions: [],
        // 表单参数
        form: {
        },
        editForm: {
        },
        // 表单校验
        rules: {
          pipeName: [
            { required: true, message: "请输入管道名称", trigger: "blur" },
          ],
          pipeCode: [
            { required: true, message: "请输入管道编号", trigger: "blur" },
          ],
          pipeLength: [
            { required: true, message: "请输入管道长度", trigger: "blur" },
          ],
          pipeAddr: [
            { required: true, message: "请输入管道所在地址", trigger: "blur" },
          ],
          pipeType: [
            { required: true, message: "请选择管道类型", trigger: "blur" },
          ],
          pipePressure: [
            { required: true, message: "请选择管道压力", trigger: "blur" },
          ],
          coordinates: [
            { required: true, message: "请选择管道坐标", trigger: "blur" },
          ],
          iconUrl: [
            { required: true, message: "请上传图片", trigger: "change" },
          ],
          installationTime: [
            { required: true, message: "请选择日期", trigger: "change" },
          ],
        }
      };
    },
    created() {
      // 如果是跳转来的，则接受初始化参数
      this.pipeId = this.$route.query.pipeId;

    },
    mounted() {
      this.getDetail();
      this.getDicts("t_pipe_type").then(response => {
        this.typeOptions = response.data;
      });
      this.getDicts("t_pipe_pressure").then(response => {
        this.pressureOptions = response.data;
      });

    },
    methods: {
      getDetail() {
        getPipe(this.pipeId).then(response => {
          this.form = response.data;
          const { coordinates } = this.form;
          const path1 = eval(coordinates)[0];
          const path2 = eval(coordinates)[1];
          const path3 = [(Number(path1[0]) + Number(path2[0])) / 2, (Number(path1[1]) + Number(path2[1])) / 2];
          let gaoMap = new gaodeMap(process.env.VUE_APP_MAP_CENTER, path3);
          this.gaoMap = gaoMap;
          this.gaoMap.onlyLine(this.form);
        });
      },
      getFileInfo(res) {
        this.editForm.iconUrl = res.url;
      },
      listRemove(e) {
        this.editForm.iconUrl = "";
        this.fileList = [];
      },
      confirmFun(res) {
        //确认选择坐标
        this.editForm.coordinates = res;
      },
      MapdialogFun() {
        this.loadmap = true;
        this.dialogTableVisible = true;
      },
      dialogcancelFun() {
        this.loadmap = false;
        this.dialogTableVisible = false;
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.fileList = [];
      },
      /** 修改按钮操作 */
      handleUpdate(res) {
        getPipe(res.pipeId).then(response => {
          this.editForm = response.data;
          if (this.editForm.iconUrl) {
            this.fileList.push({
              url: this.editForm.iconUrl,
            });
          }
          if (this.editForm.coordinates) {
            this.str = this.editForm.coordinates;
          }
          this.open = true;
          this.title = "修改管道信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["editForm"].validate(valid => {
          if (valid) {
            console.log(this.editForm);
            updatePipe(this.editForm).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              location.reload();
            });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(res) {
        res.isDel = "1";
        var devices = " ";
        var tip = '';
        if(res.deviceInfoList != null){
          for (var i = 0; i < res.deviceInfoList.length; i++) {
            var obj = res.deviceInfoList[i];
            devices = devices + obj.deviceName + " ";
          }
          tip = '该管道下包含的设备（' + devices + '）将一并被删除', "警告"
        }else{
          tip = '请确认是否删除管道名称为"' + res.pipeName + '"的数据项'
        }
        
        this.$confirm(tip, {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return updatePipe(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>
