<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;">
            <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-divider></el-divider>

        <el-row style="width: 100%;">
          <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;padding-left: 40px;">
          <el-col :span="12">
            <el-col :span="12">
              <el-form ref="form" v-model="form" :rules="rules" label-width="100px" style="width: 100%;margin-left: 25px;margin-bottom: 25px;">
                <el-form-item label="隐患名称:" prop="troubleName">
                  <font>{{form.troubleName}}</font>
                </el-form-item>
                <el-form-item label="隐患类型:" prop="troubleType">
                  <font v-if="form.troubleType == 1">设备老化</font>
                  <font v-if="form.troubleType == 2">质保过期</font>
                </el-form-item>
                <el-form-item label="隐患等级:" prop="troubleLevel">
                  <font v-if="form.troubleLevel == 1">Ⅰ级</font>
                  <font v-if="form.troubleLevel == 2">Ⅱ级</font>
                  <font v-if="form.troubleLevel == 3">Ⅲ级</font>
                </el-form-item>
                <el-form-item label="隐患描述:" prop="remarks">
                  <div style="height: 90px;width: 200%; border: 1px solid rgb(187, 183, 183);margin-top: 10px;overflow-y: auto;padding: 5px;border-radius: 5px;">
                    <font>{{form.remarks}}</font>
                  </div>
                </el-form-item>
              </el-form>
            </el-col>
            <el-col :span="12">
              <el-form ref="form" v-model="form" :rules="rules" label-width="100px" style="width: 100%;">
                <el-form-item label="上报人:" prop="reportManName">
                  <font>{{form.reportManName}}</font>
                </el-form-item>
                <el-form-item label="上报时间:" prop="createTime">
                  <font>{{form.createTime}}</font>
                </el-form-item>
                <el-form-item label="处理状态:" prop="dealStatus">
                  <font v-if="form.orderId == null || form.orderId == ''">未生成工单</font>
                  <font v-if="(form.dealStatus == null || form.dealStatus == '') &&
                              form.orderId != null && form.orderId != ''">暂未处理</font>
                  <font v-if="form.dealStatus == 1">不需处理</font>
                  <font v-if="form.dealStatus == 2">已处理完成</font>
                  <font v-if="form.dealStatus == 3">未处理完成</font>
                </el-form-item>
              </el-form>
            </el-col>
          </el-col>
          <el-col :span="12" >
            <el-form ref="form" v-model="form" :rules="rules" label-width="100px" >
              <el-form-item label="照片:" prop="pictureUrl">
                <el-image :src="form.pictureUrl" :preview-src-list="[form.pictureUrl]" style="width: 300px;height: 300px;"></el-image>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>

        <!-- 接单信息 -->
        <el-row v-if="form.orderId != null && form.orderId != ''">
          <el-divider></el-divider>
          <el-col :span="24">
            <div style="color: #31EAEA;width: 100%;height: 40px;">
              <ul><li>接单信息</li></ul>
            </div>
          </el-col>
          <el-col :span="5" style="margin-left: 50px;">
            <el-form ref="form" v-model="form" :rules="rules" label-width="100px" style="height: 30px;width: 100%;">
              <el-form-item label="下发人员:" prop="appointInspectorName">
                <font>{{form.appointInspectorName}}</font>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="6">
            <el-form ref="form" v-model="form" :rules="rules" label-width="100px" style="height: 30px;width: 100%;">
              <el-form-item label="下发时间:" prop="allotTime">
                <font>{{form.allotTime}}</font>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="6">
            <el-form ref="form" v-model="form" :rules="rules" label-width="100px" style="height: 30px;width: 100%;">
              <el-form-item label="工单编号:" prop="orderId">
                <template slot-scope="scope">
                  <el-button type="text" @click="showDetail(form.orderId)">{{form.orderId}}</el-button>
                </template>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
        <el-divider></el-divider>
        <!-- 处置信息 -->
        <div style="width: 100%;font-weight: 600;" v-if="form.feedbackList != null">
          <div style="color: #31EAEA;width: 100%;height: 40px;">
            <ul><li>处置信息</li></ul>
          </div>
          <div class="block" style="width: 95%;margin-left: 2%;">
            <el-timeline ref="timeline">
              <el-timeline-item v-for="(activity, index) in form.feedbackList"  v-show='index>1?false:true' :key="index" :timestamp="activity.feedbackTime">
                <el-card>

                  <el-row>
                    <el-col :span="6">
                      <p>处置人：
                        <span>{{form.appointInspectorName}}</span>
                      </p>

                      <p>处置时间：
                        <span>{{activity.feedbackTime}}</span>
                      </p>


                      <div style="width: 600px;">
                        <p>处置内容：
                          <span>{{activity.contents}}</span>
                        </p>
                      </div>
                    </el-col>
                    <el-col :span="6">
                      <p>是否有隐患：
                        <span v-if="activity.isHiddenDanger == 1">是</span>
                        <span v-if="activity.isHiddenDanger == 2">否</span>
                      </p>

                      <p v-if="activity.isHiddenDanger == 1">处理状态：
                        <span v-if="activity.dealStatus == 1">不需要处理</span>
                        <span v-if="activity.dealStatus == 2">已处理完成</span>
                        <span v-if="activity.dealStatus == 3">未处理完成</span>
                      </p>
                    </el-col>
                    <el-col :span="12">
                      <div class="feedbackTime-div">
                        <div class="feedbackTime">
                          <el-image :src="activity.pictureUrl1" :preview-src-list="[activity.pictureUrl1]" v-if="activity.pictureUrl1 != null && activity.pictureUrl1 != ''" style="width: 90px;"></el-image>
                        </div>
                        <div class="feedbackTime">
                          <el-image :src="activity.pictureUrl2" :preview-src-list="[activity.pictureUrl2]"  v-if="activity.pictureUrl2 != null && activity.pictureUrl2 != ''" style="width: 90px;"></el-image>
                        </div>
                        <div class="feedbackTime">
                          <el-image :src="activity.pictureUrl3" :preview-src-list="[activity.pictureUrl3]"  v-if="activity.pictureUrl3 != null && activity.pictureUrl3 != ''" style="width: 90px;"></el-image>
                        </div>
                      </div>
                    </el-col>
                  </el-row>

                </el-card>
              </el-timeline-item>
            </el-timeline>
          </div>
          <div style="width: 95%;position: relative;" @click="changeDisplay(scope)">
            <i class="el-icon-arrow-down" v-if="isDisplay==false" style="margin-left: 100px;color: #909399;font-size: 14px;">
              <span style="text-decoration:underline">显示全部</span>
            </i>
            <i class="el-icon-arrow-up" v-else style="color: #909399;font-size: 14px;margin-left: 100px;">
              <span style="text-decoration:underline">收起</span>
            </i>
          </div>
        </div>

        <div class="" style="width: 95%; height: 60px;margin-left: 50px;text-align: center;" v-if="form.orderId == '' || form.orderId == null">
          <el-button
            size="normal"
            type="primary"
            icon="el-icon-edit"
            @click="handleIssue(form.troubleId)"
            v-hasPermi="['workOrder:basicsInfo:add']"
          >生成工单
          </el-button>
          <el-button
            size="normal"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(form.troubleId)"
            v-hasPermi="['riskManagement:hiddenTrouble:edit']"
          >修改
          </el-button>
          <el-button
            size="normal"
            type="primary"
            icon="el-icon-delete"
            @click="handleIsDel(form.troubleId)"
            v-hasPermi="['riskManagement:hiddenTrouble:query']"
          >作废
          </el-button>
          <!-- 添加或修改隐患信息对话框 -->
    <el-dialog :title="title1" :visible.sync="open1" width="800px" append-to-body @close="cancel">
      <el-form ref="form1" :model="form1" :rules="rules" label-width="120px">
        <el-col>
          <el-form-item label="隐患名称" prop="troubleName" style="width: 720px">
            <el-input v-model="form1.troubleName" placeholder="请输入隐患名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="隐患类型" prop="troubleType">
            <el-select v-model="form1.troubleType" placeholder="请选择隐患类型">
              <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="11">
          <el-form-item label="隐患级别" prop="troubleLevel">
            <el-select v-model="form1.troubleLevel" placeholder="请选择隐患级别">
              <el-option
                v-for="dict in levelOptions"
                :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="reportMan">
            <el-select v-model="form1.reportMan" placeholder="请选择上报人" clearable size="small" >
              <el-option
                v-for="item in userList"
                :key="item.userId"
                :label="item.nickName"
                :value="item.userId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="隐患设备" prop="device">
            <el-cascader
              v-model="form1.device"
              :options="options"
              :props="props"
              :show-all-levels="false"
              @change="handleChange"
              filterable
              clearable
            ></el-cascader>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="地址" prop="address" style="width: 720px">
            <el-input v-model="form1.address" type="textarea" placeholder="请输入地址" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="经纬度" prop="longitude">
            <el-row>
              <el-col :span="8">
                <el-input v-model.number="form1.longitude" placeholder="请输入经度" />
              </el-col>
              <el-col :span="8" style="margin-left: 10px">
                <el-input v-model.number="form1.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>
          <el-form-item label="图片上传" prop="pictureUrl">
            <MyFileUpload
              listType="picture-card"
              @resFun="getFileInfo"
              @remove="listRemove"
              :fileArr="fileList"
            />
            <el-input v-show="false" disabled v-model="form1.pictureUrl"></el-input>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="隐患信息" prop="remarks" style="width: 720px">
            <el-input v-model="form1.remarks" type="textarea" placeholder="请输入隐患信息" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title2" :visible.sync="open2" width="800px" append-to-body @close="cancel">
      <el-form ref="form2" :model="form2" :rules="rules" label-width="120px">
        <el-form-item label="隐患名称">
          <font>{{form2.troubleName}}</font>
        </el-form-item>
        <el-form-item label="隐患类型">
          <font v-if="form2.troubleType == 1">设备老化</font>
          <font v-if="form2.troubleType == 2">质保过期</font>
        </el-form-item>
        <el-form-item label="隐患级别">
          <font v-if="form2.troubleLevel == 1">Ⅰ级</font>
          <font v-if="form2.troubleLevel == 2">Ⅱ级</font>
          <font v-if="form2.troubleLevel == 3">Ⅲ级</font>
        </el-form-item>
        <el-form-item label="工单名称" prop="orderName" style="width: 720px">
          <el-input v-model="form2.orderName" placeholder="请输入工单名称" />
        </el-form-item>
        <el-form-item label="指定执行人员" prop="appointInspector" >
          <el-select v-model="form2.appointInspector" placeholder="请选择执行人员" clearable size="small"  style="width: 600px">
            <el-option
              v-for="item in inspector"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工单描述" prop="remarks" style="width: 720px">
          <el-input type="textarea" v-model="form2.remarks" placeholder="请输入工单描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <Mapdialog
      v-if="loadmap"
      :dialogTableVisible="dialogTableVisible"
      @dialogcancelFun="dialogcancelFun"
      :slat="form1.latitude"
      :slng="form1.longitude"
      @confirmFun="confirmFun($event)"
    ></Mapdialog>
        </div>
      </div>
    </div>
  </template>

  <script>

