<template src="./template.html">

</template>

<script>
    import util from "@/utils/util";
    import imgInput from "../img_input.vue";
    import footTemplate from "../footTemplate.vue";
    // import commTemplate from '@/components/commTemplate.vue';

    import { loginService } from "@/services";
    import { Toast } from "mint-ui";
    export default {
        props:{
            switchOn:Boolean,
            perviewOn:Boolean,
            updateData:String,
        },
        components:{
            footTemplate,
        },
        data() {
            var serverUrl = 'http://skyforest.static.elab-plus.com/temp/template24/';
            return {
                uploading:false,
                disabled:false,
                serverUrl:serverUrl,
                isshow:true,
                obj:{
                    text1:'',
                    text2:'',
                    text3:'',
                    text4:'',
                    text5:'',
                    text6:'',
                    text7:'',
                    text8:'',
                    text9:'',
                    text10:'',
                    text11:'',
                    text12:'',
                    text13:'',
                    text14:'',
                    text15:'',
                    text16:'',
                    text17:'',
                    text18:'',
                    text19:'',
                    text20:'',
                    logoUrl:`${serverUrl}LOGO.png`,
                    picUrl1:`${serverUrl}pic1.png`,
                    picUrl2:`${serverUrl}pic2.png`,
                    picUrl3:`${serverUrl}pic3.png`,
                    picUrl4:`${serverUrl}pic4.png`,
                    picUrl5:`${serverUrl}pic5.png`,
                    picUrl6:`${serverUrl}pic6.png`,
                    picUrl7:`${serverUrl}pic7.png`,
                    picUrl8:`${serverUrl}pic8.png`,
                    picUrl9:`${serverUrl}pic9.png`,
                    picUrl10:`${serverUrl}pic10.png`,
                    picUrl11:`${serverUrl}pic11.png`,
                    picUrl12:`${serverUrl}pic12.png`,
                    picUrl13:`${serverUrl}pic13.png`,
                    picUrl14:`${serverUrl}pic14.png`,
                    picUrl15:`${serverUrl}pic15.png`,
                    picUrl16:`${serverUrl}pic16.png`,
                    picUrl17:`${serverUrl}pic17.png`,
                    picUrl18:`${serverUrl}pic18.png`,
                    picUrl19:`${serverUrl}pic19.png`,
                    picUrl20:`${serverUrl}pic20.png`,
                }
            }
        },
        mounted() {
            this.customerId = localStorage.getItem('localCustomerId');
            this.appcodeUrl=localStorage.getItem('_appQrcode');
            this.tel=localStorage.getItem('_projectTel');
            if(this.updateData){
                this.obj =JSON.parse( this.updateData)
            }
        },
        watch:{
            switchOn(val){
                if(val){
                    this.$emit('com',this.obj)
                }
            },
            perviewOn(val){
                this.disabled=val;
                this.isshow=!val;
            },
            updateData(val){
                if(val){
                    val=JSON.parse(val);
                    this.obj = val;
                }
            }
        },
        methods: {
            async doUpload(evt,val){
                console.log(val)
                let files=evt.target.files;
                // if(files.length===0){
                //     Toast('请选择需要上传的图片');
                //     return
                // }
                let file = files[0];
                if(!file){
                    return false;
                }
                if(!file.type.match('image.*')){
                    Toast('请选择图片');
                    return
                }
                if (file.size > 0 && file.size > 5242880) {
                    this.$message.warning('请选小于5M的图片');
                    return;
                }
                this.uploading=true;
                let form = new FormData();
                form.append('type','qiniu_upload');
                form.append('folder','template');
                form.append('uploadFile',file)
                form.append('fileName',file.name)
                let result=await loginService.uploadFile(form);
                console.log(result,'gggggg')
                if(result.data.success){
                    this.uploading=false;
                    // console.log(result);
                    console.log(this.obj[val])
                    this.obj[val]=result.data.single.filepath;
                    console.log(this.obj, this.obj[val])
                    // console.log(this.Imgurl)
                    Toast('图片上传成功')
                }else{
                    Toast('图片上传失败');
                }
            },
            async doUpload2(evt,val){
                let files=evt.target.files;
                if(files.length>3){
                    Toast('上传最多不能超过3张图片哦');
                    return
                }
                let file = files[0];
                if(files.length==0){
                    return false;
                }
                // var imageFile = new Array();
                this.uploading=true;
                this.obj[val]=[];
                for(var i = 0; i<files.length; i++){
                    if(files[i].size > 0 && files[i].size > 5242880){
                        Toast('第'+(i+1)+'张图片请选择小于5M的图片');
                        return
                    }
                    // console.log(files[i].name);
                    let form = new FormData();
                    form.append('type','qiniu_upload');
                    form.append('folder','template');
                    form.append('uploadFile',files[i]);
                    form.append('fileName', files[i].name);
                    let result=await loginService.uploadFile(form);
                    if(result.data.success){
                        this.uploading=false;
                        this.obj[val].push(
                             result.data.single.filepath
                        );

                        console.log(this.obj[val]);
                    }else{
                        Toast('图片上传失败');
                    }
                }
            },
        }
    }
</script>

<style lang="scss" scoped>
  @import "./style.scss"
</style>
