<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryParams" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="隐患类型" prop="hiddenType">
        <el-select v-model="queryParams.hiddenType" placeholder="请选择隐患类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="整治情况" prop="remediation">
        <el-select v-model="queryParams.remediation" placeholder="请选择整治情况" clearable size="small">
          <el-option
            v-for="dict in remediationOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="发现时间" prop="hiddenFindDate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.hiddenFindDateStart"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择起始时间">
        </el-date-picker><span style="color: #bebfc3"> - </span>
        <el-date-picker clearable size="small"
                        v-model="queryParams.hiddenFindDateEnd"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择截止时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="所属单位" prop="beyondEnterpriseId">
        <el-select
          v-model="queryParams.beyondEnterpriseId"
          placeholder="请选择单位"
          :disabled="-2 != $store.state.user.enterpriseId"
        >
          <el-option
            v-for="item in enterprises"
            :key="item.enterpriseId"
            :label="item.enterpriseName"
            :value="item.enterpriseId"
          ></el-option>
        </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="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="hiddenList" >
      <el-table-column label="隐患名称" align="center" prop="hiddenTitle" width="200px"/>
      <el-table-column label="隐患等级" align="center" prop="hiddenType">
        <template slot-scope="scope">
          <span v-if="scope.row.hiddenType == '1'">一级隐患</span>
          <span v-if="scope.row.hiddenType == '2'">二级隐患</span>
          <span v-if="scope.row.hiddenType == '3'">三级隐患</span>
        </template>
      </el-table-column>
      <el-table-column label="隐患位置" align="center" prop="hiddenLocation" width="150px" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.hiddenLocation != null">{{scope.row.hiddenLocation}}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="所属单位" align="center" prop="beyondEnterpriseId" width="150" :show-overflow-tooltip="true" :formatter="beyondEnterpriseFormatte"/>
      <el-table-column label="隐患发现人员" align="center" prop="hiddenFindPeople"/>
      <el-table-column label="发现时间" align="center" prop="hiddenFindDate" width="150px"/>
      <el-table-column label="处理方案" align="center" prop="dealPlanUrl">
        <template slot-scope="scope">
          <span
            class="dbtn"
            @click="checkFile(scope.row.dealPlanUrl)"
            v-if="scope.row.dealPlanUrl != null"
          >
            <i class="el-icon el-icon-view"></i>查看/下载
          </span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="整治情况" align="center" prop="remediation">
        <template slot-scope="scope">
          <span v-if="scope.row.remediation == '1'">已完成</span>
          <span v-if="scope.row.remediation == '2'">未完成</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180px">
        <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-edit"
            @click="showDetail(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="title1" :visible.sync="open1" width="800px" append-to-body @cancel="cancel1">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row>
            <el-col :span="11">
              <el-form-item label="隐患名称" prop="hiddenTitle">
                <el-input v-model="form.hiddenTitle" placeholder="请输入隐患名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="隐患等级" prop="hiddenType">
                <el-select v-model="form.hiddenType" placeholder="请选择隐患等级" 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-row>
          <el-row>
            <el-col :span="23">
              <el-form-item label="隐患内容" prop="hiddenContent">
                <el-input type="textarea" v-model="form.hiddenContent" placeholder="请输入隐患内容"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="所属乡镇" prop="township">
                <el-select v-model="form.township" placeholder="请选择所属乡镇" clearable size="small">
                  <el-option
                    v-for="dict in townshipOptions"
                    :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="source">
                <el-select v-model="form.source" placeholder="请选择隐患来源" clearable size="small">
                  <el-option
                    v-for="dict in sourceOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="23">
              <el-form-item label="隐患位置" prop="hiddenLocation">
                <el-input v-model="form.hiddenLocation" placeholder="请输入隐患位置" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="23">
              <el-form-item label="经纬度坐标" prop="longitude">
                <el-col :span="9">
                  <el-input v-model="form.longitude" placeholder="请输入经度" />
                </el-col>
                <el-col :span="9" style="margin-left: 10px">
                  <el-input v-model="form.latitude" placeholder="请输入纬度"/>
                </el-col>
                <el-col :span="3" style="margin-left: 30px">
                  <el-button type="primary" plain @click="MapdialogFun">选择经纬度</el-button>
                </el-col>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="隐患发现人员" prop="hiddenFindPeople">
                <el-input v-model="form.hiddenFindPeople" placeholder="请输入隐患发现人员" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="发现时间" prop="hiddenFindDate">
                <el-date-picker clearable size="small"
                                v-model="form.hiddenFindDate"
                                type="datetime"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                placeholder="请选择发现时间"
                                style="width: 100%">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="23">
              <el-form-item label="处理方案" prop="dealPlan">
                <FileUpload
                  listType="picture"
                  @resFun="getFileInfo"
                  @remove="listRemove"
                  :fileArr="fileList"
                />
                <el-input v-show="false" disabled v-model="form.dealPlan"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="整治情况" prop="remediation">
                <el-select v-model="form.remediation" placeholder="请选择整治情况" clearable size="small">
                  <el-option
                    v-for="dict in remediationOptions"
                    :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="hiddenTroubleType">
                <el-select v-model="form.hiddenTroubleType" placeholder="请选择隐患类型" style="width: 100%;">
                  <el-option label="漏气" value="1" />
                  <el-option label="腐蚀" value="2" />
                  <el-option label="裂缝" value="3" />
                  <el-option label="设备故障" value="4" />
                  <el-option label="安全距离不足" value="5" />
                  <el-option label="管道承重" value="6" />
                  <el-option label="其他" value="7" />
                </el-select>
              </el-form-item>
            </el-col>

          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="所属单位" prop="beyondEnterpriseId">
                <el-select v-model="form.beyondEnterpriseId" placeholder="请选择所属单位名称" style="width: 100%" @change="selectworkAssignEnterprose($event)">
                  <el-option
                    v-for="item in enterprises"
                    :key="item.enterpriseId"
                    :label="item.enterpriseName"
                    :value="parseInt(item.enterpriseId)"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="23">
              <el-form-item label="备注信息" prop="remarks">
                <el-input type="textarea" v-model="form.remarks" placeholder="请输入备注信息" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel1">取 消</el-button>
        </div>
      </el-dialog>

      <el-dialog :title="title2" :visible.sync="open2" width="1200px" append-to-body @cancel="cancel2" @close="cancel2">
        <el-row>
          <el-col :span="14">
            <el-form ref="detailForm" :model="detailForm" label-width="120px">
              <el-row>
                <el-col :span="10">
                  <el-form-item label="隐患名称:">
                    <el-input v-model="detailForm.hiddenTitle" disabled/>
                  </el-form-item>
                  <el-form-item label="隐患发现人员:">
                    <el-input v-model="detailForm.hiddenFindPeople" disabled/>
                  </el-form-item>
                  <el-form-item label="发现时间:">
                    <el-input v-model="detailForm.hiddenFindDate" disabled/>
                  </el-form-item>
                </el-col>
                <el-col :span="13">
                  <el-form-item label="隐患等级:">
                    <el-input v-if="detailForm.hiddenType == '1'" value="一级隐患" disabled/>
                    <el-input v-if="detailForm.hiddenType == '2'" value="二级隐患" disabled/>
                    <el-input v-if="detailForm.hiddenType == '3'" value="三级隐患" disabled/>
                  </el-form-item>
                  <el-form-item label="隐患位置:">
                    <el-input v-model="detailForm.hiddenLocation" disabled/>
                  </el-form-item>
                  <el-form-item label="整治情况:">
                    <el-input v-if="detailForm.remediation == '1'" value="已完成" disabled/>
                    <el-input v-if="detailForm.remediation == '2'" value="未完成" disabled/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="10">
                  <el-form-item label="处理方案:">
                    <span class="dbtn" @click="checkFile(detailForm.dealPlanUrl)" v-if="detailForm.dealPlan != ''">
                      <i class="el-icon el-icon-view"></i>查看/下载
                    </span>
                    <span v-else><el-input disabled/></span>
                  </el-form-item>
                </el-col>
                <el-col :span="13">
                  <el-form-item label="隐患类型" prop="hiddenTroubleType">
                    <el-input v-if="detailForm.hiddenTroubleType == '1'" value="漏气" disabled/>
                    <el-input v-if="detailForm.hiddenTroubleType == '2'" value="腐蚀" disabled/>
                    <el-input v-if="detailForm.hiddenTroubleType == '3'" value="裂缝" disabled/>
                    <el-input v-if="detailForm.hiddenTroubleType == '4'" value="设备故障" disabled/>
                    <el-input v-if="detailForm.hiddenTroubleType == '5'" value="安全距离不足" disabled/>
                    <el-input v-if="detailForm.hiddenTroubleType == '6'" value="管道承重" disabled/>
                    <el-input v-if="detailForm.hiddenTroubleType == '7'" value="其他" disabled/>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="23">
                  <el-form-item label="所属单位" prop="beyondEnterpriseId">
                    <el-input :value="beyondEnterpriseFormatte(detailForm)" disabled/>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="23">
                  <el-form-item label="隐患内容:">
                    <el-input type="textarea" v-model="detailForm.hiddenContent" disabled/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="23">
                  <el-form-item label="备注信息:">
                    <el-input type="textarea" v-model="detailForm.remarks" disabled/>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-col>

          <el-col :span="9">
            <div style="width: 100%;height: 390px; border: 1px solid rgb(218, 213, 213);margin-bottom: 10px;">
              <div style="width: 100%;height: 100%" id="hiddenContainer"></div>
            </div>
          </el-col>
        </el-row>
      </el-dialog>

    <GetPos
      :dialogVisible.sync="dialogTableVisible"
      device=""
      :devicePos="devicePos"
      @close="dialogcancelFun"
      @getPath="getPath"
    />

  </div>
