<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="登记部门" prop="deptId" v-show="deptShow">
        <el-select v-model="queryParams.deptId" filterable placeholder="请选择部门" clearable>
          <el-option
            v-for="dict in allDeptList"
            :key="dict.deptId"
            :label="dict.deptName"
            :value="dict.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单位名称" prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入单位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="行业" prop="enterpriseType">
        <el-select v-model="queryParams.enterpriseType" placeholder="请选择行业" clearable>
          <el-option
            v-for="dict in dict.type.enterprise_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="企业现状" prop="enterpriseStatus">
        <el-select v-model="queryParams.enterpriseStatus" placeholder="请选择行业" clearable>
          <el-option
            v-for="dict in statusOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="整改进度" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择整改进度" clearable>
          <el-option
            v-for="dict in dict.type.hidden_danger_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="隐患级别" prop="hiddenDangerGrade">
        <el-select v-model="queryParams.hiddenDangerGrade" placeholder="请选择隐患级别" clearable>
          <el-option
            v-for="dict in dict.type.hidden_danger_grade"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="排查时间" prop="checkTime">
        <el-date-picker
          v-model="daterangeCheckTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          unlink-panels
          :picker-options="pickerOptions"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="整改时间">
        <el-date-picker
          v-model="daterangeRectifyTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          unlink-panels
          :picker-options="pickerOptions"
        ></el-date-picker>
      </el-form-item>
      <!--
      <el-form-item label="整改措施" prop="rectifyMeasure">
        <el-input
          v-model="queryParams.rectifyMeasure"
          placeholder="请输入整改措施"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="整改时间" prop="rectifyTime">
        <el-input
          v-model="queryParams.rectifyTime"
          placeholder="请输入整改时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="整改责任人" prop="principalName">
        <el-input
          v-model="queryParams.principalName"
          placeholder="请输入整改责任人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="监管部门" prop="regulatoryDept">
        <el-input
          v-model="queryParams.regulatoryDept"
          placeholder="请输入监管部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="监管部门责任人" prop="regulatoryName">
        <el-input
          v-model="queryParams.regulatoryName"
          placeholder="请输入监管部门责任人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->

      <!--<el-form-item label="行政区划" prop="areaId">
        <el-select v-model="queryParams.areaId" placeholder="请选择企业地址">
          <el-option
            v-for="dict in dict.type.area_id"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="隐患类型" prop="hiddenDangerType">
        <el-select v-model="queryParams.hiddenDangerType" placeholder="请选择隐患类型" clearable>
          <el-option
            v-for="dict in dict.type.hidden_danger_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
