$("#nav_one").click(function () {
    $("#nav_one").css("color", "#000");
    $("#nav_one").css("box-shadow", "0px 3px 0px  rgb(204,204,204)");
    $("#nav_two").css("color", "rgb(170,170,170)");
    $("#nav_two").css("box-shadow", "none");
    $("#firstTab").show();
    $("#secondTab").hide();
});
$("#nav_two").click(function () {
    $("#nav_one").css("color", "rgb(170,170,170)");
    $("#nav_one").css("box-shadow", "none");
    $("#nav_two").css("color", "#000");
    $("#nav_two").css("box-shadow", "0px 3px 0px  rgb(204,204,204)");
    $("#firstTab").hide();
    $("#secondTab").show();
});

//初始化按钮
function initSecondImg(pageInfo,last,next){
    $("#"+last).attr("src",basePath+"/resources/images/CSH/last.png");
    if(pageInfo.nextFlag == true) {
        $("#"+next).attr("src",basePath+"/resources/images/CSH/next.png");
    }
    else{
        $("#"+next).attr("src",basePath+"/resources/images/CSH/nextNot.png");
    }
}
//按钮颜色变化
function lastImgChange(pageInfo,last,next){
    if(pageInfo.lastFlag == false){
        $("#"+last).attr("src",basePath+"/resources/images/CSH/last.png");
    }
    else{
        $("#"+last).attr("src",basePath+"/resources/images/CSH/lastCan.png");
    }
    if(pageInfo.nextFlag == true) {
        $("#"+next).attr("src",basePath+"/resources/images/CSH/next.png");
    }
    else{
        $("#"+next).attr("src",basePath+"/resources/images/CSH/nextNot.png");
    }
}
function nextImgChange(pageInfo,last,next){
    if(pageInfo.lastFlag == false){
        $("#"+last).attr("src",basePath+"/resources/images/CSH/last.png");
    }
    else{
        $("#"+last).attr("src",basePath+"/resources/images/CSH/lastCan.png");
    }
    if(pageInfo.nextFlag == true) {
        $("#"+next).attr("src",basePath+"/resources/images/CSH/next.png");
    }
    else{
        $("#"+next).attr("src",basePath+"/resources/images/CSH/nextNot.png");
    }
}

(function () {
    var spans = document.getElementsByClassName("tab1-span-button");
    var inputs = document.getElementsByClassName("tab1-query-input-1");
    $("#Tab1Query").click(function () {
        var data = setQueryInfo(spans,inputs);
        if(data.totalAmountFrom != ""){
            data.totalAmountFrom = parseFloat(data.totalAmountFrom||0.00);
        }
        if(data.totalAmountTo != ""){
            data.totalAmountTo = parseFloat(data.totalAmountTo||0.00);
        }
        firstTableFunction(data);
    });
})();

