<!--安全分类-->
<template>
  <div class="class-type-page">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          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:classType:add']"
      >新增</p>
    </el-form>
    <el-table class="table-box" v-loading="loading" border :data="classtypeList" @selection-change="handleSelectionChange">
      <el-table-column label="安全分类" align="left" prop="typeName" />
      <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"
               v-hasPermiRouter="['lab:classType:edit']"
               @click="handleUpdate(scope.row)"
            >修改</p>
            <p class="table-button-p"
               v-hasPermiRouter="['lab:classType:del']"
               @click="handleDelete(scope.row)"
            >删除</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
               :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="安全分类" prop="typeName">
          <el-input v-model="form.typeName" maxlength="10" placeholder="请输入安全分类名称"  style="width: 750px"/>
        </el-form-item>
        <el-form-item label="排序" prop="typeSort">
          <el-input-number v-model="form.typeSort" :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 {
  laboratoryClassTypeList,
  laboratoryClassTypeDetail,
  laboratoryClassTypeDelete,
  laboratoryClassTypeAdd,
  laboratoryClassTypeUpdate, laboratoryClassTypeGetClassCriteria
} from '@/api/integratedManagement/index'

export default {
  name: "Classtype",
  data() {
    return {
      tableButtonType:this.hasPermiDom(['lab:classType:edit','lab:classType:del']),

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 安全分级类型表格数据
      classtypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize:20,
        typeName: null,
      },
      // 表单参数
      form: {
        typeSort:0,
      },
      conditionList:[],
      // 表单校验
      rules: {
        typeName: [
          { required: true, message: "请输入安全分类名称", trigger: "blur" },
          { required: true, message: "请输入安全分类名称", validator: this.spaceJudgment, trigger: "blur" }
        ],
        typeSort:[
          { required: true, message: "请输入安全分类序号", trigger: "blur" },
          { required: true, message: "请输入安全分类序号", validator: this.spaceJudgment, trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.laboratoryClassTypeGetClassCriteria();
    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;
    },
    //分类认定条件
    laboratoryClassTypeGetClassCriteria() {
      laboratoryClassTypeGetClassCriteria().then( response => {
        response.data.forEach(function(item){
          item.allName='全选';
          item.checkAll=false;
          item.checkedCities=[];
        })
        this.$set(this,'conditionList',response.data)
      });
    },

    /** 查询安全分级类型列表 */
    getList() {
      this.loading = true;
      laboratoryClassTypeList(this.queryParams).then( response => {
        this.classtypeList =  response.data.records;
        this.total =  response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        typeId: null,
        typeName: null,
        typeSort: 0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.$set(this,'queryParams',{
        page: 1,
        pageSize:20,
        typeName:"",
      });
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.laboratoryClassTypeGetClassCriteria();
      this.reset();
      this.open = true;
      this.title = "新增安全分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      let self=this;
      this.reset();
      laboratoryClassTypeDetail({id:row.typeId}).then( response => {
        this.$set(this,'form',{
          typeId:response.data.typeId,
          typeName:response.data.typeName,
          typeSort:response.data.typeSort,
        });


        //原数组
        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.open = true;
        this.title = "修改安全分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      let self=this;
      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.classTypeCriteria=JSON.stringify(list);
          if (this.form.typeId != null) {
            laboratoryClassTypeUpdate(this.form).then( response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            laboratoryClassTypeAdd(this.form).then( response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除安全分类名称为"' + row.typeName + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return laboratoryClassTypeDelete({typeId:row.typeId});
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('laboratory/classtype/export', {
        ...this.queryParams
      }, `laboratory_classtype.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
  .class-type-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>
