/*--------------------------文档信息-------------------------------------------
///
///功能：    收款管理
///创建人：  李孟昌
///创建时间：--------------
///修改人：  王思波
///修改时间：2011年08月13日
///修改原因：代码的整理
///详细说明：--------------
///
-----------------------------------------------------------------------------*/

//页面全局变量
var strTableP0203 = "Tp0203";       //表格ID
var strPageP0203 = "divPageP0203";  //分页控件ID
var strSelRowIDP0203 = "";          //选中行ID
var objSelRowP0203 = {};            //选中行数据
var objParamP0203 = { sortfield: "", orderby: "", where: "", flag: 0 }; //数据查询参数信息

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**保存和关闭 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//保存按钮调用方法
function saveMyData(strDialog, strType, strTable) {
    try {
        if (strTable == "Tp020301") {
            saveData_P020301(strType);  //收款单
        }
        else if (strTable == "Tp020302") {
            saveData_P020302();         //回款分配
        }
        else if (strTable == ciho.layal.table) {
            ciho.saveType();            //基本信息
        }
    } catch (e) { }
}

//窗口关闭按钮调用方法
function closeMyDlg(strDialog, strType, strTable) {
    //保存当前单元格
    $("#" + strTableP020301).saveCell(objCellPlaceP020301.iRow, objCellPlaceP020301.iCol);
    $("#" + strTableP020302).saveCell(objCellPlaceP020302.iRow, objCellPlaceP020302.iCol);

    //窗口内容是否修改
    if ($("#" + strDialog + "_divform").data("change")) {
        //提示用户保存数据
        window.parent.cihoAlert(msg.other.haveChange, msgBox.dlgBtn.YesNoCancel, function (strRet) {
            if (msgBox.dlgRst.Yes == strRet) {
                saveMyData(strDialog, 0, strTable); //保存信息
            }
            else if (msgBox.dlgRst.No == strRet) {
                //关闭窗口
                cihoDlgClose(strDialog);

                //释放内存
                if (strTable == "Tp020301") {
                    freeMemory_P020301();
                }
                else if (strTable == "Tp020302") {
                    freeMemory_P020302();
                }
            }
        });
    }
    else {
        //关闭窗口
        cihoDlgClose(strDialog);

        //释放内存
        if (strTable == "Tp020301") {
            freeMemory_P020301();
        }
        else if (strTable == "Tp020302") {
            freeMemory_P020302();
        }
    }
}

/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**保存和关闭 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/


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

//加载页面的事件
function pageLoad_P02003() {
    try {
        //加载时间
        loadDateExtent_P0203();
        //加载客户信息
        loadDataClient_P0203();
        //加载药品信息
        loadDataDrug_P0203();
        //加载销售员
        loadDataStaff_P0203();

        //获得jqGrid的列名和属性
        var objConfig = ParseData(window.external.DoOptions(strTableP0203, "GetTableConfig", "{}"));
        //表格格式化方法的设置
        setColFormat_P0203(objConfig.colModel);
        //创建表格
        createGrid(strTableP0203, objConfig.colNames, objConfig.colModel, "{}", null, null, true, dblClickRow_P0203);
        //表格事件的设置
        setGridEvent_P0203();
        //调整表格的大小
        resizeGrid(strTableP0203, true, true);

        //创建分页
        createPage(strPageP0203, strTableP0203);

        //加载数据
        searchDataClick_P0203();

        //释放内存
        objConfig = null;
    } catch (e) { }
}

//查询按钮事件
function searchDataClick_P0203() {
    try {
        //参数的更新
        objParamP0203.sortfield = "djdate"; //排序字段
        objParamP0203.orderby = "desc";     //排序顺序
        objParamP0203.flag = 1;             //表示点击查询按钮
        loadGridData_P0203();               //加载数据
        objParamP0203.flag = 0;             //使用完后，设置为零  

        //清空选择行信息
        strSelRowIDP0205 = "";
        objSelRowP0205 = {};
    } catch (e) { }
}

