<template>
  <el-dialog title="隐患信息查询" :visible.sync="open"   width="1200px" @close="cancel"   append-to-body>
    <div class="app-container">

      <el-table v-loading="loading" :data="ehsHiddenDangerList" @selection-change="handleSelectionChange" max-height="550" style="width: 100%" :row-class-name="tableRowClassName">
        <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="enterpriseScale">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.enterprise_scale" :value="scope.row.enterpriseScale"/>
          </template>
        </el-table-column>
        <el-table-column label="经济类型" align="center" prop="economicType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.economic_type" :value="scope.row.economicType"/>
          </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="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>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listEhsHiddenDanger, superviseEhsHiddenDanger  } from "@/api/ehs/ehsHiddenDanger";
import {listAllDept } from "@/api/system/dept";

import importUpload from "@/components/upload/upload";
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 },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 隐患信息表格数据
      ehsHiddenDangerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isReadOnly:false,//form表单是否可以编辑，新增、修改可以编辑、明细是只读
      //是否显示部门列表查询条件
      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,
        hiddenDangerLabelArray:null,
        isTowns:null,
        overTime:null,
        address:null,
        enterpriseScale:null,
        economicType:null,

      },
      alertState: [{
        value: '7',
        label: '7天预警'
      }, {
        value: '3',
        label: '3天预警'
      }, {
        value: '0',
        label: '逾期告警'
      }],
      // 表单参数
      form: {},
      formAssist:{},

      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;
      });
    },
    // 取消按钮
    cancel() {
      this.daterangeCheckTime = [];
      this.daterangeRectifyTime = [];
      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");
      this.queryParams.status = null;
      this.queryParams.enterpriseScale = null;
      this.queryParams.economicType = null;
      this.queryParams.enterpriseType = null;
    },
    /** 搜索按钮操作 */
    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
    },


    /** 督办 */
    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;
      this.assistDeptList = row.assistDept.split(",").map(Number);
      this.currAssistHiddenDangerId = row.hiddenDangerId;
    },

    //是否督办状态变化
    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 => {

          }
        });
      }
    },

    /** 显示督办反馈 */
    handleShowAssistResult(row) {
      this.$alert(row.superviseResult, '督办反馈', {
        confirmButtonText: '确定',
        callback: action => {
          /*this.$message({
            type: 'info',
            message: `action: ${ action }`
          });*/
        }
      });
    },

    tableRowClassName({row, rowIndex}) {
      if(row.status==0 ||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 && rowIndex <= 3) {
          return 'warning3-row';
        }
      }
      return '';
    },
    openDialog1(status) {
      //this.queryParams.currMonth = 1;
      this.queryParams.status = status;
      this.getList();
      this.open = true;
    },
    openDialog2(enterpriseScale) {
      //this.queryParams.currMonth = 1;
      this.queryParams.enterpriseScale = enterpriseScale;
      this.getList();
      this.open = true;
    },
    openDialog3(economicType) {
      //this.queryParams.currMonth = 1;
      this.queryParams.economicType = economicType;
      this.getList();
      this.open = true;
    },
    openDialog4(enterpriseType) {
      //this.queryParams.currMonth = 1;
      this.queryParams.enterpriseType = enterpriseType;
      this.getList();
      this.open = true;
    },
  }
};
</script>
<style>
.el-table .warning-row {
  background: #f97762;
}

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