<template>
  <el-container>
    <el-header v-if="isSearch">
      <el-form style="margin: 10px 0" :inline="true">
        <el-form-item label="管控项措施：">
          <el-input type="text" clearable v-model="search.item"></el-input>
        </el-form-item>
        <el-form-item label="管控方式：">
          <el-select clearable v-model="search.dataSrc">
            <el-option label="自动化管控" value="1" />
            <el-option label="隐患排查" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="分析对象名称：">
          <el-select
            clearable
            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
            clearable
            v-model="search.riskUnitId"
            @change="changeEventList(1)"
          >
            <el-option
              :label="item.riskUnitName"
              :value="item.id"
              v-for="item in cellList"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="风险事件名称：">
          <el-select clearable v-model="search.riskEventId">
            <el-option
              :label="item.riskEventName"
              :value="item.id"
              v-for="item in eventList"
              :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:measure: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:measure: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"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span>{{ scope.row.hazardName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="分析单元名称"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span>{{ scope.row.riskUnitName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="风险事件名称"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span>{{ scope.row.riskEventName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="管控项措施"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span>{{ scope.row.item }}</span>
          </template>
        </el-table-column>
        <el-table-column label="管控方式" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ initDataSrc(scope.row.dataSrc) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="管控措施描述"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span>{{ scope.row.riskMeasureDesc }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" fixed="right" align="center" width="120"
          v-if="$auth.hasPermi('doubledefense:measure:edit') || $auth.hasPermi('doubledefense:measure:remove')"
        >
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="doAdd(2, scope.row)"
              v-hasPermi="['doubledefense:measure:edit']"
            >
              编辑
            </el-button>
            <el-button type="text" size="mini" @click="doDelete(scope.row)"
              v-hasPermi="['doubledefense:measure: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)"
            clearable
          >
            <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%"
            @change="changeEventList(2)"
            clearable
          >
            <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="riskEventId">
          <el-select
            v-model="ruleForm.riskEventId"
            style="width: 70%"
            clearable
          >
            <el-option
              :label="item.riskEventName"
              :value="item.id"
              v-for="item in eventList1"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="管控项措施：" prop="item">
          <el-input
            type="text"
            v-model="ruleForm.item"
            style="width: 70%"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="管控方式：" prop="dataSrc">
          <el-select v-model="ruleForm.dataSrc" style="width: 70%" clearable>
            <el-option label="自动化管控" value="1" />
            <el-option label="隐患排查" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="管控措施分类1" prop="classify1">
          <el-select
            v-model="ruleForm.classify1"
            @change="changeClass"
            style="width: 70%"
            clearable
          >
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in classifyOptions1"
              :key="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="管控措施分类2" prop="classify2">
          <el-select v-model="ruleForm.classify2" style="width: 70%" clearable>
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in classifyOptions2"
              :key="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="管控措施描述：" prop="riskMeasureDesc">
          <el-input
            type="textarea"
            :rows="4"
            v-model="ruleForm.riskMeasureDesc"
            style="width: 70%"
            clearable
            maxlength="1000"
          ></el-input>
        </el-form-item>
        <el-form-item label="隐患排查内容：" prop="troubleshootContent">
          <el-input
            type="textarea"
            :rows="4"
            v-model="ruleForm.troubleshootContent"
            style="width: 70%"
            clearable
            maxlength="1000"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            type="textarea"
            :rows="4"
            v-model="ruleForm.remarkInfo"
            style="width: 70%"
            clearable
            maxlength="500"
          ></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 {
  unitList,
  eventList,
  measureList,
  hazardList,
  delMeasure,
  saveMeasure,
  editMeasure
} from '@/api/risk/analyse.js'
export default {
  name: 'RuoyiAnalyse',

  data() {
    return {
      isSearch: false,
      search: {
        item: '',
        dataSrc: '',
        hazardId: '',
        riskUnitId: '',
        riskEventId: ''
      },
      // 管控措施分类1
      classifyOptions1: [
        { label: '工程技术', value: '1' },
        { label: '维护保养', value: '2' },
        { label: '操作行为', value: '3' },
        { label: '应急措施', value: '4' }
      ],
      // 管控措施分类2
      classifyOptions2: [],

      pageNum: 1,
      pageSize: 10,
      total: 0,
      list: [],
      analysisList: [], // 分析对象的下拉菜单
      cellList: [], // 分析单元的下拉菜单
      cellList1: [], // 分析单元的下拉菜单
      eventList: [], // 风险事件的下拉菜单
      eventList1: [], // 风险事件的下拉菜单
      multipleSelectionIdList: [],

      dialogVisible: false,
      isEdit: '',
      ruleForm: {
        hazardId: '',
        riskUnitId: '',
        riskEventId: '',
        item: '',
        dataSrc: '',
        riskMeasureDesc: '',
        classify1: '',
        classify2: '',
        troubleshootContent: '',
        remarkInfo: ''
      },
      rules: {
        hazardId: [
          { required: true, message: '请选择分析对象名称', trigger: 'change' }
        ],
        riskUnitId: [
          { required: true, message: '请选择分析单元名称', trigger: 'change' }
        ],
        riskEventId: [
          { required: true, message: '请选择风险事件名称', trigger: 'change' }
        ],
        item: [
          { required: true, message: '请输入管控项措施', trigger: 'change' }
        ],
        dataSrc: [
          { required: true, message: '请选择管控方式', trigger: 'change' }
        ],
        riskMeasureDesc: [
          { required: true, message: '请输入管控措施描述', trigger: 'change' }
        ],
        classify1: [
          { required: true, message: '请选择管控措施分类1', trigger: 'change' }
        ],
        classify2: [
          { required: true, message: '请选择管控措施分类2', trigger: 'change' }
        ],
        troubleshootContent: [
          { required: true, message: '请输入隐患排查内容', trigger: 'change' }
        ]
      }
    }
  },

  mounted() {
    this.initList()
    this.inithazardListOptions()
    // this.initEventListtOptions()
  },

  methods: {
    async initList() {
      const res = await measureList({
        ...this.search,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
      this.list = res.rows
      this.total = res.total
    },
    // 重置
    reset() {
      this.search.item = ''
      this.search.dataSrc = ''
      this.search.hazardId = ''
      this.search.riskUnitId = ''
      this.search.riskEventId = ''
      this.initList()
    },
    initDataSrc(type) {
      if (type === '1') {
        return '自动化管控'
      } else if (type === '2') {
        return '隐患排查'
      }
    },
    changeEventList(type) {
      if (type === 1) {
        this.search.riskEventId = ''
      } else {
        this.ruleForm.riskEventId = ''
      }
      this.initEventListtOptions(type)
    },
    // 风险事件的下拉菜单
    initEventListtOptions(type) {
      if (type === 1) {
        const obj = {
          hazardId: '',
          riskUnitId: this.search.riskUnitId,
          pageNum: 1,
          pageSize: 1000
        }
        eventList(obj).then((res) => {
          if (res.code === 200) {
            this.eventList = res.rows
          } else {
            this.$message.error(res.msg)
          }
        })
      } else {
        const obj = {
          hazardId: '',
          riskUnitId: this.ruleForm.riskUnitId,
          pageNum: 1,
          pageSize: 1000
        }
        eventList(obj).then((res) => {
          if (res.code === 200) {
            this.eventList1 = res.rows
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },
    changeClass(val, type) {
      if (!type) {
        this.ruleForm.classify2 = ''
      }
      this.classifyOptions2 = []
      let arr = [
        { label: '工艺控制', value: '1-1' },
        { label: '关键设备/部件', value: '1-2' },
        { label: '安全附件', value: '1-3' },
        { label: '安全仪表', value: '1-4' },
        { label: '其它', value: '1-5' },
        { label: '动设备', value: '2-1' },
        { label: '静设备', value: '2-2' },
        { label: '其它', value: '2-3' },
        { label: '人员资质', value: '3-1' },
        { label: '操作记录', value: '3-2' },
        { label: '交接班', value: '3-3' },
        { label: '其它', value: '3-4' },
        { label: '应急设施', value: '4-1' },
        { label: '个体防护', value: '4-2' },
        { label: '消防设施', value: '4-3' },
        { label: '应急预案', value: '4-4' },
        { label: '其它', value: '4-5' }
      ]
      arr.forEach((item) => {
        if (item.value.startsWith(val)) {
          this.classifyOptions2.push(item)
        } else if (item.value.startsWith(val)) {
          this.classifyOptions2.push(item)
        } else if (item.value.startsWith(val)) {
          this.classifyOptions2.push(item)
        } else if (item.value.startsWith(val)) {
          this.classifyOptions2.push(item)
        }
      })
    },
    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
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 全选/反选
    handleSelectionChange(val) {
      this.multipleSelectionIdList = val.map((item) => item.id)
    },
    // 新增
    doAdd(type, val) {
      this.classifyOptions2 = []

      if (type === 1) {
        this.isEdit = '新增'
        this.ruleForm.hazardId = ''
        this.ruleForm.riskUnitId = ''
        this.ruleForm.riskEventId = ''
        this.ruleForm.item = ''
        this.ruleForm.dataSrc = ''
        this.ruleForm.riskMeasureDesc = ''
        this.ruleForm.classify1 = ''
        this.ruleForm.classify2 = ''
        this.ruleForm.troubleshootContent = ''
        this.ruleForm.remarkInfo = ''
      } else {
        this.isEdit = '编辑'
        this.ruleForm = { ...val }
        this.initcellListtOptions(2)
        this.initEventListtOptions(2)
        this.changeClass(this.ruleForm.classify1, 1)
      }
      this.$nextTick(() => {
        this.dialogVisible = true
      })
    },
    submit() {
      this.$refs.ruleFormRef.validate(async (valid) => {
        if (valid) {
          if (this.isEdit === '新增') {
            const res = await saveMeasure(this.ruleForm)
            if (res.code !== 200) return this.$message.error(res.msg)
          } else {
            const res = await editMeasure(this.ruleForm)
            if (res.code !== 200) return this.$message.error(res.msg)
          }

          this.initList()
          this.$message.success(`${this.isEdit}成功!`)
          this.ruleForm.hazardId = ''
          this.ruleForm.riskUnitId = ''
          this.ruleForm.riskEventId = ''
          this.ruleForm.item = ''
          this.ruleForm.dataSrc = ''
          this.ruleForm.riskMeasureDesc = ''
          this.ruleForm.classify1 = ''
          this.ruleForm.classify2 = ''
          this.ruleForm.troubleshootContent = ''
          this.ruleForm.remarkInfo = ''

          this.dialogVisible = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 删除
    doDelete(type) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await delMeasure(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 delMeasure(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>
