$(function () {
    $("#tabThesis").css('display','none');

    $("#other_subject_id").change(function () {
       var other_subject_val =  $("#other_subject_id").find("option:selected").val();
       if("1" == other_subject_val){
           $("#tabThesis").css('display','');
       }
       if("2" == other_subject_val){
           $("#tabThesis").css('display','none');
       }
       if("" == other_subject_val){
            $("#tabThesis").css('display','none');
        }
    });
    
    $("#appreciationTaxTypeId").change(function () {
        var appreciationTaxType = $("#appreciationTaxTypeId").find("option:selected").val();
        if ("1" == appreciationTaxType) {
            $("#dizhi_id").css("display", "none");
            $("#dianhua_id").css("display", "none");
            $("#kaihuhang_id").css("display", "none");
            $("#zhanghao_id").css("display", "none");
            $(".xuantian").css("display", "");
            $(".bitian").css("display", "none");
            $(".xuantian").css("display", "");
            $(".bitian").css("display", "none");
            $("#addressNameId01").removeAttr("class");
            $("#telephoneId01").removeAttr("class");
            $("#bankAddressId01").removeAttr("class");
            $("#bankAccountId01").removeAttr("class");
            $("#companyNameId01").val($("#invoiceTitle_id").text());
            $("#taxpayerNumId01").val($("#taxpayerNum_id").text());
            $("#addressNameId01").val($("#addressName_id").text());
            $("#telephoneId01").val($("#telephone_id").text());
            $("#bankAddressId01").val($("#bankAddress_id").text());
            $("#bankAccountId01").val($("#bankAccount_id").text());
        } else if ("2" == appreciationTaxType) {
            $("#dizhi_id").css("display", "");
            $("#dianhua_id").css("display", "");
            $("#kaihuhang_id").css("display", "");
            $("#zhanghao_id").css("display", "");
            $(".xuantian").css("display", "none");
            $(".bitian").css("display", "");
            $(".xuantian").css("display", "none");
            $(".bitian").css("display", "");
            $("#addressNameId01").attr("class", "required");
            $("#telephoneId01").attr("class", "required");
            $("#bankAddressId01").attr("class", "required");
            $("#bankAccountId01").attr("class", "required");
			$("#companyNameId01").val($("#invoiceTitle_id").text());
			$("#taxpayerNumId01").val($("#taxpayerNum_id").text());
			$("#addressNameId01").val($("#addressName_id").text());
			$("#telephoneId01").val($("#telephone_id").text());
			$("#bankAddressId01").val($("#bankAddress_id").text());
			$("#bankAccountId01").val($("#bankAccount_id").text());
        }
    });

    var obj_id = $("#registerInfo_id").val();
    $("#saveCertificateId").click(function () {
        $.post('/training/saveCertificateId',
            {
                img1: $("#img1").attr("src"),
                obj_id: obj_id
            },
            function (data) {
                if (data.result) {
                    alert("保存证件照成功！");
                } else {
                    alert("保存证件照失败！");
                }
            });
    });

    $("#submit").on("click", function () {
        $(".message").html();
        if (checkRequired()) {
            var sn = $("input[name='subject']:checked")[0].nextSibling.nodeValue;
            var subject = $("input[name='subject']:checked")[0].value;
            var registerInfo_id = $("#registerInfo_id").val();
            $.ajax({
                url: '/training/checkSubject',
                type: 'get',
                dataType: 'json',
                data: {
                    registerInfo_id: registerInfo_id,
                    subject: subject,
                    sn : sn
                },
                success:function (data){
                    if(data.success){
                        if (confirm("您报名的科目是“" + sn + "”，请牢记您的用户名和密码，报名成功后可凭此登陆查看缴费等状态，信息提交之后不可再更改，确认要提交吗？")) {
                            $("#form").ajaxSubmit({
                                url: "/training/reg3?registerInfo_id=" + registerInfo_id,
                                type: "post",
                                dataType: "json",
                                success: function (data) {
                                    if (data.success) {
                                        location.href = "/training/infop";
                                    } else {
                                        $(".message").html(data.info);
                                    }
                                }
                            });
                        }
                        alert("报名成功！");
                    }else {
                        var title = '<h3>报名提示</h3>'
                        var html = '<span style="font-size: 16px; color: red;text-align: center">&emsp;&emsp;' + data.info + '</span>';
                        $(".bm-title").append(title);
                        $(".bm-message").append(html);
                        syalert.syopen('alert3')
                    }
                }
            })

        }

    });
    $.get('/training/getObj?obj_id=' + obj_id, function (data) {
        $("#img1").attr('src',data.obj.img1);
    });

})

