<!--安全信息配置-->
<template>
  <div class=" safetyInfoConfig">
    <div class="title-box">
      <div @click="titleClick(1)">
        <p>安全类目</p>
        <p></p>
      </div>
      <div @click="titleClick(2)">
        <p>信息配置</p>
        <p class="bottom-p-color"></p>
      </div>
    </div>
    <div class="config_main">
      <div class="config_main_l scrollbar-box">
        <div class="config_main_l_title">
          <img  src="@/assets/ZDimages/icon_xxpz_aqxxlm.png">
          安全信息类目
        </div>
        <div :class='currentIndex==index?"active":""' class="config_main_l_li" v-for="(item,index) in tabsList" :key="index" @click="handleTabClick(item,index)">{{item.classifyName}}</div>
      </div>
      <p class="config_main_border"></p>
      <div class="approval_handle-page">
        <p>
          <el-button
            style="float: right;margin-bottom: 20px"
            v-hasPermi="['laboratory:classifyDet:add']"
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleClick('','','add')"
          >新增</el-button>
        </p>

        <el-table border v-loading="loading" :data="tableData">
          <el-table-column label="序号" align="left" prop="sort"  width="50"/>
          <el-table-column label="信息名称" align="left" prop="infoName"/>
          <el-table-column label="公共信息" align="left">
            <template slot-scope="scope">
              <el-switch
                @click.native="categoryShow(scope.row)"
                class="category-switch captcha-img"
                :active-value="2"
                :inactive-value="1"
                active-color="#FF9900"
                inactive-color="#999"
                v-model="scope.row.isCollective"
                active-text="是"
                inactive-text="否"
                disabled
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="添加人" align="left" prop="createBy"/>
          <el-table-column label="添加时间" align="left" prop="createTime"/>
          <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="120">
            <template slot-scope="scope">
              <div class="button-box">
                <p class="table-min-button"
                   v-hasPermi="['laboratory:classifyDet:edit']"
                   @click="handleClick('',scope.row,'edit')"
                >编辑</p>
                <p class="table-min-button"
                   v-hasPermi="['laboratory:classifyDet:remove']"
                   @click="handleClick('',scope.row,'delete')"
                >删除</p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination :page-sizes="[20, 30, 40, 50]"
                    :total="total"
                    layout="total, prev, pager, next, sizes, jumper"
                    :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>


    <!-- 添加或修改安全分级对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :before-close="closeDialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" >
        <el-form-item label="信息名称" prop="infoName">
          <el-input v-model="form.infoName" maxlength="10" placeholder="请输入名称" style="width:320px;"/>
        </el-form-item>
        <el-form-item label="信息类目"  prop="infoClassifyId" class="form-item">
          <el-select placeholder="请选择类目" v-model="form.infoClassifyId" style="width:320px;" @change="categoryClick()">
            <el-option
              v-for="dict in categoryList"
              :key="dict.id"
              :label="dict.classifyName"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="信息类型">
          <el-input v-model="form.infoTypeName"   placeholder="" disabled style="width:320px;"/>
        </el-form-item>
        <el-form-item label="信息内容" prop="infoContent" v-if="form.infoType==1">
          <el-input type="textarea" v-model="form.infoContent"  maxlength="200" placeholder="请输入内容" style="width:320px;"/>
        </el-form-item>
        <el-form-item label="信息内容" prop="infoContent" v-if="form.infoType==2">
          <el-upload
            class="certificate-avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :headers="headers"
            :before-upload="beforeAvatarUpload">
            <div style="display: flex;">
              <img v-if="form.infoContent" :src="form.infoContent" style="width: 80px;height: 80px;margin-right: 10px">
              <i v-if="!form.infoContent" class="el-icon-plus avatar-uploader-icon"></i>
            </div>
          </el-upload>
        </el-form-item>
        <P v-if="form.infoType==2" style="margin-left: 80px">图片最大上传630*78，建议尺寸58*78</P>
      </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 { getToken } from "@/utils/auth";
