<template>
  <div>
    <div class="filter-container">
      <div class="filter-row mb-10">
        <div class="filter-group ">
          <div class="filter-lable">
            关键字：
          </div>
          <div class="filter-width">
            <el-input v-model="KEYWORDS" placeholder="搜索" />
          </div>
        </div>
        <div class="filter-group">
          <div class="filter-lable">
            风险分级：
          </div>
          <div class="filter-width">
            <el-select v-model="LEVELID" clearable placeholder="请选择">
              <el-option v-for="item in levelList" :key="item.BIANMA" :label="item.NAME" :value="item.BIANMA" />
            </el-select>
          </div>
        </div>
        <!--        <div class="filter-group">-->
        <!--          <div class="filter-lable">-->
        <!--            风险类型：-->
        <!--          </div>-->
        <!--          <div class="filter-width">-->
        <!--            <el-select v-model="RISKTYPE" clearable placeholder="请选择">-->
        <!--              <el-option v-for="item in risktypeList" :key="item.BIANMA" :label="item.NAME" :value="item.BIANMA" />-->
        <!--            </el-select>-->
        <!--          </div>-->
        <!--        </div>-->
        <div class="filter-group">
          <div class="filter-lable">
            行业类型：
          </div>
          <div class="filter-width">
            <el-cascader
              ref="industryCascader"
              v-model="INDUSTRYALL"
              :options="hylxList"
              :props="industryProps"
              :show-all-levels="false"
              style="width: 100%;"
            />
          </div>
        </div>
        <div class="filter-group">
          <div class="filter-lable">
            事故类型：
          </div>
          <div class="filter-width">
            <el-select v-model="BIANMA" clearable placeholder="请选择">
              <el-option v-for="item in accidentList" :key="item.BIANMA" :label="item.NAME" :value="item.BIANMA" />
            </el-select>
          </div>
        </div>
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left:10px" @click="getQuery">
          搜索
        </el-button>
        <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="goKeyReset">
          重置
        </el-button>
      </div>
      <div class="filter-flot"/>
    </div>
    <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="CORP_TYPE_NAME" label="行业类型" />
      <!--      <el-table-column prop="RISKTYPENAME" label="风险类型" />-->
      <el-table-column prop="RISKUNIT" label="风险点（单元）" />
      <el-table-column prop="IDENTIFICATION" label="辨识部位" />
      <el-table-column prop="RISK_DESCR" label="存在风险" />
      <el-table-column prop="LEVELIDNAME" label="风险分级" />
      <el-table-column prop="ACCIDENTS_NAME" label="事故类型" />
      <el-table-column label="操作" align="center" width="300">
        <template slot-scope="{row}">
          <el-button type="success" icon="el-icon-setting" size="mini" @click="goDetail(row)">查看</el-button>
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="editStatus(row)">选入资源库</el-button>
          <!--          <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(row.ID)">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <!--        <el-button type="danger" icon="el-icon-delete" plain @click="batchDel">删除</el-button>-->
      </div>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>

    <el-dialog :visible.sync="dialogFormShow" title="查看" width="700px">
      <table class="table-ui">
        <tr>
          <td class="tbg">行业类型</td>
          <td colspan="3">{{ form.CORP_TYPE_NAME }}</td>
        </tr>
        <tr>
          <!--          <td class="tbg">风险类型</td>-->
          <!--          <td>{{ form.RISKTYPENAME }}</td>-->
          <td class="tbg">风险分级</td>
          <td colspan="3">{{ form.LEVELIDNAME }}</td>
        </tr>
        <tr>
          <td class="tbg">风险点（单元）</td>
          <td>{{ form.RISKUNIT }}</td>
          <td class="tbg">辨识部位</td>
          <td>{{ form.IDENTIFICATION }}</td>
        </tr>
        <tr>
          <td class="tbg">事故类型</td>
          <td colspan="3">{{ form.ACCIDENTS_NAME }}</td>
        </tr>
        <tr>
          <td class="tbg">存在风险</td>
          <td colspan="3">{{ form.RISK_DESCR }}</td>
        </tr>
        <tr>
          <td class="tbg">管控措施</td>
          <td colspan="3">{{ form.MEASURES }}</td>
        </tr>
        <tr>
          <td class="tbg">应急处置措施	</td>
          <td colspan="3">{{ form.EME_MEASURES }}</td>
        </tr>
      </table>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormShow = false">关 闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import SelectTree from '@/components/SelectTree'
