/*-------------------------------文档信息-----------------------------------
///功能：往来税金核算
///创建人: 翟新乐
///创建时间：2011-07-01
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP0414 = "Tp0414";   //表格ID
var objSelRowP0414 = {};        //选择行的数据
var intSelRowIDP0414 = 0;       //选择行的ID
var objWhereJsonP0414 = {};     //记录查询时的条件
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//页面加载方法
function pageLoad_P0414() {
    //选择日期范围
    loadDateExtent_P0414();
    //职员信息
    loadKpData_P0414();    
    //加载数据
    searchDataClick_P0414();
}

//加载grid
function createTable_P0414(strjson) {
    try {
        $("#" + strTableP0414).GridUnload(strTableP0414);
        var objConfig = ParseData(window.external.DoOptions(strTableP0414, "GetTableConfig", "{}"));
        //添加列格式化方法
        for (var i = 0; i < objConfig.colModel.length; i++) {
            if (objConfig.colModel[i].name == "qcsj" || objConfig.colModel[i].name == "xxsj" || objConfig.colModel[i].name == "jxdk"
            || objConfig.colModel[i].name == "sjtz" || objConfig.colModel[i].name == "yjsj" || objConfig.colModel[i].name == "wjsj") {
                objConfig.colModel[i].formatter = formatterP0414Money;
            }
        }
        //创建表格
        var objResult = ParseData(window.external.DoOptions(strTableP0414, "GetList", strjson));
        createGrid(strTableP0414, objConfig.colNames, objConfig.colModel, objResult.Records, null, gridComplete_P0414, true, viewDetail_P041401);
        //表格事件的添加
        addGridEvent_P0414();
        resizeGrid(strTableP0414, true, false);
        resizeGridScroll(strTableP0414);
        objConfig = {};
        objResult = {};
    } catch (e) { }
}

//查询按钮的方法
function searchDataClick_P0414() {
    try {
        //combobox的key不存在，赋值空
        if ($("#cmbKpdwP0414").combobox("getValue") == undefined) {
            $("#cmbKpdwP0414").combobox("setValue", "");
        }
        //查询条件的取得
        var strWhereJson = "";
        strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP0414").val() + "\",";
        strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0414").val() + "\",";
        strWhereJson += "\"kpdwdm\":\"" + $("#cmbKpdwP0414").combobox("getValue") + "\"}";
        //记录查询条件
        objWhereJsonP0414 = ParseData(strWhereJson.replace("}", ",\"dname\":\"" + $("#cmbDateExtentP0414").combobox("getText") + "\",\"kpdwmc\":\"" + $("#cmbKpdwP0414").combobox("getText") + "\"}"));
        createTable_P0414(strWhereJson);
        //清空选择行信息
        intSelRowIDP0414 = "";
        objSelRowP0414 = {};
        strWhereJson = null;
    } catch (e) { }
}

//日期范围修改方法
function dateTimeChange_P0414() {
    if ($("#cmbDateExtentP0414").combobox("getText") != "自定义") {
        $("#cmbDateExtentP0414").combobox("setValue", "");
        $("#cmbDateExtentP0414").combobox("setText", "自定义");
    }
}

//查看明细按钮方法
function viewDetail_P041401() {
    try {
        if (intSelRowIDP0414 > 0) {
            //改变标签的Name 
            $(window.parent.document).find("#span_414").text("往来明细");
            $(window.parent.document).find("#idIframe-41401").attr("dname", objWhereJsonP0414.dname);
            $(window.parent.document).find("#idIframe-41401").attr("sdate", objWhereJsonP0414.sdate);
            $(window.parent.document).find("#idIframe-41401").attr("edate", objWhereJsonP0414.edate);
            $(window.parent.document).find("#idIframe-41401").attr("kpdwdm", objSelRowP0414.kpdwdm ? objSelRowP0414.kpdwdm : objWhereJsonP0414.kpdwdm);
            $(window.parent.document).find("#idIframe-41401").attr("kpdwmc", objSelRowP0414.kpdwdm ? objSelRowP0414.kpdwmc : objWhereJsonP0414.kpdwmc);
            //改变标签Iframe的Src
            $(window.parent.document).find("#idIframe-414").css("display", "none"); //隐藏统计页面
            $(window.parent.document).find("#idIframe-41401").css("display", ""); //显示明页面
            $(window.parent.document).find("#idIframe-41401").attr("src", "04/p041401.htm");
            $(window.parent.document).find("#idIframe-41401").load(function () {
                var iHeight = window.parent.document.documentElement.clientHeight - window.parent._iFrmHeight;
                $(this).height(iHeight);
            });
        }
        else {
            window.parent.cihoAlert(msg.other.selDataRow);
        }
    } catch (e) { }
}


/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**事件方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//选择日期范围
function loadDateExtent_P0414() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0414", "dateEndTimeP0414");

    //添加选择事件
    $("#cmbDateExtentP0414").combobox({
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 82,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP0414", "dateEndTimeP0414");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP0414", "dateEndTimeP0414");
            //searchDataClick_P0414();
        }
    });
    //加载数据
    $("#cmbDateExtentP0414").combobox("loadData", [{ dm: "本月", mc: "本月", selected: true }, { dm: "上一月", mc: "上一月" }, { dm: "过去三个月", mc: "过去三个月" }, { dm: "本年", mc: "本年"}]);
}

//加载开票单位combobox数据
function loadKpData_P0414() {
    $("#cmbKpdwP0414").combobox({
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbKpdwP0414").combobox("getText") == "") {
                $("#cmbKpdwP0414").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbKpdwP0414").combobox("getText") == "全部开票单位") {
                $("#cmbKpdwP0414").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P0414(); 
        }
    });

    //加载数据
    var objDrug = getComboboxData('Tkpdwxx2');
    if (objDrug == "{}") {
        objDrug = [];
    }
    objDrug.unshift({ "dm": "", "mc": "全部开票单位", "selected": true });
    $("#cmbKpdwP0414").combobox("loadData", objDrug);
    objDrug = {};
}

//表格事件的添加
function addGridEvent_P0414() {
    $("#" + strTableP0414).setGridParam({
        onSelectRow: function (rowId) {//当前选择行数据取得
            intSelRowIDP0414 = rowId;
            objSelRowP0414 = $(this).getRowData(rowId);
        },
        gridComplete: function () {
            gridComplete_P0414();
        }
    });

}

//合计
function gridComplete_P0414() {
    //设置合计行的数据
    var objTable = $("#" + strTableP0414);
    var arrIds = objTable.getDataIDs();
    var strQc = 0;
    var intBqxx = 0;
    var intBqjx = 0;
    var intSjtz = 0;
    var intBqyj = 0;
    var intWjsj = 0;
    for (var i = 0; i < arrIds.length; i++) {
        if (!isNaN(parseFloat(objTable.getCell(arrIds[i], "qcsj")))) {
            strQc += parseFloat(objTable.getCell(arrIds[i], "qcsj"));
        }        
        if (!isNaN(parseFloat(objTable.getCell(arrIds[i], "xxsj")))) {
            intBqxx += parseFloat(objTable.getCell(arrIds[i], "xxsj"));
        }        
        if (!isNaN(parseFloat(objTable.getCell(arrIds[i], "jxdk")))) {
            intBqjx += parseFloat(objTable.getCell(arrIds[i], "jxdk"));
        }        
        if (!isNaN(parseFloat(objTable.getCell(arrIds[i], "sjtz")))) {
            intSjtz += parseFloat(objTable.getCell(arrIds[i], "sjtz"));
        }        
        if (!isNaN(parseFloat(objTable.getCell(arrIds[i], "yjsj")))) {
            intBqyj += parseFloat(objTable.getCell(arrIds[i], "yjsj"));
        }        
        if (!isNaN(parseFloat(objTable.getCell(arrIds[i], "wjsj")))) {
            intWjsj += parseFloat(objTable.getCell(arrIds[i], "wjsj"));
        }
    }
    if (arrIds.length == 0) {
        $("#" + strTableP0414).footerData("set", { rn: "合计", qcsj: "&nbsp;", xxsj: "&nbsp;", jxdk: "&nbsp;", sjtz: "&nbsp;", yjsj: "&nbsp;", wjsj: "&nbsp;" }, false);
    }
    else {
        $("#" + strTableP0414).footerData("set", { rn: "合计", qcsj: strQc, xxsj: intBqxx, jxdk: intBqjx, sjtz: intSjtz, yjsj: intBqyj, wjsj: intWjsj }, true);
    }
    objTable = null;
    arrIds = null;
    strQc = null;
    intBqxx = null;
    intBqjx = null;
    intSjtz = null;
    intBqyj = null;
    intWjsj = null;
}

//dj,je格式化money
function formatterP0414Money(cellvalue, options, rowObject) {
    if (!isNaN(parseFloat(cellvalue))) {
        return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return parseFloat(0).toFixed(window.parent.strMoneyPlace);
    }
}

//保存方法
function saveMyData(strDialog, strType, strTable) {
    try {
        if (strTable == "Tp040401") {
            //税金调整保存
            saveData_P040401();
        }
        //添加分类
        else if (strTable == ciho.layal.table) {
            ciho.saveType();
        }
    } catch (e) { }
}

//关闭
function closeMyDlg(strDialog, strType, strTableName) {
    if ($("#" + strDialog + "_divform").data("change")) {
        window.parent.cihoAlert(
                msg.other.haveChange,
                 msgBox.dlgBtn.YesNoCancel,
                 function (strRet) {
                     if (msgBox.dlgRst.Yes == strRet) {
                         //closeMySelf("divMessage", '');
                         saveMyData(strDialog, strType, strTableName);
                     }
                     else if (msgBox.dlgRst.No == strRet) {
                         //closeMySelf("divMessage", strDialog);
                         cihoDlgClose(strDialog);
                     }
                 });
    }
    else {//0：关闭。关闭编辑窗口
        //$("#" + strDialog).dialog("close");
        cihoDlgClose(strDialog);
    }
}
///*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**自定义方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/