/*-------------------------------文档信息-----------------------------------
///功能：采购返利管理
///创建人: 翟新乐
///创建时间：------------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP0102 = "Tp0102";    //表格ID
var strPageP0102 = "divPageP0102";// 信息编辑窗口ID
var objSelRowP0102 = {};        //选择行的数据
var intSelRowIDP0102 = 0;       //选择行的ID
var objPageParamP0102 = { sortfield: "djdate", orderby: "desc", where: "", flag: 0 };  //分页控件使用

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
//保存按钮调用方法
function saveMyData(strDialog, strType, strTable) {
    try {
        if (strTable == "Tp010201") {
            //返货保存
            saveMyData_P010201(strType);
        }
        else if (strTable == "Tp010202") {
            //返现保存
            saveDataP010202(strType);
        }
        //添加分类
        else if (strTable == ciho.layal.table) {
            ciho.saveType();
        }
        strDialog = null;
        strType = null;
    } catch (e) {
        proceError(strTable, "");
    }    
}

//修改返利或返货数据
function updateP0102() {
    if (objSelRowP0102.djbh != null) {
        if (objSelRowP0102.djbh.split("-")[0] == "CGFH") {
            //返货修改
            showDialogP0102_FH("2");
        }
        else if (objSelRowP0102.djbh.split("-")[0] == "CGFL") {
            //返现修改
            showDialog_P010202("2");
        }
    }
    else {
        window.parent.cihoAlert(msg.edit.selEditRow);
    }
}

//删除数据的方法
function deleteData_P0102() {
    try {
        window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
            if (result == msgBox.dlgRst.Yes) {
                var strJsonData = "{\"djbh\":\"" + objSelRowP0102.djbh + "\",\"djtypebh\":\"" + objSelRowP0102.djtypebh + "\",\"djdate\":\"" + objSelRowP0102.djdate + "\"}";
                var objResult = ParseData(window.external.DoOptions(strTableP0102, 'Delete', strJsonData));         //后台操作

                if (objResult.ReturnCode == 0) {    //删除成功后，重新加载数据
                    //重新加载数据
                    objPageParamP0102.flag = 2;
                    loadGridDataP0102();
                    objPageParamP0102.flag = 0;
                    //resizeGridScroll(strTableP0102);
                }
                else {
                    window.parent.cihoAlert(objResult.MSG);
                }
                objResult = {};
                strJsonData = null;
            }
        });
    } catch (e) {
        proceError(strTableP0102, "");
    }
}

//关闭弹出层  yes:保存，no:不保存，关闭层；cancel:取消,回到窗口 
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") {
                             freeMemory_P010201();
                         }
                     }
                 });
    }
    else {//0：关闭。关闭编辑窗口
        cihoDlgClose(strDialog);
        if (strDialog != "divAddType") {
            freeMemory_P010201();
        }
    }
}

/*------------------------以下是页面私有事件方法-----------------------------*/
//页面加载方法
function pageLoad_P0102() {
    try {
        //选择日期范围
        dateExtent_P0102();

        //加载供应商信息
        loadGysData_P0102();

        //药品下拉框信息的加载
        loadYpData_P0102();

        var objConfig = ParseData(window.external.DoOptions(strTableP0102, "GetTableConfig", "{}")); //获得jqGrid的列名和属性

        //格式化数子列
        addColFormatter_P0102(objConfig.colModel);
        var i = 0;
        for (i = 0; i < objConfig.colModel.length; i++) {
            if (objConfig.colModel[i]["name"] == "a7") {    //返利项目列可排序
                objConfig.colModel[i].sortable = true;
            }
        }

        //创建jqgrid
        createGrid(strTableP0102, objConfig.colNames, objConfig.colModel, [], null, null, true);

        //添加事件
        addGridEvent_P0102();

        //调整表格宽度
        resizeGrid(strTableP0102, true, true);
        //创建分页
        createPage(strPageP0102, strTableP0102);

        //查询
        searchData_P0102();
        //resizeGridScroll(strTableP0102);
        objConfig = null;
        i = null;
    } catch (e) {
        proceError(strTableP0102, "");
    }
}

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

