/* 
* @Author: anchen
* @Date:   2017-05-11 10:10:01
* @Last Modified by:   anchen
* @Last Modified time: 2017-05-11 16:22:34
*/
var type = "";
$(document).ready(function(){
	var contextPath =  $("#contextPath").val();
	var type = 1;
	if($("#sianType").val()==1){
		type=$("#type").val();
		invoice(type);
	}else{
		$("#type").val('1');
		var virtualArchitectureId = $("#virtualArchitectureId").val();
	    $(".js_openmore").click(function(){
	        var _this = $(this)
	        $(".js_openmoreinfo").slideToggle();
	        _this.toggleClass("signinfopensel");
	    });
	    
	    //默认加载普票信息
		$.ajax({
	         type: "POST",
	         url: contextPath+"/wechat/signContract/findSignServiceInvoiceObj",
	         data:{"type":1,"virtualArchitectureId":virtualArchitectureId},
	         dataType: 'json',//返回值类型 一般设置为json
	         success: function(data) {
	        	 var json = data.ssi;
	        	 if(json != '' && json != null){
	    			 if(json.type == '1'){
	    		    		$("#ordinaryRaise").val(json.raise);
	    		    		$("#ordinaryIdentificationNumber").val(json.identificationNumber);
	    		    		$("#signServiceInvoiceId").val(json.id);
	    		    	}
	        	 }
	         }
	    });
	}

    // 签名
    $(document).on('ready', function() {
            var width = $(window).width();
            var height = ($(window).height() - $(".js-headersize").height()) - $(".js-btnsize").height() - 50;
        if ($('.js-signature').length) {
            $('.js-signature').jqSignature({
                width: width,
                height: height,
                lineWidth: 5
            });
        }
    });

    $(".icon-delete").click(function(event) {
        $("#signature").find("img").remove();
        $(".icon-delete").hide();
        setTimeout(function(){$('#signature').addClass("open-popup");}, 300);
    });

    //添加发票信息
    $(".js-invoicein").click(function(){
        // var _html = '<div class="list-block relist-block js_openorderinfo"><ul><li><div class="item-content"><div class="item-inner"><div class="item-title">发票信息</div></div></div><a href="javascript: void(0);" class="delete"><em></em></a></li><li><div class="item-content"><div class="item-inner"><div class="item-title">开票类型</div><div class="item-after">专票</div></div></div></li><li><div class="item-content"><div class="item-inner"><div class="item-title">发票抬头</div><div class="item-after">超级前台有限公司</div></div></div></li></ul></div>'

        // $(".addinfo").html(_html);
    	var signContractId = $("#signContractId").val();
    	var signServiceInvoiceId = $("#signServiceInvoiceId").val();
    	var organizationId = $("#organizationId").val();
    	var virtualArchitectureId = $("#virtualArchitectureId").val();
    	var renewalContractId = $("#renewalContractId").val();
    	var type = $("#type").val();
    	var raise = "";
    	var identificationNumber = "";
    	if(type == '1'){
    		raise = $("#ordinaryRaise").val();
    		identificationNumber = $("#ordinaryIdentificationNumber").val();
    	}else{
    		var address = $("#address").val();
        	identificationNumber = $("#identificationNumber").val();
        	var bank = $("#bank").val();
        	var bankCard = $("#bankCard").val();
        	var userId = $("#userId").val();
    		raise = $("#raise").val();
    		var mobile = $("#mobile").val();
    		if(isPhone(mobile) == false){
    			return;
    		};
        	if(address == null || address == ""){
        		layer.alert("请填详细地址");
        		return;
        	}
        	if(bank == null || bank == ""){
        		layer.alert("请填写开户行");
        		return;
        	}
        	if(bankCard == null || bankCard == ""){
        		layer.alert("请填写开户行卡号");
        		return;
        	}
    	}
    	if(raise == null || raise == ""){
    		layer.alert("请填写发票抬头");
    		return;
    	}
    	if(identificationNumber == null || identificationNumber == ""){
    		layer.alert("请填写纳税人识别号");
    		return;
    	}
    	$.ajax({
             type: "POST",
             url: contextPath+"/wechat/signContract/addSignServiceInvoice",
             data:{"signServiceInvoiceId":signServiceInvoiceId,"signContractId":signContractId,"organizationId":organizationId,"virtualArchitectureId":virtualArchitectureId,"type":type,"raise":raise,"address":address,"identificationNumber":identificationNumber,"mobile":mobile,"bank":bank,"bankCard":bankCard,"userId":userId,"renewalContractId":renewalContractId},
             dataType: 'json',//返回值类型 一般设置为json
             success: function(data) {
            	 alert(data.msg);
            	 if(data.code=='0'){
            		 var sianType=$("#sianType").val();
            		 if(sianType==1){
            			location.href=contextPath+"/wechat/signContract/getVirtualArchitectureBySignContract?userId="+$("#userId").val()+"&virtualArchitectureId="+virtualArchitectureId;
            		 }else{
            		 $("#signServiceInvoiceId").val(data.signServiceInvoiceId);
            		 $.closeModal(".popup-invoicen");
            		 }
            	 }
             }
        });
        
    });

    //添加协议
    $("#savePack").click(function(){
    	var imageUrl = $("#imageUrl").attr("src");
    	var signContractId = $("#signContractId").val();
    	var renewalContractId = $("#renewalContractId").val();
    	if(imageUrl == null || imageUrl == ''){
    		layer.alert("请签名");
    		return;
    	}
    	var userId = $("#userId").val();
    	var organizationId = $("#organizationId").val();
    	var virtualArchitectureId = $("#virtualArchitectureId").val();
    	//上传图片
    	$.ajax({
             type: "POST",
             url: contextPath+"/wechat/signContract/addSigningconfirmed",
             data:{"imageUrl":imageUrl,"signContractId":signContractId,"renewalContractId":renewalContractId},
             dataType: 'json',//返回值类型 一般设置为json
             success: function(data) {
            	 alert(data.msg);
            	 if(data.code == '0'){
            		 var renewalContractId = $("#renewalContractId").val();
            		 if(null != renewalContractId && renewalContractId != ''){
            			 $("#signContractId").val(renewalContractId);
            		 }
            		 window.location.href=contextPath+"/wechat/signContract/mycontract?userId="+userId+"&organizationId="+organizationId+"&virtualArchitectureId="+virtualArchitectureId
            	 }
             }
        });

    });
    
    $('.js-signature').on('jq.signature.changed', function() {
        $('#saveBtn').attr('disabled', false);
    });
    
    //返回
    $("#back").click(function(event) {
    	var organizationId = $("#organizationId").val();
    	var signContractId = $("#signContractId").val();
    	var contextPath = $("#contextPath").val();
    	var virtualArchitectureId = $("#virtualArchitectureId").val();
    	var userId = $("#userId").val();
    	window.location.href=contextPath+"/wechat/signContract/ordersevice?signContractId="+signContractId+"&virtualArchitectureId="+virtualArchitectureId+"&userId="+userId+"&organizationId="+organizationId;
    })
});


