/*************************
*功能：箱号批号管理
*创建人：张振华
*时间：2010-08-15
*备注：此时间是加注释时间
*************************/

var strTableP0301 = "Tp0301";
var objPageParamP0301 = { sortfield: "", orderby: "", where: "",flag:0 }; //分页控件使用
var strPageP0301 = "divPageP0301";  //分页层
var strRowIdP0301;  //选择行的ID
var objSelRowP0301; //一行数据


//刷新combobox值
function refreshMyCombobox(strModels) {
    for (var i = 0; i < strModels.length; i++) {
        var data;

        if (strModels[i] == "Tspxx") {
            data = getComboboxData(strModels[i] + "2");
            data.unshift({ dm: "", mc: "全部药品" });
            reLoadCombobox("cmbSpP0301", data);
        }
        if (strModels[i] == "TCkxx") {
            data = getComboboxData(strModels[i] + "2");
            data.unshift({ dm: "", mc: "全部仓库" });
            reLoadCombobox("cmbCkP0301", data);
        }
        if (strModels[i] == "Tkhxx") {
            var objKh = getComboboxData("Tkhxx2");
            data = getComboboxData("Tgysxx");
            if (objKh != "{}") {
                data = data.concat(objKh);
            }
            data.unshift({ dm: "", mc: "全部往来单位" });
            reLoadCombobox("cmbWldwP0301", data);
        }
        if (strModels[i] == "Tgysxx") {

            var objGys = getComboboxData("Tgysxx2");
            data = getComboboxData("Tkhxx2");
            if (objGys != "{}") {
                data = data.concat(objGys);
            }
            data.unshift({ dm: "", mc: "全部往来单位" });
            reLoadCombobox("cmbWldwP0301", data);
        }
    }
}

//格式化数量
function formatterP0301Count(cellValue) {
    if (!isNaN(parseFloat(cellValue)) && cellValue != "-1") {
        return parseFloat(cellValue).toFixed(window.parent.strCountPlace);
    }
    else {
        return "&nbsp;";
    }

}

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

//点击一条数据
function onSelectGridP0301(rowId) {

    //记录行ID和改行数据
    strRowIdP0301 = rowId;
    objSelRowP0301 = $(this).getRowData(rowId);

    //选择行不是空行时，才进行操作
    if (objSelRowP0301.djbh != null || 1 == 1) {
        var strButtonHtml = "<a onclick=\"showDialogP0301();\">登记批号</a>";
        //浮动按钮内容
        createFloatButton.call(this, rowId, strButtonHtml);
    }
}

//获得头部搜索信息
function getTopInfoP0301() {

    //仓库信息，商品信息，供应商信息，客户信息，往来单位
    var objCk = getComboboxData("TCkxx2");
    var objSp = getComboboxData("Tspxx2");
    var objGys = getComboboxData("Tgysxx2");
    var objKh = getComboboxData("Tkhxx2");
    var objWldw;
    if (objKh != "{}") {
        objWldw = objGys.concat(objKh);
    }
    else {
        objWldw = objGys;
    }
    var objType = ParseData(window.external.DoOptions("Tp0301", "DoOthers", "{}"));

    //设置comgbobox
    $("#cmbDateExtentP0301").combobox();
    $("#cmbCkP0301").combobox();
    $("#cmbWldwP0301").combobox();
    $("#cmbSpP0301").combobox();
    $("#cmbTypesP0301").combobox();

    //combobox下拉箭头的样式
    addArrowClass();

    //开始时间和结束时间绑定控件
    inputAndBindData("dateStartTimeP0301", false, null, dateTimeChangeP0301);
    inputAndBindData("dateEndTimeP0301", false, null, dateTimeChangeP0301);

    //绑定事件
    dateExtentCalculation("今天", "dateStartTimeP0301", "dateEndTimeP0301");

    //快捷日期
    $("#cmbDateExtentP0301").combobox({
        width: "65",
        panelHeight: 102,
        editable: false,
        onSelect: function (option) {
            dateExtentCalculation(option.value, "dateStartTimeP0301", "dateEndTimeP0301");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP0301", "dateEndTimeP0301");
        }
    });

    //仓库数据
    $("#cmbCkP0301").combobox({
        width: _iWidth80,
        listWidth: _iWidth80,
        textField: "mc",
        valueField: "dm",
        onfocus: function () {
            if ($(this).combobox("getText") == "全部仓库") {
                $(this).combobox("setText", "");
            }
        },
        onblur: function () {
            if ($(this).combobox("getText") == "") {
                $(this).combobox("setText", "全部仓库");
            }
        }
    });
    if (objCk != null && objCk != "{}") {
        objCk.unshift({ dm: "", mc: "全部仓库", selected: true });
        $("#cmbCkP0301").combobox("loadData", objCk);
    }

    //药品数据
    $("#cmbSpP0301").combobox({
        width: _iWidth80,
        listWidth: _iWidth80,
        textField: "mc",
        valueField: "dm",
        onfocus: function () {
            if ($(this).combobox("getText") == "全部药品") {
                $(this).combobox("setText", "");
            }
        },
        onblur: function () {
            if ($(this).combobox("getText") == "") {
                $(this).combobox("setText", "全部药品");
            }
        }
    });
    if (objSp != null && objSp != "{}") {
        objSp.unshift({ dm: "", mc: "全部药品", selected: true });
        $("#cmbSpP0301").combobox("loadData", objSp);
    }

    //往来单位
    $("#cmbWldwP0301").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        textField: "mc",
        valueField: "dm",
        onfocus: function () {
            if ($(this).combobox("getText") == "全部往来单位") {
                $(this).combobox("setText", "");
            }
        },
        onblur: function () {
            if ($(this).combobox("getText") == "") {
                $(this).combobox("setText", "全部往来单位");
            }
        },
        onSelect: function (Record) {
            if (Record.dm != "") {
                $(this).combobox("setText", Record.mc.replace(/&nbsp;/g, ""));
            }
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });
    if (objWldw != null && objWldw != "{}") {
        objWldw.unshift({ dm: "", mc: "全部往来单位", selected: true });
        $("#cmbWldwP0301").combobox("loadData", objWldw);
    }

    //全部业务类型
    $("#cmbTypesP0301").combobox({
        width: _iWidth100,
        panelHeight: 262,
        textField: "mc",
        valueField: "dm",
        editable: false,
        onfocus: function () {
            if ($(this).combobox("getText") == "全部业务类型") {
                $(this).combobox("setText", "");
            }
        },
        onblur: function () {
            if ($(this).combobox("getText") == "") {
                $(this).combobox("setText", "全部业务类型");
            }
        }
    });
    if (objType != null && objType.Records != "{}") {
        objType.Records.unshift({ dm: "", mc: "全部业务类型", selected: true });
        $("#cmbTypesP0301").combobox("loadData", objType.Records);
    }

    //设置层序
    $(".layoutpanel").css("z-index", "50000009");
}

