<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="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="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="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="hiddenDangerLabel">
        <el-select v-model="queryParams.hiddenDangerLabel" 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>
        <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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ehsHiddenDanger:ehsHiddenDanger:export']"
        >导出</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">
      <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" />
      <el-table-column label="企业现状" align="center" prop="enterpriseStatus">
        <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="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="hiddenDangerDesc" />
      <el-table-column label="排查时间" align="center" prop="checkTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="要求整改时间" align="center" prop="rectifyTermTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rectifyTermTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排查方式" align="center" prop="checkType" />
      <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="rectifyMeasure" />
      <el-table-column label="整改进度" align="center" prop="status" />
      <el-table-column label="整改时间" align="center" prop="rectifyTime" width="180">
        <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" />
      <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="regulatoryDept" />
      <el-table-column label="监管部门责任人" align="center" prop="regulatoryName" />
      <el-table-column label="隐患级别" align="center" prop="hiddenDangerGrade">
        <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="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="isSupervise">
        <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">
        <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="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="hiddenDangerLabel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hidden_danger_label" :value="scope.row.hiddenDangerLabel"/>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listEhsHiddenDanger, getEhsHiddenDanger, delEhsHiddenDanger, addEhsHiddenDanger, updateEhsHiddenDanger,
  superviseEhsHiddenDanger,assistEhsHiddenDanger } from "@/api/ehs/ehsHiddenDanger";
import {listAllDept } from "@/api/system/dept";
import enterpriseSelect from "./enterpriseSelect"
export default {
  name: "EhsHiddenDanger",
  dicts: ['hidden_danger_type', 'hidden_danger_grade', 'hidden_danger_label', 'enterprise_type', "hidden_danger_status"],
  components:{ enterpriseSelect  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 隐患信息表格数据
      ehsHiddenDangerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否显示部门列表查询条件
      deptShow: false,
      //所有部门列表字典
      allDeptList: [],
      currEnterpriseInfo:{},//选中的企业信息
      // 要求整改时间时间范围
      daterangeCheckTime: [],
      // 要求整改时间时间范围
      daterangeRectifyTime: [],
      openAssist:false,//打开协办弹框
      assistDeptList:"",//协办部门
      currAssistHiddenDangerId :-1,//当前协办隐患id
      statusOptions: [{
        value: '0',
        label: '停业',
      }, {
        value: '1',
        label: '正常经营',
      }],
      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,
        assistDept:this.$store.state.user.deptId
      },
      // 表单参数
      form: {},
      formAssist:{},
      // 表单校验
      rules: {
      },
      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() {
    this.getList();
    this.getListAllDept();
    this.deptShow = this.$store.state.user.parentId==0 ?true :false;
  },
  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;
      });
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCheckTime = [];
      this.daterangeRectifyTime = [];
      this.resetForm("queryForm");
      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.title = "添加隐患信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const hiddenDangerId = row.hiddenDangerId || this.ids
      getEhsHiddenDanger(hiddenDangerId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改隐患信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          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();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hiddenDangerIds = row.hiddenDangerId || this.ids;
      this.$modal.confirm('是否确认删除隐患信息编号为"' + hiddenDangerIds + '"的数据项？').then(function() {
        return delEhsHiddenDanger(hiddenDangerIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehsHiddenDanger/ehsHiddenDanger/export', {
        ...this.queryParams
      }, `ehsHiddenDanger_${new Date().getTime()}.xlsx`)
    },
    openEnterprise() {

      this.$refs.enterpriseSelectRef.openDialog()
    },
    selectInfo(row) {
      //
      this.form.enterpriseName = row.enterpriseName;
      this.form.enterpriseType = row.enterpriseType;
      this.form.enterpriseStatus = row.enterpriseStatus;
      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.openAssist = true;
      this.currAssistHiddenDangerId = row.hiddenDangerId;
    },
    /** 协办提交*/
    assistForm() {
      let deptIds = this.assistDeptList.join();
      console.log(this.currAssistHiddenDangerId)
      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 }) => {
        return updateEhsHiddenDanger({"hiddenDangerId":row.hiddenDangerId,"superviseResult":value});
      }).catch(() => {
        this.getList();
        this.$modal.msgSuccess("督办反馈成功");
      });
    },
    /** 显示督办反馈 */
    handleShowAssistResult(row) {
      this.$alert(row.superviseResult, '督办反馈', {
        confirmButtonText: '确定',
        callback: action => {
          this.$message({
            type: 'info',
            message: `action: ${ action }`
          });
        }
      });
    },
  }
};
</script>