//代开票清单 firstTable
var firstPage = new paging();
var checkId = new Array();
function firstTableFunction(data) {
    var url = basePath + '/vat/inf/index/invoice/detail/query';
    $.ajax({
        type: 'POST',
        url: url,
        dataType: "json",
        data: kendo.stringify(data),
        contentType: "application/json; charset=utf-8",
        success: function (datas) {
            firstPage.config({
                id: "firstTable",
                pageSize: 10,
                dataSourse: datas.rows,
                feilds: {
                    "": "",
                    documentNumber: "单据编号",
                    invoiceDate: "开票日期",
                    invoiceKindDesc: "发票种类",
                    bpName: "购货单位/纳税人识别号",
                    bpBankAccount: "开户行及账号",
                    totalAmount: "价税合计/金额/税额"
                },
                //非必需配置
                titleFlag: true,

                lastPageButton: {
                    id: "firstTableLast",
                    callback: function (pageInfo) {
                        lastImgChange(pageInfo, "firstTableLast", "firstTableNext");
                    }
                },
                nextPageButton: {
                    id: "firstTableNext",
                    callback: function (pageInfo) {
                        nextImgChange(pageInfo, "firstTableLast", "firstTableNext");
                    }
                }
            });

            function firstFeild(rowdata, rowIndex) {
                var num = parseInt(rowdata.invoiceHdId);
                checkId.push(num);
                firstPage.modifyField(0, '<input type="checkbox" id="check' + rowIndex + '" class="selfCheckBox" title = "' + num + '" /><label for="check' + rowIndex + '"></label>', 'template', rowIndex);
            }

            firstPage.modifyEvent(0, "template", firstFeild);

            function secondFiled(rowdata, rowIndex) {
                var str = rowdata.invoiceDate || "";
                firstPage.modifyField(2, str.replace(/(\S+)\s(\S+)/, "$1"), "template", rowIndex);
            }

            firstPage.modifyEvent(2, "template", secondFiled);
            function fourFiled(rowdata, rowIndex) {
                firstPage.modifyField(4, '<p style="margin: 0;">' + rowdata.bpName + '</p><p style="margin:0;">' + rowdata.bpTaxRegistryNum + '</p>', "template", rowIndex);
            }

            firstPage.modifyEvent(4, "template", fourFiled);

            function fiveFiled(rowdata, rowIndex) {
                if (rowdata.bpBankAccount == undefined || rowdata.bpBankAccount == null) {
                    rowdata.bpBankAccount = "";
                }
                var accountName = rowdata.bpBankAccount.match(/\D+/) || "";
                var account = rowdata.bpBankAccount.match(/\d+/) || "";
                firstPage.modifyField(5, '<p style="margin: 0;">' + accountName + '</p><p style="margin: 0;">' + account + '</p>', "template", rowIndex);
            }

            firstPage.modifyEvent(5, "template", fiveFiled);

            function sixFiled(rowdata, rowIndex) {
                var str1 = rowdata.totalAmount.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
                var str2 = rowdata.netAmount.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
                var str3 = rowdata.taxAmount.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
                firstPage.modifyField(6, '<p style="margin: 0;">' + str1 + '</p><p style="margin: 0;">' + str2 + '|' + str3 + '</p>', "template", rowIndex);
            }

            firstPage.modifyEvent(6, "template", sixFiled);
        }
    });
}
firstTableFunction({});
//第二个table  secondTabel
var datas2 = {
    'count': '',
    'invoiceKind':'',
    'invoiceTypes': '',
    'invoiceTaxRate': '',
    "invoiceTypesRate":''
};
var secondPage;
function secondTableFunction(data) {
    var url = basePath + '/vat/inf/index/invoice/status/query';
    $.ajax({
        type: 'POST',
        url: url,
        dataType: "json",
        data: kendo.stringify(data),
        contentType: "application/json; charset=utf-8",
        success: function (datas) {
            secondPage = new paging();
            secondPage.config({
                id: "secondTable",
                pageSize: 3,
                dataSourse: datas.rows,
                feilds: {
                    invoiceKindDesc: "发票种类",
                    invoiceNumber: "发票号码",
                    documentNumber: "单据号",
                    totalAmount: "价税合计/金额/税额",
                    bpName:"购货单位/纳税人识别号",
                    totalAmount213: "明细",
                    totalAmount1: "操作"
                },
                //非必需配置
                titleFlag: true,
                lastPageButton: {
                    id: "secondTableLast",
                    callback: function (pageInfo) {
                        lastImgChange(pageInfo, "secondTableLast", "secondTableNext");
                    }
                },
                nextPageButton: {
                    id: "secondTableNext",
                    callback: function (pageInfo) {
                        nextImgChange(pageInfo, "secondTableLast", "secondTableNext");
                    }
                }
            });
            initSecondImg(secondPage.pageInfo,"secondTableLast","secondTableNext");
            function sixFiled(rowdata,rowIndex){
                secondPage.modifyField(4,'<p style="margin: 0;">'+rowdata.bpName+'</p><p style="margin:0;">'+rowdata.bpTaxRegistryNum+'</p>',"template",rowIndex);
            }
            secondPage.modifyEvent(4,"template",sixFiled);

            function fiveFiled(rowdata,rowIndex){
                var str1 = rowdata.totalAmount.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g,"$1,");
                var str2 = rowdata.netAmount.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g,"$1,");
                var str3 = rowdata.taxAmount.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g,"$1,");
                secondPage.modifyField(3,'<p style="margin: 0;">'+str1+'</p><p style="margin: 0;">'+str2+'|'+str3+'</p>',"template",rowIndex);
            }
            secondPage.modifyEvent(3,"template",fiveFiled);

            function sevenFiled(rowdata,rowIndex){
                var src = basePath + "/resources/images/CONT/detail.png";
                secondPage.modifyField(5,'<img onclick="invoiceDetail('+rowdata.invoiceHdId+')" style="display:inline-block;width:15px;height: 15px;cursor:pointer;" src="'+src+'"/>',"template",rowIndex);
            }
            secondPage.modifyEvent(5,"template",sevenFiled);
            function eightFiled(rowdata,rowIndex){
                secondPage.modifyField(6,'<span onclick="reversed('+rowdata.invoiceHdId+')" style="padding:5px;color:white;background: rgb(245,123,105);cursor:pointer;border-radius:5px;">红冲</span>',"template",rowIndex);
            }
            secondPage.modifyEvent(6,"template",eightFiled);
        }
    })
}
secondTableFunction({});