//双击事件
function funcDbClick(strType, rowId) {
    strRowIdP0301 = rowId;
    objSelRowP0301 = $(this).getRowData(rowId);
    showDialogP0301('1');
}

//窗口关闭按钮调用方法
function closeMyDlg(strDialog, strType, strTable) {

    var isTextChange = $("#" + strDialog + "_divform").data("change");

    //当页面信息已修改时，弹出提示保存
    if (isTextChange == true) {
        //函数名称
        window.parent.cihoAlert(
            msg.other.haveChange  //message information
            , msgBox.dlgBtn.YesNoCancel     //按钮类型msgBox.dlgBtn.YesNo  msgBox.dlgBtn.YesNoCancel
            , function (strRet) {//回调函数
                if (msgBox.dlgRst.Yes == strRet) {
                    saveMyData(strDialog, 0, strTable);
                }
                else if (msgBox.dlgRst.No == strRet) {
                    cihoDlgClose(strDialog);
                    freeMemory_P030101();
                }
            }
        );


    }
    else {
        cihoDlgClose(strDialog);
        freeMemory_P030101();
    }
}

//保存按钮调用方法
function saveMyData(strDialog, strType, strTable) {
    try {
        if (strTable == "Tp030101") {
            saveMyDataTp030101(strDialog, strType)
        }
    }
    catch (e) {
        proceError("P0301", e.ToString());
    }
}

//搜索数据
function searchDataClickP0301() {

    //客户不能为空
    if ($("#cmbCkP0301").combobox('getValue') == undefined) {
        $("#cmbCkP0301").combobox('setValue', "");
    }

    //往来单位不能为空
    if ($("#cmbWldwP0301").combobox('getValue') == undefined) {
        $("#cmbWldwP0301").combobox('setValue', "");
    }

    //药品不能为空
    if ($("#cmbSpP0301").combobox('getValue') == undefined) {
        $("#cmbSpP0301").combobox('setValue', "");
    }

    //组查询条件
    var strWhereJson = "";
    strWhereJson += "\"sdate\":\"" + $("#dateStartTimeP0301").val() + "\",";
    strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0301").val() + "\"";
    strWhereJson += ",\"ckdm\":\"" + $("#cmbCkP0301").combobox("getValue") + "\"";
    strWhereJson += ",\"wldwdm\":\"" + $("#cmbWldwP0301").combobox("getValue") + "\"";
    strWhereJson += ",\"spdm\":\"" + $("#cmbSpP0301").combobox("getValue") + "\"";
    strWhereJson += ",\"djlx\":\"" + $("#cmbTypesP0301").combobox("getValue") + "\"";
    if ($("#chbShowP0301").attr("checked")) {
        strWhereJson += ",\"isShowAll\":\"true\"";
    }
    else {
        strWhereJson += ",\"isShowAll\":\"false\"";
    }

    objPageParamP0301.where = strWhereJson;

    //查询数据
    var objRecords = loadGridData_Page(objPageParamP0301);

    //清楚浮动按钮
    $(".operate").hide();

    //重新设置分页标志位
    objPageParamP0301.flag = 0;

    ///计算合计
    if (objRecords.Records.length == 0) {
        $("#" + strTableP0301).footerData('set', { rn: "合计", rksl: "&nbsp;", cksl: "&nbsp;" }, true);
    }
    else {
        $("#" + strTableP0301).footerData('set', { rn: "合计", rksl: objRecords.zrksl, cksl: objRecords.zcksl }, true);
    }

}

//点击列排序
function sortDataP0301(index, colindex, sortorder) {
    objPageParamP0301.sortfield = index;
    objPageParamP0301.orderby = sortorder;

    //查询数据
    var objRecords = loadGridData_Page(objPageParamP0301);

    //清楚浮动按钮
    $(".operate").hide();

    //重新设置分页标志位
    objPageParamP0301.flag = 0;

    //计算合计
    if (objRecords.Records.length == 0) {
        $("#" + strTableP0301).footerData('set', { rn: "合计", rksl: "&nbsp;", cksl: "&nbsp;" }, true);
    }
    else {
        $("#" + strTableP0301).footerData('set', { rn: "合计", rksl: objRecords.zrksl, cksl: objRecords.zcksl }, true);
    }

    return 'stop';
}

//日期改变
function dateTimeChangeP0301() {
    if ($("#cmbDateExtentP0301").combobox("getText") != "自定义") {
        $("#cmbDateExtentP0301").combobox("setValue", "");
        $("#cmbDateExtentP0301").combobox("setText", "自定义");
    }
}