<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="varList"
      :row-key="getRowKey"
      :header-cell-style="{
        'font-weight': 'bold',
        'color': '#000'
      }"
      tooltip-effect="dark"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        :reserve-selection="true"
        type="selection"
        width="55"
        align="center"/>
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="HIDDENDESCR" label="隐患描述" align="center" />
      <el-table-column label="隐患状态" align="center" >
        <template slot-scope="{row}">
          {{ translateHidden(row.HIDDEN_STATUS) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="300">
        <template slot-scope="{row}">
          <el-button icon="el-icon-view" size="mini" @click="goDetail(row)">查看</el-button>
          <el-button v-if="row.INSPECTED_SITEUSER_ID === USER_ID && (row.HIDDEN_STATUS === '101' || row.HIDDEN_STATUS === '16')" type="primary" icon="el-icon-edit" size="mini" @click="goAssign(row, '-2')">指派</el-button>
          <el-button v-if="row.CREATOR === USER_ID &&( row.HIDDEN_STATUS === '4' || row.HIDDEN_STATUS === '8') && (!row.FINAL_CHECK || row.FINAL_CHECK === '2')" type="primary" icon="el-icon-edit" size="mini" @click="goAccept(row)">验收</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div/>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <div class="ui-height" />
    <div class="ui-foot">
      <el-button plain type="info" @click="goBack">返 回</el-button>
    </div>

    <information ref="information"/>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
import information from '../../../hiddenUtil/information'
export default {
  components: { Pagination, information },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 20
      },
      total: 0,
      config: config,
      search: {
        INSPECTED_DEPARTMENT_NAME: '', // 被检查单位
        INSPECTION_DEPARTMENT_NAME: '', // 检查部门
        INSPECTION_ORIGINATOR_NAME: '', // 检查发起人
        INSPECTION_TYPE: '', // 检查类型
        HIDDEN_STATUS: '', // 隐患状态
        INSPECTION_STATUS: '3-7' // 检查状态
      },
      hiddenStatusList: [
        { ID: '', NAME: '请选择' },
        { ID: '101', NAME: '待指派确认人' },
        { ID: '1', NAME: '待整改' },
        { ID: '2', NAME: '待验收' },
        { ID: '10', NAME: '验收打回' },
        { ID: '4', NAME: '已验收' },
        { ID: '-2', NAME: '已指派确认人' },
        { ID: '8', NAME: '特殊处置通过' },
        { ID: '16', NAME: '确认打回' }
      ],
      inspectionStatusList: [
        { ID: '3-7', NAME: '请选择' },
        { ID: '3', NAME: '待指派' },
        { ID: '4', NAME: '指派中' },
        { ID: '5', NAME: '指派完成' },
        { ID: '6', NAME: '待验收' },
        { ID: '7', NAME: '已验收' }
      ],
      inspectionTypeList: [],
      varList: [],
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [], // 当前页选中的数据
      USER_ID: JSON.parse(sessionStorage.getItem('user')).USER_ID
    }
  },
  created() {
    this.getDict()
    this.getList()
  },
  methods: {
    getRowKey(row) {
      return row.HIDDEN_ID
    },
    translateHidden(id) {
      for (var i = 0; i < this.hiddenStatusList.length; i++) {
        if (this.hiddenStatusList[i].ID == id) return this.hiddenStatusList[i].NAME
      }
    },
    translateInspection(id) {
      for (var i = 0; i < this.inspectionStatusList.length; i++) {
        if (this.inspectionStatusList[i].ID == id) return this.inspectionStatusList[i].NAME
      }
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.resetSearch()
      this.getQuery()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/hidden/listForAhZp?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          ...this.search,
          INSPECTION_ID: this.$parent.INSPECTION_ID
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
        this.pd = data.pd
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 查看
    goDetail(row) {
      this.$refs.information.init(row.HIDDEN_ID)
    },
    goAssign(row, STATE) {
      this.$parent.INSPECTION_ID = row.INSPECTION_ID
      this.$parent.HIDDEN_ID = row.HIDDEN_ID
      this.$parent.HIDDEN_STATUS = STATE
      this.$parent.activeName = 'Assign'
    },
    goAccept(row) {
      this.$parent.INSPECTION_ID = row.INSPECTION_ID
      this.$parent.HIDDEN_ID = row.HIDDEN_ID
      this.$parent.activeName = 'Accept'
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/safetyenvironmental/delete',
          {
            INSPECTION_ID: id
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },

    batchDel() {
      const _selectData = this.$refs.multipleTable.selection
      if (_selectData == null || _selectData.length == 0) {
        this.$message({
          message: '请选中要删除的项...',
          type: 'error'
        })
        return false
      }
      const ids = _selectData.map((item, index) => {
        return item.INSPECTION_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/safetyenvironmental/deleteAll',
          {
            DATA_IDS: ids
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.$refs.multipleTable.clearSelection()
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 判断按钮权限，用于是否显示按钮
    hasButton: function() {
      var keys = 'safetyenvironmental:add,safetyenvironmental:del,safetyenvironmental:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.safetyenvironmentalfhadminadd		// 新增权限
        this.del = data.safetyenvironmentalfhadmindel		// 删除权限
        this.edit = data.safetyenvironmentalfhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 获取数据字典数据
    getDict: function() {
      requestFN(
        '/dictionaries/getLevels',
        {
          DICTIONARIES_ID: '60e6481d96e44a5390ff5c347c4d1ffe' // 检查类型
        }
      ).then((data) => {
        this.inspectionTypeList = data.list
      }).catch((e) => {

      })
    },
    resetSearch() {
      this.search = {
        INSPECTED_DEPARTMENT_NAME: '', // 被检查单位
        INSPECTION_DEPARTMENT_NAME: '', // 检查部门
        INSPECTION_ORIGINATOR_NAME: '', // 检查发起人
        INSPECTION_TYPE: '', // 检查类型
        HIDDEN_STATUS: '', // 隐患状态
        INSPECTION_STATUS: '' // 检查状态
      }
    },
    goBack() {
      this.$parent.INSPECTION_ID = ''
      this.$parent.activeName = 'List'
    }
  }
}

</script>
