/*-------------------------------文档信息-----------------------------------
///功能：移库管理
///创建人: 翟新乐
///创建时间：------------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP0302 = "Tp0302";    //表格ID
var strPageP0302 = "divPageP0302"; // 信息编辑窗口ID
var objSelRowP0302 = {};        //选择行的数据
var intSelRowIDP0302 = 0;       //选择行的ID
var objPageParamP0302 = { sortfield: "djdate", orderby: "desc", where: "", flag: 0 };  //分页控件使用

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

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

//修改
function updateP0302() {
    if (objSelRowP0302.djbh != null) {
        showDialog_P030201("2");
    }
    else {
        window.parent.cihoAlert(msg.edit.selEditRow);
    }
}

//删除
function deleteData_P0302() {
    try {
        window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
            if (result == msgBox.dlgRst.Yes) {
                var strJsonData = "{\"djbh\":\"" + objSelRowP0302.djbh + "\",\"djdate\":\"" + objSelRowP0302.djdate + "\"}";
                var objResult = ParseData(window.external.DoOptions(strTableP0302, 'Delete', strJsonData));
                //删除成功后，重新加载数据
                if (objResult.ReturnCode == 0) {
                    objPageParamP0302.flag = 2;
                    loadGridDataP0302();
                    objPageParamP0302.flag = 0;
                    //resizeGrid(strTableP0302);
                }
                else {
                    window.parent.cihoAlert(objResult.MSG);
                }
                strJsonData = null;
                objResult = {};
            }
        });
    } catch (e) { }
}

//关闭弹出窗口
function closeMyDlg(strDialog, strType, strTableName) {
    if ($("#" + strDialog + "_divform").data("change")) {
        window.parent.cihoAlert(
                msg.other.haveChange,
                 msgBox.dlgBtn.YesNoCancel,
                 function (strRet) {
                     if (msgBox.dlgRst.Yes == strRet) {
                         saveMyData(strDialog, strType, strTableName);
                     }
                     else if (msgBox.dlgRst.No == strRet) {
                         cihoDlgClose(strDialog);
                         if (strDialog != "divAddType") {
                             freeMemeyP030201();
                         }
                     }
                 });
    }
    else {//0：关闭。关闭编辑窗口
        cihoDlgClose(strDialog);
        if (strDialog != "divAddType") {
            freeMemeyP030201();
        }
    }
}

/*------------------------以下是页面私有事件方法-----------------------------*/

//页面加载方法
function pageLoad_P0302() {
    try {
        //选择日期范围
        dateExtent_P0302();
        //加载移库类型
        loadLxData_P0302();
        //加载调出仓库
        loadCkexportData_P0302();
        //加载调入仓库
        loadCkfoldData_P0302();
        //药品下拉框信息的加载
        loadYpData_P0302();
        //列表数据的取得
        var objConfig = ParseData(window.external.DoOptions(strTableP0302, "GetTableConfig", "{}")); //获得jqGrid的列名和属性
        addColFormatter_P0302(objConfig.colModel);
        createGrid(strTableP0302, objConfig.colNames, objConfig.colModel, [], null, null, true);
        //添加事件
        addGridEvent_P0302();

        resizeGrid(strTableP0302, true, true);
        //创建分页
        createPage(strPageP0302, strTableP0302);
        searchData_P0302();
        //resizeGrid(strTableP0302);
        objConfig = {};
    } catch (e) { }
}

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

//加载数据的事件
function loadGridDataP0302(strFlag) {
    var objResult = [];
    if ($("#cmbCkexportP0302").combobox('getValue') == undefined) {
        $("#cmbCkexportP0302").combobox('setValue', "");
    }
    if ($("#cmbCkfoldP0302").combobox('getValue') == undefined) {
        $("#cmbCkfoldP0302").combobox('setValue', "");
    }
    if ($("#cmbYpP0302").combobox('getValue') == undefined) {
        $("#cmbYpP0302").combobox('setValue', "");
    }
    if (!strFlag) {
        objPageParamP0302.where = getSelJson_P0302();
    }
    objResult = loadGridData_Page(objPageParamP0302);
    if (objResult) {
        if (objResult.Records.length > 0) {
            $("#" + strTableP0302).footerData("set", { rn: "合计", a1: objResult.yksl }, true);
        }
        else {
            $("#" + strTableP0302).footerData("set", { rn: "合计", a1: "&nbsp;" }, false);
        }
    }
    intSelRowIDP0302 = "";
    objSelRowP0302 = {};
    objResult = {};
}


