var url = API.API_URL();
mui.init();

var viewApi = mui('#app').view({
	defaultPage: '#setting'
});

//初始化单页的区域滚动
mui('.mui-scroll-wrapper').scroll();

var view = viewApi.view;

(function($, doc) {
	//处理view的后退与webview后退
	var oldBack = $.back;
	$.back = function() {
		if(viewApi.canBack()) { //如果view可以后退，则执行view的后退
			viewApi.back();
		} else { //执行webview后退
			oldBack();
		}
	};
	//监听页面切换事件方案1,通过view元素监听所有页面切换事件，目前提供pageBeforeShow|pageShow|pageBeforeBack|pageBack四种事件(before事件为动画开始前触发)
	//第一个参数为事件名称，第二个参数为事件回调，其中e.detail.page为当前页面的html对象
	view.addEventListener('pageBeforeShow',
		function(e) {
			console.log(e.detail.page.id + ' beforeShow');
		});
	view.addEventListener('pageShow', function(e) {
		console.log(e.detail.page.id + ' show');
	});
	view.addEventListener('pageBeforeBack', function(e) {
		console.log(e.detail.page.id + ' beforeBack');
	});
	view.addEventListener('pageBack', function(e) {
		console.log(e.detail.page.id + ' back');
	});
})(mui);

var did = "";
var insId = "";
mui.plusReady(function() {
	
	var self = plus.webview.currentWebview();
	insId = self.insId;
	rid = self.rid;
	getFormInfo();
	getProcessflow();
	
});

var vm = new Vue({
	el: '#flow_process',
	data: {
		flow_process: []
	}
});

var vm2 = new Vue({
	el: '#receive_info',
	data: {
		receive_infos: {},
		files: {}
	}
});

//流程流转信息
function getProcessflow() {
	var state = app.getState(); //获取登陆信息
	var staffId = state.user.useId;
	mui.ajax(url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		data: {
			'serviceId': 'bpm_2013V0100PHONE007',
			'params': '{staffId:"' + staffId + '",insid:"' + insId + '"}'
		},
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Accept': 'application/json'
		},
		success: function(data) {
			//服务器返回响应
			console.log("流转过程信息:" + JSON.stringify(data));
			if(data && data.returnCode == "0") {
				var returnParams = data.returnParams.list;
				if(returnParams.length > 0) {
					vm.flow_process = vm.flow_process.concat(returnParams);
				}

			} else {
				mui.toast("获取流程信息错误");
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(xhr + "========" + type + "===========" + errorThrown);
		}
	});
};

//获取表单信息
function getFormInfo() {
	//开启等待框
	var w = plus.nativeUI.showWaiting("处理中，请等待...", {
		loading: {
			display: "inline"
		}
	});
	var state = app.getState(); //获取登陆信息
	var staffId = state.user.useId;
	mui.ajax(url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		data: {
			'serviceId': 'oa_Receive_2018V0010PHONE004',
			'params': '{staffId:"' + staffId + '",rid:"' + rid + '"}'
		},
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Accept': 'application/json'
		},
		success: function(data) {
			//服务器返回响应
			if(data && data.returnCode == "0") {
				//关闭等待框
				var t = setInterval(function() {
					w.setTitle("处理中，请等待...");
					w.close();
					clearInterval(t);
				}, 1000);
				var result = data.returnParams.receiveInfo;
				var fileList = data.returnParams.files;
				vm2.files = fileList;
				vm2.receive_infos = result;
			} else {
				mui.toast("获取收文信息错误");
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(xhr + "========" + type + "===========" + errorThrown);
		}
	});
};

//文件下载的任务
function openFile(path) {

	if(path.indexOf("http") == -1) {

		path = API.API_IP_PORT + "" + path;
	}
	var dtask = plus.downloader.createDownload(path, {}, function(d, status) { //创建一个下载任务，可以在网上搜索这个方法了解

		if(status == 200) {
			mui.toast("下载成功!");
			plus.runtime.openFile(d.filename, {}, function(e) { //调用第三方应用打开文件  
				alert(path);
				alert('打开失败');
			});
		} else {
			alert("Download failed: " + status);

		}

	});
	dtask.start();
}