//第三个table  secondTabel
function thirdTableFunction(data) {
    var url = basePath + '/vat/inf/index/invoice/reversed/query';
    var thirdPage = new paging();
    $.ajax({
        type: 'POST',
        url: url,
        dataType: "json",
        data: kendo.stringify(data),
        contentType: "application/json; charset=utf-8",
        success: function (datas) {
            thirdPage.config({
                id: "thirdTable",
                pageSize: 3,
                dataSourse: datas.rows,
                feilds: {
                    invoiceKindDesc: "发票种类",
                    invoiceNumber: "发票号码",
                    documentNumber: "单据号",
                    totalAmount: "价税合计/金额/税额",
                    bpBankAccount: "购货单位/纳税人识别号",
                    cfType: "明细"
                },
                //非必需配置
                titleFlag: true,

                lastPageButton: {
                    id: "thirdTableLast",
                    callback: function (pageInfo) {
                        lastImgChange(pageInfo, "thirdTableLast", "thirdTableNext");
                    }
                },
                nextPageButton: {
                    id: "thirdTableNext",
                    callback: function (pageInfo) {
                        nextImgChange(pageInfo, "thirdTableLast", "thirdTableNext");
                    }
                }
            });
            function threeFiled(rowdata, rowIndex) {
                var str1 = rowdata.totalAmount.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
                var str2 = rowdata.netAmount.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
                var str3 = rowdata.taxAmount.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
                thirdPage.modifyField(3, '<p style="margin: 0;">' + str1 + '</p><p style="margin: 0;">' + str2 + '|' + str3 + '</p>', "template", rowIndex);
            }

            thirdPage.modifyEvent(3, "template", threeFiled);
            function fourFiled(rowdata, rowIndex) {
                thirdPage.modifyField(4, '<p style="margin: 0;">' + rowdata.bpName + '</p><p style="margin:0;">' + rowdata.bpTaxRegistryNum + '</p>', "template", rowIndex);
            }
            thirdPage.modifyEvent(4, "template", fourFiled);
            function sevenFiled(rowdata,rowIndex){
                var src = basePath + "/resources/images/CONT/detail.png";
                thirdPage.modifyField(5,'<img onclick="invoiceDetail('+rowdata.invoiceHdId+')" style="display:inline-block;width:15px;height: 15px;cursor:pointer;" src="'+src+'"/>',"template",rowIndex);
            }
            thirdPage.modifyEvent(5,"template",sevenFiled);
        }
    })
}
thirdTableFunction({});
//得到选中的数据
function getCheckDataId(){
    var flag = document.getElementById("checkAll").checked;
    var length = firstPage.span.length;
    var data = new Array();
    if(flag){
        return checkId;
    }
    else{
        for(var i = 0; i < length; i++) {
            var element = firstPage.span[i][0].getElementsByClassName("selfCheckBox")[0];
            if(element.checked){
                data.push(parseInt(element.title));
            }
        }
        return data;
    }
}

//选择所有的checkbox
$("#checkAll").click(function(){
    var _this = this;
    var length = firstPage.span.length;
    for(var i = 0; i < length; i++) {
        if(this.checked == true) {
            firstPage.span[i][0].getElementsByClassName("selfCheckBox")[0].checked = true;
        }
        else{
            firstPage.span[i][0].getElementsByClassName("selfCheckBox")[0].checked = false;
        }
    }
});
//设置按钮颜色
(function () {
    var spans = document.getElementsByClassName("span-color");
    var spanLength = spans.length;
    for(var i = 0; i < spanLength; i++) {
        spans[i].flag = false;
        spans[i].onclick = function(){
            if(!this.flag){
                this.style.background = "rgb(0,179,179)";
                this.style.color = "white";
            }
            else{
                this.style.background = "white";
                this.style.color = "black";
            }
            this.flag = !this.flag;
        }
    }
})();
//组装查询信息
function setQueryInfo(spans,inputs){
    var spansLength = spans.length;
    var inputsLength = inputs.length;
    var data = {};
    for(var i = 0; i < spansLength; i++) {
        if(spans[i].flag == true){
            if(data[spans[i].title] == undefined){
                data[spans[i].title] = new Array();
                data[spans[i].title].push(spans[i].getAttribute("data-value"));
            }
            else{
                data[spans[i].title].push(spans[i].getAttribute("data-value"));
            }
        }
    }
    for(var j = 0; j < inputsLength; j++){
        data[inputs[j].title]  = inputs[j].value;
    }
    return data;
}
//第一个查询信息
(function () {
    var spans = document.getElementsByClassName("span-button");
    var inputs = document.getElementsByClassName("query-input-1");
    $("#firstQuery").click(function () {
        var data = setQueryInfo(spans,inputs);
        if(data.totalAmountFrom != ""){
            data.totalAmountFrom = parseFloat(data.totalAmountFrom||0.00);
        }
        if(data.totalAmountTo != ""){
            data.totalAmountTo = parseFloat(data.totalAmountTo||0.00);
        }
        secondTableFunction(data);
    });
})();
//第二个查询信息
(function () {
    var spans = document.getElementsByClassName("span-button-1");
    var inputs = document.getElementsByClassName("query-input-2");
    $("#secondQuery").click(function () {
        var data = setQueryInfo(spans,inputs);
        if(data.totalAmountFrom != ""){
            data.totalAmountFrom = parseFloat(data.totalAmountFrom||0.00);
        }
        if(data.totalAmountTo != ""){
            data.totalAmountTo = parseFloat(data.totalAmountTo||0.00);
        }
        thirdTableFunction(data);
    });
})();