<!--      <el-form-item label="排查方式" prop="checkType">
        <el-select v-model="queryParams.checkType" placeholder="请选择排查方式" clearable>
          <el-option
            v-for="dict in dict.type.check_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->

      <el-form-item label="标签" prop="hiddenDangerLabelArray">
        <el-select v-model="queryParams.hiddenDangerLabelArray" multiple collapse-tags placeholder="请选择标签" clearable>
          <el-option
            v-for="dict in dict.type.hidden_danger_label"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="告警查询" prop="overTime">
        <el-select v-model="queryParams.overTime" placeholder="请选告警范围" clearable>
          <el-option
            v-for="dict in alertState"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否乡镇(街道)" prop="isTowns"  label-width="130px">
        <el-select v-model="queryParams.isTowns" placeholder="请选择是否乡镇（街道）" clearable>
          <el-option
            v-for="dict in dict.type.ehs_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.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"
          v-hasPermi="['ehsHiddenDanger:ehsHiddenDanger:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ehsHiddenDanger:ehsHiddenDanger:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ehsHiddenDanger:ehsHiddenDanger:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ehsHiddenDanger:ehsHiddenDanger:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['ehsHiddenDanger:ehsHiddenDanger:import']"
        >导入</el-button >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ehsHiddenDangerList" @selection-change="handleSelectionChange" max-height="550" style="width: 100%" :row-class-name="tableRowClassName">
     <!-- <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="主键" align="center" prop="hiddenDangerId" />-->
      <el-table-column label="上报部门" align="center" prop="deptName"  v-if="deptShow"/>
      <el-table-column label="单位名称" align="center" prop="enterpriseName" >
      <template slot-scope="scope">
          <el-link type="primary" @click="handleUpdate(scope.row,true)">{{scope.row.enterpriseName}} </el-link>
      </template>
      </el-table-column>
      <el-table-column label="行业" align="center" prop="enterpriseType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.enterprise_type" :value="scope.row.enterpriseType"/>
        </template>
      </el-table-column>
      <el-table-column label="企业现状" align="center" prop="enterpriseStatus" width="50">
        <template slot-scope="scope">
          <span> {{ (scope.row.enterpriseStatus !=null && scope.row.enterpriseStatus != "") ? statusOptions[scope.row.enterpriseStatus].label:""}}</span>
        </template>
      </el-table-column>
      <el-table-column label="隐患数量" align="center" prop="hdCount" width="50"/>
      <el-table-column label="隐患情况" align="center" prop="hiddenDangerDesc" />
      <el-table-column label="排查时间" align="center" prop="checkTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排查方式" align="center" prop="checkType"width="50">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.check_type" :value="scope.row.checkType"/>
        </template>
      </el-table-column>
      <el-table-column label="整改措施" align="center" prop="rectifyMeasure" />
      <el-table-column label="要求整改时间" align="center" prop="rectifyTermTime" width="80">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rectifyTermTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="整改进度" align="center" prop="status"width="50">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hidden_danger_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="整改时间" align="center" prop="rectifyTime" width="80">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rectifyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="整改责任人" align="center" prop="principalName" width="50"/>
      <el-table-column label="监管部门" align="center" prop="regulatoryDept" />
      <el-table-column label="监管部门责任人" align="center" prop="regulatoryName" width="50"/>
      <el-table-column label="隐患级别" align="center" prop="hiddenDangerGrade"width="50">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hidden_danger_grade" :value="scope.row.hiddenDangerGrade"/>
        </template>
      </el-table-column>
      <!--<el-table-column label="是否乡镇（街道）" align="center" prop="isTowns">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ehs_yes_no" :value="scope.row.isTowns"/>
        </template>
      </el-table-column>-->
      <!--<el-table-column label="隐患照片" align="center" prop="hiddenDangerImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.hiddenDangerImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="整改照片" align="center" prop="rectifyImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.rectifyImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="隐患类型" align="center" prop="hiddenDangerType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hidden_danger_type" :value="scope.row.hiddenDangerType"/>
        </template>
      </el-table-column>
            <el-table-column label="整改情况" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hidden_danger_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="整改资金" align="center" prop="rectifyMoney" />-->
      <el-table-column label="是否督办" align="center" prop="isSupervise"width="50">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isSupervise==1" @change="val => {isSuperviseChange(val, scope.row);}" ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否协办" align="center" prop="assistDept"width="50">
        <template slot-scope="scope">
          <el-link type="primary" @click="val => {assistDeptClick(val, scope.row);}" > {{(scope.row.assistDept=='' ||scope.row.assistDept==null)?"无协办单位": "显示协办单位" }} </el-link>
        </template>
      </el-table-column>

      <el-table-column label="标签" align="center" prop="hiddenDangerLabel"width="50">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hidden_danger_label" :value="scope.row.hiddenDangerLabel"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,false)"
            v-hasPermi="['ehsHiddenDanger:ehsHiddenDanger:edit']"
          >修改</el-button>
         <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleUpdate(scope.row,true)"
          >明细</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ehsHiddenDanger:ehsHiddenDanger:remove']"
          >删除</el-button>
          <!--<el-button v-if="scope.row.isSupervise==0"
            size="mini"
            type="text"
            icon="el-icon-s-tools"
            @click="handleIsSupervise(scope.row)"
            v-hasPermi="['ehsHiddenDanger:ehsHiddenDanger:supervise']"
          >督办</el-button>-->
         <!-- v-if="scope.row.assistDept=='' || scope.row.assistDept==null"-->
          <el-button
             size="mini"
             type="text"
             icon="el-icon-setting"
             @click="handleAssist(scope.row)"
             v-hasPermi="['ehsHiddenDanger:ehsHiddenDanger:assist']"
          >协办</el-button>
          <el-button v-if="scope.row.isSupervise==1 && ( scope.row.superviseResult=='' || scope.row.superviseResult==null)"
             size="mini"
             type="text"
             icon="el-icon-s-comment"
             @click="handleAssistResult(scope.row)"
             v-hasPermi="['ehsHiddenDanger:ehsHiddenDanger:assistResult']"
          >录入反馈</el-button>
          <el-button v-if="scope.row.superviseResult!=null && scope.row.superviseResult!=''  "
                     size="mini"
                     type="text"
                     icon="el-icon-document"
                     @click="handleShowAssistResult(scope.row)"
                     v-hasPermi="['ehsHiddenDanger:ehsHiddenDanger:showAssistResult']"
          >显示反馈</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"  >
    <el-form ref="form" :model="form" :rules="rules" label-width="110px">
      <el-form-item label="单位名称" prop="enterpriseName">
        <el-input placeholder="请输入单位名称" v-model="form.enterpriseName" class="input-with-select" :readonly="isReadOnly">
          <el-button slot="append" icon="el-icon-search" @click="openEnterprise"></el-button>
        </el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="行业" prop="enterpriseType">
            <el-select v-model="form.enterpriseType" placeholder="请选择行业" :disabled="isReadOnly">
              <el-option
                v-for="dict in dict.type.enterprise_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="企业现状"   prop="enterpriseStatus">
            <el-select v-model="form.enterpriseStatus" placeholder="请选择企业现状" clearable :disabled="isReadOnly">
              <el-option
                v-for="dict in statusOptions"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="经济类型" prop="economicType">
            <el-select v-model="form.economicType" placeholder="请选择经济类型" :disabled="isReadOnly">
              <el-option
                v-for="dict in dict.type.economic_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="企业规模"   prop="enterpriseScale">
            <el-select v-model="form.enterpriseScale" placeholder="请选择企业规模" clearable :disabled="isReadOnly">
              <el-option
                v-for="dict in dict.type.enterprise_scale"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="企业地址" prop="areaId">
            <el-select v-model="form.areaId" placeholder="请选择企业地址" :disabled="isReadOnly">
              <el-option
                v-for="(item, index) in areaInfo"
                :key="index"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排查方式" prop="checkType">
            <el-select v-model="form.checkType" placeholder="请选择排查方式" :disabled="isReadOnly">
              <el-option
                v-for="dict in dict.type.check_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="整改进度" prop="status">
            <el-select v-model="form.status" placeholder="请选择整改进度" :disabled="isReadOnly">
              <el-option
                v-for="dict in dict.type.hidden_danger_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排查时间" prop="checkTime" >
            <el-date-picker clearable :readonly="isReadOnly"
                            v-model="form.checkTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择排查时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22">
          <el-form-item label="是否乡镇（街道）" prop="isTowns" label-width="130px">
            <el-select v-model="form.isTowns" placeholder="请选择是否乡镇（街道）" :disabled="isReadOnly">
              <el-option
                v-for="dict in dict.type.ehs_yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="form.address" type="textarea" placeholder="请输入内容" :readonly="isReadOnly" />
      </el-form-item>
      <el-form-item label="隐患情况" prop="hiddenDangerDesc">
        <el-input v-model="form.hiddenDangerDesc" type="textarea" placeholder="请输入内容" :readonly="isReadOnly" @blur="handleBlur" />
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="整改期限" prop="rectifyTermTime" >
            <el-date-picker clearable :readonly="isReadOnly"
                            v-model="form.rectifyTermTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择整改期限">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="隐患数量" prop="hdCount">
            <el-input v-model="form.hdCount"   :readonly="true"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="隐患照片" prop="hiddenDangerImg">
        <image-upload  v-if="!isReadOnly" v-model="form.hiddenDangerImg"  />
        <image-preview  v-if="isReadOnly" :src="form.hiddenDangerImg"  />
      </el-form-item>
      <el-form-item label="整改措施" prop="rectifyMeasure">
        <el-input v-model="form.rectifyMeasure" type="textarea" placeholder="请输入整改措施" :readonly="isReadOnly"/>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="整改时间" prop="rectifyTime">
            <el-date-picker clearable :readonly="isReadOnly"
                            v-model="form.rectifyTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择整改时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="整改责任人" prop="principalName">
            <el-input v-model="form.principalName" placeholder="请输入整改责任人" :readonly="isReadOnly"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="整改照片" prop="rectifyImg">
        <image-upload  v-if="!isReadOnly" v-model="form.rectifyImg"  />
        <image-preview  v-if="isReadOnly" :src="form.rectifyImg"  />
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="监管部门" prop="regulatoryDept">
            <el-input v-model="form.regulatoryDept" placeholder="请输入监管部门" :readonly="isReadOnly"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="监管责任人" prop="regulatoryName">
            <el-input v-model="form.regulatoryName" placeholder="请输入监管部门责任人" :readonly="isReadOnly"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="隐患级别" prop="hiddenDangerGrade">
            <el-select v-model="form.hiddenDangerGrade" placeholder="请选择隐患级别" :disabled="isReadOnly">
              <el-option
                v-for="dict in dict.type.hidden_danger_grade"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="整改资金" prop="rectifyMoney">
            <el-input v-model="form.rectifyMoney" placeholder="请输入整改资金" :readonly="isReadOnly" />
          </el-form-item>

        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="标签" prop="hiddenDangerLabelArray">
            <el-select v-model="form.hiddenDangerLabelArray" multiple collapse-tags placeholder="请选择标签" :disabled="isReadOnly">
              <el-option
                v-for="dict in dict.type.hidden_danger_label"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!--<el-form-item label="隐患类型" prop="hiddenDangerType">
            <el-select v-model="form.hiddenDangerType" placeholder="请选择隐患类型" :disabled="isReadOnly">
              <el-option
                v-for="dict in dict.type.hidden_danger_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>-->
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-if="!isReadOnly" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
    <enterpriseSelect ref="enterpriseSelectRef" @selectInfo='selectInfo'></enterpriseSelect>
  </el-dialog>
    <!-- 协办 -->
    <el-dialog :title="title" :visible.sync="openAssist" width="500px" append-to-body>
      <el-form ref="form" :model="formAssist" :rules="rules" label-width="80px">
        <el-form-item label="协办部门" prop="deptList">
          <el-select  v-model="assistDeptList" multiple collapse-tags
                      style="margin-left: 20px;" placeholder="请选择">
            <el-option
              v-for="dict in allDeptList"
              :key="dict.deptId"
              :label="dict.deptName"
              :value="dict.deptId"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="assistForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 导入 -->
    <importUpload
      ref="uploadRef"
      @ok="upLoadOk"
      downUrl="/ehsHiddenDanger/ehsHiddenDanger/importTemplate"
      temName="隐患信息导入模板"
    ></importUpload>
  </div>
