<!--一体机配置-->
<template>
  <div class="public-config-page integrationConfig">
    <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="isStart">
          <el-radio-group v-model="form.isStart" style="margin-top:4px;">
            <el-radio :label="1" style="margin-right:30px;">启用</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </div>
      <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 class="time-input-box" label="自动返回时间：" prop="returnTime">
          <el-input-number :controls="false" :min="5" :max="600" placeholder="请输入内容" v-model="form.returnTime" style="width:500px;"/>
          <span>秒</span>
        </el-form-item>
      </div>
      <div class="form-max-box">
        <el-form-item class="time-input-box" label="自动注销时间：" prop="logoutTime">
          <el-input-number :controls="false" :min="5" :max="600" placeholder="请输入内容" v-model="form.logoutTime" style="width:500px;"/>
          <span>秒</span>
        </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/jpg,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="fullScreenViewClick(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="signMap">
          <el-upload
            class="certificate-avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            accept="image/jpeg,image/jpg,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="operationGuide">
          <el-upload
            class="certificate-avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            accept="image/jpeg,image/jpg,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>
    <!--全屏图片预览-->
    <fullScreenView :fullScreenViewProps="fullScreenViewProps" ref="fullScreenView"></fullScreenView>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import { getConfigByType } from "@/api/commonality/permission";
import { addOrUpdate } from "@/api/systemManagement/index";
import fullScreenView from "@/components/fullScreenView/fullScreenView.vue";
export default {
  name: "publicConfig",
  components: {
    fullScreenView,
  },
  data() {
    return {
      uploadImgUrl: this.uploadUrl(), // 上传的图片服务器地址
      headers: {
        Authorization:getToken(),
      },
      configId:null,
      form:{
        lotControl:[],
        userType:[],
        expandType:[],
        isStart:1,
        returnTime:60,
        logoutTime:120,
      },
      rules: {
        lotControl:[
          { required: true, message: "请选择物联控制权限", trigger: "blur" }
        ],
        videoCover: [
          { required: true, message: "视频封面不能为空", trigger: "blur" }
        ],
        signMap: [
          { required: true, message: "签到/离开示意图不能为空", trigger: "blur" }
        ],
        operationGuide: [
          { required: true, message: "操作指南不能为空", trigger: "blur" }
        ],
        isStart:[
          { required: true, message: "请选择信息牌模块", trigger: "blur" }
        ],
        returnTime:[
          { required: true, message: "请输入自动返回时间", trigger: "blur" }
        ],
        logoutTime:[
          { required: true, message: "请输入自动注销时间", trigger: "blur" }
        ],



      },
      //物联控制权限
      lotControl:[],
      citiesList:[
        //物联控制 1.实验室负责人，2.安全员，3.安全准入人员，4.白名单，-1.全部
        {
          id:'1',
          name:"实验室负责人"
        },
        {
          id:'2',
          name:"安全员"
        },
        {
          id:'3',
          name:"安全准入人员"
        },
        {
          id:'4',
          name:"白名单"
        },
        {
          id:'-1',
          name:"全部人员"
        },
      ],
      fullScreenViewProps:[],
    };
  },
  created() {

  },
  mounted(){
    this.getConfigByType();
  },
  methods: {
    /** 提交按钮 */
    addOrUpdate() {
      let self = this;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(self.form.returnTime == self.form.logoutTime){
            this.msgError('自动返回时间与自动注销时间不能重复');
            return
          }
          this.$confirm('是否确认提交?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            let configValue = {
              videoCover:self.form.videoCover,
              signMap:self.form.signMap,
              operationGuide:self.form.operationGuide,
              isStart:self.form.isStart,
              returnTime:self.form.returnTime,
              logoutTime:self.form.logoutTime,
            };
            let num = 0;
            for(let i=0;i<self.form.lotControl.length;i++){
              if(self.form.lotControl[i] == '-1'){
                num++
              }
            }
            if(num == 0){
              configValue.lotControl = self.form.lotControl+'';
            }else{
              configValue.lotControl = '-1';
            }
            let obj = {
              id:self.configId,
              configName: "一体机配置",
              category:2,
              configKey:2,
              configValue : JSON.stringify(configValue),
            }
            addOrUpdate(obj).then(response => {
              self.msgSuccess(response.message);
              self.getConfigByType();
            });
          }).catch(() => {});
        }
      });
    },
    fullScreenViewClick(url){
      this.$set(this,'fullScreenViewProps',[url]);
      this.$refs['fullScreenView'].initialize();
    },
    //获取数据
    getConfigByType(){
      getConfigByType({ category: 2, configType: 2 }).then(response => {
        this.$set(this,'configId',response.data.id);
        let obj = JSON.parse(response.data.configValue)
        localStorage.setItem('videoCover',obj.videoCover)
        if(!obj.returnTime){
          obj.returnTime = 60;
        }
        if(!obj.logoutTime){
          obj.logoutTime = 120;
        }
        this.$set(this,'form',obj)
        if(obj.lotControl){
          let list = obj.lotControl.split(",");
          this.$set(this.form,'lotControl',list);
        }else{
          this.$set(this.form,'lotControl',['1','2','3','4']);
        }
      });
    },
    //上传
    handleAvatarSuccess(res,type) {
      if(type == 'videoCover'){
        this.$set(this.form,'videoCover',res.data.url);
      }else if(type == 'signMap'){
        this.$set(this.form,'signMap',res.data.url);
      }else if(type == 'operationGuide'){
        this.$set(this.form,'operationGuide',res.data.url);
      }
      this.$forceUpdate()
    },
    beforeAvatarUpload(file) {
      let type = false;
      if (file.type == 'image/png' || file.type == 'image/jpeg' || file.type == 'image/jpg' || file.type == 'image/gif') {
        type = true;
      }else{
        this.msgError('只能上传png/jpeg/jpg/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;
    }
  }
  .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;
    border:1px dashed #CCCCCC;
    border-radius:10px;
  }
  .avatar{
    width: 200px;
    height: 120px;
  }
}
</style>
<style lang="scss">
  .integrationConfig{
    .time-input-box{
      input{
        text-align: left;
        border-top-right-radius:0;
        border-bottom-right-radius:0;
      }
      span{
        font-size:14px;
        font-weight:500;
        display: inline-block;
        width:40px;
        height:40px;
        background: #E0E0E0;
        text-align: center;
        line-height: 40px;
        border-top-right-radius:4px;
        border-bottom-right-radius:4px;
      }
    }
  }
</style>

