<!--小程序配置-->
<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/jpg,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="fullScreenViewClick(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/jpg,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="fullScreenViewClick(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>
      <div class="form-max-box">
        <el-form-item label="供应商入口：" prop="supplier">
          <el-radio-group v-model="form.supplier">
            <el-radio :label="true">开启</el-radio>
            <el-radio :label="false">关闭</el-radio>
          </el-radio-group>
        </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";

import store from '@/store'
export default {
  name: "publicConfig",
  components: {
    fullScreenView,
  },
  data() {
    return {
      uploadImgUrl: this.uploadUrl(), // 上传的图片服务器地址
      headers: {
        Authorization:getToken(),
      },
      configId:null,
      form:{
        userType:[],
        expandType:[],
        supplier:false,
      },
      rules: {
        loginBanner: [
          { required: true, message: "登录页banner不能为空", trigger: "blur" }
        ],
        homepageBanner: [
          { required: true, message: "首页banner不能为空", trigger: "blur" }
        ],
        supplier: [
          { required: true, message: "请选择供应商入口开启或关闭", 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 obj = {
              id:self.configId,
              configName: "小程序配置",
              category:2,
              configKey:4,
              configValue : JSON.stringify(self.form),
            }
            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: 4 }).then(response => {
        this.$set(this,'configId',response.data.id);
        this.$set(this,'form',JSON.parse(response.data.configValue))
      });
    },
    //上传
    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;
      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{
    /*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;
    }
  }
  .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>

