/*--------------------------文档信息-------------------------------------------
///功能：      纯销明细
///创建人：  王群
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///详细说明：--------------
-----------------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP021403 = "Tp021403";               //表格ID
var strPageP021403 = "divPageP021403";    //分页层的ID
var objSelRowP021403 = {};                          //选择行的数据
var intSelRowIDP021403 = 0;                        //选择行的ID
var objPageParamP021403 = { sortfield: "djdate", orderby: "asc", where: "", flag: 1 };     //分页控件使用

//窗口关闭按钮调用方法
function closeMyDlg(strDialog, strType, strTable) {
    // $("#" + strDialog).dialog("close");
    cihoDlgClose(strDialog);                //关闭弹出层
}

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//页面加载方法
function pageLoad_P021403() {
    try {
        //选择日期范围
        dateExtent_P021403();
        //加载客户信息
        loadProviderData_P021403();
        //药品下拉框信息的加载
        loadDrugData_P021403();
        //销售人员下拉框信息的加载
        loadXsryData_P021403();
        objConfig = ParseData(window.external.DoOptions(strTableP021403, "GetTableConfig", "{}"));
        //添加列格式化方法
        setDataP021403(objConfig.colModel);
        //创建表格
        createGrid(strTableP021403, objConfig.colNames, objConfig.colModel, [], null, null, true, null);
        //添加事件
        addGridEvent_P021403();
        //调整表格的大小
        resizeGrid(strTableP021403, true, true);
        //创建分页
        createPage(strPageP021403, strTableP021403);
        //加载表格数据(触发查询按钮的点击事件)
        searchDataClick_P021403();
    } catch (e) { }
}

//查询按钮的方法
function searchDataClick_P021403() {
    try {
        var objResult = {};              //存放查询结果
        if (!$("#cmbProviderP021403").combobox("getValue")) {  //"当前选择的客户不存在，赋值为【全部客户】！"
            $("#cmbProviderP021403").combobox("setValue", "");
        }
        if (!$("#cmbDrugP021403").combobox("getValue")) {  //"当前选择的药品不存在，赋值为【全部药品】！"
            $("#cmbDrugP021403").combobox("setValue", "");
        }
        if (!$("#cmbXsyP021403").combobox("getValue")) {  //"当前选择的销售员不存在，赋值为【全部销售员】！"
            $("#cmbXsyP021403").combobox("setValue", "");
        }

        //查询条件的取得
        objPageParamP021403.where = mySearchWhereJson();
        objPageParamP021403.sortfield = "djdate";
        objPageParamP021403.orderby = "asc";
        objPageParamP021403.flag = 1;
        objResult = loadGridData_Page(objPageParamP021403);
        var intIDLength = $("#" + strTableP021403).getDataIDs();
        if (intIDLength.length > 0) {
            $("#" + strTableP021403).footerData("set", { rn: "合计",
                sl: parseFloat(objResult.sl).toFixed(window.parent.strCountPlace),
                cxje: parseFloat(objResult.cxje).toFixed(window.parent.strMoneyPlace)
            }, false);
        }
        else {
            $("#" + strTableP021403).footerData("set", { rn: "合计",
                sl: "&nbsp;",
                cxje: "&nbsp;"
            }, false);
        }
        //释放内存
        objResult = null;
    } catch (e) { }
}

//添加格式化数据
function setDataP021403(objP021403) {
    for (var i = 0; i < objP021403.length; i++) {
        if (objP021403[i].name == "sl") {   //格式化成整数的格式
            objP021403[i].formatter = formatCountPlace;
        }
        if (objP021403[i].name == "cxje" || objP021403[i].name == "dj") {  //格式化成money的格式
            objP021403[i].formatter = formatMoneyPlace;
        }
    }
}

//查看单据按钮的方法
function viewReceipt_P021403() {
    if (intSelRowIDP021403 > 0) {
        if (objSelRowP021403.djbh.substr(0, 4) == "ZDXS") {  //销售单据
            showDialogP021103_xsd("divP021103_xsd", objSelRowP021403.djbh);
        }
        if (objSelRowP021403.djbh.substr(0, 4) == "XSTH") { //退货单据
            showDialogP021103_thd("divP021103_thd", objSelRowP021403.djbh);
        }
        if (objSelRowP021403.djbh.substr(0, 2) == "LX") {  //流向单据
            showDialogP021403_lxd("divP021403_lxd", objSelRowP021403.djbh);
        }
    }
    else {
        //"请选择数据！"
        window.parent.cihoAlert(msg.other.selDataRow);
    }
}

//返回按钮的方法
function pageBack_P021403() {
    //改变标签的Name
    $(window.parent.document).find("#span_214").text("纯销统计");
    //改变标签Iframe的Src
    $(window.parent.document).find("#idIframe-214").css("display", ""); //显示统计页面
    $(window.parent.document).find("#idIframe-21403").css("display", "none"); //隐藏明细页面
    $(window.parent.document).find("#idIframe-21403").attr("src", "");
}

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

//表格中的事件
function addGridEvent_P021403() {
    $("#" + strTableP021403).setGridParam({
        onSelectRow: function (rowid) {
            intSelRowIDP021403 = rowid;
            objSelRowP021403 = $(this).getRowData(rowid);
        },
        gridComplete: function () {
            var arrDataRowID = $("#" + strTableP021403).getDataIDs();
            for (var intI = 0; intI < arrDataRowID.length; intI++) {
                if ($("#" + strTableP021403).getCell(arrDataRowID[intI], "djbh").substr(0, 4) == "XSTH") {
                    $("#" + arrDataRowID[intI]).css("color", "#996600");
                }
            }
        },
        ondblClickRow: function () {
            viewReceipt_P021403();
        },
        onSortCol: function (index, colindex, sortorder) {
            objPageParamP021403.sortfield = index;
            objPageParamP021403.orderby = sortorder;
            objPageParamP021403.flag = "";
            loadGridData_Page(objPageParamP021403);
            return "stop";
        }
    });

}
/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**事件方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/


/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//选择日期范围
function dateExtent_P021403() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP021403", "dateEndTimeP021403");
    $("#cmbDateExtentP021403").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        panelHeight: 102,
        editable: false,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP021403", "dateEndTimeP021403");
        },
        enterDown: function () {
            dateExtentCalculation($("#cmbDateExtentP021403").combobox("getValue"), "dateStartTimeP021403", "dateEndTimeP021403");
        }
    });
    //加载数据
    $("#cmbDateExtentP021403").combobox("loadData",
    [{ dm: "本月", mc: "本月", selected: true },
    { dm: "上一月", mc: "上一月" },
    { dm: "本周", mc: "本周" },
    { dm: "过去三个月", mc: "过去三个月" },
    { dm: "本年", mc: "本年"}]);
    var strDateItem = $(window.parent.document).find("#idIframe-21403").attr("dname");
    //设置默认日期为本月
    if (strDateItem != "") {
        $("#cmbDateExtentP021403").combobox("setValue", strDateItem);
        dateExtentCalculation(strDateItem, "dateStartTimeP021403", "dateEndTimeP021403");
    }
    else {
        var strStartTime = $(window.parent.document).find("#idIframe-21403").attr("sdate");
        var strEndTime = $(window.parent.document).find("#idIframe-21403").attr("edate");
        $("#cmbDateExtentP021403").combobox("setText", "自定义");
        $("#dateStartTimeP021403").val(strStartTime);
        $("#dateEndTimeP021403").val(strEndTime);
    }
    // 释放内存
    strDateItem = null;
    strStartTime = null;
    strEndTime = null;
}

//客户下拉框信息的加载
function loadProviderData_P021403() {
    //添加变更事件
    $("#cmbProviderP021403").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbProviderP021403").combobox("getText") == "") {
                $("#cmbProviderP021403").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbProviderP021403").combobox("getText") == "全部客户") {
                $("#cmbProviderP021403").combobox("setText", "");
            }
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });
    //客户商信息的取得
    var objProvider = getComboboxData("Tkhxx2");
    var strProviderID = $(window.parent.document).find("#idIframe-21403").attr("khdm");
    var strProviderText = $(window.parent.document).find("#idIframe-21403").attr("khmc");
    if (objProvider == "{}") {
        objProvider = [];
    }
    //加载数据
    objProvider.unshift({ "dm": "", "mc": "全部客户", "selected": true });
    $("#cmbProviderP021403").combobox("loadData", objProvider);
    //设定默认值
    $("#cmbProviderP021403").combobox("setValue", strProviderID);
    $("#cmbProviderP021403").combobox("setText", strProviderText);
    // 释放内存
    objProvider = null;
    strProviderID = null;
}

//药品下拉框信息的加载
function loadDrugData_P021403() {
    //添加变更事件
    $("#cmbDrugP021403").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbDrugP021403").combobox("getText") == "") {
                $("#cmbDrugP021403").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbDrugP021403").combobox("getText") == "全部药品") {
                $("#cmbDrugP021403").combobox("setText", "");
            }
        },
        enterDown: function () {

        }
    });
    //药品信息的取得
    var objDrug = getComboboxData("Tspxx2");
    var strDrugID = $(window.parent.document).find("#idIframe-21403").attr("spdm");
    var strDrugText = $(window.parent.document).find("#idIframe-21403").attr("spmc");
    if (objDrug == "{}") {
        objDrug = [];
    }
    //加载数据
    objDrug.unshift({ "dm": "", "mc": "全部药品", "selected": true });
    $("#cmbDrugP021403").combobox("loadData", objDrug);
    //设定默认值
    $("#cmbDrugP021403").combobox("setValue", strDrugID);
    $("#cmbDrugP021403").combobox("setText", strDrugText);
    //释放内存
    objDrug = null;
    strDrugID = null;
}

// 销售人员下拉框信息的加载
function loadXsryData_P021403() {
    //添加变更事件
    $("#cmbXsyP021403").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbXsyP021403").combobox("getText") == "") {
                $("#cmbXsyP021403").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbXsyP021403").combobox("getText") == "全部销售员") {
                $("#cmbXsyP021403").combobox("setText", "");
            }
        }
    });
    //药品信息的取得
    var objXsy = getComboboxData("TZyxx2");
    objXsy.push({ "dm": "-1", "mc": "其他销售员" });
    var strXsyID = $(window.parent.document).find("#idIframe-21403").attr("xsydm");
    var strXsyText = $(window.parent.document).find("#idIframe-21403").attr("xsymc");
    if (objXsy == "{}") {
        objXsy = [];
    }
    //加载数据
    objXsy.unshift({ "dm": "", "mc": "全部销售员", "selected": true });
    $("#cmbXsyP021403").combobox("loadData", objXsy);
    //设定默认值
    $("#cmbXsyP021403").combobox("setValue", strXsyID);
    $("#cmbXsyP021403").combobox("setText", strXsyText);

    //释放内存
    objXsy = null;
    strXsyID = null;
}

//数量的格式化
function formatCountPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strCountPlace);
    }
    else {
        return 0;
    }
}

//百分比的格式化
function formatPricePlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strPricePlace);
    }
    else {
        return 0;
    }
}

//money的格式化
function formatMoneyPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return 0;
    }
}

//当基本资料数据有修改时 刷新相关的combobox
function refreshMyCombobox() {
    var strModels = $("#idRefreshCombobox").val();
    var data = getComboboxData(strModels + "2");

    if (strModels == "Tspxx") {
        data.unshift({ dm: "", mc: "全部药品", "selected": true });
        reLoadCombobox("cmbDrugP021403", data);
    }
    if (strModels == "TZyxx") {
        data.unshift({ "dm": "", "mc": "全部销售员", "selected": true });
        data.push({ dm: "-1", mc: "其他销售员" });
        reLoadCombobox("cmbXsyP021403", data);
    }
    if (strModels == "Tkhxx") {
        data.unshift({ dm: "", mc: "全部客户", "selected": true });
        reLoadCombobox("cmbProviderP021403", data);
    }
}

//查询条件的取得
function mySearchWhereJson() {
    //查询条件的取得
    strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP021403").val() + "\",";
    strWhereJson += "\"edate\":\"" + $("#dateEndTimeP021403").val() + "\",";
    strWhereJson += "\"khdm\":\"" + $("#cmbProviderP021403").combobox("getValue") + "\",";
    strWhereJson += "\"spdm\":\"" + $("#cmbDrugP021403").combobox("getValue") + "\",";
    strWhereJson += "\"zydm\":\"" + $("#cmbXsyP021403").combobox("getValue") + "\",";
    strWhereJson += "\"sfth\":\"" + $(window.parent.document).find("#idIframe-21403").attr("yNo") + "\"}";
    return strWhereJson.replace(/[{}]/g, "");
}
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 结束**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