</template>

<script>
import { listHidden, getHidden, delHidden, addHidden, updateHidden, exportHidden } from "@/api/standingBook/hidden";
import FileUpload from '@/components/FileUpload';
import GetPos from '@/components/GetPos';
import { EditorMap } from "@/utils/mapClass/getPath.js";
import { enterpriseLists } from "@/api/regulation/info";

let uploadfile = require("@/assets/uploadfile.png");
export default {
  name: "Hidden",
  components: {
    FileUpload,
    GetPos
  },
  data() {
    return {
      enterprises: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 隐患整治台账表格数据
      hiddenList: [],
      // 弹出层标题
      title1: "",
      title2: "",
      // 是否显示弹出层
      open1: false,
      open2: false,
      // 隐患类型字典
      typeOptions: [],
      // 整治情况字典
      remediationOptions: [],
      // 所属乡镇字典
      townshipOptions: [],
      // 隐患来源
      sourceOptions: [],

      // 上传文件列表
      fileList: [],
      // 地图
      dialogTableVisible: false,
      devicePos: [],
      map: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hiddenTitle: null,
        hiddenType: null,
        remediation: null,
        hiddenFindDateStart: null,
        hiddenFindDateEnd: null,
        beyondEnterpriseId:null
      },
      // 表单参数
      form: {},
      detailForm: {},
      // 表单校验
      rules: {
        hiddenTitle: [
          { required: true, message: "请输入隐患名称", trigger: "blur" },
        ],
        hiddenType: [
          { required: true, message: "请选择隐患等级", trigger: "blur" },
        ],
        hiddenContent: [
          { required: true, message: "请输入隐患内容", trigger: "blur" },
        ],

        longitude: [
          { required: true, message: "请输入经纬度", trigger: "blur" },
        ],
        hiddenFindPeople: [
          { required: true, message: "请输入隐患发现人员", trigger: "blur" },
        ],
        hiddenFindDate: [
          { required: true, message: "请选择发现时间", trigger: "change" },
        ],
        remediation: [
          { required: true, message: "请选择整治情况", trigger: "change" },
        ],
        // dealPlan: [
        //   { required: true, message: "请上传文件", trigger: "change" },
        // ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("t_hidden_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("t_remediation").then(response => {
      this.remediationOptions = response.data;
    });
    this.getDicts("t_township").then(response => {
      this.townshipOptions = response.data;
    });
    this.getDicts("t_source").then(response => {
      this.sourceOptions = response.data;
    });
    if(-2 != this.$store.state.user.enterpriseId){
      this.queryParams.beyondEnterpriseId = this.$store.state.user.enterpriseId;
    }
    this.getEnterpriseLists();

  },
  methods: {
    /** 查询隐患整治台账列表 */
    getList() {
      this.loading = true;
      listHidden(this.queryParams).then(response => {
        this.hiddenList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel1() {
      this.open1 = false;
      this.reset1();
      this.fileList = [];
    },
    cancel2() {
      this.open2 = false;
      this.reset2();
      this.map.destroy();
    },
    // 表单重置
    reset1() {
      this.form = {
        hiddenId: null,
        hiddenTitle: null,
        hiddenContent: null,
        hiddenLocation: null,
        longitude:null,
        latitude: null,
        hiddenType: null,
        hiddenFindPeople: null,
        hiddenFindDate: null,
        dealPlan: null,
        remediation: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: null
      };
      this.resetForm("form");
      this.fileList = [];
      this.devicePos = [];
    },
    reset2() {
      this.detailForm = {
        hiddenId: null,
        hiddenTitle: null,
        hiddenContent: null,
        hiddenLocation: null,
        longitude:null,
        latitude: null,
        hiddenType: null,
        hiddenFindPeople: null,
        hiddenFindDate: null,
        dealPlan: null,
        remediation: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: null
      };
      this.resetForm("detailForm");
      this.devicePos = [];
    },
    //所属单位
    getEnterpriseLists(){
      const param = {};
      // this.judgeOperateType(param);
      enterpriseLists(param).then(response => {
        this.enterprises = response.rows;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        hiddenTitle: null,
        hiddenType: null,
        remediation: null,
        hiddenFindDateStart: null,
        hiddenFindDateEnd: null
    },
      this.resetForm("queryParams");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset1();
      this.open1 = true;
      this.title1 = "添加隐患整治台账";
      this.getEnterpriseLists();
    },
    selectworkAssignEnterprose(enterpriseId){
      this.getInspectionUserList(enterpriseId);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset1();
      getHidden(row.hiddenId).then(response => {
        this.form = response.data;
        if (this.form.dealPlan) {
          this.fileList.push({
            name: this.form.dealPlan,
            url: uploadfile,
          });
        }
        this.devicePos = [this.form.longitude, this.form.latitude];
        this.open1 = true;
        this.title1 = "修改隐患整治台账";
        this.getEnterpriseLists();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.hiddenId != null) {
            updateHidden(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open1 = false;
              this.getList();
            });
          } else {
            addHidden(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const hiddenIds = row.hiddenId || this.ids;
      row.isDel = "1";
      this.$confirm('是否确认删除"' + row.hiddenTitle + '"的台账?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updateHidden(row);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有隐患整治台账数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportHidden(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    /** 详细信息跳转 */
    showDetail(row) {
      this.reset2();
      getHidden(row.hiddenId).then(response => {
        this.detailForm = response.data;
        this.getEnterpriseLists();
        this.devicePos = [this.detailForm.longitude, this.detailForm.latitude];
        this.open2 = true;
        this.title2 = "隐患整治台账详情";

        this.$nextTick(() => {
          this.map = new EditorMap("hiddenContainer", {}, this);
          this.map.addDevice({ path: this.devicePos });
          this.map.nowMouseTarget = null;
          this.map.mousetoolClose(false);
        });
      });
    },
    checkFile(url) {
      window.open(url,'_blank');
    },
    getFileInfo(res){
      this.form.dealPlan = res.fileName;
      this.form.dealPlanUrl = res.url;
      this.fileList.push({
        name: res.fileName,
        url: uploadfile,
      });
    },
    listRemove(e) {
      this.form.dealPlan = "";
      this.fileList = [];
    },
    MapdialogFun() {
      this.dialogTableVisible = true;
      // this.devicePos = [this.form.longitude, this.form.latitude];
      console.log("devicePos",this.devicePos)
    },
    dialogcancelFun() {
      this.dialogTableVisible = false;
    },
    getPath(res){
      console.log("res", res);
      console.log(this.form.longitude, this.form.latitude);
      //确认选择经纬度
      this.form.longitude = res[0];
      this.form.latitude = res[1];
    },
    beyondEnterpriseFormatte(row){
      let enterpeiseInfo = this.enterprises.find(item =>item.enterpriseId == row.beyondEnterpriseId);
      return enterpeiseInfo?enterpeiseInfo.enterpriseName:"-";
    }

  }
};
</script>
<style>
  .dbtn {
    display: inline-block;
    line-height: normal;
    padding-left: 2px;
    padding-right: 2px;
    cursor: pointer;
    border-radius: 3px;
    border-style: solid;
    border-width: 0;
    color: rgb(48, 180, 107);
  }
  .dbtn:hover {
    border-width: 1px;
    border-color: rgb(48, 180, 107);
  }
</style>
