<!--安全分级-->
<template>
  <div class="class-ified-page">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="levelName">
        <el-input
          v-model="queryParams.levelName"
          placeholder="安全分级"
          clearable
          size="small"
        />
      </el-form-item>
      <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
      <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
      <p class="page-submit-common-style-button"
         style="float: right;"
         @click="handleAdd"
         v-hasPermiRouter="['lab:classLevel:add']"
      >新增</p>
    </el-form>

    <el-table class="table-box" v-loading="loading" border :data="classifiedList" @selection-change="handleSelectionChange">
      <el-table-column label="安全分级" align="left" prop="levelName" />
      <el-table-column label="颜色" align="left" prop="levelColor" >
        <template slot-scope="scope">
          <p :style="'width:40px;height:23px;margin:0;background:'+scope.row.levelColor"></p>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="left" prop="createName" />
      <el-table-column label="创建时间" align="left" prop="createTime">
        <template slot-scope="scope">{{ parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}") }}</template>
      </el-table-column>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="160" v-if="tableButtonType">
        <template slot-scope="scope">
          <div class="table-button-box">
            <p class="table-button-null"></p>
            <p class="table-button-p"
               @click="handleUpdate(scope.row)"
               v-hasPermiRouter="['lab:classLevel:edit']"
            >修改</p>
            <p class="table-button-p"
               @click="handleDelete(scope.row)"
               v-hasPermiRouter="['lab:classLevel:del']"
            >删除</p>
            <p class="table-button-null"></p>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改安全分级对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="970px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分级名称" prop="levelName">
          <el-input v-model="form.levelName" maxlength="10" placeholder="请输入分级名称"  style="width: 750px"/>
        </el-form-item>
        <el-form-item label="颜色" prop="levelColor">
          <el-color-picker v-model="form.levelColor"></el-color-picker>
        </el-form-item>
        <el-form-item label="排序" prop="levelSort">
          <el-input-number v-model="form.levelSort" :min="0" :max="999" ></el-input-number>
        </el-form-item>
        <div class="classify-name"><i>*</i>分级认定条件:</div>
        <div class="classify">
          <div class="classify-li" v-for="(item,index) in conditionList">
            <div class="classify-li-l">{{item.bigClassName}}</div>
            <div class="classify-li-r">
              <p><el-checkbox v-model="item.checkAll" @change="handleCheckAllChange($event,item)">{{item.allName}}</el-checkbox></p>
              <p>
                <el-checkbox-group v-model="item.checkedCities" @change="handleCheckedCitiesChange($event,item)">
                  <el-checkbox v-for="item2 in item.smallClassList" :label="item2.smallClassName" :key="item2.smallClassValue">{{item2.smallClassName}}</el-checkbox>
                </el-checkbox-group>
              </p>
            </div>
          </div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer dialog-footer-box">
        <p class="dialog-footer-button-null"></p>
        <p class="dialog-footer-button-info" @click="cancel">取消</p>
        <p class="dialog-footer-button-primary" @click="submitForm">提交</p>
        <p class="dialog-footer-button-null"></p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  laboratoryClassLevelAdd,
  laboratoryClassLevelDelete,
  laboratoryClassLevelDetail,
  laboratoryClassLevelList,
  laboratoryClassLevelUpdate, laboratoryClassTypeGetClassCriteriaLevel
} from '@/api/integratedManagement/index'

    export default {
  name: "Classified",
  data() {
    return {
      tableButtonType:this.hasPermiDom(['lab:classLevel:edit','lab:classLevel:del']),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 安全分级表格数据
      classifiedList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize:20,
        levelName: null
      },
      // 表单参数
      form: {
        levelSort:0,
      },
      conditionList:[],
      // 表单校验
      rules: {
        levelName: [
          { required: true, message: "请输入分级名称", trigger: "blur" },
          { required: true, message: "请输入分级名称", validator: this.spaceJudgment, trigger: "blur" }
        ],
        levelColor: [
          { required: true, message: "请选择颜色", trigger: "blur" }
        ],
        levelSort:[
          { required: true, message: "请输入安全分类序号", trigger: "blur" },
          { required: true, message: "请输入安全分类序号", validator: this.spaceJudgment, trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.laboratoryClassTypeGetClassCriteriaLevel();
    this.getList();
  },
  methods: {
    //全选按钮
    handleCheckAllChange(val,item) {
      let name=[];
      for (let i=0;i<item.smallClassList.length;i++){
        name.push(item.smallClassList[i].smallClassName)
      }
      item.checkedCities = val ? name : [];
    },
    //单个选择按钮
    handleCheckedCitiesChange(value,item) {
      console.log(value)
      console.log(item)
      let checkedCount = value.length;
      item.checkAll = checkedCount === item.smallClassList.length;
    },
    //分类认定条件
    laboratoryClassTypeGetClassCriteriaLevel() {
      laboratoryClassTypeGetClassCriteriaLevel().then( response => {
        response.data.forEach(function(item){
          item.allName='全选';
          item.checkAll=false;
          item.checkedCities=[];
        })
        this.$set(this,'conditionList',response.data)
      });
    },

    /** 查询安全分级列表 */
    getList() {
      this.loading = true;
      laboratoryClassLevelList(this.queryParams).then( response => {
        this.classifiedList =  response.data.records;
        this.total =  response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        levelId: null,
        levelName: null,
        levelColor: null,
        levelSort: 0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.$set(this,'queryParams',{
        page: 1,
        pageSize:20,
        levelName:"",
      });
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.laboratoryClassTypeGetClassCriteriaLevel();
      this.reset();
      this.title = "新增安全分级";
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      let self=this;
      this.reset();
      laboratoryClassLevelDetail({id:row.levelId}).then( response => {
        this.$set(this,'form',{
          levelId:response.data.levelId,
          levelName:response.data.levelName,
          levelColor:response.data.levelColor,
          levelSort:response.data.levelSort,
        });
        //原数组
        self.conditionList.forEach(function(item){
          let list=[];
          //详情返回的数组
          response.data.classInfoList.forEach(function(item2) {
            if (item.bigClassValue==item2.bigClassValue){
              item.smallClassList.forEach(function(item3){
                item2.smallClassValue.forEach(function(item4){
                  if (item3.smallClassValue==item4){
                    list.push(item3.smallClassName)
                  }
                })
              })
              if (item.smallClassList.length ==item2.smallClassValue.length){
                item.checkAll=true;
              }
            }
          })
          item.checkedCities=list;
        })
        this.title = "修改安全分级";
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          //分类认定条件数据筛选
          let list=[];
          this.conditionList.forEach(function(item) {
            let list2=[];
            if (item.checkedCities.length>0){
              item.smallClassList.forEach(function(item2) {
                item.checkedCities.forEach(function(item3) {
                  if (item3==item2.smallClassName){
                    list2.push(item2.smallClassValue)
                  }
                })
              })
              list.push({bigClassValue:item.bigClassValue,bigClassName:item.bigClassName,smallClassValue:list2})
            }
          })
          if(!list[0]){
            this.msgError('请勾选分级条件')
            return
          }
          this.form.classLevelCriteria=JSON.stringify(list);
          if (this.form.levelId != null) {
            laboratoryClassLevelUpdate(this.form).then( response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            laboratoryClassLevelAdd(this.form).then( response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除安全分级名称为"' + row.levelName + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return laboratoryClassLevelDelete({levelId:row.levelId});
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('laboratory/classified/export', {
        ...this.queryParams
      }, `laboratory_classified.xlsx`)
    }
  }
};
</script>


<style scoped lang="scss">
  .class-ified-page {
    flex:1;
    display: flex!important;
    flex-direction: column;
    padding:20px!important;
    overflow: hidden;
  }
  .classify-name{
    margin-bottom: 20px;
    margin-left: 34px;
    >i{
      color: #ff4949;
      margin-right: 4px;
    }
    color: #333;
    font-size: 14px;
    font-weight: 500;
  }
  .classify{
    width: 820px;
    height: 236px;
    overflow-y: auto;
    margin-left: 38px;
    .classify-li{
      min-height: 80px;
      display: flex;
      justify-content: flex-start;
      border: 1px solid #E0E0E0;
      border-top: 0;
      .classify-li-l{
        width: 150px;
        min-height: 80px;
        display: flex;
        justify-content: center;
        align-items: center;
        border-right: 1px solid #E0E0E0;
      }
      .classify-li-r{
        .el-checkbox{
          margin:4px 30px 4px 0;
        }
        flex: 1;
        >p{
          display: flex;
          align-items: center;
          padding-left: 14px;
          box-sizing: border-box;
        }
        >p:nth-of-type(1){
          min-height: 40px;
          border-bottom:1px solid #E0E0E0;
        }
        >p:nth-of-type(2){
          min-height: 40px;
        }
      }
    }
    .classify-li:first-child{
      border-top: 1px solid #E0E0E0;
    }
  }
</style>