//删除按钮事件
function deleteDataP0203() {
    try {
        //提示用户是否删除
        window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
            if (result == msgBox.dlgRst.Yes) {
                //删除数据信息
                var objResult = ParseData(window.external.DoOptions(strTableP0203, 'Delete', "{\"djbh\":\"" + objSelRowP0203.djbh + "\",djdate:\"" + objSelRowP0203.djdate + "\"}"));

                //删除成功后的处理
                if (objResult.ReturnCode == 0) {
                    objParamP0203.flag = 2; //表示点击查询按钮
                    loadGridData_P0203();   //加载数据
                    objParamP0203.flag = 0; //使用完后，设置为零  

                    //清空选择行信息
                    strSelRowIDP0203 = "";
                    objSelRowP0203 = {};
                }
                else {
                    window.parent.cihoAlert(objResult.MSG);
                }
            }
        });
    } catch (e) { }
}

//日期范围变更事件
function dateTimeChange_P0203() {
    if ($("#cmbDateExtentP0203").combobox("getText") != "自定义") {
        $("#cmbDateExtentP0203").combobox("setValue", "");
        $("#cmbDateExtentP0203").combobox("setText", "自定义");
    }
}

//双击行事件
function dblClickRow_P0203(strType, selRowId) {
    showDialog_P020301('3', '2');
}

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


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

//选择日期范围
function loadDateExtent_P0203() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0203", "dateEndTimeP0203");

    //设置属性
    $("#cmbDateExtentP0203").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        panelHeight: 102,
        valueField: "dm",
        textField: "mc",
        editable: false,
        //选择事件
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP0203", "dateEndTimeP0203");
        },
        //回车事件
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP0203", "dateEndTimeP0203");
        }
    });

    //加载数据
    $("#cmbDateExtentP0203").combobox("loadData", [{ dm: "本月", mc: "本月", selected: true },
                                                   { dm: "上一月", mc: "上一月" },
                                                   { dm: "本周", mc: "本周" },
                                                   { dm: "过去三个月", mc: "过去三个月" },
                                                   { dm: "本年", mc: "本年"}]);
}

//客户信息的加载
function loadDataClient_P0203() {
    //设置属性
    $("#cmbClientP0203").combobox({
        width: _iWidth150,
        listWidth: _iWidth150,
        valueField: "dm",
        textField: "mc",
        //获得焦点事件
        onfocus: function () {
            if ($("#cmbClientP0203").combobox("getText") == "全部客户") {
                $("#cmbClientP0203").combobox("setValue", undefined);
                $("#cmbClientP0203").combobox("setText", "");
            }
        },
        //失去焦点事件
        onblur: function () {
            if ($("#cmbClientP0203").combobox("getText") == "") {
                $("#cmbClientP0203").combobox("setValue", "");
            }
        },
        //信息变更前的事件
        afterChange: function (newValue, newText) {
            $("#cmbClientP0203").combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });

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

    //释放内存
    objProvider = null;
}

//药品信息的加载
function loadDataDrug_P0203() {
    //设置属性
    $("#cmbDrugP0203").combobox({
        width: _iWidth150,
        listWidth: _iWidth150,
        valueField: "dm",
        textField: "mc",
        //获得焦点事件
        onfocus: function () {
            if ($("#cmbDrugP0203").combobox("getText") == "全部药品") {
                $("#cmbDrugP0203").combobox("setValue", undefined);
                $("#cmbDrugP0203").combobox("setText", "");
            }
        },
        //失去焦点事件
        onblur: function () {
            if ($("#cmbDrugP0203").combobox("getText") == "") {
                $("#cmbDrugP0203").combobox("setValue", "");
            }
        }
    });

    //加载数据
    var objMedicine = getComboboxData("Tspxx2");
    objMedicine.unshift({ "dm": "", "mc": "全部药品", "selected": true });
    $("#cmbDrugP0203").combobox("loadData", objMedicine);

    //释放内存
    objMedicine = null;
}

//销售员信息加载
function loadDataStaff_P0203() {
    $("#cmbSalesmanP0203").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        //获得焦点事件
        onfocus: function () {
            if ($("#cmbSalesmanP0203").combobox("getText") == "全部销售员") {
                $("#cmbSalesmanP0203").combobox("setValue", undefined);
                $("#cmbSalesmanP0203").combobox("setText", "");
            }
        },
        //失去焦点事件
        onblur: function () {
            if ($("#cmbSalesmanP0203").combobox("getText") == "") {
                $("#cmbSalesmanP0203").combobox("setValue", "");
            }
        }
    });

    //加载数据
    var objStaff = getComboboxData("TZyxx2");
    objStaff.unshift({ "dm": "", "mc": "全部销售员", "selected": true });
    objStaff.push({ "dm": "-1", "mc": "其他销售员" });
    $("#cmbSalesmanP0203").combobox("loadData", objStaff);

    //释放内存
    objStaff = null;
}