//加载数据的事件
function loadGridDataP0102(strFlag) {
    var objResult = [];
    //combobox的key不存在，赋值空
    if ($("#cmbGysP0102").combobox('getValue') == undefined) {
        $("#cmbGysP0102").combobox('setValue', "");
    }
    if ($("#cmbYpP0102").combobox('getValue') == undefined) {
        $("#cmbYpP0102").combobox('setValue', "");
    }
    if (!strFlag) {
        objPageParamP0102.where = getSelJson_P0102();
    }
    objResult = loadGridData_Page(objPageParamP0102);
    //后台返回的合计
    if (objResult) {
        if (objResult.Records.length > 0) {
            $("#" + strTableP0102).footerData("set", { rn: "合计",
                fhsl: objResult.FhslHj,
                fhje: objResult.FhjeHj,
                fxje: objResult.FxjeHj
            }, true);
        }
        else {
            $("#" + strTableP0102).footerData("set", { rn: "合计", fhsl: "&nbsp;", fhje: "&nbsp;", fxje: "&nbsp;" }, false);
        }
    }
    objResult = {};
    intSelRowIDP0102 = "";
    objSelRowP0102 = {};
}

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

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

//日期范围的选择方法
function dateExtent_P0102() {
    //设置默认日期为本月
    dateExtentCalculation("本年", "dateStartTimeP0102", "dateEndTimeP0102");

    //添加选择事件
    $("#cmbDateExtentP0102").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 62,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP0102", "dateEndTimeP0102");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP0102", "dateEndTimeP0102");
            //searchData_P0102();
        }
    });
    //加载数据
    $("#cmbDateExtentP0102").combobox("loadData", [
      { dm: "本年", mc: "本年", selected: true },
      { dm: "上一年", mc: "上一年" },
      { dm: "近两年", mc: "近两年"}]);
}

//供应下拉框信息的加载
function loadGysData_P0102() {
    $("#cmbGysP0102").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",        
        onfocus: function () {  //获得焦点
            if ($('#cmbGysP0102').combobox('getText') == "全部供应商") {
                $("#cmbGysP0102").combobox("setText", "");
            }
        },       
        onblur: function () {   //失去焦点
            if ($("#cmbGysP0102").combobox("getText") == "") {
                $("#cmbGysP0102").combobox("setValue", "");
            }
        },               
        enterDown: function () { //按回车键，进行查询
            //searchData_P0102();
        }
    });
    var objProvider = getComboboxData('Tgysxx2');
    if (objProvider=="{}") {
        objProvider = [];
    }
    objProvider.unshift({ "dm": "", "mc": "全部供应商", "selected": true });
    $("#cmbGysP0102").combobox("loadData", objProvider);
    objProvider = {};
}

//药品下拉框信息的加载
function loadYpData_P0102() {
    $("#cmbYpP0102").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",       
        onfocus: function () {  //获得焦点
            if ($('#cmbYpP0102').combobox('getText') == "全部药品") {
                $("#cmbYpP0102").combobox("setText", "");
            }
        },       
        onblur: function () {   //失去焦点
            if ($("#cmbYpP0102").combobox("getText") == "") {
                $("#cmbYpP0102").combobox("setValue", "");
            }
        },                
        enterDown: function () {    //按回车键，进行查询
            //searchData_P0102();
        }
    });
    var objDrug = getComboboxData('Tspxx2');
    if (objDrug=="{}") {
        objDrug=[];
    }
    objDrug.unshift({ "dm": "", "mc": "全部药品", "selected": true });
    $("#cmbYpP0102").combobox("loadData", objDrug);
    objDrug = {};
}

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

//得到查询字符串
function getSelJson_P0102() {
    var strRecord = "";
    strRecord += "\"sdate\":\"" + $("#dateStartTimeP0102").val() + "\",";
    strRecord += "\"edate\":\"" + $("#dateEndTimeP0102").val() + "\",";
    strRecord += "\"gysdm\":\"" + $("#cmbGysP0102").combobox('getValue') + "\",";
    strRecord += "\"spdm\":\"" + $("#cmbYpP0102").combobox('getValue') + "\"";
    return strRecord;
}

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