<!--一体机配置-->
<template>
  <div class="public-config-page">
    <el-form class="public-form-box scrollbar-box"  :model="form" ref="form" :inline="true" :rules="rules" label-width="160px">
      <div class="form-max-box">
        <el-form-item label="物联控制权限：" prop="lotControl">
          <el-checkbox-group v-model="form.lotControl">
            <el-checkbox v-for="item in citiesList" :label="item.id" :key="item.id" style="margin-right:20px;margin-top:2px;">{{item.name}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </div>
      <div class="form-max-box">
        <el-form-item label="签到/离开示意图：" prop="signMap">
          <el-upload
            class="certificate-avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            accept="image/jpeg,image/gif,image/png"
            :on-success="(res)=>handleAvatarSuccess(res,'signMap')"
            :headers="headers"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.signMap" :src="form.signMap" class="avatar">
            <i v-if="!form.signMap" class="el-icon-plus avatar-uploader-icon" >上传</i>
          </el-upload>
          <i v-if="form.signMap" class="el-icon-view" @click="lookImg(form.signMap)"
             style="position: absolute;top:0;right:0;z-index:999;border-radius:3px;cursor:pointer;width:20px;
            height:20px;line-height:20px;text-align: center;background: rgba(0,0,0,0.2);color:#fff;"></i>
        </el-form-item>
      </div>
      <div class="form-max-box">
        <el-form-item label="视频封面：" prop="videoCover">
          <el-upload
            class="certificate-avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            accept="image/jpeg,image/gif,image/png"
            :on-success="(res)=>handleAvatarSuccess(res,'videoCover')"
            :headers="headers"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.videoCover" :src="form.videoCover" class="avatar" >
            <i v-if="!form.videoCover" class="el-icon-plus avatar-uploader-icon">上传</i>
          </el-upload>
          <i v-if="form.videoCover" class="el-icon-view" @click="lookImg(form.videoCover)"
             style="position: absolute;top:0;right:0;z-index:999;border-radius:3px;cursor:pointer;width:20px;
            height:20px;line-height:20px;text-align: center;background: rgba(0,0,0,0.2);color:#fff;"></i>
        </el-form-item>
      </div>
      <div class="form-max-box">
        <el-form-item label="操作指南：" prop="operationGuide">
          <el-upload
            class="certificate-avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            accept="image/jpeg,image/gif,image/png"
            :on-success="(res)=>handleAvatarSuccess(res,'operationGuide')"
            :headers="headers"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.operationGuide" :src="form.operationGuide" class="avatar">
            <i v-if="!form.operationGuide" class="el-icon-plus avatar-uploader-icon">上传</i>
          </el-upload>
          <i v-if="form.operationGuide" class="el-icon-view" @click="lookImg(form.operationGuide)"
             style="position: absolute;top:0;right:0;z-index:999;border-radius:3px;cursor:pointer;width:20px;
            height:20px;line-height:20px;text-align: center;background: rgba(0,0,0,0.2);color:#fff;"></i>
        </el-form-item>
      </div>
    </el-form>
    <div class="sub_btn">
      <p class="null-p"></p>
      <p class="inquire-button-one"  @click="submitForm">保存</p>
      <p class="null-p"></p>
    </div>
    <!--查看图片-->
    <el-dialog title="查看" :visible.sync="imgOpen" width="1200px" append-to-body>
      <div class="scrollbar-box" style="width:1160px;margin:0 auto;max-height:700px;overflow-y: scroll">
        <img :src="imgUrl" style="display:block;max-width:1160px;margin:0 auto">
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import { getLogoInfo,updateLogoInfo } from "@/api/system/publicConfig";
import store from '@/store'
export default {
  name: "publicConfig",
  data() {
    return {
      uploadImgUrl: this.uploadUrl(), // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      form:{
        lotControl:[],
        userType:[],
        expandType:[]
      },
      rules: {
        lotControl:[
          { required: true, message: "请选择物联控制权限", trigger: "blur" }
        ],
        signMap: [
          { required: true, message: "签到/离开示意图不能为空", trigger: "blur" }
        ],
        videoCover: [
          { required: true, message: "视频封面不能为空", trigger: "blur" }
        ],
        operationGuide: [
          { required: true, message: "操作指南不能为空", trigger: "blur" }
        ],
      },
      imgUrl:"",
      imgOpen:false,
      //物联控制权限
      lotControl:[],
      citiesList:[
        //物联控制 1.实验室负责人，2.安全责任人，3.安全准入人员，4.白名单，-1.全部
        {
          id:'1',
          name:"实验室负责人"
        },
        {
          id:'2',
          name:"安全责任人"
        },
        {
          id:'3',
          name:"安全准入人员"
        },
        {
          id:'4',
          name:"白名单"
        },
        {
          id:'-1',
          name:"全部人员"
        },
      ],
    };
  },
  created() {

  },
  mounted(){
    this.getLogoInfo();
  },
  methods: {
    /** 提交按钮 */
    submitForm: function() {
      let self = this;
      this.$refs["form"].validate(valid => {
        if (valid) {
          let obj = {
            signMap:this.form.signMap,
            videoCover:this.form.videoCover,
            operationGuide:this.form.operationGuide,
          }
          let num = 0;
          for(let i=0;i<self.form.lotControl.length;i++){
            if(self.form.lotControl[i] == '-1'){
              num++
            }
          }
          console.log('num',num);
          if(num == 0){
            obj.lotControl = self.form.lotControl+'';
          }else{
            obj.lotControl = '-1';
          }
          updateLogoInfo(obj).then(response => {
            this.msgSuccess(response.msg);
            this.getLogoInfo();
          });
        }
      });
    },
    //查看图片
    lookImg(url){
      this.imgUrl = url;
      this.imgOpen = true;
    },
    //获取数据
    getLogoInfo(){
      getLogoInfo().then(response => {
        localStorage.setItem('videoCover',response.data.videoCover)
        this.$set(this,'form',response.data)
        if(response.data.lotControl){
          let list = response.data.lotControl.split(",");
          this.$set(this.form,'lotControl',list);
        }else{
          this.$set(this.form,'lotControl',['1','2','3','4']);
        }
      });
    },
    //上传
    handleAvatarSuccess(res,type) {
       if(type == 'signMap'){
        this.$set(this.form,'signMap',res.data.url);
      }else if(type == 'videoCover'){
        this.$set(this.form,'videoCover',res.data.url);
      }else if(type == 'operationGuide'){
        this.$set(this.form,'operationGuide',res.data.url);
      }
      this.$forceUpdate()
    },
    beforeAvatarUpload(file) {
      let type = false;
      console.log('file',file);
      if (file.type == 'image/png' || file.type == 'image/jpeg' || file.type == 'image/gif') {
        type = true;
      }else{
        this.$message.error('只能上传png/jpeg/gif格式图片');
        type = false;
      }
      return type;
    },
  },
};
</script>

<style scoped lang="scss">
.public-config-page{
  flex:1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  .public-form-box{
    flex:1;
    overflow-y: scroll;
    overflow-x: hidden;
  }
  /*底部按钮弹框*/
  .sub_btn{
    display: flex;
    background: #fff;
    height: 112px;
    .null-p{
      flex:1;
    }
    .inquire-button-one{
      cursor:pointer;
      display: inline-block;
      text-align: center;
      width: 70px;
      height: 40px;
      border-radius: 6px;
      border: 1px solid #0045af;
      color:#ffffff;
      background:#0045af;
      margin:36px 10px 0 0;
    }
  }
  *{
    margin:0;
  }
  .title-box{
    border-bottom:1px solid #dedede;
    p{
      margin-left:40px;
      color:#0045af;
      line-height:60px;
    }
  }
  .form-max-box{
    margin-top:20px;
    margin-left: 100px;
  }
  .avatar-uploader-icon{
    width: 200px;
    height: 120px;
    line-height: 120px;
    font-size: 16px;
    font-weight: 400;
    color: #CCCCCC;
  }
  .avatar{
    width: 200px;
    height: 120px;
  }
}
</style>