import { listHiddenTrouble, getHiddenTrouble, addHiddenTrouble, updateHiddenTrouble, exportHiddenTrouble } from "@/api/riskManagement/hiddenTrouble";
import { addBasicsInfo } from "@/api/workOrder/basicsInfo";
import { inspectorList, allListUser } from "@/api/system/user";
import { deviceNodeTree } from "@/api/device/deviceInfo";
import MyFileUpload from '@/components/MyFileUpload';
import Mapdialog from "@/components/mapDialog/checkDeviceLoaction.vue";

export default {
    name: "HiddenTroubleDetail",
    components: {
      MyFileUpload,
      Mapdialog
    },
    data() {
      return {
        troubleId: '',
        isDisplay:false,
        showAndHide : false,
        form: {  },
        // 隐患类型字典
        typeOptions: [],
        levelOptions: [],
        // 用户列表
        userList: [],
        props: {
          value: "id",
          label: "name",
          level: "level",
          children: "childList",
        },
        // 上传文件列表
        fileList: [],
        // 巡检员列表
        inspector: [],
        // 表单校验
      rules: {
        troubleName: [
          { required: true, message: "请输入隐患名称", trigger: "blur" }
        ],
        troubleType: [
          { required: true, message: "请选择隐患类型", trigger: "blur" }
        ],
        troubleLevel: [
          { required: true, message: "请选择隐患级别", trigger: "blur" }
        ],
        reportMan: [
          { required: true, message: "请选择上报人", trigger: "blur" }
        ],
        address: [
          { required: true, message: "请输入地址", trigger: "blur" }
        ],
        pictureUrl: [
          { required: true, message: "请上传图片", trigger: "blur" }
        ],
        longitude: [
          { required: true, message: "请输入经纬度", trigger: "blur" }
        ],
        orderName: [
          { required: true, message: "请输入工单名称", trigger: "blur" }
        ],
        appointInspector: [
          { required: true, message: "请选择巡检人员", trigger: "blur" }
        ],
      },
      // 查询参数
      queryParams: {
        troubleName: null,
        troubleType: null,
        deviceId: null,
        troubleLevel: null,
        orderId: null,
        longitude: null,
        deviceType: null,
        latitude: null,
        dealStatus: null,
        address: null,
        remarks: null,
        pictureUrl: null,
        reportMan: null
      },
        // 弹出层标题
        title1: "",
        title2: "",
        // 是否显示弹出层
        open1: false,
        open2: false,
        // 表单参数
        form1: {},
        form2: {},
        // 设备级联
        options: [],
      };
    },
    created() {
      
      this.getList();
      // 如果是跳转来的，则接受初始化参数
      this.troubleId = this.$route.query.troubleId;
      this.getDetail();
      this.getDicts("t_trouble_type").then(response => {
      this.typeOptions = response.data;
      });
      this.getDicts("t_trouble_level").then(response => {
        this.levelOptions = response.data;
      });
    },
    methods: {
      /** 查询隐患信息列表 */
    getList() {
      this.loading = true;
      listHiddenTrouble(this.queryParams).then(response => {
        this.hiddenTroubleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
      /** 获取隐患信息详情 */
      getDetail (){
        getHiddenTrouble(this.troubleId).then(response =>{
          this.form = response.data;
          console.log("this.form",this.form)
        });
      },
      /** 详细信息跳转 */
      showDetail(orderId) {
        this.$router.push({
           path: '/workOrder/detail',
           query:{
             orderId : orderId
           }
        })
      },
      /** 提交按钮 */
    submitForm1() {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          if (this.form1.troubleId != null) {
            updateHiddenTrouble(this.form1).then(response => {
              this.msgSuccess("修改成功");
              this.open1 = false;
              this.getList();
            });
          } else {
            addHiddenTrouble(this.form1).then(response => {
              this.msgSuccess("新增成功");
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    getInspectorList(){
      this.loading = true;
      inspectorList().then(response => {
        this.inspector = response.data;
        this.loading = false;
      });
    },
      /** 修改按钮操作 */
    handleUpdate(troubleId) {
      this.reset();
      this.getUserList();
      deviceNodeTree().then(response => {
        this.options = response.data;
      });
      getHiddenTrouble(troubleId).then(response => {
        this.form1 = response.data;
        console.log("this.form1", this.form1);
        if(this.form1.deviceType != null && this.form1.deviceId != null){
          this.form1.device = [this.form1.deviceType, this.form1.deviceId];
        }
        this.open1 = true;
        this.title1 = "修改隐患信息";
        if (this.form1.pictureUrl) {
          this.fileList.push({
            url: this.form1.pictureUrl,
          });
        }
      });
    },
    cancel() {
      this.open1 = false;
      this.open2 = false;
      this.fileList = [];
      this.reset();
    },
    /** 生成工单提交按钮 */
    submitForm2() {
      this.$refs["form2"].validate(valid => {
        if (valid) {
          this.form2.resourceId = this.form2.troubleId;
          this.form2.orderType = "2";
          addBasicsInfo(this.form2).then(response => {
            this.msgSuccess("生成工单成功");
            this.open2 = false;
            this.getList();
          });
        }
      });
    },
    /** 下发按钮操作 */
    handleIssue(troubleId) {
      this.reset();
      this.getInspectorList();
      getHiddenTrouble(troubleId).then(response => {
        this.form2 = response.data;
        this.form2.remarks = "";
        this.open2 = true;
        this.title2 = "填写工单信息";
      });
    },
    getFileInfo(res){
      this.form1.pictureUrl = res.url;
    },
    listRemove(e) {
      this.form1.pictureUrl = "";
      this.fileList = [];
    },
    confirmFun(res) {
      //确认选择经纬度
      this.form1.longitude = res.lng;
      this.form1.latitude = res.lat;
    },
    MapdialogFun() {
      this.loadmap = true;
      this.dialogTableVisible = true;
    },
    handleChange(value) {
      console.log(this.form1.device);
      let arr = this.form1.device;
      this.form1.deviceType = arr[0];
      this.form1.deviceId = arr[1];
    },
    getUserList(){
      this.loading = true;
      allListUser().then(response => {
        this.userList = response.data;
        this.loading = false;
      });
    },
      // 点击按钮显示隐藏
      changeDisplay(e) {
        this.isDisplay = !this.isDisplay
        let $timeline = this.$refs.timeline;
        if (!this.showAndHide) {
          for (let i = 0; i < $timeline.$children.length; i++) {
            if (i > 1) {
              $timeline.$children[i].$el.style.display = "block";
            }
          }
          this.showAndHide = true;
        } else {
          for (let i = 0; i < $timeline.$children.length; i++) {
            if (i > 1) {
              $timeline.$children[i].$el.style.display = "none";
            }
          }
          this.showAndHide = false;
        }
      },
      
      
      // 表单重置
    reset() {
      this.form1 = {
        troubleId: null,
        troubleType: null,
        deviceId: null,
        troubleLevel: null,
        orderId: null,
        longitude: null,
        device: null,
        deviceType: null,
        latitude: null,
        dealStatus: null,
        updateTime: null,
        address: null,
        createTime: null,
        remarks: null,
        pictureUrl: null,
        reportMan: null
      };
      this.resetForm("form1");
      this.form2 = {
        troubleId: null,
        troubleName: null,
        troubleType: null,
        troubleLevel: null,
        orderId: null,
        orderName: null,
        appointInspector: null,
        updateTime: null,
        createTime: null,
        remarks: null
      };
      this.resetForm("form2");
    },
    /** 作废按钮操作 */
    handleIsDel(troubleId) {
      // this.form.troubleId.isDel = "1";
      this.$confirm('是否确认作废隐患名称为"' + this.form.troubleName + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return updateHiddenTrouble(troubleId);
      }).then(() => {
        this.getList();
        this.msgSuccess("已作废");
      }).catch(() => {
      });
    },
    }
  }
  </script>
  <style>
    .el-tree-node__content{
      width: 150px;
    }
    .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;
    }
    .el-tree{
      margin-top: 5px;
    }
    .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>