</template>

<script>
import { listEhsHiddenDanger, getEhsHiddenDanger, delEhsHiddenDanger, addEhsHiddenDanger, updateEhsHiddenDanger,
  superviseEhsHiddenDanger,assistEhsHiddenDanger, getAdminArea } from "@/api/ehs/ehsHiddenDanger";
import {listAllDept } from "@/api/system/dept";
import enterpriseSelect from "./enterpriseSelect"
import importUpload from "@/components/upload/upload";
import { eventBus } from '@/main.js';
export default {
  name: "EhsHiddenDanger",
  dicts: ['hidden_danger_type', 'hidden_danger_grade', 'hidden_danger_label', 'enterprise_type','enterprise_scale',
    'economic_type','area_id', "hidden_danger_status","check_type", "ehs_yes_no"],
  components:{ importUpload,enterpriseSelect  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 隐患信息表格数据
      ehsHiddenDangerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isReadOnly:false,//form表单是否可以编辑，新增、修改可以编辑、明细是只读

      //是否显示部门列表查询条件
      deptShow: false,
      curDeptId:null,//登录用户的部门id
      //所有部门列表字典
      allDeptList: [],
      currEnterpriseInfo:{},//选中的企业信息
      // 要求整改时间时间范围
      daterangeCheckTime: [],
      // 要求整改时间时间范围
      daterangeRectifyTime: [],
      openAssist:false,//打开协办弹框
      assistDeptList:"",//协办部门
      currAssistHiddenDangerId :-1,//当前协办隐患id
      statusOptions: [{
        value: '0',
        label: '停业',
      }, {
        value: '1',
        label: '正常经营',
      }],
      areaInfo: [],
      enterpriseSelectOpen:false,//企业信息查询窗口
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseName: null,
        enterpriseStatus: null,
        enterpriseType: null,
        hiddenDangerDesc: null,
        checkTime: null,
        checkType: null,
        hiddenDangerImg: null,
        rectifyMeasure: null,
        status: null,
        rectifyTime: null,
        principalName: null,
        rectifyImg: null,
        regulatoryDept: null,
        regulatoryName: null,
        hiddenDangerGrade: null,
        hiddenDangerType: null,
        hiddenDangerLabel: null,
        hiddenDangerLabelArray:null,
        isTowns:null,
        overTime:null,
        address:null,
        hdCount:0,//隐患条数
      },
      alertState: [{
        value: '7',
        label: '7天预警'
      }, {
        value: '3',
        label: '3天预警'
      }, {
        value: '0',
        label: '逾期告警'
      },{
        value: '10',
        label: '全部临期或逾期'
      } ],
      // 表单参数
      form: {},
      formAssist:{},
      // 表单校验
      rules: {
        enterpriseName: [ { type: 'string',required: true,message: "单位名称必填", trigger: 'blur'}],
        enterpriseType: [ { type: 'string',required: true,message: "行业必须选择", trigger: 'blur'}],
        enterpriseStatus: [ { type: 'string',required: true,message: "企业现状必须选择", trigger: 'blur'}],
        address: [ { type: 'string',required: true,message: "企业详细地址必填", trigger: 'blur'}],
        hiddenDangerDesc: [ { type: 'string',required: true,message: "隐患情况必填", trigger: 'blur'}],
        checkTime: [ {  required: true,message: "排查时间必填", trigger: 'blur'}],
        rectifyTermTime: [ {  required: true,message: "整改期限必填", trigger: 'blur'}],
        checkType: [ {  required: true,message: "排查方式必须选择", trigger: 'blur'}],
       /* isTowns: [ {  required: true,message: "是否乡镇（街道必须选择", trigger: 'blur'}],*/
        status: [ {  required: true,message: "整改进度必须选择", trigger: 'blur'}],
        // rectifyMeasure: [ { type: 'string',required: true,message: "整改措施必填", trigger: 'blur'}],
        // rectifyTime: [ { type: 'date',required: true,message: "整改时间必填", trigger: 'blur'}],
        hiddenDangerImg: [{required:true,message: "隐患照片必填", trigger: 'blur'}],
        rectifyImg: [{ validator: this.validateRectifyImg,message: "整改照片必填", trigger: 'blur'}],
        hiddenDangerGrade: [ { type: 'string',required: true,message: "隐患级别必填", trigger: 'blur'}],
        // hiddenDangerType: [ { type: 'string',required: true,message: "隐患类型必填", trigger: 'blur'}],
        rectifyMoney: [ {
          pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
          message: "请输入合法的金额数字，最多两位小数",
          trigger: "change"
        }],
        areaId: [
          { required: true, message: "不能为空", trigger: "change" }
        ],
/*        economicType: [
          { required: true, message: "不能为空", trigger: "change" }
        ],*/
  /*      enterpriseScale: [
          { required: true, message: "不能为空", trigger: "change" }
        ],*/
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
    };
  },
  created() {
    eventBus.$emit('toLogo', this.$route,'安全生产隐患管理');
    this.queryParams.overTime = this.$route.query.overTime;
    this.getList();
    this.$route.query.overTime = null;
    this.getListAllDept();
    this.deptShow = this.$store.state.user.parentId==0 ?true :false;
    this.curDeptId = this.$store.state.user.deptId;
    console.log(this.$route.query.overTime);
    this.getAdminArea();

  },
 /* beforeDestroy() {
    eventBus.$emit('toLogo', this.$router,'');
  },*/
  methods: {
    /** 查询隐患信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCheckTime && '' != this.daterangeCheckTime) {
        this.queryParams.params["beginCheckTime"] = this.daterangeCheckTime[0];
        this.queryParams.params["endCheckTime"] = this.daterangeCheckTime[1];
      }
      if (null != this.daterangeRectifyTime && '' != this.daterangeRectifyTime) {
        this.queryParams.params["beginRectifyTime"] = this.daterangeRectifyTime[0];
        this.queryParams.params["endRectifyTime"] = this.daterangeRectifyTime[1];
      }
      listEhsHiddenDanger(this.queryParams).then(response => {
        this.ehsHiddenDangerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 单位信息列表 */
    getListAllDept() {
      console.log(this.$store.state.user.deptId);
      this.loading = true;
      listAllDept().then(response => {
        this.allDeptList = response.data;
        this.loading = false;
      });
    },
    /** 获取企业地址下拉列表 */
    getAdminArea() {
      getAdminArea().then((res) => {
        this.areaInfo = res.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openAssist = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hiddenDangerId: null,
        enterpriseName: null,
        enterpriseStatus: null,
        enterpriseType: null,
        hiddenDangerDesc: null,
        checkTime: null,
        checkType: null,
        hiddenDangerImg: null,
        rectifyMeasure: null,
        status: null,
        rectifyTime: null,
        principalName: null,
        rectifyImg: null,
        regulatoryDept: null,
        regulatoryName: null,
        hiddenDangerGrade: null,
        hiddenDangerType: null,
        hiddenDangerLabel: null,
        hiddenDangerLabelArray:null,
        areaId:null,
        economicType:null,
        enterpriseScale:null,
        isTowns:null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCheckTime = [];
      this.daterangeRectifyTime = [];

      this.resetForm("queryForm");
      this.queryParams.overTime = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.hiddenDangerId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.isReadOnly = false;
      this.title = "添加隐患信息";
      if (this.areaInfo.length === 1) {
        this.form.areaId = this.areaInfo[0].dictValue;
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row,isReadOnly) {
      this.reset();
      this.isReadOnly = isReadOnly;
      const hiddenDangerId = row.hiddenDangerId || this.ids
      getEhsHiddenDanger(hiddenDangerId).then(response => {
        this.form = response.data;
  /*      this.form.hiddenDangerLabelArray.push("0");
        this.form.hiddenDangerLabelArray.push("1");*/
      console.log(this.form.hiddenDangerLabelArray);
        this.open = true;
        if(isReadOnly){
          this.title = "显示隐患信息";
        }else {
          this.title = "修改隐患信息";
        }

      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.hiddenDangerLabelArray!=null){
            this.form.hiddenDangerLabel = this.form.hiddenDangerLabelArray.join();
          }
          let str =this.form.hiddenDangerDesc.replace(/；/g, ';');
          this.form.hdCount=str.split(';').length;
          if (this.form.hiddenDangerId != null) {
            updateEhsHiddenDanger(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEhsHiddenDanger(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /**  */
    handleBlur() {
      console.log("handleBlur");
      if(this.form.hiddenDangerDesc!=null &&this.form.hiddenDangerDesc !=null){
        let str =this.form.hiddenDangerDesc.replace(/；/g, ';');
        this.form.hdCount=str.split(';').length;
      }else{
        this.form.hdCount =0;
      }

    },
    validateRectifyImg(rule, value, callback) {
      if (this.form.status == 10 && !value) {
        callback(new Error('整改照片是必填项'));
        this.$message.warning('整改照片不能为空!');
      } else {
        callback();
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hiddenDangerIds = row.hiddenDangerId || this.ids;
      this.$modal.confirm('是否确认删除隐患信息？').then(() => {
        delEhsHiddenDanger(hiddenDangerIds).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehsHiddenDanger/ehsHiddenDanger/export', {
        ...this.queryParams
      }, `风险信息_${new Date().getTime()}.xls`)
    },
    openEnterprise() {

      this.$refs.enterpriseSelectRef.openDialog()
    },
    selectInfo(row) {
      //
      this.form.enterpriseName = row.enterpriseName;
      this.form.enterpriseType = row.enterpriseType;
      this.form.enterpriseStatus = row.enterpriseStatus;

      this.form.areaId = row.areaId;
      this.form.economicType = row.economicType;
      this.form.enterpriseScale = row.enterpriseScale;
      console.log(this.form);
    },
    /** 督办 */
    handleIsSupervise(row) {
      const hiddenDangerIds = row.hiddenDangerId || this.ids;
      this.$modal.confirm('是否确认督办隐患信息？').then(function() {
        return superviseEhsHiddenDanger(hiddenDangerIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("督办成功");
      }).catch(() => {});
    },
    /** 协办 */
    handleAssist(row) {
      this.reset();
      this.openAssist = true;
      if (row.assistDept) {
        this.assistDeptList = row.assistDept.split(",").map(Number);
      } else {
        this.assistDeptList = [];
      }
      this.currAssistHiddenDangerId = row.hiddenDangerId;
    },
    /** 协办提交*/
    assistForm() {
      let deptIds = this.assistDeptList.join();
      if (this.currAssistHiddenDangerId != null) {
        assistEhsHiddenDanger({"hiddenDangerId":this.currAssistHiddenDangerId,"assistDept":deptIds}).then(response => {
          this.$modal.msgSuccess("协办成功");
          this.openAssist = false;
          this.getList();
        });
      }
    },
    //是否督办状态变化
    isSuperviseChange(value,row) {
      if(this.deptShow==false){
        return;
      }
      console.log(row);
      if(row.isSupervise==0){
        this.handleIsSupervise(row);
      }else{
        this.$modal.alert("此隐患已督办，不能撤回督办。");
      }
    },
    //是否协办
    assistDeptClick(value,row) {
      if(row.assistDeptName!="" && row.assistDeptName!=null){
        this.$alert(row.assistDeptName, '协办单位', {
          confirmButtonText: '确定',
          callback: action => {

          }
        });
      }
    },
    /** 输入督办反馈 */
    handleAssistResult(row) {
      this.$prompt('请输入督办反馈', '督办反馈', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        }).then(({ value }) => {
        updateEhsHiddenDanger({"hiddenDangerId":row.hiddenDangerId,"superviseResult":value}).then(response => {
          this.$modal.msgSuccess("督办反馈成功");
          this.openAssist = false;
          this.getList();
        });
        return;
        //return updateEhsHiddenDanger({"hiddenDangerId":row.hiddenDangerId,"superviseResult":value});
      }).catch(() => {

      });
    },
    /** 显示督办反馈 */
    handleShowAssistResult(row) {
      this.$alert(row.superviseResult, '督办反馈', {
        confirmButtonText: '确定',
        callback: action => {
          /*this.$message({
            type: 'info',
            message: `action: ${ action }`
          });*/
        }
      });
    },
    /** 批量导入 */
    handleImport() {
      console.log(this.$refs.uploadRef.upload);
      this.$refs.uploadRef.upload.open = true;
      this.$refs.uploadRef.upload.title = "隐患信息导入";
      this.$refs.uploadRef.upload.url =
        process.env.VUE_APP_BASE_API + "/ehsHiddenDanger/ehsHiddenDanger/importData";
    },
    /** 导入成功 */
    upLoadOk() {
      // console.log("导入成功")
      this.getList();
    },
    tableRowClassName({row, rowIndex}) {
      if(row.status=="0" ||row.status=="5"){
        if (row.overTime <0 ) {
          return 'warning-row';
        } else if (row.overTime >  3 && row.overTime <= 7) {
          return 'warning7-row';
        } else if (row.overTime >= 0 && row.overTime <= 3) {
          return 'warning3-row';
        }
      }
      return '';
    },
  }
};
</script>
<style>
  .el-table .warning-row {
    background: #f97762;
  }

  .el-table .warning7-row {
    background: #57a6f9;
  }
  .el-table .warning3-row {
    background: #f9c43d;
  }
</style>