import {
  infoCategoryList,
  infoConfigAdd, infoConfigList, infoConfigPut,infoConfigDelete
} from '@/api/laboratory/safetyInfo'
import safetyInfoConfig from "./safetyInfoConfig.vue"
import { listHazard } from '@/api/permissionRequired'
export default {
  name: "Approval",
  components: {
    safetyInfoConfig
  },
  data() {
    return {
      uploadImgUrl: this.uploadUrl(), // 上传的图片服务器地址
      activeName: '',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //页面状态
      pageType:1,
      loading:false,
      headers: {
        Authorization: "Bearer " + getToken()
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize:20,
        infoClassifyId:'',
      },
      total:0,
      tableData:[{}],
      dateRange:[],
      pageData2:{},
      tabsList:[],
      categoryList:[],
      // 表单参数
      form: {
        infoName:'',
        infoClassifyId:'',
        infoType:'',
        infoTypeName:'',
        infoContent:'',
        isCollective:'',

      },
      currentIndex:0,
      // 表单校验
      rules: {
        infoName: [
          { required: true, message: "请输入名称", trigger: "blur" },
        ],
        infoClassifyId: [
          { required: true, message: "请选择类目", trigger: "blur" },
        ],
        infoContent: [
          { required: true, message: "请添加信息内容", trigger: "blur" },
        ],
      }
    };


  },
  methods: {
      titleClick(type){
        this.$parent.titleClick(type);
      },
      closeDialog(){
        this.$refs['form'].clearValidate()
        this.open=false
      },
      handleTabClick(item,index){
        this.currentIndex = index
        this.queryParams.infoClassifyId=item.id;
        this.getList()
      },
      handleClick(index,row,doType){
        let self=this;
        if(doType=='add'){//添加
          this.title='新增安全信息';
          delete this.form.id
          this.form.infoName='';
          this.form.infoClassifyId='';
          this.form.infoType='';
          this.form.infoTypeName='';
          this.form.infoContent='';
          this.open = true;
        }else if(doType=='edit'){//编辑
          this.title='编辑安全信息';
          this.form.id=row.id
          this.form.infoName=row.infoName
          this.form.infoClassifyId=row.infoClassifyId
          this.form.infoContent=row.infoContent
          this.form.infoType=row.infoType
          this.form.infoTypeName=row.infoType==1?'文字':'图片';
          this.open = true;

        }else if(doType=='deploy'){//信息牌配置
          self.pageType=2;
        }else if(doType=='delete'){//删除
          this.$confirm('确认要删除吗?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            //  确定
            infoConfigDelete(row.id).then(response => {
              self.msgSuccess(response.msg);
              self.getList();
            });
          }).catch(function() {});
        }else if(doType=='back'){//返回
          self.pageType=1;
        }
      },
      categoryShow(row){
        let obj={
          id:row.id,
          isCollective:row.isCollective==1?2:1,
        }
        infoConfigPut(obj).then( response => {
          this.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      },
      categoryClick(){
        let _this=this;
        this.form.infoContent='';
        this.categoryList.forEach(function(item) {
          if(item.id==_this.form.infoClassifyId){
            _this.form.infoTypeName=item.classifyType==1?'文字':'图片';
            _this.form.infoType=item.classifyType;
          }
        })

      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.handleQuery();
      },
      /** 取消按钮 */
      cancel() {
        this.$refs['form'].clearValidate()
        this.open = false;
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              infoConfigPut(this.form).then( response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              infoConfigAdd(this.form).then( response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      //上传
      handleAvatarSuccess(res, file) {
        this.form.infoContent = res.data.url;
        this.$forceUpdate()
      },
      beforeAvatarUpload(file) {
        return new Promise((resolve, reject) => {
          if (file.type == 'image/png') {
            let reader = new FileReader()
            reader.readAsDataURL(file) // 必须用file.raw
            reader.onload = e => { // 让页面中的img标签的src指向读取的路径
              let img = e.target.result;
              const image = new Image()
              image.src=img
              image.onload = _=>{
                console.log(image.width)
                console.log(image.height)
                if(image.width <= 630 && image.height <= 78){
                  resolve()
                }else{
                  this.$message.error('尺寸限制为最大630*78px，支持PNG格式')
                  reject();
                }
              }
            }
          }else{
            this.$message.error('请上传PNG格式');
            reject();
          }
        })
      },
      //获取危险源列表
      getCategory(){
        listHazard({}).then(response=>{
          this.$set(this,'categoryList',response.data);
        })
      },
      //查询信息类目列表
      getCategoryList(){
        let _this=this;
        let obj={
          pageNum: 1,
          pageSize:100,
          isShow:1,
          remark:'sort',
        }
        infoCategoryList(obj).then( response => {
          let res=response.rows;
          if(response.code==200){
            if(res){
              _this.tabsList=res;
              _this.categoryList=res;
              _this.activeName=response.rows[0].id+''
              _this.queryParams.infoClassifyId=response.rows[0].id;
              this.getList()
            }
          }

        });
      },
      getList(){
        let _this=this;
        infoConfigList(_this.queryParams).then( response => {
          let res=response.rows;
          _this.tableData=res;
          _this.total=response.total;
        });
      },
  },
  mounted() {
    this.getCategoryList()

  }
};
</script>

<style scoped lang="scss">
  .safetyInfoConfig {
    overflow: hidden;
    flex:1;
    display: flex!important;
    flex-direction: column;
    .title-box{
      display: flex;
      border-bottom:1px solid #E0E0E0;
      margin-bottom:20px;
      div{
        height:80px;
        margin-right:20px;
        cursor: pointer;
        p:nth-child(1){
          font-size:18px;
          text-align: center;
          padding:0 20px;
          margin-top:26px;
        }
        p:nth-child(2){
          width:40px;
          height:4px;
          border-radius:40px;
          margin:12px auto;
        }
        .top-p-color{
          color: #0045AF;
        }
        .bottom-p-color{
          background: #0045AF;
        }
      }
      .buttonTitleColorA{
        color:#0045AF;
      }
      .buttonTitleColorB{
        color:#999999;
      }
    }
    .config_main{
        overflow: hidden;
        flex:1;
        display: flex;
        overflow: hidden;
       .config_main_l{
         flex: 1;
         padding-left: 42px;
         padding-top: 20px;
         .config_main_l_title{
           font-size: 16px;
           font-weight: 400;
           color: #0183FA;
           >img{
             width: 16px;
             height: 13px;
             margin-right: 12px;
           }
         }
         .config_main_l_li{
           font-size: 14px;
           font-family: Microsoft YaHei-Regular, Microsoft YaHei;
           font-weight: 400;
           color: #333333;
           line-height: 68px;
           cursor: pointer;
         }
         .active{
           color: #0183FA;
         }
       }
       .config_main_border{
         border-right: 1px dashed #A2A2A2;
         margin: 20px 35px 36px 0;
       }
      /*表格*/
      .approval_handle-page{
        display: flex!important;
        flex-direction: column;
        width: 1264px;
        padding:0px 20px 20px!important;
        .button-box{
          width:200px;
          display: flex;
        }
      }
    }


  }

</style>
<style  lang="scss">
.category-switch .el-switch__label {
  position: absolute;
  display: none;
  color: #fff !important;
}
.category-switch .el-switch__label--right span {
  margin-left: 10px;
}

.category-switch .el-switch__label--left {
  z-index: 1;
}

.category-switch .el-switch__label--left span {
  margin-left: 24px;
}

.category-switch .el-switch__label.is-active {
  display: block;
}

.category-switch.el-switch .el-switch__core,
.el-switch .el-switch__label {
  width: 60px !important;
  margin: 0;
}
</style>
