/*-------------------------------文档信息-----------------------------------
///功能：应收查询
///创建人: 翟新乐
///创建时间：------------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP0215 = "Tp0215";   //表格ID
var objSelRowP0215 = {};        //选择行的数据
var intSelRowIDP0215 = 0;       //选择行的ID
var objWhereJsonP0215 = {};     //记录查询时的条件
var objYsgcModel;           //要展开的列
var sortfieldP0215 = "";    //排序字段
var orderbyP0215 = "";      //排序顺序
var strRecordsP0215 = "";
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//页面加载方法
function pageLoad_P0215() {
    //选择日期范围
    loadDateExtent_P0215();
    //加载客户信息
    loadProviderData_P0215();
    //药品下拉框信息的加载
    loadDrugData_P0215();
    //职员信息
    loadZyData_P0215();
    //加载数据
    searchDataClick_P0215();
}

function searchDataClick_P0215(strFlag) {
    try {
        var objConfig = {};
        var objResult = {};
        //combobox的key不存在，赋值空
        if (!$("#cmbCustomerP0215").combobox("getValue")) {
            $("#cmbCustomerP0215").combobox("setValue", "");
        }
        if (!$("#cmbDrugP0215").combobox("getValue")) {
            $("#cmbDrugP0215").combobox("setValue", "");
        }
        if (!$("#cmbZyP0215").combobox("getValue")) {
            $("#cmbZyP0215").combobox("setValue", "");
        }
        //清空选择行信息
        intSelRowIDP0215 = "";
        objSelRowP0215 = {};
        //查询条件的取得
        var strWhereJson = "";
        if (strFlag != "1") {
            strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP0215").val() + "\",";
            strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0215").val() + "\",";
            strWhereJson += "\"khdm\":\"" + $("#cmbCustomerP0215").combobox("getValue") + "\",";
            strWhereJson += "\"spdm\":\"" + $("#cmbDrugP0215").combobox("getValue") + "\",";
            strWhereJson += "\"zydm\":\"" + $("#cmbZyP0215").combobox("getValue") + "\"}";
//            strWhereJson += "\"sortfield\":\"" + sortfieldP0215 + "\",";
//            strWhereJson += "\"orderby\":\"" + orderbyP0215 + "\"}";
            strRecordsP0215 = strWhereJson;
            //记录查询条件
            objWhereJsonP0215 = ParseData(strWhereJson.replace("}", ",\"dname\":\"" + $("#cmbDateExtentP0215").combobox("getText") + "\",\"khmc\":\"" + $("#cmbCustomerP0215").combobox("getText") + "\"}"));
        }
        objConfig = ParseData(window.external.DoOptions(strTableP0215, "GetTableConfig", "{}"));
        for (var i = 0; i < objConfig.colModel.length; i++) {
            if (objConfig.colModel[i].name == "qcys" || objConfig.colModel[i].name == "xs" || objConfig.colModel[i].name == "th"
            || objConfig.colModel[i].name == "yh" || objConfig.colModel[i].name == "hkje" || objConfig.colModel[i].name == "yszj"
            || objConfig.colModel[i].name == "ysjs" || objConfig.colModel[i].name == "ysye") {
                objConfig.colModel[i].formatter = formatterP0215Money;
            }
            if (objConfig.colModel[i].name == "xh") {
                objConfig.colModel[i].hidden = true;
            }
        }
        var strTreeJson = strRecordsP0215.replace("}", ",\"sortfield\":\"" + sortfieldP0215 + "\",\"orderby\":\"" + orderbyP0215 + "\"}");
        objResult = ParseData(window.external.DoOptions(strTableP0215, "GetList", strTreeJson));
        if (strFlag == "0" || strFlag == "1"){
            setModel();
            loadTreeData(strTableP0215, objResult.Records);
        }
        else {
            //创建表格
            createTreeGrid_P0215(strTableP0215, objConfig.colNames, objConfig.colModel, [], "khmc", viewDetail_P021503);

            resizeGrid(strTableP0215, true, false);
            setModel();
            //表格事件的添加
            addGridEvent_P0215(strTableP0215);
            loadTreeData(strTableP0215, objResult.Records);
        }
        resizeGridScroll(strTableP0215);
        objResult = {};
        objConfig = {};
        strWhereJson = "";
        sortfieldP0215 = "";
        orderbyP0215 = "";
    } catch (e) { }
}

//树Grid
function createTreeGrid_P0215(strTableID, objColNames, objColModel, objJsonDatas, strColExpandKey, funcDbClick) {
    //当数据集为空时，格式化数据集的空值
    if (objJsonDatas == "{}") {
        objJsonDatas = [];
    }
    //创建表格
    $("#" + strTableID).jqGrid({
        datatype: "local",
        colNames: objColNames,
        colModel: objColModel,
        data: objJsonDatas[0],
        treeGrid: true,
        treedatatype: "local",
        treeGridModel: 'adjacency',
        ExpandColumn: strColExpandKey,
        ExpandColClick: true,
        editable: true,
        footerrow: true,
        rownumbers: false,
        rowNum: 999999,
        sortname: "khdm",
        localReader: { id: "bh" },
        autowidth: true,
        //height: "100%",
        ondblClickRow: function (selRowId) {
            if (funcDbClick) {
                funcDbClick.call(this, selRowId);
            }
        }
    }); //.parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");
}

//显示隐藏列
function setModel() {
    try {
        var isHidden = $("#ckbGc").attr("checked");
        if (isHidden == true) {
            $("#" + strTableP0215).jqGrid("showCol", "qcys");
            $("#" + strTableP0215).jqGrid("showCol", "xs");
            $("#" + strTableP0215).jqGrid("showCol", "th");
            $("#" + strTableP0215).jqGrid("showCol", "yh");
            $("#" + strTableP0215).jqGrid("showCol", "hkje");
            $("#" + strTableP0215).jqGrid("showCol", "yszj");
            $("#" + strTableP0215).jqGrid("showCol", "ysjs");
        }
        else {
            $("#" + strTableP0215).jqGrid("hideCol", "qcys");
            $("#" + strTableP0215).jqGrid("hideCol", "xs");
            $("#" + strTableP0215).jqGrid("hideCol", "th");
            $("#" + strTableP0215).jqGrid("hideCol", "yh");
            $("#" + strTableP0215).jqGrid("hideCol", "hkje");
            $("#" + strTableP0215).jqGrid("hideCol", "yszj");
            $("#" + strTableP0215).jqGrid("hideCol", "ysjs");
        }
        isHidden = null;
    } catch (e) { }
}

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

//查看销售员分布方法
function viewDetail_P021501() {
    try {
        if (intSelRowIDP0215 > 0) {
            //改变标签的Name 
            $(window.parent.document).find("#span_215").text("销售员应收分布");
            $(window.parent.document).find("#idIframe-21501").attr("sdate", objWhereJsonP0215.sdate);
            $(window.parent.document).find("#idIframe-21501").attr("edate", objWhereJsonP0215.edate);
            //客户代码
            $(window.parent.document).find("#idIframe-21501").attr("khdm", objSelRowP0215.khdm ? objSelRowP0215.khdm : objWhereJsonP0215.khdm);
            $(window.parent.document).find("#idIframe-21501").attr("khmc", objSelRowP0215.khdm ? objSelRowP0215.khmc : objWhereJsonP0215.khmc);
            //药品代码
            $(window.parent.document).find("#idIframe-21501").attr("spdm", objSelRowP0215.spdm ? objSelRowP0215.spdm : objWhereJsonP0215.spdm);
            $(window.parent.document).find("#idIframe-21501").attr("zydm", objSelRowP0215.zydm ? objSelRowP0215.zydm : objWhereJsonP0215.zydm);
            //改变标签Iframe的Src
            $(window.parent.document).find("#idIframe-215").css("display", "none"); //隐藏统计页面
            $(window.parent.document).find("#idIframe-21501").css("display", ""); //显示明页面
            $(window.parent.document).find("#idIframe-21501").attr("src", "02/p021501.htm");
        }
        else {
            window.parent.cihoAlert(msg.other.selDataRow);
        }
    } catch (e) { }
}

//查看药品分布方法
function viewDetail_P021502() {
    try {
        if (intSelRowIDP0215 > 0) {
            //改变标签的Name 
            $(window.parent.document).find("#span_215").text("药品应收分布");
            $(window.parent.document).find("#idIframe-21502").attr("sdate", objWhereJsonP0215.sdate);
            $(window.parent.document).find("#idIframe-21502").attr("edate", objWhereJsonP0215.edate);
            //客户代码
            $(window.parent.document).find("#idIframe-21502").attr("khdm", objSelRowP0215.khdm ? objSelRowP0215.khdm : objWhereJsonP0215.khdm);
            $(window.parent.document).find("#idIframe-21502").attr("khmc", objSelRowP0215.khdm ? objSelRowP0215.khmc : objWhereJsonP0215.khmc);
            //药品代码
            $(window.parent.document).find("#idIframe-21502").attr("spdm", objSelRowP0215.spdm ? objSelRowP0215.spdm : objWhereJsonP0215.spdm);
            $(window.parent.document).find("#idIframe-21502").attr("zydm", objSelRowP0215.zydm ? objSelRowP0215.zydm : objWhereJsonP0215.zydm);

            //改变标签Iframe的Src
            $(window.parent.document).find("#idIframe-215").css("display", "none"); //隐藏统计页面
            $(window.parent.document).find("#idIframe-21502").css("display", ""); //显示明页面
            $(window.parent.document).find("#idIframe-21502").attr("src", "02/p021502.htm");
        }
        else {
            window.parent.cihoAlert(msg.other.selDataRow);
        }
    } catch (e) { }
}

//查看对账明细方法
function viewDetail_P021503() {
    try {
        if (intSelRowIDP0215 > 0) {
            //应收往来对账 
            $(window.parent.document).find("#span_215").text("应收往来对账");
            //选择的日期
            $(window.parent.document).find("#idIframe-21503").attr("dname", objWhereJsonP0215.dname);
            $(window.parent.document).find("#idIframe-21503").attr("sdate", objWhereJsonP0215.sdate);
            $(window.parent.document).find("#idIframe-21503").attr("edate", objWhereJsonP0215.edate);
            //客户代码
            $(window.parent.document).find("#idIframe-21503").attr("khdm", objSelRowP0215.khdm ? objSelRowP0215.khdm : objWhereJsonP0215.khdm);
            $(window.parent.document).find("#idIframe-21503").attr("khmc", objSelRowP0215.khdm ? objSelRowP0215.khmc : objWhereJsonP0215.khmc);
            //药品代码
            $(window.parent.document).find("#idIframe-21503").attr("spdm", objSelRowP0215.spdm ? objSelRowP0215.spdm : objWhereJsonP0215.spdm);
            $(window.parent.document).find("#idIframe-21503").attr("zydm", objSelRowP0215.zydm ? objSelRowP0215.zydm : objWhereJsonP0215.zydm);
            //改变标签Iframe的Src
            $(window.parent.document).find("#idIframe-215").css("display", "none"); //隐藏统计页面
            $(window.parent.document).find("#idIframe-21503").css("display", ""); //显示明页面
            $(window.parent.document).find("#idIframe-21503").attr("src", "02/p021503.htm");
            $(window.parent.document).find("#idIframe-21503").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_P0215() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0215", "dateEndTimeP0215");

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

//客户下拉框信息的加载
function loadProviderData_P0215() {
    $("#cmbCustomerP0215").combobox({
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbCustomerP0215").combobox("getText") == "") {
                $("#cmbCustomerP0215").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbCustomerP0215").combobox("getText") == "全部客户") {
                $("#cmbCustomerP0215").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P0215();
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });

    //加载数据
    var objProvider = getComboboxData('Tkhxx2');
    if (objProvider == "{}") {
        objProvider = [];
    }
    objProvider.unshift({ "dm": "", "mc": "全部客户", "selected": true });
    $("#cmbCustomerP0215").combobox("loadData", objProvider);
    objProvider = {};
}

//药品下拉框信息的加载
function loadDrugData_P0215() {
    $("#cmbDrugP0215").combobox({
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbDrugP0215").combobox("getText") == "") {
                $("#cmbDrugP0215").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbDrugP0215").combobox("getText") == "全部药品") {
                $("#cmbDrugP0215").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P0215(); 
        }
    });

    //加载数据
    var objDrug = getComboboxData('Tspxx2');
    if (objDrug == "{}") {
        objDrug = [];
    }
    objDrug.unshift({ "dm": "", "mc": "全部药品", "selected": true });
    $("#cmbDrugP0215").combobox("loadData", objDrug);
    objDrug = {};
}

//加载销售员信息
function loadZyData_P0215() {
    $("#cmbZyP0215").combobox({
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbZyP0215").combobox("getText") == "") {
                $("#cmbZyP0215").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbZyP0215").combobox("getText") == "全部销售员") {
                $("#cmbZyP0215").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P0215(); 
        }
    });
    //加载数据
    var objDrug = getComboboxData('TZyxx2');
    if (objDrug == "{}") {
        objDrug = [];
    }
    objDrug.unshift({ "dm": "", "mc": "全部销售员", "selected": true });
    objDrug.push({ "dm": "-1", "mc": "其他销售员" });
    $("#cmbZyP0215").combobox("loadData", objDrug);
    objDrug = {};
}

//表格事件的添加
function addGridEvent_P0215(strTableID) {
    $("#" + strTableID).setGridParam({
        onSelectRow: function (rowId) {//当前选择行数据取得
            intSelRowIDP0215 = rowId;
            objSelRowP0215 = $(this).getRowData(rowId);
            resizeGridScroll(strTableID);
        },
        gridComplete: function () {
            gridComplete_P0215();
        },
        onSortCol: function (index, colindex, sortorder) {
            sortfieldP0215 = index;
            orderbyP0215 = sortorder;
            //loadData_P0215();
            searchDataClick_P0215("1");
            return "stop";
        }
    });

}

//合计
function gridComplete_P0215() {
    //设置合计行的数据
    //隐藏合计行中不显示的列
    if ($("#ckbGc").attr("checked") == false) {
        $("table.ui-jqgrid-ftable td:eq(3)", $("#gbox_" + strTableP0215)).hide();
        $("table.ui-jqgrid-ftable td:eq(4)", $("#gbox_" + strTableP0215)).hide();
        $("table.ui-jqgrid-ftable td:eq(5)", $("#gbox_" + strTableP0215)).hide();
        $("table.ui-jqgrid-ftable td:eq(6)", $("#gbox_" + strTableP0215)).hide();
        $("table.ui-jqgrid-ftable td:eq(7)", $("#gbox_" + strTableP0215)).hide();
        $("table.ui-jqgrid-ftable td:eq(8)", $("#gbox_" + strTableP0215)).hide();
        $("table.ui-jqgrid-ftable td:eq(9)", $("#gbox_" + strTableP0215)).hide();
        $("table.ui-jqgrid-ftable td:eq(10)", $("#gbox_" + strTableP0215)).hide();
        $("table.ui-jqgrid-ftable td:eq(2)", $("#gbox_" + strTableP0215)).attr("colspan", "8");
    }
    else {
        $("table.ui-jqgrid-ftable td:eq(3)", $("#gbox_" + strTableP0215)).hide();
        $("table.ui-jqgrid-ftable td:eq(4)", $("#gbox_" + strTableP0215)).show();
        $("table.ui-jqgrid-ftable td:eq(5)", $("#gbox_" + strTableP0215)).show();
        $("table.ui-jqgrid-ftable td:eq(6)", $("#gbox_" + strTableP0215)).show();
        $("table.ui-jqgrid-ftable td:eq(7)", $("#gbox_" + strTableP0215)).show();
        $("table.ui-jqgrid-ftable td:eq(8)", $("#gbox_" + strTableP0215)).show();
        $("table.ui-jqgrid-ftable td:eq(9)", $("#gbox_" + strTableP0215)).show();
        $("table.ui-jqgrid-ftable td:eq(10)", $("#gbox_" + strTableP0215)).show();
        $("table.ui-jqgrid-ftable td:eq(2)", $("#gbox_" + strTableP0215)).show();
    }
    var objTable = $("#" + strTableP0215);
    var arrIds = objTable.getDataIDs();
    var strTotal = 0;
    for (var i = 0; i < arrIds.length; i++) {
        if (objTable.getCell(arrIds[i], "isLeaf") == "true") {
            strTotal += parseFloat(objTable.getCell(arrIds[i], "ysye"));
        }
    }
    if (arrIds.length == 0) {
        $("#" + strTableP0215).footerData("set", { khmc: "合计", ysye: "&nbsp;" }, false);
    }
    else {
        $("#" + strTableP0215).footerData("set", { khmc: "合计", ysye: strTotal }, true);
    }
    objTable = null;
    arrIds = null;
    strTotal = null;
}

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

//导出方法
function toExcelMethod() {
    makeExportData(strTableP0215, "应收查询");
}
///*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**自定义方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/