<template>
  <div class="app-container">
    <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>
        <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>
    <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="INDUSTRYNAME" 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="350">
        <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="handleEdit(row.ID)">选入资源库</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>{{ form.INDUSTRYNAME }}</td>
          <td class="tbg">二级行业类型</td>
          <td>{{ form.INDUSTRYSECNAME }}</td>
        </tr>
        <tr>
          <td class="tbg">风险类型</td>
          <td>{{ form.RISKTYPENAME }}</td>
          <td class="tbg">风险分级</td>
          <td>{{ 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>

    <el-dialog :visible.sync="dialogFormEdit" :title="dialogType==='edit'?'修改':'新增'" width="700px">
      <el-form ref="form" :rules="rules" :model="form" label-width="120px" style="padding:0 40px 0 20px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="一级行业类型" prop="INDUSTRY">
              <el-select v-model="form.INDUSTRY" placeholder="请选择" style="width: 180px" class="filter-item" >
                <el-option v-for="item in corptypeList" :key="item.BIANMA" :label="item.NAME" :value="item.BIANMA" @click.native="getCorpType2(item.BIANMA)"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="corptype2List.length>0" :span="12">
            <el-form-item label="二级行业类型" prop="INDUSTRYSEC">
              <el-select v-model="form.INDUSTRYSEC" placeholder="请选择" style="width: 180px" class="filter-item" >
                <el-option v-for="item in corptype2List" :key="item.BIANMA" :label="item.NAME" :value="item.BIANMA"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="风险点（单元）" prop="RISKUNIT">
              <el-input v-model="form.RISKUNIT" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="辨识部位" prop="IDENTIFICATION">
              <el-input v-model="form.IDENTIFICATION" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="风险类型" prop="RISKTYPE">
              <el-select v-model="form.RISKTYPE" clearable placeholder="请选择">
                <el-option v-for="item in risktypeList" :key="item.BIANMA" :label="item.NAME" :value="item.BIANMA" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="风险分级" prop="LEVELID">
              <el-select v-model="form.LEVELID" clearable placeholder="请选择">
                <el-option v-for="item in levelList" :key="item.BIANMA" :label="item.NAME" :value="item.BIANMA" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="存在风险" prop="RISK_DESCR">
          <el-input id="RISK_DESCR" ref="RISK_DESCR" :rows="3" v-model="form.RISK_DESCR" type="textarea" />
        </el-form-item>

        <el-form-item label="管控措施" prop="MEASURES">
          <el-input id="MEASURES" ref="MEASURES" :rows="3" v-model="form.MEASURES" type="textarea" />
        </el-form-item>

        <el-form-item label="事故类型" prop="ACCIDENTS">
          <el-select v-model="form.ACCIDENTS" multiple placeholder="请选择" @change="handleChange">
            <el-option v-for="item in accidentList" :key="item.BIANMA" :label="item.NAME" :value="item.BIANMA" />
          </el-select>
        </el-form-item>

        <el-form-item label="应急处置措施" prop="EME_MEASURES">
          <el-input id="EME_MEASURES" ref="EME_MEASURES" :rows="3" v-model="form.EME_MEASURES" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormEdit = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</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: '',
      RISKTYPE: '',
      LEVELID: '',
      TYPE: '',
      varList: [],
      form: {
        IDEREPOSITORY_ID: '',
        INDUSTRY: '',
        INDUSTRYSEC: '',
        RISKTYPE: '',
        RISKUNIT: '',
        IDENTIFICATION: '',
        RISK_DESCR: '',
        LEVELID: '',
        MEASURES: '',
        ACCIDENTS: '',
        ACCIDENTS_NAME: '',
        EME_MEASURES: '',
        RESOURCETYPE: ''
      },
      dialogFormEdit: false,
      dialogFormShow: false,
      dialogType: 'add',

      initFlag: false,
      levelList: [],
      accidentList: [],
      risktypeList: [],
      corptypeList: [],
      corptype2List: [],
      rules: {
        DEPARTMENT_ID: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        USER_ID: [{ required: true, message: '管控责任人不能为空', trigger: 'change' }],
        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' }],
        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' }]
      }
    }
  },
  created() {
    this.getList()
    this.getDict()
    // this.getUnitList()
  },
  methods: {
    getRowKey(row) {
      return row.ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.INDUSTRY = ''
      this.RISKTYPE = ''
      this.LEVELID = ''
      this.BIANMA = ''
      this.getQuery()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/iderepository/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          INDUSTRY: this.INDUSTRY,
          RISKTYPE: this.RISKTYPE,
          LEVELID: this.LEVELID,
          BIANMA: this.BIANMA,
          TYPE: 2
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 修改
    handleEdit(ID) {
      this.resetForm()
      this.corptype2List = []
      this.dialogType = 'edit'
      requestFN(
        '/iderepository/goEdit',
        {
          IDEREPOSITORY_ID: ID,
          RESOURCETYPE: 2
        }
      ).then((data) => {
        this.form = Object.assign({}, data.pd) // copy obj
        this.form.ACCIDENTS = this.form.ACCIDENTS.split(',')
        this.form.ACCIDENTS_NAME = this.form.ACCIDENTS_NAME.split(',')
        this.dialogFormEdit = true
      }).catch((e) => {
      })
    },
    // 保存
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.listLoading = true
          requestFN(
            '/iderepository/' + this.dialogType,
            {
              IDEREPOSITORY_ID: this.form.IDEREPOSITORY_ID,
              INDUSTRY: this.form.INDUSTRY,
              INDUSTRYSEC: this.form.INDUSTRYSEC,
              RISKTYPE: this.form.RISKTYPE,
              RISKUNIT: this.form.RISKUNIT,
              IDENTIFICATION: this.form.IDENTIFICATION,
              RISK_DESCR: this.form.RISK_DESCR,
              LEVELID: this.form.LEVELID,
              MEASURES: this.form.MEASURES,
              ACCIDENTS: this.form.ACCIDENTS.join(','),
              ACCIDENTS_NAME: this.form.ACCIDENTS_NAME.join(','),
              EME_MEASURES: this.form.EME_MEASURES,
              RESOURCETYPE: 2
            }
          ).then((data) => {
            this.listLoading = false
            this.dialogFormEdit = false
            this.getQuery()
          }).catch((e) => {
            this.listLoading = false
          })
        } else {
          return false
        }
      })
    },
    handleChange(val) {
      this.form.ACCIDENTS_NAME = this.accidentList.filter((ele, index, arr) => {
        return val.some(item => { return item === ele.BIANMA })
      }).map(item => { return item.NAME })
      console.log(this.form.ACCIDENTS_NAME)
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/iderepository/delete',
          {
            IDEREPOSITORY_ID: id,
            RESOURCETYPE: 2
          }
        ).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',
          {
            RISK_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
      })
    },
    getCorpType2(BIANMA) {
      requestFN(
        '/dictionaries/getLevelsByBianMa',
        {
          BIANMA: BIANMA
        }
      ).then((data) => {
        this.corptype2List = data.list
      })
    },
    resetForm() {
      this.form = {
        IDEREPOSITORY_ID: '',
        INDUSTRY: '',
        INDUSTRYSEC: '',
        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>

