<template>
  <el-container>
    <el-header v-if="isSearch">
      <el-form style="margin: 10px 0" :inline="true">
        <el-form-item label="所属区域：">
          <el-cascader
            ref="cascaderRef"
            :props="{
              value: 'id',
              label: 'areaName',
              children: 'childList',
              checkStrictly: true
            }"
            clearable
            v-model="search.areaId"
            :options="areaTreeList"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="隐患等级：">
          <el-select v-model="search.dangerLevel">
            <el-option label="一般隐患" :value="0" />
            <el-option label="重大隐患" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="隐患类型：">
          <el-select v-model="search.hazardDangerType">
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in hazardDangerTypeOptions"
              :key="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="隐患来源：">
          <el-select v-model="search.dangerSrc">
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in dangerSrcOptions"
              :key="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="隐患名称：">
          <el-input v-model="search.dangerName" placeholder="请输入隐患名称" />
        </el-form-item>

        <el-form-item>
          <el-button
            style="margin-left: 10px"
            type="primary"
            icon="el-icon-search"
            @click="getDangerList"
          >
            搜索
          </el-button>
          <el-button class="commonBtn" icon="el-icon-refresh" @click="reset"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </el-header>
    <el-main>
      <div class="headerTop">
        <div>
          <el-button
            type="primary"
            class="el-icon-plus"
            @click="handleItem('add')"
          >
            新增</el-button
          >
          <!-- <el-button type="primary" class="el-icon-upload2"> 导入</el-button>
          <el-button type="primary" class="el-icon-document-copy">
            下载模板</el-button
          > -->
          <!-- <el-button type="primary" class="el-icon-download" @click="handleExport">
            导出文档</el-button
          > -->
          <!-- <el-button type="primary" class="el-icon-download">
            导出表格</el-button
          > -->
          <el-button
            icon="el-icon-delete"
            type="primary"
            :disabled="multipleSelection.length < 1"
            @click="del"
            >删除</el-button
          >
        </div>
        <div>
          <i
            class="commonSearch el-icon-refresh-left"
            :disabled="loading"
            v-loading="loading"
            element-loading-spinner="el-icon-loading"
            @click="getDangerList"
          ></i>
          <i
            class="commonSearch el-icon-search"
            circle
            @click="isSearch = !isSearch"
          ></i>
        </div>
      </div>
      <el-table
        height="calc(100% - 200px)"
        :data="list"
        :row-style="{ height: '0px' }"
        :header-cell-style="{ 'text-align': 'center', padding: '0px' }"
        :cell-style="{ 'text-align': 'center', padding: '2px' }"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" align="center" width="50px" />
        <el-table-column label="序号" align="center" width="80px">
          <template slot-scope="scope">
            <span> {{ (pageNum - 1) * pageSize + (scope.$index + 1) }}</span>
          </template>
        </el-table-column>

        <el-table-column label="所属区域" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ initAreaId(scope.row.areaId) || "-" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="隐患描述" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.dangerDesc || "-" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="隐患图片" align="center" width="80">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="handleCheckImg(scope.row.dangerImage)"
            >
              查看
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="隐患等级" align="center">
          <template slot-scope="scope">
            <span>{{ initLevel(scope.row.dangerLevel) || "-" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="治理类型" align="center">
          <template slot-scope="scope">
            <span>{{ initType(scope.row.dangerManageType) || "-" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="隐患名称" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.dangerName || "-" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="隐患来源" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ initSrc(scope.row.dangerSrc) || "-" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="隐患状态" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ initStatus(scope.row.dangerStatus) || "-" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="隐患类型" align="center" show-overflow-tooltip width="80">
          <template slot-scope="scope">
            <span>{{ initDangerType(scope.row.hazardDangerType) || "-" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="登记时间" align="center" show-overflow-tooltip width="110">
          <template slot-scope="scope">
            <span>{{ scope.row.registTime || "-" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="整改治理期限" align="center" show-overflow-tooltip width="110">
          <template slot-scope="scope">
            <span>{{ scope.row.dangerManageDeaddline || "-" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="200">
          <template slot-scope="scope">
            <el-button v-show="scope.row.dangerStatus === 50 || scope.row.dangerStatus === 60" type="text" size="mini" @click="edit(scope.row)"> 查看 </el-button>
            <el-button style="margin-left: 0 !important;" v-show="scope.row.dangerStatus !== 50 && scope.row.dangerStatus !== 60" type="text" size="mini" @click="edit(scope.row)"> 编辑 </el-button>
            <el-button type="text" size="mini" @click="del(scope.row)"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :page.sync="pageNum"
        :limit.sync="pageSize"
        :page-sizes="[10, 20, 30, 50]"
        :total="total"
        @pagination="getDangerList"
      />
    </el-main>
    <el-dialog
      :visible.sync="addAlert"
      width="70%"
      :title="title"
      :show-close="false"
    >
      <el-form
        ref="addForm"
        :model="addForm"
        label-position="right"
        label-width="130px"
        inline
        :rules="rules1"
        style="height: 63vh; overflow: auto"
        :disabled="isCheck"
      >
        <el-form-item label="隐患图片：" prop="dangerImage" style="width: 98%">
          <ImageUpload
            v-model="addForm.dangerImage"
            :isShowTip="false"
          />
          <!-- <el-upload
            multiple
            class="avatar-uploader imageUpload"
            action="https://jsonplaceholder.typicode.com/posts/"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :on-success="handleProgress"
          >
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload> -->
          <!-- <el-upload
            style="width: 950px"
            class="avatar-uploader imageUpload"
            :action="uploadFileUrl"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload> -->
        </el-form-item>
        <el-form-item label="发现时间：" style="width: 48%" prop="registTime">
          <el-date-picker
            style="width: 20vw"
            v-model="addForm.registTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择日期"
            :picker-options="beginDateOption"
        >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="隐患名称：" style="width: 48%" prop="dangerName">
          <el-input
            v-model="addForm.dangerName"
            placeholder="请输入隐患名称"
            style="width: 20vw"
          />
        </el-form-item>
        <el-form-item label="隐患描述：" style="width: 48%" prop="dangerDesc">
          <el-input
            v-model="addForm.dangerDesc"
            placeholder="请输入隐患描述"
            style="width: 20vw"
          />
        </el-form-item>
        <el-form-item label="所属区域：" style="width: 48%" prop="areaId">
          <el-cascader
          style="width: 20vw"
            ref="cascaderRef"
            :props="{
              value: 'id',
              label: 'areaName',
              children: 'childList',
              checkStrictly: true
            }"
            v-model="addForm.areaId"
            :options="areaTreeList"
            @change="handleChange"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="隐患来源：" prop="dangerSrc" style="width: 48%">
          <el-select v-model="addForm.dangerSrc" clearable style="width: 20vw">
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in dangerSrcOptions"
              :key="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="隐患类型：" prop="hazardDangerType" style="width: 48%">
          <el-select v-model="addForm.hazardDangerType" clearable style="width: 20vw">
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in hazardDangerTypeOptions"
              :key="item.value"
          />
                  </el-select>
        </el-form-item>
        <el-form-item label="隐患等级：" style="width: 48%" prop="dangerLevel">
          <el-select v-model="addForm.dangerLevel" style="width: 20vw" clearable>
            <el-option label="一般隐患" :value="0" />
            <el-option label="重大隐患" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="治理类型：" style="width: 48%" prop="dangerManageType">
          <el-select v-model="addForm.dangerManageType" style="width: 20vw">
            <el-option label="即查即改" :value="0" />
            <el-option label="限期整改" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属部门：" style="width: 48%" prop="deptId">
          <el-cascader
            :props="{
              value: 'id',
              label: 'label',
              children: 'children',
              checkStrictly: true
            }"
            v-model="addForm.deptId"
            :options="deptTreeList"
            style="width: 20vw"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="整改负责人：" style="width: 48%" prop="manageUserId">
          <!-- <el-select v-model="addForm.manageUserId" style="width: 20vw">
            <el-option label="整改负责人" :value="101" />
          </el-select> -->
          <el-cascader
          style="width: 20vw"
            :props="{emitPath:false}"
              :show-all-levels="false"
              v-model="addForm.manageUserId"
              :options="userList"
            ></el-cascader>
        </el-form-item>
        <el-form-item label="整改治理期限：" style="width: 48%" prop="dangerManageDeaddline">
          <el-date-picker
            style="width: 20vw"
            v-model="addForm.dangerManageDeaddline"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择日期"
            :picker-options="endDateOption"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="整改建议：" style="width: 48%" prop="suggest">
          <el-input
            v-model="addForm.suggest"
            placeholder="请输入整改建议"
            style="width: 20vw"
          />
        </el-form-item>
        <el-form-item label="任务名称：" style="width: 48%" prop="controlMeasures">
          <el-input
            v-model="addForm.controlMeasures"
            placeholder="请输入任务名称"
            style="width: 20vw"
          />
        </el-form-item>

      </el-form>
      <el-row
        style="
          text-align: right;
          margin-top: 50px;
          padding-top: 10px;
          border-top: 1px solid rgb(33, 67, 91);
        "
      >
          <el-button :loading="loading" type="primary" @click="handleSave" v-if="!isCheck">
            确定
          </el-button>
        <el-button class="abrogateBtn" @click="addCancel"> 取消 </el-button>
      </el-row>
      <el-dialog :visible.sync="dialogVisible" append-to-body>
        <img width="100%" :src="dialogImageUrl" alt="" />
      </el-dialog>
    </el-dialog>
    <el-dialog
      :visible.sync="imgDialog"
      width="600px"
      title="隐患图片"
    >
    <div style="padding: 0 20px; height: 500px; overflow: auto">
      <div style="height: 500px; margin-bottom: 20px;" v-for="(item, index) in imgList" :key="index">
        <el-image
          style="width: 100%; height: 100%"
          :src="item"
          fit="cover"
          :preview-src-list="imgList"
        >
        </el-image>
      </div>
    </div>
    </el-dialog>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'
import {
  getDangerList,
  addDanger,
  editDanger,
  delDanger,
  getDangerDetails } from "@/api/trouble.js";
import {
  areaTree,
  deptTree
} from '@/api/risk/facility.js'
import {
  getDeptUserTree
} from "@/api/emergencies";
export default {
  name: "RuoyiIndex",
computed: {
    ...mapState('baseUrl', ['baseUrl']),
  },
  data() {
    return {
      //上传服务器地址
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload",
      //模糊搜索
      search: {
        areaId: "", //所属区域
        dangerManageType: "", //治理类型
        dangerLevel: "", //隐患等级
        dangerName: "", //隐患名称
        dangerSrc: "", //隐患来源
        dangerStatus: "", //隐患状态
        hazardDangerType: "", //隐患类型
      },
      //添加，编辑数据
      addForm: {
        areaId: "", //所属区域
        dangerName: "", //隐患名称
        controlMeasures: "", //控制措施
        cost: "", //整改资金
        dangerDesc: "", //隐患描述
        dangerImage: "", //隐患图片
        dangerLevel: "", //隐患等级
        dangerManageDeaddline: "", //整改治理期限
        dangerManageType: "", //整改类型
        dangerSrc: "", //隐患来源
        dangerStatus: "", //隐患状态
        deptId: "", //整改部门id
        deviceId: "", //设备uuid
        hazardDangerType: "", //隐患描述
        manageUserId: "", //整改负责人id
        registTime: "", //登记时间
        suggest: "", //整改建议
        controlMeasures: "", //任务名称
      },
      imgDialog: false,
        imgList: [],
        total: 1,
        isSearch: false,
        list: [],
        multipleSelection: [],
        pageNum: 1,
        pageSize: 10,
        addAlert: false,
        addForm: {},
        dialogImageUrl: "",
        imageUrl: "",
        dialogVisible: false,
        isCheck: false,
        loading: false,
        title:"",
        rowId: "",
      areaTreeList: [], // 所属区域列表
      deptTreeList: [],
      treeList: [],
      userList: [],
      dangerSrcOptions: [
        { label: '日常排查', value: 1 },
        { label: '综合排查', value: 2 },
        { label: '专业性排查', value: 3 },
        { label: '季节性排查', value: 4 },
        { label: '重点时段及节假日前排查', value: 5 },
        { label: '事故类比排查', value: 6 },
        { label: '复产复工前排查', value: 7 },
        { label: '外聘专家诊断式排查', value: 8 },
        { label: '管控措施失效', value: 9 },
        { label: '其它', value: 10 }
      ],
      hazardDangerTypeOptions: [
        { label: '安全', value: 1 },
        { label: '工艺', value: 2 },
        { label: '电气', value: 3 },
        { label: '仪表', value: 4 },
        { label: '消防', value: 5 },
        { label: '总图', value: 6 },
        { label: '设备', value: 7 },
        { label: '其它', value: 8 }
      ],
      beginDateOption: {
        disabledDate: (time) => {
          if (this.addForm.dangerManageDeaddline) {
            return time.getTime() > new Date(this.addForm.dangerManageDeaddline).getTime();
          }
        },
      },
      endDateOption: {
        disabledDate: (time) => {
          if (this.addForm.registTime) {
            return (
              time.getTime() <
              new Date(this.addForm.registTime).getTime() - 24 * 3600 * 1000
            );
          }
        },
      },
      rules1: {
        suggest: [
          { required: false, message: "请输入整改建议", trigger: "blur" },
        ],
        cost: [{ required: true, message: "请输入整改资金", trigger: "blur" }],
        manageUserId: [
          { required: true, message: "请选择整改负责人", trigger: "change" },
        ],
        registTime: [
          { required: true, message: "请选择登记时间", trigger: "change" },
        ],
        dangerSrc: [
          { required: true, message: "请选择隐患来源", trigger: "change" },
        ],
        dangerStatus: [
          { required: true, message: "请选择隐患状态", trigger: "change" },
        ],
        deptId: [
          { required: true, message: "请选择隐患部门", trigger: "change" },
        ],
        areaId: [
          { required: true, message: "请选择所属区域", trigger: "change" },
        ],
        hazardDangerType: [
          { required: true, message: "请输入隐患描述", trigger: "blur" },
        ],
        dangerName: [
          { required: true, message: "请输入隐患名称", trigger: "blur" },
        ],
        controlMeasures: [
          { required: false, message: "请输入任务名称", trigger: "blur" },
        ],
        dangerDesc: [
          { required: true, message: "请输入隐患描述", trigger: "blur" },
        ],
        dangerLevel: [
          { required: true, message: "请选择隐患等级", trigger: "change" },
        ],
        dangerManageDeaddline: [
          { required: true, message: "请输入整改治理期限", trigger: "blur" },
        ],
        dangerManageType: [
          { required: true, message: "请选择整改类型", trigger: "change" },
        ],
        dangerImage: [
          { required: true, message: "请添加隐患图片", trigger: "change" },
        ],
      },
    };
  },

  mounted() {
    // 获取部门用户列表
    getDeptUserTree().then((res) => {
      if (res.code === 200) {
       this.userList = this.getUser(res.data)
       console.log(this.userList)
      }
    })
    this.initAreaTree();
    this.initDeptTree();
    this.getDangerList();
  },

  methods: {
    //编辑
    edit(item){
      this.addAlert = true;
      this.title = "编辑隐患";
      this.getDetails(item.id);
    },
    //删除
    del(item){
      this.$confirm("删除后无法恢复，确定删除该条数据吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
      delDanger(ids).then(() => {
        if (data.code === 200) {
            this.$message({
              message: "删除成功",
              type: "success",
            });
            this.getDangerList();
          } else {
            this.$message.error(data.msg);
          }
        })
        .catch(() => {});
    },
    handleSave() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          if(new Date(this.addForm.registTime).getTime() > new Date(this.addForm.dangerManageDeaddline).getTime()) {
            this.$message.error("整改治理时间不能早于发现时间");
            return false;
          }
          this.loading = true;
          if (this.type === "add") {
            const obj = {
              registTime: this.addForm.registTime,
              dangerName: this.addForm.dangerName,
              dangerDesc: this.addForm.dangerDesc,
              dangerSrc: this.addForm.dangerSrc,
              dangerImage: this.addForm.dangerImage,
              hazardDangerType: this.addForm.hazardDangerType,
              dangerLevel: this.addForm.dangerLevel,
              dangerManageType: this.addForm.dangerManageType,
              manageUserId: this.addForm.manageUserId,
              dangerManageDeaddline: this.addForm.dangerManageDeaddline,
              suggest: this.addForm.suggest,
              controlMeasures: this.addForm.controlMeasures,
              deptId: this.addForm.deptId[this.addForm.deptId.length - 1],
              areaId: this.addForm.areaId[this.addForm.areaId.length - 1]
            }
            console.log('areaId ' + this.addForm.areaId + ' ' + obj.areaId)
            console.log('deptId ' + this.addForm.deptId + ' ' + obj.deptId)
            console.log('manageUserId ' + this.addForm.manageUserId + ' ' + obj.manageUserId)
            addDanger(obj)
              .then((data) => {
                if (data.code === 200) {
                  this.$message({
                    message: "新增成功",
                    type: "success",
                  });
                  this.addCancel();
                  this.getDangerList();
                } else {
                  this.$message.error(data.msg);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            console.log('==r=====' + this.addForm.id);
            const obj = {
              id: this.addForm.id,
              registTime: this.addForm.registTime,
              dangerName: this.addForm.dangerName,
              dangerDesc: this.addForm.dangerDesc,
              dangerSrc: this.addForm.dangerSrc,
              dangerImage: this.addForm.dangerImage,
              hazardDangerType: this.addForm.hazardDangerType,
              dangerLevel: this.addForm.dangerLevel,
              dangerManageType: this.addForm.dangerManageType,
              manageUserId: this.addForm.manageUserId,
              dangerManageDeaddline: this.addForm.dangerManageDeaddline,
              suggest: this.addForm.suggest,
              controlMeasures: this.addForm.controlMeasures,
              deptId: this.addForm.deptId[this.addForm.deptId.length - 1],
              areaId: this.addForm.areaId[this.addForm.areaId.length - 1],
            }
            if(!obj.areaId) {
              obj.areaId = this.addForm.areaId
            }
            if(!obj.deptId) {
              obj.deptId = this.addForm.deptId
            }
            console.log('areaId ' + this.addForm.areaId + ' ' + obj.areaId)
            console.log('deptId ' + this.addForm.deptId + ' ' + obj.deptId)
            console.log('manageUserId ' + this.addForm.manageUserId + ' ' + obj.manageUserId)
            editDanger(obj)
              .then((data) => {
                if (data.code === 200) {
                  this.$message({
                    message: "编辑成功",
                    type: "success",
                  });
                  this.addCancel();
                  this.getDangerList();
                } else {
                  this.$message.error(data.msg);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          }
        } else {
          return false;
        }
      });
    },
    handleProgress(e, file, fileList) {
      console.log(e, file, fileList);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      console.log(file);
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    getDangerList() {
      const obj = {
        areaId: this.search.areaId[this.search.areaId.length - 1] || null,
        dangerLevel: this.search.dangerLevel,
        hazardDangerType: this.search.hazardDangerType,
        dangerSrc: this.search.dangerSrc,
        dangerName: this.search.dangerName,
      }
      getDangerList({
        ...obj,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }).then((data) => {
        console.log(data);
        if (data.code === 200) {
          this.list = data.rows;
          this.total = data.total
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
    // 全选/反选
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val;
    },
    // 取消添加
    addCancel() {
      this.rowId = "";
      this.addAlert = false;
      this.isCheck = false;
      this.addForm = {};
      this.$refs.addForm.resetFields();
    },
    handleItem(type, row = {}) {
      this.addAlert = true;
      this.type = type;
      this.rowId = row.id;
      if (type === "add") {
        this.title = "新增隐患";
      } else if (type === "edit") {
        this.title = "编辑隐患";
        this.getDangerDetails(row.id);
      } else if (type === "check") {
        this.title = "查看隐患";
        this.isCheck = true;
        this.getDangerDetails(row.id);
      }
    },
    del(row) {
      let ids =
        row.id || this.multipleSelection.map((item) => item.id).join(",");
      this.$confirm("是否删除？", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delDanger(ids).then((data) => {
            if (data.code === 200) {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              this.getDangerList();
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {
          // 取消后退出
        });
    },
    getDetails(id) {
      getDangerDetails(id).then((data) => {
        if (data.code === 200) {
          this.addForm = data.data;
          this.addForm.dangerSrc = parseInt(this.addForm.dangerSrc)
          this.addForm.hazardDangerType = parseInt(this.addForm.hazardDangerType)
          this.addForm.manageUserId = parseInt(this.addForm.manageUserId)
          if (data.data.dangerStatus == 50 || data.data.dangerStatus == 60) {
            this.isCheck = true;
          }
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    handleExport() {
      this.download(
        "/doubledefense/danger/export",
        {},
        `job_${new Date().getTime()}.xlsx`
      );
    },
    handleCheckImg(imgLists) {
      this.imgList = [];
      if (imgLists) {
        this.imgDialog = true;
        this.imgList = imgLists.split(",,").map((item) => {
          return process.env.VUE_APP_BASE_API + item;
        });
      }
      console.log(this.imgList)
      if(this.imgList.length == 0) {
        this.$message.error('该隐患没有图片');
      }
    },
    async initAreaTree() {
      const res = await areaTree()
      this.areaTreeList = res
      this.initTree(this.areaTreeList)
    },
    // 所属 部门的下拉内容
    async initDeptTree() {
      const res = await deptTree()
      this.deptTreeList = res.data
    },
    initTree(params) {
      params.forEach((item) => {
        this.treeList.push(item)
        if (item.childList.length > 0) {
          this.initTree(item.childList)
        }
      })
    },
    initAreaId(val) {
      return this.treeList.find((item) => item.id === val).areaName
    },
    // 层级选择器
    handleChange() {
      this.loaderList =
        this.$refs['cascaderRef'].getCheckedNodes()[0].data.areaUserList
      this.search.hazardStatus = ''
    },
    initLevel(val) {
      switch (val) {
        case 0:
          return '一般隐患'
        case 1:
          return '重大隐患'
      }
    },
    initType(val) {
      switch (val) {
        case 0:
          return '即查即改'
        case 1:
          return '限期整改'
      }
    },
    initSrc(val) {
      if(val == 1) {
        return '日常排查'
      } else if(val == 2) {
        return '综合排查'
      } else if(val == 3) {
        return '专业性排查'
      } else if(val == 4) {
        return '季节性排查'
      } else if(val == 5) {
        return '重点时段及节假日前排查'
      } else if(val == 6) {
        return '事故类比排查'
      } else if(val == 7) {
        return '复产复工前排查'
      } else if(val == 8) {
        return '外聘专家诊断式排查'
      } else if(val == 9) {
        return '管控措施失效'
      } else if(val == 10) {
        return '其它'
      }
    },
    initStatus(val) {
      switch (val) {
        case 10:
          return '待审核'
        case 20:
          return '认领中'
        case 30:
          return '整改中'
        case 40:
          return '待验收'
        case 50:
          return '已验收'
        case 60:
          return '取消'
      }
    },
    initDangerType(val) {
      if(val == 1) {
        return '安全'
      } else if(val == 2) {
        return '工艺'
      } else if(val == 3) {
        return '电气'
      } else if(val == 4) {
        return '仪表'
      } else if(val == 5) {
        return '消防'
      } else if(val == 6) {
        return '总图'
      } else if(val == 7) {
        return '设备'
      } else if(val == 8) {
        return '其它'
      }
    },
    // 重置
    reset() {
      this.search.areaId = ''
      this.search.deviceName = ''
      this.search.dangerLevel = ''
      this.search.hazardDangerType = ''
      this.search.dangerSrc = ''
      this.search.dangerName = ''
      this.getDangerList()
    },
    getUser (data) {
      return data.map(item => {
        if (item.children && item.children.length && !item.sysUsers) {
          return {
            label: item.label,
            value: item.id + 'd',
            children: this.getUser(item.children)
          }
        }
        if (item.children && item.children.length && item.sysUsers) {
          return {
            label: item.label,
            value: item.id + 'd',
            children: [
              ...this.getUser(item.children),
              ...this.getUsers(item.sysUsers)
            ]
          }
        }
        if (!(item.children && item.children.length) && item.sysUsers) {
          return {
            label: item.label,
            value: item.id + 'd',
            children: this.getUsers(item.sysUsers)
          }
        }
        if (!(item.children && item.children.length) && !item.sysUsers) {
          return {
            label: item.label,
            value: item.id + 'd',
            children: []
          }
        }
        return { label: item.label, value: item.id + 'd' }
      })
    },
    getUsers (data) {
      return data.map(item => {
        return { label: item.nickName, value: item.userId }
      })
    },
  },
};
</script>

<style lang="scss" scoped></style>
