<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">
            <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-input type="text" v-model="search.riskUnitName"></el-input>
        </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:unit: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:unit: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.remarkInfo?scope.row.remarkInfo :'-' }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" fixed="right" align="center" width="120"
            v-if="$auth.hasPermi('doubledefense:unit:edit') || $auth.hasPermi('doubledefense:unit:remove')"
        >
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="doAdd(2, scope.row)"
                v-hasPermi="['doubledefense:unit:edit']"
            >
              编辑
            </el-button>
            <el-button type="text" size="mini" @click="doDelete(scope.row)"
                v-hasPermi="['doubledefense:unit: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%">
            <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="riskUnitName">
          <el-input
            v-model="ruleForm.riskUnitName"
            style="width: 70%"
          ></el-input>
        </el-form-item>
        <el-form-item label="风险分析单元备注：">
          <el-input type="textarea" :rows="4" v-model="ruleForm.remarkInfo" 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 {
  unitList,
  hazardList,
  saveUnit,
  editUnit,
  delUnit
} from '@/api/risk/analyse.js'
export default {
  name: 'RuoyiAnalyse',

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

      dialogVisible: false,
      isEdit: '',
      ruleForm: {
        hazardId: '',
        riskUnitName: '',
        remarkInfo: ''
      },
      rules: {
        hazardId: [
          { required: true, message: '请选择分析对象名称', trigger: 'change' }
        ],
        riskUnitName: [
          { required: true, message: '请输入分析单元名称', trigger: 'blur' }
        ]
      }
    }
  },

  mounted() {
    this.inithazardListOptions()
  },

  methods: {
    async initList() {
      const res = await unitList({
        ...this.search,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
      this.list = res.rows
      this.total = res.total
    },
    // 重置
    reset() {
      this.search.hazardId = ''
      this.search.riskUnitName = ''
      this.initList()
    },
    // 分析对象的下拉菜单
    inithazardListOptions() {
      const obj = {
        hazardName: '',
        hazardLevel: '',
        areaId: '',
        pageNum: 1,
        pageSize: 1000
      }
      hazardList(obj).then((res) => {
        if (res.code === 200) {
          this.initList()
          this.analysisList = res.rows
        } 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.riskUnitName = ''
        this.ruleForm.remarkInfo = ''
      } else {
        this.isEdit = '编辑'
        this.ruleForm = { ...val }
      }
      this.dialogVisible = true
    },
    submit() {
      this.$refs.ruleFormRef.validate(async (valid) => {
        if (valid) {
          if (this.isEdit === '新增') {
            const res = await saveUnit(this.ruleForm)
            if (res.code !== 200) return this.$message.error(res.msg)
          } else {
            const res = await editUnit(this.ruleForm)
            if (res.code !== 200) return this.$message.error(res.msg)
          }

          this.initList()
          this.$message.success(`${this.isEdit}成功!`)
          this.ruleForm.hazardId = ''
          this.ruleForm.hazardLevel = ''
          this.ruleForm.hazardName = ''

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