//表格事件的添加
function addGridEvent_P0302() {
    $("#" + strTableP0302).setGridParam({
        onSelectRow: function (rowId) {     //选择行使用方法            
            intSelRowIDP0302 = rowId;   //当前选择行数据
            objSelRowP0302 = $(this).getRowData(rowId);
            var strBtnHtml = "";
            if (objSelRowP0302.djbh != null) {
                //浮动按钮内容
                strBtnHtml = "<a onclick=\"updateP0302()\">修改</a>-<a onclick=\"deleteData_P0302()\">删除</a>";
            }
            createFloatButton.call(this, rowId, strBtnHtml);
        },
        ondblClickRow: function (rowId) {
            updateP0302();
        },
        onSortCol: function (index, colindex, sortorder) {  //排序
            $("div.operate").hide();                    //浮动层隐藏
            objPageParamP0302.sortfield = index;        //排序字段
            objPageParamP0302.orderby = sortorder;      //排序顺序
            loadGridDataP0302(true);                        //加载数据
            return "stop";
        }
    });
}

//添加列格式化方法
function addColFormatter_P0302(objColModel) {
    //添加列格式化方法
    for (var intI = 0; intI < objColModel.length; intI++) {
        if (objColModel[intI]["name"] == "a1") {
            objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                if (cellvalue != "") {
                    return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
                }
                else {
                    return "&nbsp;";
                }
            }
        }
    }
}

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

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

//类型下拉框信息的加载
function loadLxData_P0302() {
    $("#cmbLxP0302").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 62,
        onblur: function () {
            if ($("#cmbLxP0302").combobox("getText") == "") {
                $("#cmbLxP0302").combobox("setValue", "");
                $("#cmbLxP0302").combobox("setText", "移库类型");
            }
        },
        onfocus: function () {
            if ($("#cmbLxP0302").combobox("getText") == "移库类型") {
                $("#cmbLxP0302").combobox("setValue", "");
                $("#cmbLxP0302").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchData_P0302();
        },
        onSelect: function (option) {
            var ckxx = getComboboxData('TCkxx2');
            var objKh = getComboboxData('Tkhxx2');     //客户
            if (ckxx == "{}") {
                ckxx = [];
            }
            if (objKh == "{}") {
                objKh = [];
            }
            ckxx.unshift({ "dm": "", "mc": "调入仓库", "selected": true });

            var ckList = ckxx;

            if (option.dm == "601") {//移库时，选择实际仓库
                $("#cmbCkfoldP0302").combobox("loadData", ckList);
                $("#cmbCkfoldP0302").combobox("setValue", ckList[0].dm);
            } else if (option.dm == "602") {//配送移库时，选择客户
                objKh.unshift({ "dm": "", "mc": "调入仓库", "selected": true });
                var khList = objKh;
                $("#cmbCkfoldP0302").combobox("loadData", khList);
                $("#cmbCkfoldP0302").combobox("setValue", khList[0].dm);
                khList = {};
            }
            else {//为移库类型时，可以选择仓库和客户
                objKh.shift({ "dm": "", "mc": "调入仓库", "selected": true });
                var khDatas = objKh;
                var items = ckList.concat(khDatas);
                $("#cmbCkfoldP0302").combobox("loadData", items);
                $("#cmbCkfoldP0302").combobox("setValue", items[0].dm);
                khDatas = {};
                items = {};
            }
            ckxx = {};
            objKh = {};
            ckList = {};
        }
    });
    //加载数据
    $("#cmbLxP0302").combobox("loadData", [{ dm: "", mc: "移库类型", selected: true }, { dm: "601", mc: "移库" }, { dm: "602", mc: "配送移库"}]);
}