function clearCanvas() {
    $('#signature').find("img").remove();
    $('.js-signature').jqSignature('clearCanvas');
    $('#saveBtn').attr('disabled', true);
    $(".icon-delete").hide();
}

function saveSignature() {
    var dataUrl = $('.js-signature').jqSignature('getDataURL');
    var img = $('<img id="imageUrl">').attr('src', dataUrl);
    $('#signature').append(img);
    $.closeModal(".popup-sign");
    $(".icon-delete").show();
    $('#signature').removeClass("open-popup");
}

//发票类型切换
function invoice(type) {
	var contextPath =  $("#contextPath").val();
	var virtualArchitectureId = $("#virtualArchitectureId").val();
	if(type == '1'){
		$("#tab1").show();
        $("#tab2").hide();
        $("#typeTwo").removeClass("active");
        $("#typeOne").attr("class","tab-link active button");
        
        //加载发票信息
    	$.ajax({
             type: "POST",
             url: contextPath+"/wechat/signContract/findSignServiceInvoiceObj",
             data:{"type":type,"virtualArchitectureId":virtualArchitectureId},
             dataType: 'json',//返回值类型 一般设置为json
             success: function(data) {
            	 var json = data.ssi;
            	 if(json != '' && json != null){
            		$("#ordinaryRaise").val(json.raise);
            		$("#ordinaryIdentificationNumber").val(json.identificationNumber);
 		    		$("#signServiceInvoiceId").val(json.id);
 		    		$("#type").val(type);
            	 }else{
            		 $("#signServiceInvoiceId").val('');
            	 }
             }
        });
    	
	}else{
		$("#tab1").hide();
        $("#tab2").show();
        $("#typeOne").removeClass("active");
        $("#typeTwo").attr("class","tab-link active button");
      //默认加载发票信息
    	$.ajax({
             type: "POST",
             url: contextPath+"/wechat/signContract/findSignServiceInvoiceObj",
             data:{"type":type,"virtualArchitectureId":virtualArchitectureId},
             dataType: 'json',//返回值类型 一般设置为json
             success: function(data) {
            	 var json = data.ssi;
            	 if(json != '' && json != null){
		    		$("#signServiceInvoiceId").val(json.id);
		    		$("#raise").val(json.raise);
		    		$("#type").val(json.type);
		    		$("#address").val(json.address);
		        	$("#identificationNumber").val(json.identificationNumber);
		        	$("#bank").val(json.bank);
		        	$("#bankCard").val(json.bankCard);
		        	$("#userId").val(json.userId);
		    		$("#majorRaise").val(json.majorRaise);
		    		$("#mobile").val(json.mobile);
            	 }else{
            		 $("#signServiceInvoiceId").val('');
            	 }
             }
        });
	}
	$("#type").val(type);
}

//验证手机号码
function isPhone(val){
	if(val == null || val == "" ){
		layer.alert("手机号码不能为空！");
		return false;
	}
	/*中移动手机号码*/
	var reg = /^(((13[0-9]{1})|(15[0-9]{1})|17[0-9]{1}|(18[0-9]{1}))+\d{8})$/;
	
    var isMobile=/^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;//电话号码--带区号
    var isMobileq=/^[1-9]{1}[0-9]{5,8}$/;//电话号码--不带区号
    var isMobile48=/^[48]00\d+$/;//400
    
	if(reg.test(val) || isMobile.test(val) || isMobileq.test(val) || isMobile48.test(val)){
		return true;
	}
	layer.alert("手机号码格式不正确");
	return false;
}

