<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :label="$t('申请人')" prop="recorderId" label-width="50">
        <el-input
          v-model="queryParams.recorderId"
          :placeholder="$t('请输入') + $t('申请人') + 'ID'"
          clearable
          size="small"
          @input="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('隐患级别')" prop="hiddendangerLevel" label-width="50">
        <el-select v-model="queryParams.hiddendangerLevel" :placeholder="$t('请选择') + $t('隐患级别')" clearable size="small" @change="handleQuery">
          <el-option
            v-for="dict in hiddendangerLevelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('预计完成时间')" prop="completeTime" label-width="50">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.completeTime"
          @change="handleQuery"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('请选择') + $t('预计完成时间')">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('申请状态')" prop="status" label-width="50">
        <el-select v-model="queryParams.status" :placeholder="$t('请选择') + $t('申请状态')" clearable size="small" @change="handleQuery">
          <el-option :label="$t('请选择') + $t('字典生成')" value="" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('申请开始时间')" prop="creattime" label-width="50">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.creattime"
          @change="handleQuery"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('请选择') + $t('申请开始时间')">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('申请编号')" prop="approveNo" label-width="50">
        <el-input
          v-model="queryParams.approveNo"
          :placeholder="$t('请输入') + $t('申请编号')"
          clearable
          size="small"
          @input="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('开项分类')" prop="dangerSort" label-width="50">
        <el-select v-model="queryParams.dangerSort" :placeholder="$t('请选择') + $t('开项分类')" clearable size="small" @change="handleQuery">
          <el-option
            v-for="dict in dangerSortOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ehs:approvedanger:export']"
        >{{ $t('导出') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-s-data"
          size="mini"
          @click="handleData"
        >{{ $t('数据分析') }}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="approvedangerList" @selection-change="handleSelectionChange" :height="clientHeight" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('装置名称')" align="center" prop="plantCode" :formatter="plantCodeFormat" />
      <el-table-column :label="$t('开项分类')" align="center" prop="dangerSort" :formatter="dangerSortFormat" />
      <el-table-column :label="$t('申请编号')" align="center" prop="approveNo" :show-overflow-tooltip="true" width="150" />
      <el-table-column :label="$t('隐患级别')" align="center" prop="hiddendangerLevel" :formatter="hiddendangerLevelFormat" />
      <el-table-column :label="$t('申请人')" align="center" prop="recorderId" :formatter="recorderFormat" />
      <el-table-column :label="$t('隐患内容')" align="center" prop="hiddendangerContent" :show-overflow-tooltip="true" width="300"/>
      <el-table-column :label="$t('改造措施')" align="center" prop="measures" :show-overflow-tooltip="true" width="300" />
      <el-table-column :label="$t('预计完成时间')" align="center" prop="completeTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请状态')" align="center" prop="status" :formatter="statusFormat" width="150" />
      <el-table-column :label="$t('申请开始时间')" align="center" prop="creattime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creattime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请结束时间')" align="center" prop="endtime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('执行人')" align="center" prop="executorId" :formatter="executorFormat" />
      <el-table-column :label="$t('验证人')" align="center" prop="verificationId" :formatter="verificationFormat" />
      <el-table-column :label="$t('发现隐患时间')" align="center" prop="discoveryTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.discoveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('参会人员')" align="center" prop="participants" :formatter="participantsFormat" width="500" />
      <el-table-column :label="$t('备注')" align="center" prop="remarks" :show-overflow-tooltip="true"/>
    </el-table>

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

    <el-drawer
      :title="$t('数据分析')"
      size="600px"
      :visible.sync="drawer"
      :direction="direction">
      <el-row>
        <el-col>
          <el-card class="box-card" shadow="hover">
            <div slot="header" class="clearfix">
              <span>{{ $t('隐患级别比例') }}</span>
            </div>
            <div class="text item">
              <!--<danger-level />-->
            </div>
          </el-card>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-card class="box-card" shadow="hover">
            <div slot="header" class="clearfix">
              <span>{{ $t('历年开项状态') }}</span>
            </div>
            <div class="text item">
              <!--<danger-level />-->
            </div>
          </el-card>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-card class="box-card" shadow="hover">
            <div slot="header" class="clearfix">
              <span>{{ $t('开项关闭率') }}</span>
            </div>
            <div class="text item">
              <!--<danger-level />-->
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
import { listApprovedanger, getApprovedanger, delApprovedanger, addApprovedanger, updateApprovedanger, exportApprovedanger, importTemplate} from "@/api/ehs/approvedanger";
import { listUser } from "@/api/system/user";
import { treeselect } from "@/api/system/dept";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import DangerLevel from "./dangerLevel";

export default {
  name: "Approvedanger",
  components: {DangerLevel, Treeselect },
  data() {
    return {
      //侧边栏
      drawer: false,
      direction: 'rtl',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 隐患申请表格数据
      approvedangerList: [],
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      clientHeight:300,
      //用户表列表
      userOptions: [],
      //人员表查询条件
      userQueryParams: {},
      // 装置名称字典
      plantCodeOptions: [],
      // 隐患级别字典
      hiddendangerLevelOptions: [],
      // 开项分类字典
      dangerSortOptions: [],
      // 申请状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        plantCode: null,
        recorderId: null,
        hiddendangerLevel: null,
        hiddendangerContent: null,
        measures: null,
        completeTime: null,
        status: null,
        creattime: null,
        endtime: null,
        processId: null,
        approveNo: null,
        deptId: null,
        executorId: null,
        verificationId: null,
        confirmoneId: null,
        confirmtwoId: null,
        discoveryTime: null,
        remarks: null,
        participants: null,
        dangerSort: null
      },
      // 表单参数
      form: {},
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
        this.$refs.tree.filter(val);
    }
   },
  created() {
    //设置表格高度对应屏幕高度
    this.$nextTick(() => {
        this.clientHeight = document.body.clientHeight -250
    })
    this.getUser();
    this.getList();
    this.getTreeselect();
    this.getDicts("PLANT_DIVIDE").then(response => {
      this.plantCodeOptions = response.data;
    });
    this.getDicts("DANGER_LEVEL").then(response => {
      this.hiddendangerLevelOptions = response.data;
    });
    this.getDicts("APPROVE_STATUS").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("DANGER_SORT").then(response => {
      this.dangerSortOptions = response.data;
    });
  },
  methods: {
    /** 查询隐患申请列表 */
    getList() {
      this.loading = true;
      listApprovedanger(this.queryParams).then(response => {
        this.approvedangerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
     /** 查询部门下拉树结构 */
     getTreeselect() {
        treeselect().then(response => {
            this.deptOptions = response.data;
        });
     },
    //获取用户表
    getUser() {
      listUser(this.userQueryParams).then(response => {
        this.userOptions = response.rows;
      });
    },
    //申请人字典翻译
    recorderFormat(row) {
      let recorderName = "";
      this.userOptions.forEach(function (item) {
        if (item.userId === row.recorderId) {
          recorderName = item.nickName
        }
      });
      return recorderName
    },
    //执行人字典翻译
    executorFormat(row) {
      let executorName = "";
      this.userOptions.forEach(function (item) {
        if (item.userId === row.executorId) {
          executorName = item.nickName
        }
      });
      return executorName
    },
    //验证人字典翻译
    verificationFormat(row) {
      let verificationName = "";
      this.userOptions.forEach(function (item) {
        if (item.userId === row.verificationId) {
          verificationName = item.nickName
        }
      });
      return verificationName
    },
    //参会人员字典翻译
    participantsFormat(row) {
      let participantsName = "";
      let ids = "";
      if (row.participants != null) {
        ids = row.participants.split(",");
      }
      for(let i = 0; i < ids.length; i++) {
        this.userOptions.forEach(function (item, index) {
          if (item.userId.toString() === ids[i]) {
            if (index === 0) { participantsName = item.nickName }
            else { participantsName = participantsName + "，" + item.nickName }
          }
        });
      }
      return participantsName
    },
    // 装置名称字典翻译
    plantCodeFormat(row, column) {
      return this.selectDictLabel(this.plantCodeOptions, row.plantCode);
    },
    // 隐患级别字典翻译
    hiddendangerLevelFormat(row, column) {
      return this.selectDictLabel(this.hiddendangerLevelOptions, row.hiddendangerLevel);
    },
    // 申请状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 开项分类字典翻译
    dangerSortFormat(row, column) {
      return this.selectDictLabel(this.dangerSortOptions, row.dangerSort);
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        plantCode: null,
        recorderId: null,
        hiddendangerLevel: null,
        hiddendangerContent: null,
        measures: null,
        completeTime: null,
        status: 0,
        creattime: null,
        endtime: null,
        processId: null,
        approveNo: null,
        deptId: null,
        delFlag: null,
        executorId: null,
        verificationId: null,
        confirmoneId: null,
        confirmtwoId: null,
        discoveryTime: null,
        remarks: null,
        participants: null,
        dangerSort: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm(this.$t('是否确认导出所有隐患申请数据项?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: "warning"
        }).then(function() {
          return exportApprovedanger(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    //数据分析
    handleData(){
      this.drawer = true
    }
  }
};
</script>