//调出仓库信息加载
function loadCkexportData_P0302() {
    $("#cmbCkexportP0302").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        //获得焦点
        onfocus: function () {
            if ($('#cmbCkexportP0302').combobox('getText') == "调出仓库") {
                $("#cmbCkexportP0302").combobox("setText", "");
            }
        },
        //失去焦点
        onblur: function () {
            if ($("#cmbCkexportP0302").combobox("getText") == "") {
                $("#cmbCkexportP0302").combobox("setValue", "");
                $("#cmbCkexportP0302").combobox("setText", "调出仓库");
            }
        },
        enterDown: function () {
            //searchData_P0302();
        }
    });
    var objCk = getComboboxData('TCkxx2');    // 仓库
    if (objCk == "{}") {
        objCk = [];
    }
    objCk.unshift({ "dm": "", "mc": "调出仓库", "selected": true });
    $("#cmbCkexportP0302").combobox("loadData", objCk);
    objCk = {};
}

//调入仓库信息加载
function loadCkfoldData_P0302() {
    $("#cmbCkfoldP0302").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        //获得焦点
        onfocus: function () {
            if ($('#cmbCkfoldP0302').combobox('getText') == "调入仓库") {
                $("#cmbCkfoldP0302").combobox("setText", "");
            }
        },
        //失去焦点
        onblur: function () {
            if ($("#cmbCkfoldP0302").combobox("getText") == "") {
                $("#cmbCkfoldP0302").combobox("setValue", "");
                $("#cmbCkfoldP0302").combobox("setText", "调入仓库");
            }
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        },
        enterDown: function () {
            //searchData_P0302();
        }
    });
    var objCk = getComboboxData('TCkxx2');    // 仓库
    var objKh = getComboboxData('Tkhxx2');     //客户
    if (objCk == "{}") {
        objCk = [];
    }
    objCk.unshift({ "dm": "", "mc": "调入仓库", "selected": true });
    if (objKh=="{}") {
        objKh = [];
    }
    var ckList = objCk.concat(objKh);
    $("#cmbCkfoldP0302").combobox("loadData", ckList);
    objCk = {};
    objKh = {};
    ckList = {};
}

//药品下拉框信息的加载
function loadYpData_P0302() {
    $("#cmbYpP0302").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        //获得焦点
        onfocus: function () {
            if ($('#cmbYpP0302').combobox('getText') == "全部药品") {
                $("#cmbYpP0302").combobox("setText", "");
            }
        },
        //失去焦点
        onblur: function () {
            if ($("#cmbYpP0302").combobox("getText") == "") {
                $("#cmbYpP0302").combobox("setValue", "");
            }
        },
        enterDown: function () {
            //searchData_P0302();
        }
    });
    var objSp = getComboboxData('Tspxx2');      //药品
    if (objSp == "{}") {
        objSp = [];
    }
    objSp.unshift({ "dm": "", "mc": "全部药品", "selected": true });
    $("#cmbYpP0302").combobox("loadData", objSp);
    objSp = {};
}

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

//得到查询字符串
function getSelJson_P0302() {
    var strRecord = "";
    strRecord += "\"sdate\":\"" + $("#dateStartTimeP0302").val() + "\",";
    strRecord += "\"edate\":\"" + $("#dateEndTimeP0302").val() + "\",";
    strRecord += "\"yklxdm\":\"" + $("#cmbLxP0302").combobox('getValue') + "\",";
    strRecord += "\"outckdm\":\"" + $("#cmbCkexportP0302").combobox('getValue') + "\",";
    strRecord += "\"inckkhdm\":\"" + $("#cmbCkfoldP0302").combobox('getValue') + "\",";
    strRecord += "\"spdm\":\"" + $("#cmbYpP0302").combobox('getValue') + "\"";
    return strRecord;
}

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