<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="100px">
      <el-row>
        <el-col :span="4">
          <el-form-item label="隐患描述">
            <el-input v-model="searchForm.hiddendescr" placeholder="请输入关键字"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="隐患来源">
            <el-select v-model="searchForm.SOURCE" style="width: 100%;">
              <el-option value="1" label="隐患快报"/>
              <el-option value="2" label="清单排查"/>
              <!--              <el-option value="3" label="清单排查"/>-->
              <el-option value="4" label="安全环保检查(监管端)"/>
              <el-option value="5" label="安全环保检查(企业端)"/>
              <el-option value="6" label="消防点检"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="发现部门">
            <Treeselect
              :options="treeData"
              :normalizer="normalizer"
              v-model="searchForm.DEPARTMENT_ID"
              placeholder="请选择发现部门"
              no-options-text="暂无数据"
              no-children-text="暂无数据"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="隐患发现人 ">
            <el-input v-model="searchForm.KEYWORDS"/>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="隐患发现时间">
            <el-date-picker
              v-model="searchForm.dates"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 100%;"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <el-form-item label="考评状态">
            <el-select v-model="searchForm.STATE" style="width: 100%;">
              <el-option value="1" label="是"/>
              <el-option value="2" label="否"/>
              <el-option value="3" label="考评合格"/>
              <el-option value="4" label="考评不合格"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="隐患类型">
            <Treeselect
              :options="hiddenTypeList"
              :normalizer="normalizer"
              v-model="searchForm.HIDDENTYPE"
              placeholder="请选择隐患类型"
              no-options-text="暂无数据"
              no-children-text="暂无数据"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="本人是否考评">
            <el-select v-model="searchForm.userIs" clearable style="width: 100%;">
              <el-option value="1" label="是"/>
              <el-option value="2" label="否"/>
            </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="getList">
              搜索
            </el-button>
            <el-button v-waves type="success" icon="el-icon-refresh" native-type="reset" @click="goKeyReset">
              重置
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="varList"
      border
      tooltip-effect="dark"
      style="width: 100%">
      <el-table-column type="index" label="序号" width="55" align="center"/>
      <el-table-column label="隐患来源">
        <template slot-scope="{row}">
          <span v-if="row.SOURCE=='1'">
            隐患快报
          </span>
          <span v-else-if="row.SOURCE=='2'">
            清单排查
          </span>
          <span v-else-if="row.SOURCE=='3'">
            清单排查
          </span>
          <span v-else-if="row.SOURCE=='4'">
            安全环保检查(监管端)
          </span>
          <span v-else-if="row.SOURCE=='5'">
            安全环保检查(企业端)
          </span>
          <span v-else-if="row.SOURCE=='6'">
            消防点检
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="HIDDENDESCR" label="隐患描述"/>
      <el-table-column prop="DISCOVERYTIME" label="隐患发现时间"/>
      <el-table-column prop="CREATOR_NAME" label="隐患发现人"/>
      <el-table-column label="隐患状态">已验收</el-table-column>
      <el-table-column prop="CHECKOR_NAME" label="隐患验收人">
        <template slot-scope="{row}">
          <span v-if="row.RECTIFICATIONTYPE=='1'">
            --
          </span>
          <span v-else>
            {{ row.CHECKOR_NAME }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="CHECK_USER" label="已考评组成员"/>
      <el-table-column prop="UN_CHECK_USER" label="待考评组成员"/>
      <el-table-column prop="PASS_STATUS" label="考评结果">
        <template slot-scope="{row}">
          <template v-if="row.CHECK_COUNT !== 5">
            <span>考核未完成</span>
          </template>
          <template v-else>
            <template v-if="row.PASS_COUNT > 2">
              <span>考核合格</span>
            </template>
            <template v-else>
              <span>考核不合格</span>
            </template>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="400px">
        <template slot-scope="{row}">
          <el-button type="info" icon="el-icon-view" @click="fnViewHidden(row.HIDDEN_ID)">查看隐患详情</el-button>
          <el-button type="primary" icon="el-icon-view" @click="fnEvaluation(row.HIDDEN_ID,row.SOURCE, row.CREATOR,'view')">考评详情</el-button>
          <el-button
            v-if="isEvaluationShowButton(row.UN_CHECK_USER_ID)"
            type="success"
            icon="el-icon-view"
            @click="fnEvaluation(row.HIDDEN_ID,row.SOURCE, row.CREATOR,'edit')">考评
          </el-button>
          <el-button
            v-if="USER_ID=='1'"
            type="danger"
            icon="el-icon-view"
            @click="fnDelvaluation(row)">重置考评
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <el-button icon="el-icon-arrow-left" @click="goBack">返回</el-button>
      </div>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList"/>
    </div>
    <el-dialog :visible.sync="evaluationDialog.visible" title="考评">
      <table class="table-ui">
        <template v-for="item in evaluationDialog.list">
          <tr :key="item.CONFIG_USER_ID">
            <td class="tbg">考评人员</td>
            <td>{{ item.USER_NAME }}</td>
            <td class="tbg">是否符合</td>
            <td>
              <span v-if="item.RESULT === 1">符合</span>
              <span v-if="item.RESULT === 2">不符合</span>
              <span v-if="!item.RESULT">未考评</span>
            </td>
          </tr>
          <tr v-if="item.RESULT === 2" :key="item.CONFIG_USER_ID">
            <td class="tbg">不符合原因</td>
            <td colspan="3">{{ item.DESCR }}</td>
          </tr>
        </template>
      </table>
      <el-form
        v-if="evaluationDialog.type === 'edit'"
        ref="evaluationDialogForm"
        :model="evaluationDialog.form"
        :rules="evaluationDialog.rules"
        label-width="100px"
        class="mt-20">
        <el-form-item label="考评人员">
          <el-input v-model="evaluationDialog.form.USER_NAME" disabled/>
        </el-form-item>
        <el-form-item label="是否符合" prop="RESULT">
          <el-select v-model="evaluationDialog.form.RESULT" style="width: 100%;">
            <el-option value="1" label="符合"/>
            <el-option value="2" label="不符合"/>
          </el-select>
        </el-form-item>
        <el-form-item v-if="evaluationDialog.form.RESULT === '2'" label="不符合原因" prop="DESCR">
          <el-input v-model="evaluationDialog.form.DESCR" type="textarea" autosize/>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button @click="fnEvaluationDialogChangeShow">关闭</el-button>
        <el-button v-if="evaluationDialog.type === 'edit'" type="primary" @click="fnEvaluationDialogSubmit">确认</el-button>
      </template>
    </el-dialog>
    <hidden-info v-if="visible" :visible.sync="visible" :id="HIDDEN_ID"/>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import hiddenInfo from './hidden_info.vue'

export default {
  components: { Pagination, Treeselect, hiddenInfo },
  directives: { waves },
  data() {
    return {
      total: 0,
      varList: [],
      USER_ID: JSON.parse(sessionStorage.getItem('user')).USER_ID,
      listQuery: {
        page: 1,
        limit: 20
      },
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.nodes
        }
      },
      treeData: [],
      hiddenTypeList: [],
      listLoading: false,	// 加载状态
      searchForm: {
        KEYWORDS: '',
        SOURCE: '',
        HIDDENTYPE: null,
        dates: [],
        DEPARTMENT_ID: null,
        STATE: '',
        hiddendescr: '',
        userIs: ''
      },
      evaluationDialog: {
        visible: false,
        type: '',
        HIDDEN_ID: '',
        SOURCE: '',
        CREATOR: '',
        form: {
          EVALUATIONCONFIGINFO_ID: '',
          USER_NAME: '',
          DESCR: '',
          RESULT: ''
        },
        rules: {
          RESULT: [{ required: true, message: '请选择是否符合', trigger: 'change' }],
          DESCR: [{ required: true, message: '请输入不符合原因', trigger: 'blur' }]
        },
        list: []
      },
      visible: false,
      HIDDEN_ID: ''
    }
  },
  created() {
    this.getList()
    this.getTreeData()
  },
  methods: {
    // 获得部门树
    getTreeData() {
      requestFN(
        '/corpDepartment/listTreeManageAndCorp',
        {}
      ).then((data) => {
        const treeData = this.listTransTree(JSON.parse(data.zTreeNodes), 'id', 'pId', 'nodes')
        this.treeData = this.removeEmptyChildren(treeData)
      }).catch((e) => {
      })

      requestFN(
        '/dictionaries/listSelectTree',
        {
          DICTIONARIES_ID: '3babc15144444bdc8d763d0af2bdfff6'
        }
      ).then((data) => {
        this.hiddenTypeList = this.removeEmptyChildren(JSON.parse(data.zTreeNodes))
      })
    },
    goKeyReset() {
      this.searchForm.KEYWORDS = ''
      this.searchForm.SOURCE = ''
      this.searchForm.HIDDENTYPE = null
      this.searchForm.dates = []
      this.searchForm.DEPARTMENT_ID = null
      this.searchForm.STATE = ''
      this.searchForm.hiddendescr = ''
      this.searchForm.userIs = ''
      this.getList()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/hiddenevaluationrecord/hiddenList?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          ...this.searchForm,
          START_TIME: this.searchForm.dates && this.searchForm.dates[0],
          END_TIME: this.searchForm.dates && this.searchForm.dates[1],
          CORPINFO_ID: this.$parent.CORPINFO_ID,
          EVALUATIONCONFIG_ID: this.$parent.EVALUATIONCONFIG_ID,
          TYPE: this.$parent.TYPE
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
      }).catch((e) => {
        this.listLoading = false
      })
    },
    isEvaluationShowButton(UN_CHECK_USER_ID) {
      if (!UN_CHECK_USER_ID) return false
      else {
        return UN_CHECK_USER_ID.split(',').indexOf(this.USER_ID) !== -1
      }
    },
    goBack() {
      this.$parent.activeName = 'List'
    },
    fnViewHidden(HIDDEN_ID) {
      this.visible = true
      this.HIDDEN_ID = HIDDEN_ID
      // this.$parent.HIDDEN_ID = HIDDEN_ID
      // this.$parent.activeName = 'HiddenInfo'
    },
    fnEvaluationDialogChangeShow() {
      this.evaluationDialog.visible = !this.evaluationDialog.visible
    },
    fnEvaluation(HIDDEN_ID, SOURCE, CREATOR, type) {
      this.evaluationDialog.HIDDEN_ID = HIDDEN_ID
      this.evaluationDialog.SOURCE = SOURCE
      this.evaluationDialog.CREATOR = CREATOR
      this.evaluationDialog.type = type
      this.evaluationDialog.list = []
      this.fnEvaluationDialogChangeShow()
      if (type === 'edit') {
        this.$nextTick(() => {
          this.$refs.evaluationDialogForm.resetFields()
        })
      }
      requestFN(
        '/hiddenevaluationrecord/goEdit',
        {
          HIDDEN_ID,
          EVALUATIONCONFIG_ID: this.$parent.EVALUATIONCONFIG_ID
        }
      ).then((data) => {
        const list = []
        for (let i = 0; i < data.varList.length; i++) {
          if (type === 'edit') {
            if (data.varList[i].CONFIG_USER_ID !== this.USER_ID) {
              list.push(data.varList[i])
            } else {
              this.evaluationDialog.form.EVALUATIONCONFIGINFO_ID = data.varList[i].CONFIG_EVALUATIONCONFIGINFO_ID
              this.evaluationDialog.form.USER_NAME = data.varList[i].USER_NAME
              this.evaluationDialog.form.DESCR = ''
              this.evaluationDialog.form.RESULT = ''
            }
          } else {
            list.push(data.varList[i])
          }
        }
        this.evaluationDialog.list = list
      }).catch((e) => {
      })
    },
    fnEvaluationDialogSubmit() {
      this.$refs.evaluationDialogForm.validate((valid) => {
        if (valid) {
          requestFN(
            '/hiddenevaluationrecord/add',
            {
              CORPINFO_ID: this.$parent.CORPINFO_ID,
              HIDDENTYPE: this.$parent.TYPE,
              EVALUATIONCONFIG_ID: this.$parent.EVALUATIONCONFIG_ID,
              SOURCE: this.evaluationDialog.SOURCE,
              USER_ID: this.evaluationDialog.CREATOR,
              HIDDEN_ID: this.evaluationDialog.HIDDEN_ID,
              ...this.evaluationDialog.form
            }
          ).then((data) => {
            if (data.result === 'success') {
              this.$message.success('保存成功')
              this.fnEvaluationDialogChangeShow()
              this.getList()
            }
          }).catch((e) => {
          })
        }
      })
    },
    fnDelvaluation(row) {
      this.$confirm('确定要重置选中的评审记录吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/hiddenevaluationrecord/deleteEvaluation',
          {
            HIDDEN_ID: row.HIDDEN_ID,
            USER_ID: row.CREATOR
          }
        ).then((data) => {
          this.listLoading = false
          if (data.result === 'success') {
            this.$message.success('删除成功')
            this.getList()
          } else {
            this.$message({
              message: data.message,
              type: 'error'
            })
          }
        }).catch((e) => {
        })
      }).catch(() => {
      })
    }
  }
}

</script>

<style lang="sass" scoped>
</style>
