
(function(KDApi,_){ 
    function MyComponent(model){
        this._setModel(model);
    }

    MyComponent.prototype = {
        _setModel: function(model) {
            this.model = model;
        },
        init:function(props){					
			var model = this.model;			
        },
        update:function(props){
			console.log('-----addFunc',this.model,props);
			addFunc(this.model,props);
        },
        destoryed:function(){
            console.log('-----destoryed',this.model)
        }
    }
	
	var addFunc = function(model, props) {
		let flexKey;
		let multiple = true;//是否支持多选
		let fileType = 'image/*';//默认图片, 上传pdf/ofd   "*ofd, application/pdf"
		let fileSet = {
			fileLimitSize: 3, // 大于3M才压缩
            fileQuality: 0.98, // 清晰度
            fileLimitPixel: 1500, // 原始像素小于该值则不处理
		};//压缩比例
		let url = '';
		let fid;
		if (props != null && props.data != null) {
			let popsData = props.data; 
			flexKey = popsData['flexKey'];
			fid=popsData['fid'];
			url = popsData['uploadUrl'];
			if (popsData['multiple']) {
				multiple = popsData['multiple'];
			}
			if (popsData['fileType']) {
				fileType = popsData['fileType'];
			}
			if (popsData['fileSet']) {
				fileSet = popsData['fileSet'];
			}
		}
		
		KDApi.loadFile(['./style.css', '../common/jquery-min.js', '../common/pwyPolyfill.js', '../common/UploadFiles.min.js', './thirdCameraJsTo.js'], model, () => {
			const opt = {
				multiple, //选择本地文件默认选多张， false： 单张选择
				fileType,  //默认图片, 上传pdf/ofd   "*ofd, application/pdf"     
				fileSet,
				url
			};
			
			console.log('flexKey8888的值',flexKey);
			const thirdCameraJs = new CamareJs();
			if (flexKey) {
				thirdCameraJs.init(opt, flexKey, fileType, (result)=>{
					console.log('result---->>44>--', result);
					const { errcode, uploadFile } = result;
					if (errcode == '0000') {
						const flexName = result.inputObj.target.getAttribute('flexName');
						console.log('告诉后台flexName--',flexName);
						model.invoke('cameraUploadStart', flexName);
						if (uploadFile === 'file') { //需要转为地址
							const { data , inputObj } = result;
							const successArr = [];
							UploadFiles.upload(opt.url, data, {
								uploadData:{"appId":"rim","fId":fid},
								fileLimitSize: 3, // 大于3M才压缩
								fileQuality: 0.98, // 清晰度
								fileLimitPixel: 1500, // 原始像素小于该值则不处理
								stepUploadFinish: function(res) { 
									var file=res.file;  
									successArr.push({"errcode":res.errcode,"status":res.status,"size":file.size,"type":file.type,"name":file.name,"url":res.url});
									console.log('单个文件上传结束', res);
								},
								uploadFinish: function() {					
									inputObj.target.value = '';
									const successDatas = {
										"flex":flexName,
										"fileArray":successArr
									};
									console.log('全部文件上传完成', successDatas);
									model.invoke('cameraUpload', successDatas);							
								}
							});
						} else if (uploadFile === 'url') {
							const { data } = result;
							console.log('全部文件上传完成-2--', data);
							const successDatas = {
								"flex":flexName,
								"fileArray":data
							};
							model.invoke('cameraUpload', successDatas);
						}
					} else {
						console.log('上传失败----', result.description);
					}
				});
			};	
		});
    }
	KDApi.register('cameraUpload', MyComponent);
})(window.KDApi);