//加载表格数据
function loadGridData_P0203() {
    var objDataSet = {}; //数据集

    //客户
    if ($("#cmbClientP0203").combobox("getValue") == undefined) {
        $("#cmbClientP0203").combobox("setValue", "");
    }
    //药品
    if ($("#cmbDrugP0203").combobox("getValue") == undefined) {
        $("#cmbDrugP0203").combobox("setValue", "");
    }
    //销售员
    if ($("#cmbSalesmanP0203").combobox("getValue") == undefined) {
        $("#cmbSalesmanP0203").combobox("setValue", "");
    }

    //数据的取得
    objParamP0203.where = getWhereJson_P0203();
    //数据的取得
    objDataSet = loadGridData_Page(objParamP0203);
    //合计的计算
    if (objDataSet && objDataSet.Records.length > 0) {
        $("#" + strTableP0203).footerData("set", { rn: "合计", yh: objDataSet.yh, je: objDataSet.je }, true);
    }
    else {
        $("#" + strTableP0203).footerData("set", { rn: "合计", yh: "&nbsp;", je: "&nbsp;" }, false);
    }

    //释放内存
    objDataSet = null;
}

//表格格式化方法的设置
function setColFormat_P0203(objColModel) {
    for (var intI = 0; intI < objColModel.length; intI++) {
        switch (objColModel[intI].name) {
            case "yh": //优惠
            case "je": //收款金额
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (cellvalue != "") {
                        return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
                    }
                    else {
                        return "0";
                    }
                }
                break;
        }
    }
}

//表格事件的设置
function setGridEvent_P0203() {
    $("#" + strTableP0203).setGridParam({
        //选择行事件
        onSelectRow: function (rowid) {
            //记录选中行的信息
            strSelRowIDP0203 = rowid;
            objSelRowP0203 = $(this).getRowData(rowid);

            //创建浮动按钮
            createFloatButton.call(this, rowid, "<a onclick=\"showDialog_P020301('3','2')\">修改</a>-<a onclick=\"deleteDataP0203()\">删除</a>");
        },
        //排序事件
        onSortCol: function (index, colindex, sortorder) {
            $("div.operate").hide();            //隐藏浮动按钮
            objParamP0203.sortfield = index;    //排序字段
            objParamP0203.orderby = sortorder;  //排序顺序
            loadGridData_P0203();               //加载数据
            return "stop";
        }
    });
}

//查询条件json字符串的取得
function getWhereJson_P0203() {
    var strWhereJson = "";
    strWhereJson += "\"sdate\":\"" + $("#dateStartTimeP0203").val() + "\",";
    strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0203").val() + "\",";
    strWhereJson += "\"khdm\":\"" + $("#cmbClientP0203").combobox("getValue") + "\",";
    strWhereJson += "\"spdm\":\"" + $("#cmbDrugP0203").combobox("getValue") + "\",";
    strWhereJson += "\"zydm\":\"" + $("#cmbSalesmanP0203").combobox("getValue") + "\"";
    return strWhereJson;
}

/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**自定义方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/