
/**
 * 按期申报填报列表页控制脚本
 * 
 * @author 于广洋
 * @modify 王雷
 */
// -------------------------------

/**
 * 绘制税种填报页面
 * 
 * @param {*} companyInfo 
 */
function initPage(companyInfo) {
    getIEVersion();
    console.log(companyInfo);
    var companyInfo = companyInfo;
    var djxh = companyInfo.djxh;
    var nowdate = ""; // 存储当前选择的时间框;
    var submitData
    /**
     * 展示税种申报信息
     */
    var showTax = function(taxInfo) {
        //时间格式化
        $.each(taxInfo,function(i,item){
            if(item.sssqQ){
                item.sssqQ = tool.formatDate(item.sssqQ);
            }
            if(item.sssqZ){
                item.sssqZ = tool.formatDate(item.sssqZ);
            }
        })
        var id = "#taxGrid";
        var buttonHtml = "填写申报表";
        var order = ["pzzlMc", "sssqQ", "sssqZ", "do"];
        var promptStatement = "";
        var headerList = [{
            name: "序号",
            width: "5%"
        }, {
            name: "申报表",
            width: "65%"
        }, {
            name: "税款所属期起",
            width: "10%"
        }, {
            name: "税款所属期止",
            width: "10%"
        },  {
            name: "操作",
            width: "10%"
        }];
        //渲染表
        getTableDeclaration(id, order, headerList, taxInfo, buttonHtml,promptStatement)
    };
    /**
     * 展示财务报表申报信息
     */
    var showFinancial = function(financialInfo,type) {
        //时间格式化
        $.each(financialInfo,function(i,item){
            if(item.sssqQ){
                item.sssqQ = tool.formatDate(item.sssqQ);
            }
            if(item.sssqZ){
                item.sssqZ = tool.formatDate(item.sssqZ);
            }
        })
        var id = "#financialGrid";
        var buttonHtml = "填写申报表";
        var order = ["kjzd", "pzzlMc", "sssqQ", "sssqZ", "do"];
        var promptStatement="";
        if(financialInfo.length<1&&type==1){
            promptStatement = "您尚未进行财务报表类型备案，请先备案后再进行申报";
        }
        var headerList = [{
            name: "序号",
            width: "5%"
        }, {
            name: "财务、会计制度",
            width: "10%"
        }, {
            name: "财务报表报送小类",
            width: "55%"
        }, {
            name: "报送所属期起",
            width: "10%"
        }, {
            name: "报送所属期止",
            width: "10%"
        }, {
            name: "操作",
            width: "10%"
        }];
        //渲染表
        getTableDeclaration(id, order, headerList, financialInfo, buttonHtml,promptStatement)
    };


    /**
     * 税务申报table渲染
     * @param {*} id 
     * @param {*} order 
     * @param {*} headerList 
     * @param {*} ddt 
     * @param {*} buttonHtml 
     */
    var getTableDeclaration = function(id, order, headerList, ddt, buttonHtml,promptStatement) {
        var tableObj = {
            id: id, //tabel父元素的id
            autoHeight: ddt.length>0?10:"",
            containerWidth: "99%", //table占父元素的百分比
            autoHeight: ddt.length>0?10:"", // 默认10行
            order: order, //数据按照什么顺序排序  1.do为是否保函操作 2.序号列会自动添加不需要写
            onlyId: "uuId", //数据的唯一标识
            height: "320", //表格的高度
            buttonHeight: "", //表格的高度
            buttonHtml: buttonHtml, //按钮里的字
            headerList: headerList, //表头名字及占比，顺序与order对应
            promptStatement:promptStatement, // 没有数据时默认提示
            data: {
                page: "1",
                pageSize: ddt.length
            },
            jsonReady: ddt,
            callback: function(data, node) {
                if (data.sourceFlag=="ssfwpt_sbzj") {
                    window.mlistener = {
                        selectRowData: {},
                    };
                    data.fillingFlag = "inventory" // 填报标记
                    data.saveUserId = "zxtb";// 用户id
                    window.mlistener.selectRowData = data;
                    window.open(config.ssfwptPage,'_blank');
                }else{
                    data.djxh = companyInfo.djxh;
                    data.iszzsybjc = companyInfo.iszzsybjc; // 一表申报代码
                    data.operateType = companyInfo.operateType; //标志位
                    data.nsrsbh = companyInfo.nsrsbh; // 那随人识别号
                    // data.nssbq = nowdate;
                    data.checkTime = false;
                    if(data.pzzlDm === '11602') {
                        var List = companyInfo.taxInfos.filter(function (item) {
                            return item.pzzlDm == '10101' || item.pzzlDm == '10102'
                        })
                        if(List.length === 0) {
                            data.checkTime = true;
                        }
                        for(var i=0;i<List.length;i++){
                            if(List[i].sssqQ !== data.sssqQ || List[i].sssqZ !== data.sssqZ) {
                                data.checkTime = true;
                                break;
                            }
                        }
                    }

                    var str=location.href;
                    str = str.split("/")[4];
                    if(str && "cancellation" == str.substring(0,12)){
                        console.log("============",str)
                        localStorage.setItem("cancellation", str.substring(0,12));
                    }
                    console.log("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
                    console.log("data",data);
                    console.log("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
                    submitData = data;
                    declarationMethod("N")
                }

            //     if(node.context.innerText=="填写申报表"&&companyInfo.iszzsybjc == "01" &&data.pzzlDm == "10101"){
            //    // if(node.context.innerText=="填写申报表"){
            //         $('#myMask #myModal').show();
            //         $('#dName #ybName').html(data.pzzlMc);
            //         $('#dName #ybName').attr("title",data.pzzlMc);//修改title值
            //     }else{
            //         declarationMethod("N")
            //     }
                } //点击按钮后的回调函数
        };
        var object = tool.tableCreate(tableObj);
    };

    /**
     * 税务申报 初始化数据获取
     * @param {*} mydate
     */
    //地址
    //http://sbjs.tjsat.gov.cn:18090
    //http://localhost:7070
    var url;
    if(companyInfo){
        url = companyInfo.config.url;
    }
    var getTaxDeclaration = function(date, type) {
        // TODO 开始加载等待动画
        showLoading();
        var param = {
            djxh: djxh,
            operateType: companyInfo.operateType,
            sbsq: date,
            isFresh: type.toUpperCase() == "Y"
        }
        $.ajax({
            // url: url + "/ssfwpt/declare/getPzzlList",
            url: url + "/ssfwpt/declare/getPzzlList",
            data: param,
            type: "POST",
            dataType: "JSON"
        }).done(function(data) {
            if (data.success) {
                companyInfo = data.bean;
                drawGrid(data.bean);
            } else {
                alertWarning(data.returnMessage);
            }
        }).always(function(data) {
            hideLoading();
        });
    };
    /**
     * 隐藏modal
     */
    var closeModal = function () {
        $('#ybName #dName').html("");
        $('#myMask #myModal').hide();
    }
    var drawGrid = function (bean) {
        if(bean.taxInfos.length==0){
            alertInfo("税费申报暂无数据");
        }
        showTax(bean.taxInfos);
        if(bean.financialInfos.length==0){
            showFinancial(bean.financialInfos,0);
            alertInfo("财务报表报送暂无数据");
        }else{
            if(bean.cwbbbabz=="N"){
                showFinancial([],1);
            }else if(bean.cwbbbabz!="N"){
                showFinancial(bean.financialInfos,0);
            }
        }
        
    };

    /**
     * 日期改变时触发
     * N 非强制刷新
     * @param {*} obj
     * @param {*} dp
     */
    var changeDate = function(obj, dp) {
        date = dp.cal.getNewDateStr();
        nowdate = date; // 给缓存时间赋值
        // 加了一个延时，时间改变后时间框没有收起隐藏就加载数据，没有找到好的解决办法，暂时用延时处理
        setTimeout(function(){
            getTaxDeclaration(date, "N");
        },50);
       
    };

    var refresh = function() {
        myConfirm("消息", "您确定需要重置申报清册吗？点击确定继续", "确定", "取消", function() {
            getTaxDeclaration(nowdate, "Y");
        });
    };
    //申报方式 当iszzsybjc=01同时pzzlDm=010101时，调用改方法
    var declarationMethod = function (statu) {
        submitData.ybjcState = statu;
        if (submitData.sourceFlag=="ssfwpt_sbzj") {
            window.mlistener = {
                selectRowData: {},
            };
            submitData.fillingFlag = "inventory" // 填报标记
            data.saveUserId = "zxtb";// 用户id
            window.mlistener.selectRowData = submitData;
            window.open(config.ssfwptPage,'_blank');
        }else {
            //  var form = $("#changeForm");
            //  form.find("#changeForm_data").val(JSON.stringify(submitData));
            //  form.submit();
            showLoading()
            var falg = "declare";
            $.ajax({
                type: "POST",
                // url: url + "/ssfwpt/declare/toPzzl",
                url: url + "/ssfwpt/declare/toPzzl",
                dataType: "json",  // 请求方式为jsonp
                data: {
                    data: JSON.stringify(submitData)
                },
                success: function (data) {
                    hideLoading()
                    localStorage.setItem("data",JSON.stringify(submitData));
                    console.log("请求成功", data)
                    if (data.url == "edit.jsp") {
                        window.open(config.webUrl+"declare/edit.html?falg="+falg+'&params=' + encodeURI(JSON.stringify(submitData)));
                    }else if (data.url == "yearReport.jsp") {
                        window.open(config.webUrl+"declare/yearReport.html?falg="+falg+'&params=' + encodeURI(JSON.stringify(submitData)));
                    }else if (data.url == "yearReportGLB.jsp") {
                        window.open(config.webUrl+"declare/yearReportGLB.html?falg="+falg+'&params=' + encodeURI(JSON.stringify(submitData)));
                    }else {
                        window.open(config.webUrl+"error/index.html?falg="+falg+'&params=' + encodeURI(JSON.stringify(submitData)));
                    }
                },
                error: function(xhr){
                    hideLoading()
                    alertWarning(data.returnMessage);
                    //jsonp 方式此方法不被触发
                    console.log("=======error======================")
                }
            })
            // closeModal();
        }
    }
    if(companyInfo){
        nowdate = companyInfo.sbsq;
        //var aaa =  {taxInfos:[],financialInfos:[]};
        drawGrid(companyInfo);
    }
    //页面出现滚动条，监听滚动条变化将高赋值给模态框
    window.onscroll= function(){
        $("#myModal").css("top", $(document).scrollTop()+235 );
    }
    return {
      //  declarationMethod: declarationMethod,
      //  closeModal:closeModal
    }
}
