<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="部门" prop="deptId" v-show="deptShow">
        <el-select v-model="queryParams.deptId" filterable  placeholder="请选择部门" clearable>
          <el-option
            v-for="dict in allDeptList"
            :key="dict.deptId"
            :label="dict.deptName"
            :value="dict.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="风险点等级" prop="riskGrade">
        <el-select v-model="queryParams.riskGrade" placeholder="请选择风险点等级" clearable>
          <el-option
            v-for="dict in dict.type.risk_grade"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上报地址类别" prop="reportAddressType">
        <el-select v-model="queryParams.reportAddressType" placeholder="请选择上报地址类别" clearable>
          <el-option
            v-for="dict in dict.type.risk_report_address_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="风险点位置" prop="riskLocation">
        <el-input
          v-model="queryParams.riskLocation"
          placeholder="请输入风险点位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="涉及群众数量" prop="peopleQuantity">
        <el-input
          v-model="queryParams.peopleQuantity"
          placeholder="请输入涉及群众数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预备物资数量" prop="materialsQuantity">
        <el-input
          v-model="queryParams.materialsQuantity"
          placeholder="请输入预备物资数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="抢修队伍人员" prop="repairTeam">
        <el-input
          v-model="queryParams.repairTeam"
          placeholder="请输入抢修队伍人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="责任人" prop="personResponsible">
        <el-input
          v-model="queryParams.personResponsible"
          placeholder="请输入责任人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="乡级责任人" prop="villagePersonResponsible">
        <el-input
          v-model="queryParams.villagePersonResponsible"
          placeholder="请输入乡级责任人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="村级责任人" prop="villagePersonResponsible2">
        <el-input
          v-model="queryParams.villagePersonResponsible2"
          placeholder="请输入村级责任人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="tel2">
        <el-input
          v-model="queryParams.tel2"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:risk:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:risk:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:risk:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:risk:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="riskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
 <!--     <el-table-column label="${comment}" align="center" prop="riskId" />-->
      <el-table-column label="风险点等级" align="center" prop="riskGrade">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.risk_grade" :value="scope.row.riskGrade"/>
        </template>
      </el-table-column>
      <el-table-column label="上报地址类别" align="center" prop="reportAddressType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.risk_report_address_type" :value="scope.row.reportAddressType"/>
        </template>
      </el-table-column>
      <el-table-column label="上报单位" v-show="false"  align="center" prop="deptName" />
      <el-table-column label="风险点位置" align="center" prop="riskLocation" />
      <el-table-column label="联系电话" align="center" prop="tel" />
      <el-table-column label="涉及群众数量" align="center" prop="peopleQuantity" />
      <el-table-column label="预备物资数量" align="center" prop="materialsQuantity" />
      <el-table-column label="抢修队伍人员" align="center" prop="repairTeam" />
      <el-table-column label="责任人" align="center" prop="personResponsible" />
      <el-table-column label="乡级责任人" align="center" prop="villagePersonResponsible" />
      <el-table-column label="村级责任人" align="center" prop="villagePersonResponsible2" />
      <el-table-column label="联系电话" align="center" prop="tel2" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:risk:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:risk:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改风险信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="风险点等级" prop="riskGrade">
          <el-select v-model="form.riskGrade" placeholder="请选择风险点等级">
            <el-option
              v-for="dict in dict.type.risk_grade"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上报地址类别" prop="reportAddressType">
          <el-select v-model="form.reportAddressType" placeholder="请选择上报地址类别">
            <el-option
              v-for="dict in dict.type.risk_report_address_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="风险点位置" prop="riskLocation">
          <el-input v-model="form.riskLocation" placeholder="请输入风险点位置" />
        </el-form-item>
        <el-form-item label="联系电话" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="涉及群众数量" prop="peopleQuantity">
          <el-input v-model="form.peopleQuantity" placeholder="请输入涉及群众数量" />
        </el-form-item>
        <el-form-item label="预备物资数量" prop="materialsQuantity">
          <el-input v-model="form.materialsQuantity" placeholder="请输入预备物资数量" />
        </el-form-item>
        <el-form-item label="抢修队伍人员" prop="repairTeam">
          <el-input v-model="form.repairTeam" placeholder="请输入抢修队伍人员" />
        </el-form-item>
        <el-form-item label="责任人" prop="personResponsible">
          <el-input v-model="form.personResponsible" placeholder="请输入责任人" />
        </el-form-item>
        <el-form-item label="乡级责任人" prop="villagePersonResponsible">
          <el-input v-model="form.villagePersonResponsible" placeholder="请输入乡级责任人" />
        </el-form-item>
        <el-form-item label="村级责任人" prop="villagePersonResponsible2">
          <el-input v-model="form.villagePersonResponsible2" placeholder="请输入村级责任人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="tel2">
          <el-input v-model="form.tel2" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRisk, getRisk, delRisk, addRisk, updateRisk } from "@/api/ehs/ehsRisk";
import {listAllDept } from "@/api/system/dept";
export default {
  name: "Risk",
  dicts: ['risk_grade', 'risk_report_address_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 风险信息表格数据
      riskList: [],
      //所有部门列表字典
      allDeptList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否显示部门列表查询条件
      deptShow: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        riskGrade: null,
        reportAddressType: null,
        riskLocation: null,
        tel: null,
        peopleQuantity: null,
        materialsQuantity: null,
        repairTeam: null,
        personResponsible: null,
        villagePersonResponsible: null,
        villagePersonResponsible2: null,
        tel2: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getListAllDept();
    this.deptShow = this.$store.state.user.parentId==0 ?true :false;
  },
  methods: {
    /** 查询风险信息列表 */
    getList() {
      this.loading = true;
      listRisk(this.queryParams).then(response => {
        this.riskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 单位信息列表 */
    getListAllDept() {
      console.log(this.$store.state.user.deptId);
      this.loading = true;
      listAllDept().then(response => {
        this.allDeptList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        riskId: null,
        riskGrade: null,
        reportAddressType: null,
        riskLocation: null,
        tel: null,
        peopleQuantity: null,
        materialsQuantity: null,
        repairTeam: null,
        personResponsible: null,
        villagePersonResponsible: null,
        villagePersonResponsible2: null,
        tel2: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.riskId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加风险信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const riskId = row.riskId || this.ids
      getRisk(riskId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改风险信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.riskId != null) {
            updateRisk(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRisk(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const riskIds = row.riskId || this.ids;
      this.$modal.confirm('是否确认删除风险信息编号为"' + riskIds + '"的数据项？').then(function() {
        return delRisk(riskIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/risk/export', {
        ...this.queryParams
      }, `risk_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
.color-red{
color: white;
border-color: red;
background-color: red;
  border-radius: 50px;
}

.color-orange{
color: white;
background-color: orange;
  border-radius: 50px;
}

.color-yellow{
background-color: yellow;
color: black;
  border-radius: 50px;
}

.color-blue{
color: white;
background-color: blue;
  border-radius: 50px;
}
</style>