//导出excel
/*$("#excel_export").click(function(){
    Hls.exportPageExcel({
        contextPath:basePath,
        param:{invoiceIdParam:getCheckDataId()},
        controller:"/acr/inf/index/excel/export",
        fileName:"产品线",
        pageConfig:[
            {"name":"documentNumber","title":"单据编号","width":260,"align":"","type":"string"},
            {"name":"invoiceDate","title":"开票日期","align":"","type":"string"},
            {"name":"invoiceKindDesc","title":"发票种类","width":160,"align":"center","type":"boolean"},
            {"name":"bpName","title":"购货单位"},
            {"name":"bpTaxRegistryNum","title":"纳税人识别号"},
            {"name":"bpBankAccount","title":"开户行及账号"},
            {"name":"totalAmount","title":"价税合计"},
            {"name":"netAmount","title":"金额"},
            {"name":"taxAmount","title":"税额"}
        ]
    });
});*/
//导出txt
$("#excel_export").click(function(){


    var token  = $('meta[name=_csrf]').attr('content');
    var param = {invoiceIdParam:getCheckDataId()};
    var url = basePath + '/acr/inf/index/excel/export';
    /*$.ajax({
        type: 'POST',
        url: url,
        data:JSON.stringify(param),
        contentType: "application/json; charset=utf-8",
        success: function (datas) {
            kendo.ui.showInfoDialog({
                title:$l('提示'),
                message:"导出成功"
            });
        }
    })*/
    var token  = $('meta[name=_csrf]').attr('content');
    var idStr = "";
    var arr = getCheckDataId();
    for(var i = 0; i < arr.length; i++){
        if(i == arr.length-1){
            idStr = idStr + arr[i];
        }else{
            idStr = arr[i] + ',';
        }
    }
    var $inputImg = $('<input>').attr({name: "paramStr", value: idStr});
    var $inputToken = $('<input>').attr({name: "_csrf", value: token, type: "hidden"});
    var $inputSubmitBtn=$('<input>').attr({type:"submit",value:"submit"});
    var $form = $("<form>");
    $form.attr({
        target: '_self',
        method: 'post',
        action: url
    }).append($inputImg);
    $form.append($inputToken);
    $form.append($inputSubmitBtn);
    $form.id = 'smbForm';
    var parentdiv = $('<div></div>');        //创建一个父div
    parentdiv.appendTo('body');
    parentdiv.hide();
    parentdiv.empty().append($form);//添加到隐藏div中
    $($form).submit();

});

$("#excelImport").click(function(){
    hlscomponent_gridupload();
});

//导入excel
function hlscomponent_gridupload(){
    var dialog = $("#editWin2").kendoWindow({
        width: 	400,
        height: 250,
        modal: true,
        content: basePath+'/vat-inf/VAT510A/vat_inf_excel_import.html',
        visible: false,
        iframe: true,
        close:function(){
        }
    }).data("kendoWindow");
    dialog.title('发票txt导入');
    //hlsWindow(dialog,'HALF','${base.contextPath}/cont/CON100/project_excel_import.html');
    dialog.center().open();
}
//确认信息
function confirmInfo(headId){
    top.openTab('VAT520A','数据回传确认','vat-inf/VAT520A/vat_inf_data_confirm.view?headerId='+headId,false,'',false ,headId,true);
}
//发票详细
function addFunction1(url) {
    var dialog = $("#editWin2").kendoWindow({
        // actions: ["Maximize", "Minimize", "Close"],
        width: 1000,
        height: 500,
        resizable: false,
        title: false,
        visible: false,
        iframe: true,
        modal: false,
        content:url
    }).data("kendoWindow");
    hlsWindow(dialog,"","","","center");
    // dialog.center().open();
}
function invoiceDetail(invoiceHdId){
    addFunction1(basePath + '/vat/VAT230A/vat_sales_invoice_detail.html?invoiceHdId='+invoiceHdId);
}
//进行红冲
function reversed(invoiceHdId){
    var url = basePath + '/vat/inf/invoice/by/id/reversed?invoiceHdId='+invoiceHdId;
    $.ajax({
        type: 'GET',
        url: url,
        contentType: "application/json; charset=utf-8",
        success: function (datas) {
            secondTableFunction({});
            thirdTableFunction({});
        }
    })
}