<template>
  <div class="app-container">
    <el-form label-width="100px">
      <el-row>
        <el-col :span="4">
          <el-form-item label="被检查单位">
            <el-input v-model="search.INSPECTED_DEPARTMENT_NAME" placeholder="被检查单位..." />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="检查部门">
            <el-input v-model="search.INSPECTION_DEPARTMENT_NAME" placeholder="检查部门..." />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="检查发起人">
            <el-input v-model="search.INSPECTION_ORIGINATOR_NAME" placeholder="检查发起人..." />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="检查类型">
            <SelectTree
              ref="tree_InspectionType"
              :clearable="false"
              :options="inspectionTypeList"
              :props="defaultProps"
              v-model="search.INSPECTION_TYPE"
              :canparent="false"
              placeholder="这里选择检查类型..."
              children-name="nodes" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="检查时间">
            <el-date-picker
              v-model="search.INSPECTION_TIME"
              style="width: 100%;"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <el-form-item label="隐患处理状态">
            <el-select v-model="search.INSPECTION_STATUS" placeholder="隐患处理状态..." clearable style="width: 100%">
              <el-option v-for="item in inspectionStatusList" :key="item.ID" :label="item.NAME" :value="item.ID" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="检查题目">
            <el-select v-model="search.INSPECTION_SUBJECT" placeholder="检查题目" clearable style="width: 100%">
              <el-option v-for="item in INSPECTION_SUBJECT_LIST" :key="item.ID" :label="item.NAME" :value="item.ID" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button v-waves type="primary" icon="el-icon-search" @click="getQuery">
              搜索
            </el-button>
            <el-button v-waves type="success" icon="el-icon-refresh" @click="goKeyReset">
              重置
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <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" fixed="left" />
      <el-table-column prop="INSPECTED_DEPARTMENT_NAME" show-overflow-tooltip label="被检查单位" align="center" width="150" />
      <el-table-column prop="INSPECTION_LEADDEPARTMENT_NAME" show-overflow-tooltip label="检查部门" align="center" width="150" />
      <el-table-column prop="INSPECTION_DEPARTMENT_NAME" show-overflow-tooltip label="检查发起人" align="center" />
      <!--      <el-table-column prop="INSPECTED_SITEUSER_NAME" label="被检查单位现场负责人" align="center" />-->
      <el-table-column prop="INSPECTION_TYPE_NAME" show-overflow-tooltip label="检查类型" align="center" />
      <!--      <el-table-column prop="INSPECTION_PLACE" label="检查场所" />-->
      <el-table-column label="检查时间" width="280" align="center" >
        <template slot-scope="{row}">
          <span>自 {{ row.INSPECTION_TIME_START }} 至 {{ row.INSPECTION_TIME_END }} 止</span>
        </template>
      </el-table-column>
      <el-table-column prop="INSPECTION_STATUS" show-overflow-tooltip label="状态" align="center" width="150" >
        <template slot-scope="{row}">
          {{ translateInspection(row.INSPECTION_STATUS) }}
        </template>
      </el-table-column>
      <el-table-column prop="INSPECTION_SUBJECT" show-overflow-tooltip label="检查题目" align="center" width="200"/>
      <el-table-column label="操作" align="center" width="200" fixed="right">
        <template slot-scope="{row}">
          <el-button icon="el-icon-view" size="mini" @click="goDetail(row)">查看</el-button>
          <el-button
            v-if="(row.INSPECTION_STATUS === '3' || row.INSPECTION_STATUS === '4') && row.INSPECTED_SITEUSER_ID.includes(USER_ID)"
            type="primary"
            icon="el-icon-view"
            size="mini"
            @click="goAssign(row)">指派</el-button>
          <el-button v-if="(row.INSPECTION_STATUS === '6' || row.INSPECTION_STATUS === '5'||row.INSPECTION_STATUS === '7') && row.checkout === 1" type="success" icon="el-icon-view" size="mini" @click="goAccept(row)">验收</el-button>
          <!--          <el-button v-if="row.INSPECTION_STATUS === '3'" type="success" icon="el-icon-view" size="mini" @click="goAccept(row)">验收</el-button>-->

        </template>
      </el-table-column>
    </el-table>

    <div class="page-btn-group">
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
import SelectTree from '@/components/SelectTree'
import { archiveList } from '../Mock'

