/*-------------------------------文档信息-----------------------------------
///功能：应收款调整
///创建人: 翟新乐
///创建时间：------------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP0202 = "Tp0202";       //表格ID
var strPageP0202 = "divPageP0202";  //药品信息编辑窗口ID
var objSelRowP0202 = {};            //选择行的数据
var intSelRowIDP0202 = 0;           //选择行的ID
var objPageParamP0202 = { sortfield: "djdate", orderby: "desc", where: "", flag: 0 };  //分页控件使用

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

//保存按钮调用方法
function saveMyData(strDialog, strType, strTable) {
    try {
        if (strTable == "Tp020201") {
            saveDataP020201(strType);
        } //添加分类
        else if (strTable == ciho.layal.table) {
            ciho.saveType();
        }
    } catch (e) { }
}

//窗口关闭按钮调用方法
function closeMyDlg(strDialog, strType, strTable) {
    var isTextChange = $("#" + strDialog + "_divform").data("change");
    //当页面信息已修改时，弹出提示保存
    if (isTextChange == true) {
        window.parent.cihoAlert(
                msg.other.haveChange,
                 msgBox.dlgBtn.YesNoCancel,
                 function (strRet) {
                     if (msgBox.dlgRst.Yes == strRet) {
                         saveDataP020201(strType);
                     }
                     else if (msgBox.dlgRst.No == strRet) {
                         cihoDlgClose(strDialog);
                         if (strDialog != "divAddType") {
                             freeMemory_P020201();
                         }
                     }
                 });
    }
    else {
        cihoDlgClose(strDialog);
        if (strDialog != "divAddType") {
            freeMemory_P020201();
        }
    }
    isTextChange = null;
}

/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**保存和关闭 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//页面加载方法
function pageLoad_P0202() {
    try {
        //选择日期范围
        dateExtent_P0202();
        //加载客户信息
        loadProviderData_P0202();
        //药品下拉框信息的加载
        loadDrugData_P0202();
        //单据类型下拉框的设置
        setBillsType_P0202();

        //获得jqGrid的列名和属性
        var objConfig = ParseData(window.external.DoOptions(strTableP0202, "GetTableConfig", "{}"));
        //创建表格
        createGrid(strTableP0202, objConfig.colNames, objConfig.colModel, "{}", null, null, false, showDialog_P020201);
        //添加事件
        addGridEvent_P0202();
        //添加列格式化方法
        addColFormatter_P0202();

        //调整表格大小
        resizeGrid(strTableP0202, false, true);
        //创建分页
        createPage(strPageP0202, strTableP0202);

        //加载表格数据
        searchDataClick_P0202();
        //resizeGridScroll(strTableP0202);
        objConfig = {};
    } catch (e) { }
}

//查询按钮的事件
function searchDataClick_P0202() {
    try {
        objPageParamP0202.sortfield = "djdate";     //排序字段
        objPageParamP0202.orderby = "desc";         //排序顺序
        objPageParamP0202.flag = 1;                 //表示点击查询按钮
        loadGridDataP0202();                        //加载数据
        objPageParamP0202.flag = 0;                 //使用完后，设置为零
    } catch (e) { }
}

//加载数据的事件
function loadGridDataP0202(strFlag) {
    //combobox的key不存在，赋值空
    if ($("#cmbCoustmerP0202").combobox("getValue") == undefined) {
        $("#cmbCoustmerP0202").combobox("setValue", "");
    }
    if ($("#cmbYpP0202").combobox("getValue") == undefined) {
        $("#cmbYpP0202").combobox("setValue", "");
    }
    if (!strFlag) {
        objPageParamP0202.where = getWhereJson_P0202();
    }
    loadGridData_Page(objPageParamP0202);
    intSelRowIDP0202 = "";
    objSelRowP0202 = {};
}

//删除按钮的方法
function deleteData_P0202() {
    try {
        window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
            if (result == msgBox.dlgRst.Yes) {
                var strJsonData = "{\"djbh\":\"" + objSelRowP0202.djbh + "\"}";
                var objResult = ParseData(window.external.DoOptions(strTableP0202, 'Delete', strJsonData));
                //删除成功后，重新加载数据
                if (objResult.ReturnCode == 0) {
                    objPageParamP0202.flag = 2;
                    loadGridDataP0202();
                    objPageParamP0202.flag = 0;
                }
                else {
                    window.parent.cihoAlert(objResult.MSG);
                }
                strJsonData = null;
                objResult = null;
            }
        });
    } catch (e) { }
}

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

//日期范围的选择方法
function dateExtent_P0202() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0202", "dateEndTimeP0202");

    //添加选择事件
    $("#cmbDateExtentP0202").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 102,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP0202", "dateEndTimeP0202");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP0202", "dateEndTimeP0202");
            //searchDataClick_P0202();
        }
    });
    //加载数据
    $("#cmbDateExtentP0202").combobox("loadData", [{ dm: "本月", mc: "本月", selected: true },
        { dm: "上一月", mc: "上一月" },
        { dm: "过去三个月", mc: "过去三个月" },
        { dm: "本年", mc: "本年" }, 
        { dm: "上一年", mc: "上一年"}]);
}

//客户下拉框信息的加载
function loadProviderData_P0202() {
    //添加变更事件
    $("#cmbCoustmerP0202").combobox({
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbCoustmerP0202").combobox("getText") == "") {
                $("#cmbCoustmerP0202").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbCoustmerP0202").combobox("getText") == "全部客户") {
                $("#cmbCoustmerP0202").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P0202();
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });
    //加载数据
    var objProvider = getComboboxData('Tkhxx2');
    if (objProvider == "{}") {
        objProvider = [];
    }
    objProvider.unshift({ "dm": "", "mc": "全部客户", "selected": true });
    $("#cmbCoustmerP0202").combobox("loadData", objProvider);
    objProvider = {};
}

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

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

//单据类型下拉框的设置
function setBillsType_P0202() {
    //添加变更事件
    $("#cmbBillsTypeP0202").combobox({
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 62,
        onblur: function () {
            if ($("#cmbBillsTypeP0202").combobox("getText") == "") {
                $("#cmbBillsTypeP0202").combobox("setValue", "");
                $("#cmbBillsTypeP0202").combobox("setText", "全部调整");
            }
        },
        onfocus: function () {
            if ($("#cmbBillsTypeP0202").combobox("getText") == "全部调整") {
                $("#cmbBillsTypeP0202").combobox("setValue", "");
                $("#cmbBillsTypeP0202").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P0202();
        }
    });
    //加载数据
    $("#cmbBillsTypeP0202").combobox("loadData", [{ dm: "", mc: "全部调整", selected: true }, { dm: "301", mc: "应收款增加" }, { dm: "302", mc: "应收款减少"}]);
}

//表格事件的添加
function addGridEvent_P0202() {
    $("#" + strTableP0202).setGridParam({
        onSelectRow: function (rowId) {     //当前选择行数据取得
            //页面全局变量
            intSelRowIDP0202 = rowId;
            objSelRowP0202 = $(this).getRowData(rowId);
            var strButtonHtml = "";
            //选择行不是空行时，才进行操作
            if (objSelRowP0202.djbh != null) {
                //浮动按钮内容
                strButtonHtml = "<a onclick=\"showDialog_P020201('2')\">修改</a>-<a onclick=\"deleteData_P0202()\">删除</a>";
            }
            createFloatButton.call(this, rowId, strButtonHtml);
        },
        gridComplete: function () {     //表格加载事件
            setTzjsColor_P0202();
        },
        onSortCol: function (index, colindex, sortorder) {
            $("div.operate").hide();                    //浮动层隐藏
            objPageParamP0202.sortfield = index;        //排序字段
            objPageParamP0202.orderby = sortorder;      //排序顺序
            loadGridDataP0202(true);                        //加载数据
            //一定要加 此返回值【stop】。
            return "stop";
        }

    });
}

//设置类型为减少de数据的颜色
function setTzjsColor_P0202() {
    var arrDataRowID = $("#" + strTableP0202).getDataIDs();
    var objGrid = $("#" + strTableP0202);
    var strReceiptCode = "";
    for (var intI = 0; intI < arrDataRowID.length; intI++) {
        strReceiptCode = objGrid.getCell(arrDataRowID[intI], "tzlx");
        if (strReceiptCode == "应收款减少") {
            $("#" + arrDataRowID[intI]).css("color", "#996600");
        }
    }
    arrDataRowID = null;
    objGrid = null;
    strReceiptCode = null;
}

//添加列格式化方法
function addColFormatter_P0202(objColModel) {
    //金额
    $("#" + strTableP0202).setColProp("a3",
    {
        formatter: function (cellvalue, options, rowObject) {
            if (cellvalue != "") {
                return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
            }
            else {
                return "0";
            }
        }
    });
}

//查询条件json字符串的取得
function getWhereJson_P0202() {
    var strWhereJson = "";
    strWhereJson += "\"sdate\":\"" + $("#dateStartTimeP0202").val() + "\",";
    strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0202").val() + "\",";
    strWhereJson += "\"khdm\":\"" + $("#cmbCoustmerP0202").combobox("getValue") + "\",";
    strWhereJson += "\"spdm\":\"" + $("#cmbYpP0202").combobox("getValue") + "\",";
    strWhereJson += "\"djtypebh\":\"" + $("#cmbBillsTypeP0202").combobox("getValue") + "\"";
    return strWhereJson;
}
/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**自定义方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/