<template>
  <el-container>
    <el-header v-if="isSearch">
      <el-form style="margin: 10px 0" :inline="true">
        <el-form-item label="分析对象名称：">
          <el-select v-model="search.hazardId" @change="changeCellList(1)">
            <el-option
              :label="item.hazardName"
              :value="item.id"
              v-for="item in analysisList"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="分析单元名称：">
          <el-select v-model="search.riskUnitId">
            <el-option
              :label="item.riskUnitName"
              :value="item.id"
              v-for="item in cellList"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            style="margin-left: 10px"
            type="primary"
            icon="el-icon-search"
            @click="initList"
          >
            搜索
          </el-button>
          <el-button class="commonBtn" icon="el-icon-refresh" @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-header>
    <el-main>
      <div class="headerTop">
        <div>
          <el-button 
            type="primary" 
            class="el-icon-plus" 
            @click="doAdd(1)"
            v-hasPermi="['doubledefense:event:add']"
          >
            新增</el-button
          >
          <!-- <el-button type="primary" class="el-icon-upload2"> 导入</el-button> -->
          <!-- <el-button type="primary" class="el-icon-download"> 导出</el-button> -->
          <el-button 
            type="primary" 
            class="el-icon-delete" 
            @click="doBatchDelete"
            v-hasPermi="['doubledefense:event:remove']"
          > 删除</el-button>
        </div>
        <div>
          <right-toolbar  @queryTable="initList" :search="true" @update:showSearch="isSearch = !isSearch"></right-toolbar>
        </div>
      </div>

      <el-table
        height="calc(100% - 200px)"
        :data="list"
        :row-style="{ height: '0px' }"
        :header-cell-style="{ 'text-align': 'center', padding: '0px' }"
        :cell-style="{ 'text-align': 'center', padding: '2px' }"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" align="center" width="80" />
        <el-table-column label="序号" align="center" width="100">
          <template slot-scope="scope">
            <span> {{ (pageNum - 1) * pageSize + (scope.$index + 1) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="分析对象名称" align="center">
          <template slot-scope="scope">
            <span>{{ initHazardId(scope.row.hazardId) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="分析单元名称" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.riskUnitName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="风险事件名称" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.riskEventName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="风险事件内容" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.riskEventContent?scope.row.riskEventContent:'-' }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" fixed="right" align="center" width="120"
          v-if="$auth.hasPermi('doubledefense:event:edit') || $auth.hasPermi('doubledefense:event:remove')"
        >
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="doAdd(2, scope.row)"
              v-hasPermi="['doubledefense:event:edit']"
            >
              编辑
            </el-button>
            <el-button type="text" size="mini" @click="doDelete(scope.row)"
              v-hasPermi="['doubledefense:event:remove']"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :page.sync="pageNum"
        :limit.sync="pageSize"
        :page-sizes="[10, 20, 30, 50]"
        :total="total"
        @pagination="initList"
      />
    </el-main>

    <!-- 新增/编辑 的dialog -->
    <el-dialog
      :visible.sync="dialogVisible"
      width="700px"
      v-if="dialogVisible"
      :title="isEdit"
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleFormRef"
        label-width="150px"
        class="demo-ruleForm"
      >
        <el-form-item label="分析对象名称：" prop="hazardId">
          <el-select v-model="ruleForm.hazardId" style="width: 70%" @change="changeCellList(2)">
            <el-option
              :label="item.hazardName"
              :value="item.id"
              v-for="item in analysisList"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="分析单元名称：" prop="riskUnitId">
          <el-select v-model="ruleForm.riskUnitId" style="width: 70%">
            <el-option
              :label="item.riskUnitName"
              :value="item.id"
              v-for="item in cellList1"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="风险事件名称：" prop="riskEventName">
          <el-input
            v-model="ruleForm.riskEventName"
            style="width: 70%"
          ></el-input>
        </el-form-item>
        <el-form-item label="风险事件内容：">
          <el-input type="textarea" :rows="4" v-model="ruleForm.riskEventContent" style="width: 70%"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">提 交</el-button>
      </span>
    </el-dialog>
  </el-container>
</template>
<script>

import {
  eventList,
  unitList,
  hazardList,
  saveEvent,
  editEvent,
  delEvent
} from '@/api/risk/riskEvent.js'
export default {
  name: 'RuoyiAnalyse',

  data() {
    return {
      isSearch: false,
      search: {
        hazardId: '',
        riskUnitId: ''
      },
      pageNum: 1,
      pageSize: 10,
      total: 0,
      list: [],
      analysisList: [], // 分析对象的下拉菜单
      cellList: [], // 分析单元的下拉菜单
      cellList1: [], // 分析单元的下拉菜单(添加)
      multipleSelectionIdList: [],

      dialogVisible: false,
      isEdit: '',
      ruleForm: {
        hazardId: '',
        riskUnitId: '',
        riskEventName: '',
        riskEventContent: '',
      },
      rules: {
        hazardId: [
          { required: true, message: '请选择分析对象名称', trigger: 'change' }
        ],
        riskUnitId: [
          { required: true, message: '请选择分析单元名称', trigger: 'change' }
        ],
        riskEventName: [
          { required: true, message: '请输入风险事件名称', trigger: 'blur' }
        ]
      }
    }
  },

  mounted() {
    this.inithazardListOptions()
  },

  methods: {
    async initList() {
      const res = await eventList({
        ...this.search,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
      this.list = res.rows
      this.total = res.total
    },
    // 重置
    reset() {
      this.search.hazardId = ''
      this.search.riskUnitId = ''
      this.initList()
    },
    changeCellList(type){
      if(type === 1){
        this.search.riskUnitId = ""
      }else {
        this.ruleForm.riskUnitId = ""
      }
      this.initcellListtOptions(type)
    },
    // 分析单元的下拉菜单
    initcellListtOptions(type){
      if(type === 1){
        const obj = {
          hazardId: '',
          riskUnitName: '',
          hazardId: this.search.hazardId,
          pageNum: 1,
          pageSize: 1000
        }
        unitList(obj).then((res) => {
        if (res.code === 200) {
          this.cellList = res.rows
        } else {
          this.$message.error(res.msg)
        }
      })
      }else{
        const obj = {
          hazardId: '',
          riskUnitName: '',
          hazardId: this.ruleForm.hazardId,
          pageNum: 1,
          pageSize: 1000
        }
        unitList(obj).then((res) => {
        if (res.code === 200) {
          this.cellList1 = res.rows
        } else {
          this.$message.error(res.msg)
        }
      })
      }
    },
    // 分析对象的下拉菜单
    inithazardListOptions() {
      const obj = {
        hazardName: '',
        hazardLevel: '',
        areaId: '',
        pageNum: 1,
        pageSize: 1000
      }
      hazardList(obj).then((res) => {
        if (res.code === 200) {
          this.analysisList = res.rows
          this.initList()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    initHazardId(val) {
        return this.analysisList.find((item) => item.id == val).hazardName
    },
    // 全选/反选
    handleSelectionChange(val) {
      this.multipleSelectionIdList = val.map((item) => item.id)
    },
    // 新增
    doAdd(type, val) {
      if (type === 1) {
        this.isEdit = '新增'
        this.ruleForm.hazardId = ''
        this.ruleForm.riskUnitId = ''
        this.ruleForm.riskEventName = ''
        this.ruleForm.riskEventContent = ''
      } else {
        this.isEdit = '编辑'

        this.hazardId
        this.ruleForm = { ...val }
        this.initcellListtOptions(2)
      }
      this.dialogVisible = true
    },
    submit() {
      this.$refs.ruleFormRef.validate(async (valid) => {
        if (valid) {
          if (this.isEdit === '新增') {
            const res = await saveEvent(this.ruleForm)
            if (res.code !== 200) return this.$message.error(res.msg)
          } else {
            const res = await editEvent(this.ruleForm)
            if (res.code !== 200) return this.$message.error(res.msg)
          }

          this.initList()
          this.$message.success(`${this.isEdit}成功!`)
          this.isEdit = '新增'
          this.ruleForm.hazardId = ''
          this.ruleForm.riskUnitId = ''
          this.ruleForm.riskEventName = ''
          this.ruleForm.riskEventContent = ''

          this.dialogVisible = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 删除
    doDelete(type) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await delEvent(type.id)
          if (res.code !== 200) return this.$message.error(res.msg)
          this.initList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message.warning('已取消删除')
        })
    },
    // 批量删除
    doBatchDelete() {
      if (!this.multipleSelectionIdList.length)
        return this.$message.info('请先选择需要删除的选项')
      this.$confirm('是否删除所有选中项?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await delEvent(this.multipleSelectionIdList.join())
          if (res.code !== 200) return this.$message.error(res.msg)
          this.initList()
          this.$message({
            type: 'success',
            message: '批量删除成功!'
          })
        })
        .catch(() => {
          this.$message.warning('已取消删除')
        })
    }
  }
}
</script>

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