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 tinsId = "";
var insId = "";
mui.plusReady(function() {

	var self = plus.webview.currentWebview();
	tinsId = self.tinsId;
	insId = self.insId;
	modelName = self.modelName;
	var title1 = mui('#title');
	title1[0].innerHTML = modelName;
	console.log("tinsId=====" + tinsId + "insId====" + insId)
	getProcessflow();
	getFormInfo();
});

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

var vm3 = new Vue({
	el: '#form_element',
	data: {
		formInfo: {},
		formElements: []
	}
});

//流程流转信息
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.item3mobileCs = vm.item3mobileCs.concat(returnParams);
				}

				console.log("vm3.formElements********" + vm3.item3mobileCs);

			} 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': 'bpm_2013V0100PHONE006',
			'params': '{staffId:"' + staffId + '",tinsid:"' + tinsId + '",insId:"' + insId + '"}'
		},
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Accept': 'application/json'
		},
		success: function(data) {
			//服务器返回响应
			console.log("表单信息:" + JSON.stringify(data));
			if(data && data.returnCode == "0") {
				//关闭等待框
				var t = setInterval(function() {
					w.setTitle("处理中，请等待...");
					w.close();
					clearInterval(t);
				}, 1000);
				var returnParams = data.returnParams;
				var returnParams = data.returnParams;
				vm3.formInfo = returnParams.formInfo[0];
				console.log("vm3.formInfo********" + vm3.formInfo);
				vm3.formElements = returnParams.formElements;
				console.log("vm3.formElements********" + vm3.formElements);
				
				//填充流程审批人数据			
				var srclist = vm.item3mobileCs;
				var tarlist = vm3.formElements;
				for(var i = 0;  i < srclist.length ; i++){
					for(var j = 0;  j < tarlist.length ; j++){
						if( srclist[i].tmodelName ==  tarlist[j].elementName && srclist[i].state == 2 ){  
							console.log(tarlist[j].defaultValue)
							if(tarlist[j].type != "13" || tarlist[j].defaultValue == ''){		//不是签名   或者   是签名但是为空
								tarlist[j].defaultValue = srclist[i].name;
							}else{
								tarlist[j].canEdit = '2';										//是签名   与且      不为空，使用img显示
							}
						}
					}
				}

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