mui.plusReady(function() {
	var token = localStorage.getItem('token');
	var self = plus.webview.currentWebview();
	var data_order_id = self.data_order_id;

	mui.ajax(hyhUrl('app/Ordercomplains/getComplainCause'), {
		data: {},
		dataType: 'json', //服务器返回json格式数据  
		type: 'post', //HTTP请求类型  
		timeout: 10000, //超时时间设置为10秒；  
		success: function(data) { //服务器返回响应，根据响应结果，分析是否登录成功；  
			var data = toJson(data, 1);
			if (data.status == 1) {
				data = data.data;
				var html = '<option value="0">选择投诉类型</option>';
				$.each(data, function() {
					html += '<option value="' + this.dataVal + '">' + this.dataName + '</option>';
				})
				$('select').html(html);

			} else {
				mui.alert(data.msg)
				//				//console.log(data.status)
			}
		},
		error: function(xhr, type, errorThrown) { //异常处理；  
			//								mui.alert(errorThrown);      
		}
	});

	var uploader = new plupload.Uploader({
		runtimes: 'html5,flash,silverlight,html4',
		browse_button: 'selectfiles',
		//multi_selection: false,
		//	container: document.getElementById('container'),
		flash_swf_url: '../lib/plupload-2.1.2/js/Moxie.swf',
		silverlight_xap_url: '../lib/plupload-2.1.2/js/Moxie.xap',
		url: 'http://oss.aliyuncs.com',

		filters: {
			mime_types: [ //只允许上传图片和zip,rar文件
				{
					title: "Image files",
					extensions: "jpg,gif,png,bmp"
				}
			],
			max_file_size: '10mb', //最大只能上传10mb的文件
			prevent_duplicates: true //不允许选取重复文件
		},

		init: {
			PostInit: function() {
				document.getElementById('ossfile').innerHTML = '';
				//			document.getElementById('postfiles').onclick = function() {
				//				set_upload_param(uploader, '', false);
				//				return false;
				//			};
				uploader.bind('FilesAdded', function() {
					set_upload_param(uploader, '', false, 'complains');
					return false;
				});
			},

			FilesAdded: function(up, files) {
				plupload.each(files, function(file) {
					document.getElementById('ossfile').innerHTML += '<div class="files_out" id="' + file.id + '"><b></b>' +
						'<div class="progress"><div class="progress-bar" style="width: 60px"></div></div>' +
						'</div>';
				});
			},

			BeforeUpload: function(up, file) {
				check_object_radio();
				set_upload_param(up, file.name, true);
			},

			UploadProgress: function(up, file) {
				var d = document.getElementById(file.id);
				d.getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
				var prog = d.getElementsByTagName('div')[0];
				var progBar = prog.getElementsByTagName('div')[0]
				progBar.style.width = 2 * file.percent + 'px';
				progBar.setAttribute('aria-valuenow', file.percent);
			},

			FileUploaded: function(up, file, info) {
				if (info.status == 200) {
					document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<img data-src="' +
						get_uploaded_object_name(file.name) + '" src="' + hyhImgUrl(get_uploaded_object_name(file.name)) + '" />';
				} else {
					document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = info.response;
				}
			},

			Error: function(up, err) {
				if (err.code == -600) {
					mui.alert("\n选择的文件太大了");
				} else if (err.code == -601) {
					mui.alert("\n选择的文件后缀不对");
				} else if (err.code == -602) {
					mui.alert("\n这个文件已经上传过一遍了");
				} else {
					mui.alert("\nError xml:" + err.response);
				}
			}
		}
	});

	uploader.init();

	$('button').on('tap', function() {
		var complainType = $('select').val();
		var complainContent = $('textarea').val();
		var imagesArr = [];
		$('#ossfile').children('.files_out').children('b').children('img').each(function() {
			imagesArr.push($(this).attr('data-src'))
		})
		var images = imagesArr.join(',');
		if (complainType == 0) {
			mui.alert('请选择投诉原因！');
			return;
		}
		if (complainContent.length < 5) {
			mui.alert('投诉内容不小于5个字！');
			return;
		}
		var that = $(this);
		mui.ajax(hyhUrl('app/Ordercomplains/saveComplain'), {
			data: {
				orderId: data_order_id,
				complainType: complainType,
				complainContent: complainContent,
				complainAnnex: images
			},
			dataType: 'json', //服务器返回json格式数据  
			type: 'post', //HTTP请求类型  
			timeout: 10000, //超时时间设置为10秒；  
			success: function(data) { //服务器返回响应，根据响应结果，分析是否登录成功；  
				var data = toJson(data, 1);
				if (data.status == 1) {
					mui.alert('提交成功!');
					var targetTab = plus.webview.getWebviewById('complain.html');
					mui.fire(targetTab, 'refresh');
					mui.back();
				} else {
					mui.alert(data.msg)
					//				//console.log(data.status)
				}
			},
			error: function(xhr, type, errorThrown) { //异常处理；  
				//								mui.alert(errorThrown);      
			}
		});
	})

})
