<!--一体机配置-->
<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="登录页banner：" prop="loginBanner">
          <el-upload
            class="certificate-avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            accept="image/jpeg,image/gif,image/png"
            :on-success="(res)=>handleAvatarSuccess(res,'loginBanner')"
            :headers="headers"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.loginBanner" :src="form.loginBanner" class="avatar" >
            <i v-if="!form.loginBanner" class="el-icon-plus avatar-uploader-icon">上传</i>
          </el-upload>
          <i v-if="form.loginBanner" class="el-icon-view" @click="lookImg(form.loginBanner)"
             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="首页banner：" prop="homepageBanner">
          <el-upload
            class="certificate-avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            accept="image/jpeg,image/gif,image/png"
            :on-success="(res)=>handleAvatarSuccess(res,'homepageBanner')"
            :headers="headers"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.homepageBanner" :src="form.homepageBanner" class="avatar" >
            <i v-if="!form.homepageBanner" class="el-icon-plus avatar-uploader-icon">上传</i>
          </el-upload>
          <i v-if="form.homepageBanner" class="el-icon-view" @click="lookImg(form.homepageBanner)"
             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:{
        userType:[],
        expandType:[]
      },
      rules: {

        loginBanner: [
          { required: true, message: "登录页banner不能为空", trigger: "blur" }
        ],
        homepageBanner: [
          { required: true, message: "首页banner不能为空", trigger: "blur" }
        ],
      },
      imgUrl:"",
      imgOpen:false,
    };
  },
  created() {

  },
  mounted(){
    this.getLogoInfo();
  },
  methods: {
    /** 提交按钮 */
    submitForm: function() {
      let self = this;
      this.$refs["form"].validate(valid => {
        if (valid) {
          let obj = {
            loginBanner:this.form.loginBanner,
            homepageBanner:this.form.homepageBanner,
          }

          updateLogoInfo(obj).then(response => {
            this.msgSuccess(response.msg);
            this.getLogoInfo();
          });
        }
      });
    },
    //查看图片
    lookImg(url){
      this.imgUrl = url;
      this.imgOpen = true;
    },
    //获取数据
    getLogoInfo(){
      getLogoInfo().then(response => {
        this.$set(this,'form',response.data)
      });
    },
    //上传
    handleAvatarSuccess(res,type) {
      if(type == 'loginBanner'){
        this.$set(this.form,'loginBanner',res.data.url);
      }else if(type == 'homepageBanner'){
        this.$set(this.form,'homepageBanner',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{
    /*position: absolute;*/
    display: flex;
    right: 20px;
    bottom: 0;
    background: #fff;
    height: 112px;
    text-align: center;
    .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>