function lookInvoice(invoiceTitle,taxpayerNum,addressName,telephone,bankAddress,bankAccount,taxIssue,invoiceInfo,appreciationTaxType,subject) {
    var title = '<h3>'+ changeTitle(subject) +'</h3>'
    var html = '<table id="in_voice" ><tr><td class="td_left">增值税发票：</td><td class="td_right">' + taxType(appreciationTaxType) + '</td></tr>'
    html += '<tr><td class="td_left" >发票内容：</td><td class="td_right">' + invoiceInfo + '</td></tr>'
    html += '<tr><td class="td_left">发票抬头：</td><td class="td_right">' + invoiceTitle + '</td></tr>'
    html += '<tr><td class="td_left">纳税人识别号：</td><td class="td_right">' + taxpayerNum + '</td></tr>'
    html += '<tr><td class="td_left">公司地址：</td><td class="td_right">' + addressName + '</td></tr>'
    html += '<tr><td class="td_left">公司电话：</td><td class="td_right">' + telephone + '</td></tr>'
    html += '<tr><td class="td_left">开户行：</td><td class="td_right">' + bankAddress + '</td></tr>'
    html += '<tr><td class="td_left">银行账号：</td><td class="td_right">' + bankAccount + '</td></tr>'
    html += '<tr><td class="td_left">发票开具时间：</td><td class="td_right">' + ch(taxIssue) + '</td></tr></table>'
    $(".sy-title").append(title);
    $(".invoiceWin").append(html);
    syalert.syopen('alert2')

}

function ok(id){
    syalert.syhide(id);
    $(".sy-title").empty();
    $(".invoiceWin").empty();
    $(".bm-title").empty();
    $(".bm-message").empty();
}



function ch(val){
    if(val == 1){
        return "款项到账当年开具";
    }
    if(val == 2){
        return "款项到账次年开具";
    }
}

function taxType(val){
    if(val == 1){
        return "增值税普通发票";
    }
    if(val == 2){
        return "增值税专用发票";
    }
}

function changeTitle(val){
    if(val == 0){
        return "跟单信用证专家（CDCS）首次报名发票信息";
    }
    if(val == 1){
        return "保函与备用证专家（CSDG）首次报名发票信息";
    }
    if(val == 2){
        return "国际贸易金融专家（CITF）首次报名发票信息";
    }
    if(val == 3){
        return "跟单信用证专家（CDCS）补考报名发票信息";
    }
    if(val == 4){
        return "保函与备用证专家（CSDG）补考报名发票信息";
    }
    if(val == 5){
        return "国际贸易金融专家（CITF）补考报名发票信息";
    }
    if(val == 6){
        return "国际支付专家（CertPAY）首次报名发票信息";
    }
    if(val == 7){
        return "贸易金融合规专家（CTFC）首次报名发票信息";
    }
    if(val == 8){
        return "供应链金融专家(CSCF)首次报名发票信息";
    }
    if(val == 9){
        return "国际支付专家（CertPAY）补考报名发票信息";
    }
    if(val == 10){
        return "贸易金融合规专家（CTFC）补考报名发票信息";
    }
    if(val == 11){
        return "供应链金融专家(CSCF)补考报名发票信息";
    }
}

function checkRequired() {
    var success = true;
    $(".required").each(function (n, v) {
        if ($.trim($(this).val()) == '') {
            showWarning($(this), "请填写此信息");
            success = false;
        }
    });
    // $("[name=taxpayerNum]").on("blur",function (){
    //     var code = $(this).val();
    //     if(!checkSocialCreditCode(code)){
    //         showWarning($(this),"不是有效的纳税人识别号！");
    //         success = false;
    //     }
    // });
    return success;
}

function showWarning(obj, text) {
    $(obj).parent().parent().find("span.reg_body").html("<b style='color:red'>" + text + "</b>");
}