/*-------------------------------文档信息-----------------------------------
///功能：销售统计明细
///创建人: 翟新乐
///创建时间：------------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP021103 = "Tp021103";   //表格ID
var objSelRowP021103 = {};          //选择行的数据
var intSelRowIDP021103 = 0;         //选择行的ID
var strPageP021103 = "divPageP021103";
var objPageParamP021103 = { sortfield: "djdate", orderby: "asc", where: "", flag: 0 };  //分页控件使用

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

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

//页面加载方法
function pageLoad_P021103() {
    //选择日期范围
    dateExtent_P021103();
    //加载客户信息
    loadCustomerData_P021103();
    //药品下拉框信息的加载
    loadDrugData_P021103();
    //销售员信息
    loadZyData_P021103();
    var objConfig = ParseData(window.external.DoOptions(strTableP021103, "GetTableConfig", "{}"));
    //添加列格式化
    for (var i = 0; i < objConfig.colModel.length; i++) {
        if (objConfig.colModel[i].name == "sl") {
            objConfig.colModel[i].formatter = formatterP021103Count;
        }
        if (objConfig.colModel[i].name == "je" || objConfig.colModel[i].name == "dj" || objConfig.colModel[i].name == "ysykfpje") {
            objConfig.colModel[i].formatter = formatterP021103Money;
        }
    }
    //创建表格
    createGrid(strTableP021103, objConfig.colNames, objConfig.colModel, [], null, null, true);
    //表格事件的添加
    addGridEvent_P021103();
    resizeGrid(strTableP021103, true, true);
    //创建分页
    createPage(strPageP021103, strTableP021103);
    //加载表格数据(触发查询按钮的点击事件)
    searchDataClick_P021103();
    //resizeGrid(strTableP021103);
    objConfig = {};
}

//查询按钮事件
function searchDataClick_P021103() {
    try {
        objPageParamP021103.sortfield = "djdate"; //排序字段
        objPageParamP021103.orderby = "asc";     //排序顺序
        objPageParamP021103.flag = 1;             //表示点击查询按钮
        loadGridDataP021103(true);                     //加载数据
        objPageParamP021103.flag = 0;              //使用完后，设置为零
        //resizeGrid(strTableP021103);
    } catch (e) { }
}

//加载数据
function loadGridDataP021103(strFlag) {
    var objResult = [];
    //combobox的key不存在，赋值空
    if (!$("#cmbCustomerP021103").combobox("getValue")) {
        $("#cmbCustomerP021103").combobox("setValue", "");
    }
    if (!$("#cmbDrugP021103").combobox("getValue")) {
        $("#cmbDrugP021103").combobox("setValue", "");
    }
    if (!$("#cmbZyP021103").combobox("getValue")) {
        $("#cmbZyP021103").combobox("setValue", "");
    }
    if (strFlag == true) {
        objPageParamP021103.where = getSelJson_P021103();
    }
    objResult = loadGridData_Page(objPageParamP021103);
    //后台返回的合计
    if (objResult) {
        if (objResult.Records.length > 0) {
            $("#" + strTableP021103).footerData("set", { rn: "合计",
                sl: objResult.sl.toFixed(window.parent.strCountPlace),
                je: objResult.je.toFixed(window.parent.strMoneyPlace)
                //ysykfpje: objResult.ysykfpje
            }, false);
        }
        else {
            $("#" + strTableP021103).footerData("set", { rn: "合计", sl: "&nbsp;", je: "&nbsp;" }, false);
        }
    }
    objResult = {};
    intSelRowIDP021103 = "";
    objSelRowP021103 = {};
}

//取得查询条件
function getSelJson_P021103() {
    var bhth = $(window.parent.document).find("#idIframe-21103").attr("bhth");
    //查询条件的取得
    var strWhereJson = "";
    strWhereJson = "\"sdate\":\"" + $("#dateStartTimeP021103").val() + "\",";
    strWhereJson += "\"edate\":\"" + $("#dateEndTimeP021103").val() + "\",";
    strWhereJson += "\"khdm\":\"" + $("#cmbCustomerP021103").combobox("getValue") + "\",";
    strWhereJson += "\"spdm\":\"" + $("#cmbDrugP021103").combobox("getValue") + "\",";
    strWhereJson += "\"zydm\":\"" + $("#cmbZyP021103").combobox("getValue") + "\",";
    strWhereJson += "\"bhth\":\"" + bhth + "\"";
    return strWhereJson;
}

//查看单据按钮的方法
function viewReceipt_P021103() {
    try {
        if (intSelRowIDP021103 > 0) {
            if (objSelRowP021103.djbh.substr(0, 4) == "XSTH") {
                //退货单据
                showDialogP021103_thd("divP021103_thd", objSelRowP021103.djbh);
            }
            else if (objSelRowP021103.djbh.substr(0, 4) == "ZDXS" || objSelRowP021103.djbh.substr(0, 4) == "PSXS") {
                //销售单据
                showDialogP021103_xsd("divP021103_xsd", objSelRowP021103.djbh);
            }
        }
        else {
            window.parent.cihoAlert(msg.other.selDataRow);
        }
    } catch (e) {
        proceError(strTableP021103, "");
    }
}

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

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

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

//选择日期范围
function dateExtent_P021103() {
    $("#cmbDateExtentP021103").combobox({
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 122,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP021103", "dateEndTimeP021103");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP021103", "dateEndTimeP021103");
            //searchDataClick_P021103();
        }
    });
    //加载数据
    $("#cmbDateExtentP021103").combobox("loadData", [{ dm: "今天", mc: "今天" },
        { dm: "昨天", mc: "昨天" },
        { dm: "本月", mc: "本月", selected: true },
        { dm: "上一月", mc: "上一月" },
        { dm: "本年", mc: "本年" },
        { dm: "上一年", mc: "上一年"}]);
    $("#cmbDateExtentP021103").combobox("setValue", $(window.parent.document).find("#idIframe-21103").attr("dname"));
    $("#cmbDateExtentP021103").combobox("setText", $(window.parent.document).find("#idIframe-21103").attr("dname"));
    $("#dateStartTimeP021103").val($(window.parent.document).find("#idIframe-21103").attr("sdate"));
    $("#dateEndTimeP021103").val($(window.parent.document).find("#idIframe-21103").attr("edate"));
}

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

    //客户信息的取得
    var objProvider = getComboboxData('Tkhxx2');
    if (objProvider == "{}") {
        objProvider = [];
    }
    //加载数据
    objProvider.unshift({ "dm": "", "mc": "全部客户" });
    $("#cmbCustomerP021103").combobox("loadData", objProvider);
    //设定默认值
    $("#cmbCustomerP021103").combobox("setValue", $(window.parent.document).find("#idIframe-21103").attr("khdm"));
    $("#cmbCustomerP021103").combobox("setText", $(window.parent.document).find("#idIframe-21103").attr("khmc").replace(/&nbsp;/g, ""));
    objProvider = {};
}

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

    //药品信息的取得
    var objDrug = getComboboxData('Tspxx2');
    if (objDrug == "{}") {
        objDrug = [];
    }
    //加载数据
    objDrug.unshift({ "dm": "", "mc": "全部药品" });
    $("#cmbDrugP021103").combobox("loadData", objDrug);
    //设定默认值
    $("#cmbDrugP021103").combobox("setValue", $(window.parent.document).find("#idIframe-21103").attr("spdm"));
    objDrug = {};
}

//加载销售员信息
function loadZyData_P021103() {
    $("#cmbZyP021103").combobox({
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbZyP021103").combobox("getText") == "") {
                $("#cmbZyP021103").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbZyP021103").combobox("getText") == "全部销售员") {
                $("#cmbZyP021103").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P021103();
        }
    });

    //销售员信息的取得
    var objZydm = getComboboxData('TZyxx2');
    if (objZydm == "{}") {
        objZydm = [];
    }
    //加载数据
    objZydm.unshift({ "dm": "", "mc": "全部销售员", "selected": true });
    objZydm.push({ "dm": "-1", "mc": "其他销售员" });
    $("#cmbZyP021103").combobox("loadData", objZydm);
    //设定默认值
    $("#cmbZyP021103").combobox("setValue", $(window.parent.document).find("#idIframe-21103").attr("zydm"));
    objZydm = {};
}

//表格事件的添加
function addGridEvent_P021103() {
    $("#" + strTableP021103).setGridParam({
        //当前选择行数据取得
        onSelectRow: function (rowId) {
            intSelRowIDP021103 = rowId;
            objSelRowP021103 = $(this).getRowData(rowId);
        },
        ondblClickRow: function (rowId) {   //双击事件
            viewReceipt_P021103();
        },
        onSortCol: function (index, colindex, sortorder) {  //排序
            //$("div.operate").hide();                    //浮动层隐藏
            objPageParamP021103.sortfield = index;        //排序字段
            objPageParamP021103.orderby = sortorder;      //排序顺序
            loadGridDataP021103(false);                        //加载数据
            return "stop";
        },
        gridComplete: function () {         //表格加载完成事件
            var arrDataRowID = $(this).getDataIDs();
            for (var intI = 0; intI < arrDataRowID.length; intI++) {
                //设置退货单的字体颜色
                if ($(this).getCell(arrDataRowID[intI], "djbh").substr(0,4) == "XSTH") {
                    $("#" + arrDataRowID[intI]).css("color", "#996600");
                }
            }
        }
    });
}

//sl格式化Count
function formatterP021103Count(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = "&nbsp;";
    }
    return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
}

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

}

//szbl格式化
function formatterP021103Percent(cellvalue, options, rowObject) {
    if (!isNaN(parseFloat(cellvalue))) {
        return (parseFloat(cellvalue) * 100).toFixed(window.parent.strPercentPlace) + "%";
    }
    else {
        return "&nbsp;";
    }
}

//导出数据的方法
function toExcelMethod() {
    makeExportData(strTableP021103, "销售明细");
}
/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**自定义方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/