/* 
* @Author: anchen
* @Date:   2017-05-03 15:04:23
* @Last Modified by:   anchen
* @Last Modified time: 2017-05-04 17:13:01
*/

$(document).ready(function(){
	var contextPath = $("#contextPath").val();
	var organizationId = $("#organizationId").val();
	var virtualArchitectureId = $("#virtualArchitectureId").val();
	var serviceId = $("#serviceId").val();
	var userId = $("#userId").val();
	var visitPrefix = $("#visitPrefix").val();
	if(serviceId != null && serviceId != ''){
		$(".sevicecontent").show();
	}
    // 点击添加出现服务列表
    $(".opensevice").click(function(event) {
    	var renewalContractId = $("#renewalContractId").val();
    	if(null != renewalContractId && renewalContractId != ""){
    		layer.alert("您现在续约服务，不能添加新服务！");
    		return;
    	}
        if(!$(".modal-overlay").hasClass('modal-overlay-visible')){
            $.popover(".popoveritem", ".opensevice");
            //初始化加载服务项目
        	$.ajax({
        		type:'POST',
        		url:contextPath+'/wechat/signContract/queryServiceList',
        		dataType: 'json',//返回值类型 一般设置为json
        		data:{"userId":userId,"virtualArchitectureId":virtualArchitectureId},
        		success: function (data)  //服务器成功响应处理函数
        		{
        			$("#popover-inner").empty();
        			var subs = data.list;// 获取data中list的数组
        			var service = "";
        			for ( var i = 0; i < subs.length; i++) {//循环列表
        				service +='<a href="'+contextPath+'/wechat/signContract/toOrderSeviceselectitem?serviceId='+subs[i].id+'&organizationId='+organizationId+'&virtualArchitectureId='+virtualArchitectureId+'&code='+subs[i].code+'&userId='+userId+'" external>';
        				service +='<dl class="sevice-item open-popup">';
        				service +='<dt><img src='+visitPrefix+subs[i].serviceIcon+' alt="" /></dt>';
        				service +='<dd>'+subs[i].name+'<span class="goodsnum"></span></dd></dl></a>';
        			}
        			$("#popover-inner").html(service);
        		}
        	});
            
        } else {
            $.closeModal(".popoveritem");
        }
    });
    
    //删除
    $(".delete").click(function(event) {
    	var renewalContractId = $("#renewalContractId").val();
    	layer.confirm('确定删除该服务?', {icon: 3, title:'提示'}, function(index){
    		if(null != renewalContractId && renewalContractId != ""){
        		layer.alert("您现在续约服务，不能删除服务！");
        		return;
        	}
			layer.close(index);
			$("#paymentType").val('');
			$("#serviceId").val('');
			$("#startTime").val('');
			$("#endTime").val('');
			$("#remarks").val('');
			$("#dataIdarr").val('');
			$("#code").val('');
			$("#deposititem").val('');
			$("#deposititemId").val('');
			$(".sevicecontent").hide();
    	});
    });
    
    //返回
    $("#back").click(function(event) {
    	var signContractId = $("#signContractId").val();
    	var organizationId = $("#organizationId").val();
    	var renewalContractId = $("#renewalContractId").val();
    	var virtualArchitectureId = $("#virtualArchitectureId").val();
    	var serviceId = $("#serviceId").val();
    	var userId = $("#userId").val();
    	var paymentType = $("#paymentType").val();
		var startTime = $("#startTime").val();
		var endTime = $("#endTime").val();
		var remarks = $("#remarks").val();
		var dataIdarr = $("#dataIdarr").val();
		var deposititemId = $("#deposititemId").val();
		var deposititem = $("#deposititem").val();
		var code = $("#code").val();
		var typeCode = $("#typeCode").val();
		var name = $("#name").val();
		name = encodeURI(encodeURI(name));
		remarks = encodeURI(encodeURI(remarks));
		var serviceIcon = $("#serviceIcon").val();
		window.location.href=contextPath+'/wechat/signContract/toOrderSeviceselectitem?organizationId='+organizationId+'&virtualArchitectureId='+virtualArchitectureId+"&signContractId="+signContractId+"&userId="+userId+"&name="+name+"&serviceIcon="+serviceIcon+
		"&serviceId="+serviceId+"&paymentType="+paymentType+"&startTime"+startTime+"&endTime="+endTime+"&remarks="+remarks+"&dataIdarr="+dataIdarr+"&deposititemId="+deposititemId+"&deposititem="+deposititem+"&code="+code+"&typeCode="+typeCode+"&renewalContractId="+renewalContractId;
    });
    
    //点击列表返回
    $("#aback").click(function(event) {
    	var signContractId = $("#signContractId").val();
    	var organizationId = $("#organizationId").val();
    	var renewalContractId = $("#renewalContractId").val();
    	var virtualArchitectureId = $("#virtualArchitectureId").val();
    	var serviceId = $("#serviceId").val();
    	var userId = $("#userId").val();
    	var paymentType = $("#paymentType").val();
    	var startTime = $("#startTime").val();
    	var endTime = $("#endTime").val();
    	var remarks = $("#remarks").val();
    	var dataIdarr = $("#dataIdarr").val();
    	var deposititemId = $("#deposititemId").val();
    	var deposititem = $("#deposititem").val();
    	var code = $("#code").val();
    	var typeCode = $("#typeCode").val();
    	var name = $("#name").val();
    	name = encodeURI(encodeURI(name));
    	remarks = encodeURI(encodeURI(remarks));
    	var serviceIcon = $("#serviceIcon").val();
    	window.location.href=contextPath+'/wechat/signContract/toOrderSeviceselectitem?organizationId='+organizationId+'&virtualArchitectureId='+virtualArchitectureId+"&signContractId="+signContractId+"&userId="+userId+"&name="+name+"&serviceIcon="+serviceIcon+
    	"&serviceId="+serviceId+"&paymentType="+paymentType+"&startTime"+startTime+"&endTime="+endTime+"&remarks="+remarks+"&dataIdarr="+dataIdarr+"&deposititemId="+deposititemId+"&deposititem="+deposititem+"&code="+code+"&typeCode="+typeCode+"&renewalContractId="+renewalContractId;
    });
    
    //添加签约信息
    $("#addSignContract").click(function(event) {
    	var renewalContractId = $("#renewalContractId").val();
    	var signContractId = $("#signContractId").val();
    	var organizationId = $("#organizationId").val();
    	var virtualArchitectureId = $("#virtualArchitectureId").val();
    	var serviceId = $("#serviceId").val();
    	var paymentType = $("#paymentType").val();
		var startTime = $("#startTime").val();
		var endTime = $("#endTime").val();
		var remarks = $("#remarks").val();
		var dataIdarr = $("#dataIdarr").val();
		var deposititemId = $("#deposititemId").val();
		var deposititem = $("#deposititem").val();
		var userId = $("#userId").val();
		var code = $("#code").val();
		var typeCode = $("#typeCode").val();
		var renewalContractId = $("#renewalContractId").val();
		
		if(remarks && remarks.length > 200){
			layer.alert("详情长度超过200字符，请重新填写");
			return;
		}
		if(serviceId == null || serviceId == ''){
			layer.alert("请选择服务项");
			return;
		}
		
		var property = new Array();
    	layer.confirm('确定添加该服务?', {icon: 3, title:'提示'}, function(index){
    		layer.close(index);
			dataIdarr = JSON.stringify(dataIdarr);
			deposititem = JSON.stringify(deposititem);
			deposititemId = JSON.stringify(deposititemId);
	    	$.ajax({
	    		type:'POST',
	    		url:contextPath+'/wechat/signContract/addSignContract',
	    		dataType: 'json',//返回值类型 一般设置为json
	    		data:{"organizationId":organizationId,"virtualArchitectureId":virtualArchitectureId,"serviceId":serviceId,"paymentType":paymentType,"startTime":startTime,"endTime":endTime,"remarks":remarks,"dataIdarr":dataIdarr,"deposititem":deposititem,"code":code,"deposititemId":deposititemId,"staffId":userId,"typeCode":typeCode,"signContractId":signContractId,"renewalContractId":renewalContractId},
	    		success: function (data)  //服务器成功响应处理函数
	    		{
	    			alert(data.msg);
	    			if(data.code == '0'){
	    				$("#signContractId").val(data.signContractId);
	    				window.location.href=contextPath+'/wechat/signContract/toSigningconfirmed?serviceId='+serviceId+"&virtualArchitectureId="+virtualArchitectureId+"&signContractId="+data.signContractId+"&userId="+userId+"&organizationId="+organizationId+"&signContractId="+data.signContractId+"&renewalContractId="+renewalContractId;
	    			}
	    		}
	    	});
    	});
    });

});