export default {
  components: { Pagination, SelectTree },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 20
      },
      total: 0,
      config: config,
      KEYWORDS: '',
      search: {
        INSPECTED_DEPARTMENT_NAME: '', // 被检查单位
        INSPECTION_DEPARTMENT_NAME: '', // 检查部门
        INSPECTION_ORIGINATOR_NAME: '', // 检查发起人
        INSPECTION_TYPE: '', // 检查类型
        INSPECTION_SUBJECT: '', // 检查题目
        INSPECTION_TIME: ['', ''], // 检查时间
        INSPECTION_STATUS: '' // 检查状态
      },
      inspectionTypeList: [],
      inspectionStatusList: [
        { ID: '3', NAME: '待指派' },
        { ID: '4', NAME: '指派中' },
        { ID: '5', NAME: '指派完成' },
        { ID: '6', NAME: '检查待验收' },
        { ID: '7', NAME: '检查已验收' },
        { ID: '8', NAME: '已归档' }
      ],
      INSPECTION_SUBJECT_LIST: [
        { ID: '安全', NAME: '安全' },
        { ID: '环保', NAME: '环保' },
        { ID: '综合', NAME: '综合' }
      ],
      varList: [],
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [], // 当前页选中的数据
      USER_ID: JSON.parse(sessionStorage.getItem('user')).USER_ID,
      defaultProps: { value: 'id', children: 'nodes', label: 'name' }
    }
  },
  created() {
    this.getDict()
    this.getList(this.ROLE_ID)
  },
  methods: {
    /** 获取行数据 Key，用于优化 table 表格的渲染 */
    getRowKey(row) {
      return row.INSPECTION_ID
    },
    /** 转换检验的状态 */
    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(
        '/safetyenvironmental/checkList?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          ...this.search,
          ARCHIVE_USER_ID: JSON.parse(sessionStorage.getItem('user')).USER_ID,
          INSPECTION_TIME_START: this.search.INSPECTION_TIME[0],
          INSPECTION_TIME_END: this.search.INSPECTION_TIME[1]
        }
      ).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
      })

      setTimeout(() => {
        this.listLoading = false
        this.varList = archiveList
        this.total = archiveList.length
      }, 400)
    },
    /** 查看指定项 */
    goDetail(row) {
      this.$parent.activeName = 'Info'
      this.$parent.INSPECTION_ID = row.INSPECTION_ID
    },
    /** 处理指派逻辑 */
    goAssign(row) {
      this.$parent.activeName = 'HiddenList'
      // 是否可以指派
      this.$parent.assign = row.INSPECTED_SITEUSER_ID.includes(this.USER_ID) ? '0' : '1'
      this.$parent.INSPECTION_ID = row.INSPECTION_ID
    },
    /** 处理验收 */
    goAccept(row) {
      this.$parent.activeName = 'HiddenList'
      this.$parent.INSPECTION_ID = row.INSPECTION_ID
    },
    /** 处理删除 */
    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() {
      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() {
      requestFN(
        '/dictionaries/listSelectTree',
        {
          DICTIONARIES_ID: '60e6481d96e44a5390ff5c347c4d1ffe' // 检查类型
        }
      ).then((data) => {
        this.inspectionTypeList = JSON.parse(data.zTreeNodes)
      }).catch((e) => {

      })
    },
    /** 重置搜索状态 */
    resetSearch() {
      this.search = {
        INSPECTED_DEPARTMENT_NAME: '', // 被检查单位
        INSPECTION_DEPARTMENT_NAME: '', // 检查部门
        INSPECTION_ORIGINATOR_NAME: '', // 检查发起人
        INSPECTION_TYPE: '', // 检查类型
        INSPECTION_TIME: ['', ''], // 检查时间
        INSPECTION_STATUS: '3-7' // 检查状态
      }
      this.$refs.tree_InspectionType.clearHandle()
    }
  }
}

</script>