import waves from '@/directive/waves' // waves directive
export default {
  components: { Pagination, SelectTree },
  directives: { waves },
  data() {
    return {
      config: config,
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      KEYWORDS: '',
      INDUSTRYALL: [],
      RISKTYPE: '',
      LEVELID: '',
      BIANMA: '',
      TYPE: '',
      varList: [],
      form: {
        IDEREPOSITORY_ID: '',
        CORP_TYPE: '',
        CORP_TYPE2: '',
        CORP_TYPE3: '',
        CORP_TYPE4: '',
        CORP_TYPE_NAME: '',
        RISKTYPE: '',
        RISKUNIT: '',
        IDENTIFICATION: '',
        RISK_DESCR: '',
        LEVELID: '',
        LEVEL_NAME: '',
        MEASURES: '',
        ACCIDENTS: '',
        ACCIDENTS_NAME: '',
        EME_MEASURES: '',
        RESOURCETYPE: '',
        LIKELIHOOD: '',
        EXPOSURE: '',
        CONSEQUENCE: '',
        DANGER: ''
      },
      dialogFormEdit: false,
      dialogFormShow: false,
      dialogType: 'add',

      initFlag: false,
      levelList: [],
      accidentList: [],
      risktypeList: [],
      hylxList: [],
      industryProps: {
        lazy: true,
        lazyLoad: this.lazyLoad,
        value: 'id',
        id: 'id',
        label: 'label',
        children: 'children',
        checkStrictly: false
      },
      rules: {
        INDUSTRYALL: [{ required: true, message: '所属行业不能为空', trigger: 'blur' }],
        RISKUNIT: [{ required: true, message: '风险点（单元）不能为空', trigger: 'blur' }],
        IDENTIFICATION: [{ required: true, message: '辨识部位不能为空', trigger: 'blur' }],
        RISK_DESCR: [{ required: true, message: '存在风险不能为空', trigger: 'blur' }],
        // RISKTYPE: [{ required: true, message: '风险类型不能为空', trigger: 'change' }],
        LIKELIHOOD: [{ required: true, message: '事故发生的可能性不能为空', trigger: 'change' }],
        EXPOSURE: [{ required: true, message: '人员暴露于危险环境中的频繁程度不能为空', trigger: 'change' }],
        CONSEQUENCE: [{ required: true, message: '一旦发生事故可能造成的后果不能为空', trigger: 'change' }],
        LEVELID: [{ required: true, message: '风险分级不能为空', trigger: 'change' }],
        MEASURES: [{ required: true, message: '管控措施不能为空', trigger: 'blur' }],
        EME_MEASURES: [{ required: true, message: '应急处置措施不能为空', trigger: 'blur' }],
        ACCIDENTS: [{ required: true, message: '事故类型不能为空', trigger: 'blur' }]
      },
      dialogFormDaoru: false,
      daoruFrom: {
        FFILE: '',
        FFILEName: ''
      }
    }
  },
  computed: {
    DANGER() {
      if (this.form.LIKELIHOOD && this.form.EXPOSURE && this.form.CONSEQUENCE) {
        return (this.form.LIKELIHOOD * this.form.EXPOSURE * this.form.CONSEQUENCE).toFixed(2)
      }
    }
  },
  watch: {
    DANGER(val) {
      if (val > 320) {
        this.$set(this.form, 'LEVELID', 'levelA')
        this.$set(this.form, 'LEVEL_NAME', '重大风险/A级')
      } else if (val > 160) {
        this.$set(this.form, 'LEVELID', 'levelB')
        this.$set(this.form, 'LEVEL_NAME', '较大风险/B级')
      } else if (val > 70) {
        this.$set(this.form, 'LEVELID', 'levelC')
        this.$set(this.form, 'LEVEL_NAME', '一般风险/C级')
      } else {
        this.$set(this.form, 'LEVELID', 'levelD')
        this.$set(this.form, 'LEVEL_NAME', '低风险/D级')
      }
      this.$set(this.form, 'DANGER', val)
    }
  },
  created() {
    this.getList()
    this.getDict()
    this.getDictByDicId('f2598ba72e864eadabf0ca4b664d26b9').then(data => {
      this.hylxList = data
    })
    // this.getUnitList()
  },
  methods: {
    getRowKey(row) {
      return row.ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.INDUSTRYALL = []
      this.RISKTYPE = ''
      this.LEVELID = ''
      this.BIANMA = ''
      this.getQuery()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      var CORP_TYPE = ''
      var CORP_TYPE2 = ''
      var CORP_TYPE3 = ''
      var CORP_TYPE4 = ''
      if (this.INDUSTRYALL && this.INDUSTRYALL.length > 0) {
        CORP_TYPE = this.INDUSTRYALL[0] || ''
        CORP_TYPE2 = this.INDUSTRYALL[1] || ''
        CORP_TYPE3 = this.INDUSTRYALL[2] || ''
        CORP_TYPE4 = this.INDUSTRYALL[3] || ''
      }
      requestFN(
        '/iderepository/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          CORP_TYPE: CORP_TYPE,
          CORP_TYPE2: CORP_TYPE2,
          CORP_TYPE3: CORP_TYPE3,
          CORP_TYPE4: CORP_TYPE4,
          RISKTYPE: this.RISKTYPE,
          LEVELID: this.LEVELID,
          BIANMA: this.BIANMA,
          RESOURCETYPE: 0
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 添加
    handleAdd() {
      this.dialogType = 'add'
      this.corptype2List = []
      this.$refs.form && this.$refs.form.clearValidate()
      this.resetForm()
      this.dialogFormEdit = true
    },
    // 修改
    editStatus(row) {
      this.$confirm('确定要选入资源库吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        requestFN(
          '/iderepository/editStatus',
          {
            IDEREPOSITORY_ID: row.ID,
            RISKTYPE: row.RISKTYPE,
            RISKUNIT: row.RISKUNIT,
            IDENTIFICATION: row.IDENTIFICATION,
            RISK_DESCR: row.RISK_DESCR,
            LIKELIHOOD: row.LIKELIHOOD,
            EXPOSURE: row.EXPOSURE,
            CONSEQUENCE: row.CONSEQUENCE,
            DANGER: row.DANGER,
            LEVELID: row.LEVELID,
            MEASURES: row.MEASURES,
            EME_MEASURES: row.EME_MEASURES
          }
        ).then((data) => {
          this.listLoading = false
          if (data.ISTRUE === '1') {
            this.$message({
              message: '数据库中已存在相同的存在风险、管控措施、应急处置措施、辨识部位、风险单元',
              type: 'error'
            })
          } else {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.varList = []
            this.listQuery.page = 1
            this.getQuery()
          }
        }).catch((e) => {
        })
      })
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/iderepository/delete',
          {
            IDEREPOSITORY_ID: id,
            RESOURCETYPE: 1
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getQuery()
        }).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.ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/iderepository/deleteAll',
          {
            RES_IDS: ids
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.$refs.multipleTable.clearSelection()
          this.getQuery()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 判断按钮权限，用于是否显示按钮
    hasButton() {
      var keys = 'riskunit:add,riskunit:del,riskunit:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.riskunitfhadminadd		// 新增权限
        this.del = data.riskunitfhadmindel		// 删除权限
        this.edit = data.riskunitfhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 获取数据字典数据
    getDict() {
      requestFN(
        '/dictionaries/getLevels',
        {
          DICTIONARIES_ID: '5a81e63ec0e94d919b3138bc01dbef6b'
        }
      ).then((data) => {
        this.levelList = data.list
      })

      requestFN(
        '/dictionaries/getLevels',
        {
          DICTIONARIES_ID: 'cee1190ea96a4ca9b7bca81e11f0d0f8'
        }
      ).then((data) => {
        this.accidentList = data.list
      })

      requestFN(
        '/dictionaries/getLevels',
        {
          DICTIONARIES_ID: 'a726649082d147d88478283f64529b7b'
        }
      ).then((data) => {
        this.risktypeList = data.list
      })

      requestFN(
        '/dictionaries/getLevels',
        {
          DICTIONARIES_ID: 'f2598ba72e864eadabf0ca4b664d26b9'
        }
      ).then((data) => {
        this.corptypeList = data.list
      })
    },
    // 加载级联的方法
    lazyLoad(nodeValue, resolve) {
      if (nodeValue.data && (!nodeValue.children || nodeValue.children.length == 0)) {
        this.getDictByDicId(nodeValue.data.id).then(data => {
          resolve(data)
        })
      } else {
        resolve(null)
      }
    },
    // 获取数据字典数据
    getDictByDicId(dicId) {
      var dicListVal = []
      return new Promise((resolve) => {
        let id = ''
        if (!dicId) {
          id = '0'
        } else {
          id = dicId
        }
        requestFN(
          '/dictionaries/getLevelsAndSCount',
          {
            DICTIONARIES_ID: id
          }
        ).then((data) => {
          if (data.list.length > 0) {
            dicListVal = data.list.map((e) => {
              if (e.zcount == '0') {
                return { value: e.DICTIONARIES_ID.toString(), numValue: e.BIANMA.toString(), label: e.NAME, id: e.DICTIONARIES_ID, leaf: true }
              } else {
                return { value: e.DICTIONARIES_ID.toString(), numValue: e.BIANMA.toString(), label: e.NAME, id: e.DICTIONARIES_ID, children: [], leaf: false }
              }
            })
          } else {
            dicListVal = undefined
          }
          return resolve(dicListVal)
        }).catch((e) => {
        })
      })
    },
    resetForm() {
      this.form = {
        IDEREPOSITORY_ID: '',
        CORP_TYPE: '',
        CORP_TYPE2: '',
        CORP_TYPE3: '',
        CORP_TYPE4: '',
        CORP_TYPE_NAME: '',
        RISKTYPE: '',
        RISKUNIT: '',
        IDENTIFICATION: '',
        RISK_DESCR: '',
        LEVELID: '',
        MEASURES: '',
        ACCIDENTS: '',
        ACCIDENTS_NAME: '',
        EME_MEASURES: '',
        RESOURCETYPE: ''
      }
    },
    goDetail(row) {
      this.dialogFormShow = true
      this.form = Object.assign({}, row)
    }
  }
}

</script>
<style lang="sass" scoped>
.filter-container
  position: relative
  .filter-flot
    position: absolute
    right: 0
    top: 0
.uploader
  width: 570px
  display: flex
  align-items: center
.el-form-item__content
  line-height: 1

.table-ui
  td
    line-height: 34px
  .tbg
    width: 200px
  .img-flex
    display: flex
    .img-ui
      width: 110px
      height: 110px
      margin-right: 20px
      & img
        width: 100%
        height: 100%
</style>

