<!--基础配置-->
<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="schoolName">
          <el-input
            v-model="form.schoolName"
            maxLength="20"
            placeholder="请输入学校名称"
          />
        </el-form-item>
      </div>
      <div class="form-max-box">
        <el-form-item label="圆形Logo：" prop="circularLogo">
          <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,'circularLogo')"
            :headers="headers"
            :before-upload="(file)=>beforeAvatarUpload(file,'1')">
            <img v-if="form.circularLogo" :src="form.circularLogo" class="avatar" style="height:200px;width:200px;background: #dedede;padding:5px;border-radius:4px;">
            <i v-if="!form.circularLogo" class="el-icon-plus avatar-uploader-icon" style="height:80px;width:80px;border-radius: 40px;line-height: 80px;">上传</i>
          </el-upload>
          <i v-if="form.circularLogo" class="el-icon-view" @click="fullScreenViewClick(form.circularLogo)"
             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>
        <p style="margin-left:160px;font-size:16px;color:#666;font-weight:500;">尺寸限制为200*200px，支持PNG格式</p>
      </div>
      <div class="form-max-box">
        <el-form-item label="长方形Logo：" prop="rectangleLogo">
          <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,'rectangleLogo')"
            :headers="headers"
            :before-upload="(file)=>beforeAvatarUpload(file,'2')">
            <img v-if="form.rectangleLogo" :src="form.rectangleLogo" class="avatar" style="height:110px;width:500px;background: #dedede;padding:5px;border-radius:4px;">
            <i v-if="!form.rectangleLogo" class="el-icon-plus avatar-uploader-icon">上传</i>
          </el-upload>
          <i v-if="form.rectangleLogo" class="el-icon-view" @click="fullScreenViewClick(form.rectangleLogo)"
             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>
        <p style="margin-left:160px;font-size:16px;color:#666;font-weight:500;">尺寸限制为110*500px，支持PNG格式</p>
      </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:{
          userType:[],
          expandType:[]
        },
        rules: {
          schoolName: [
            { required: true, message: "学校名称不能为空", trigger: "blur" },
            { required: true, message: "学校名称不能为空", validator: this.spaceJudgment, trigger: "blur" }
          ],
          circularLogo: [
            { required: true, message: "圆形Logo不能为空", trigger: "blur" }
          ],
          rectangleLogo: [
            { required: true, message: "长方形Logo不能为空", trigger: "blur" }
          ],
        },
        fullScreenViewProps:[],
      };
    },
    created() {

    },
    mounted(){
      this.getConfigByType();
    },
    methods: {
      /** 提交按钮 */
      addOrUpdate() {
        let self = this;
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.$confirm('是否确认提交?', "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(function() {
            }).then(() => {
              let configValue = {
                schoolName:self.form.schoolName,
                circularLogo:self.form.circularLogo,
                rectangleLogo:self.form.rectangleLogo,
              }
              let obj = {
                id:self.configId,
                configName: "基础配置",
                category:2,
                configKey:1,
                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: 1 }).then(response => {
          this.$set(this,'configId',response.data.id);
          let obj = JSON.parse(response.data.configValue)
          this.$set(this,'form',obj)
        });
      },
      //上传
      handleAvatarSuccess(res,type) {
        if(type == 'circularLogo'){
          this.$set(this.form,'circularLogo',res.data.url);
        }else if(type == 'rectangleLogo'){
          this.$set(this.form,'rectangleLogo',res.data.url);
        }
        this.$forceUpdate()
      },
      beforeAvatarUpload(file,type) {
        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 = _=>{
                if(type == '1'){
                  if(image.width == 200 && image.height == 200){
                    resolve()
                  }else{
                    this.msgError('尺寸限制为200*200px，支持PNG格式')
                    reject();
                  }
                }else if(type == '2'){
                  if(image.width == 500 && image.height == 110){
                    resolve()
                  }else{
                    this.msgError('尺寸限制为110*500px，支持PNG格式')
                    reject();
                  }
                }
              }
            }
          }else{
            this.msgError('请上传PNG格式');
            reject();
          }
        })
      },
    },
  };
</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>
