<#include "/common/defaultEngine.html"/>
<@pageTheme mark="${config.optimize()?string('true', 'false')}">
<@header title="修改用户头像" libs=["cropbox"] style="height: 95%;"/>
    <div class="container"> 
        <div class="imageBox"> 
	        <div class="thumbBox"></div> 
	        <div class="spinner" style="display: none">Loading...</div> 
	   </div> 
	   <div class="action"> 
	       <div class="new-contentarea tc">
	           <a href="javascript:void(0)" class="upload-img">
				   <label for="avatar"><@ctx.i18n text = "选择图片"/></label>
			   </a>
	           <input type="file" class="" name="avatar" id="avatar" accept="image/*"/>
	       </div> 
		   <input type="button" id="btnCrop" class="Btnsty_peyton" value="<@ctx.i18n text = "图片裁剪"/>" />
		   <input type="button" id="btnZoomIn" class="Btnsty_peyton" value="+" />
		   <input type="button" id="btnZoomOut" class="Btnsty_peyton" value="-" />
	   </div>
	   <div class="cropped"></div> 
    </div>
<@footer>
<script type="text/javascript">
	var cropper;
	$(function() {
		var avatar = '${user.avatar?default("")}';
		var options = {
			thumbBox: '.thumbBox',
			spinner: '.spinner',
			imgSrc: opt.common.isEmpty(avatar) ? baseURL + 'statics/img/user2-160x160.jpg' : baseURL + avatar.substr(1)
		};
		cropper = $('.imageBox').cropbox(options);
		$('#avatar').on('change', function() {
			var reader = new FileReader();
			reader.onload = function(e) {
				options.imgSrc = e.target.result;
				/*根据MIME判断上传的文件是不是图片类型*/
				if((options.imgSrc).indexOf("image/")==-1){
					opt.modal.alertWarning("文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。");
				} else {
					cropper = $('.imageBox').cropbox(options);
				}
			};
			reader.readAsDataURL(this.files[0]);
		});

		$('#btnCrop').on('click', function(){
			var img = cropper.getDataURL();
			$('.cropped').html('');
			$('.cropped').append('<img src="'+img+'" align="absmiddle" style="width:64px;margin-top:4px;border-radius:64px;box-shadow:0px 0px 12px #7E7E7E;" ><p>64px*64px</p>');
			$('.cropped').append('<img src="'+img+'" align="absmiddle" style="width:128px;margin-top:4px;border-radius:128px;box-shadow:0px 0px 12px #7E7E7E;"><p>128px*128px</p>');
			$('.cropped').append('<img src="'+img+'" align="absmiddle" style="width:180px;margin-top:4px;border-radius:180px;box-shadow:0px 0px 12px #7E7E7E;"><p>180px*180px</p>');
		});

		$('#btnZoomIn').on('click', function(){
			cropper.zoomIn();
		});

		$('#btnZoomOut').on('click', function(){
			cropper.zoomOut();
		});
	});

	function submitHandler(index, layero) {
		var img = cropper.getBlob();
		var formdata = new FormData();
		formdata.append("avatarfile", img);
		opt.common.sendAjax({
			url: baseURL + "sys/user/profile/updateAvatar",
			data: formdata,
			type: "POST",
			processData: false,
			contentType: false,
			success: function(result) {
				if(result.code == opt.variable.web_status.SUCCESS){
					parent.parent.location.reload();
				}
				opt.operate.saveSuccess(result);
			}
		})
	}
</script>
</@footer>
</@pageTheme>