/*-------------------------------文档信息-----------------------------------
///功能：销售退货统计
///创建人: 翟新乐
///创建时间：------------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
var strTableP0212 = "Tp0212";         //要生成的grid。
var objSelRowP0212 = {};        //选择行的数据
var intSelRowIDP0212 = 0;       //选择行的Id
var objWherejson = {};      //记录查询时的条件
var sortfieldP0212 = "";
var orderbyP0212 = "";
var strRecordP0212 = "";

//加载按默认统计的grid
function pageLoad_P0212() {
    //查询类型
    loadSearchType_P0212();
    //日期下拉框加载
    dateExtent_P0212();
    //加载供应商
    loadKhData_P0212();
    //加载药品
    loadYpData_P0212();
    //查询数据
    searchData_P0212();
    //createTable_P0212();
}

//查询按钮的方法
function searchData_P0212(strFlag) {
    try {
        var objConfig = {};
        var objResult = {};
        //combobox的key不存在，赋值空
        if ($("#cmbCustmoerP0212").combobox("getValue") == undefined) {
            $("#cmbCustmoerP0212").combobox("setValue", "");
        }
        if ($("#cmbYpP0212").combobox("getValue") == undefined) {
            $("#cmbYpP0212").combobox("setValue", "");
        }
        intSelRowIDP0212 = "";
        objSelRowP0212 = {};
        //查询条件的取得
        var strWhereJson = "";
        if (strFlag != "1") {
            strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP0212").val() + "\",";
            strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0212").val() + "\",";
            strWhereJson += "\"khdm\":\"" + $("#cmbCustmoerP0212").combobox("getValue") + "\",";
            strWhereJson += "\"spdm\":\"" + $("#cmbYpP0212").combobox("getValue") + "\"}";
            strRecordP0212 = strWhereJson;
            //记录查询条件
            objWherejson = ParseData(strWhereJson.replace("}", ",\"dname\":\"" + $("#cmbDateExtentP0212").combobox("getText") + "\",\"khmc\":\"" + $("#cmbCustmoerP0212").combobox("getText") + "\"}"));
        }
        
        objConfig = ParseData(window.external.DoOptions(strTableP0212, "GetTableConfig", "{}"));
        setColMFamatter(objConfig.colModel);
        if (strTableP0212 == "Tp0212") {
            var strTreeJson = strRecordP0212.replace("}", ",\"sortfield\":\"" + sortfieldP0212 + "\",\"orderby\":\"" + orderbyP0212 + "\"}");
            objResult = ParseData(window.external.DoOptions(strTableP0212, "GetList", strTreeJson));
            if (strFlag=="0"||strFlag == "1") {
                loadTreeData(strTableP0212, objResult.Records);
            }
            else {
                $("#" + strTableP0212).GridUnload();
                createTreeGrid_P0212(strTableP0212, objConfig.colNames, objConfig.colModel, [], "khmc", showDetail);
                resizeGrid(strTableP0212, true, false);
                addGridEvent_P0212("Tp0212");
                loadTreeData(strTableP0212, objResult.Records);
            }
            sortfieldP0212 = "";
            orderbyP0212 = "";
        }
        else {
            $("#" + strTableP0212).GridUnload();
            objResult = ParseData(window.external.DoOptions(strTableP0212, "GetList", strWhereJson));
            createGrid(strTableP0212, objConfig.colNames, objConfig.colModel, objResult.Records, null, gridComplete_P0212, true, showDetail);
            addGridEvent_P0212(strTableP0212);
            resizeGrid(strTableP0212, true, false);
        }
        resizeGridScroll(strTableP0212);
        objConfig = {};
        objResult = {};
        strWhereJson = "";
    } catch (e) { }
}

function setColMFamatter(objColModel) {
    //添加列格式化方法
    for (var i = 0; i < objColModel.length; i++) {

        if (objColModel[i].name == "sl") {
            objColModel[i].formatter = formatterP0212Count;
        }
        if (objColModel[i].name == "je" || objColModel[i].name == "dj") {
            objColModel[i].formatter = formatterP0212Money;
        }
        if (objColModel[i].name == "szbl") {
            objColModel[i].formatter = formatterP0212Percent;
        }
        if (objColModel[i].name == "xh") {
            objColModel[i].hidden = true;
        }
    }
}

//树Grid
function createTreeGrid_P0212(strTableID, objColNames, objColModel, objJsonDatas, strColExpandKey,funcDbClick) {
    //当数据集为空时，格式化数据集的空值
    if (objJsonDatas == "{}") {
        objJsonDatas = [];
    }
    //创建表格
    $("#" + strTableID).jqGrid({
        datatype: "local",
        colNames: objColNames,
        colModel: objColModel,
        data: objJsonDatas[0],
        treeGrid: true,
        treedatatype: "local",
        treeGridModel: 'adjacency',
        ExpandColumn: strColExpandKey,
        ExpandColClick: true,
        editable: true,
        footerrow: true,
        rownumbers: false,
        rowNum: 999999,
        sortname: "khdm",
        localReader: { id: "bh" },
        autowidth: true,
        //height: "100%",
        ondblClickRow: function (selRowId) {
            if (funcDbClick) {
                funcDbClick.call(this, selRowId);
            }
        }
    }); //.parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");
}

//查询类型
function loadSearchType_P0212() {
    $("#cmbSelBy_P0212").combobox({
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 42,
        onSelect: function (option) {
            searchType_P0212(option.dm);
        },
        enterDown: function () {
            searchType_P0212($(this).combobox("getValue"));
        }
    });
    $("#cmbSelBy_P0212").combobox("loadData", [{ dm: "按客户", mc: "按客户", selected: true }, { dm: "按药品", mc: "按药品"}]);
}

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

//添加事件
function addGridEvent_P0212(strTableID) {
    $("#" + strTableID).setGridParam({
        onSelectRow: function (rowId) {
            intSelRowIDP0212 = rowId;
            objSelRowP0212 = $(this).getRowData(rowId);
            if (strTableID == "Tp0212") {
                resizeGridScroll(strTableID);
            }
        },
        gridComplete: function () {
            gridComplete_P0212();
        },
        onSortCol: function (index, colindex, sortorder) {
            if (strTableID == "Tp0212") {
                sortfieldP0212 = index;
                orderbyP0212 = sortorder;
                searchData_P0212("1");
                return "stop";
            }
        }
    })
}

//合计
function gridComplete_P0212() {
    var intNumSl = $("#" + strTableP0212).getCol("sl", false, "sum");
    var intNumJe = $("#" + strTableP0212).getCol("je", false, "sum");
    if (strTableP0212 == "Tp0212") {
        var objTable = $("#" + strTableP0212);
        var arrIds = objTable.getDataIDs();
        var strTotal = 0;
        var intTotal = 0;
        for (var i = 0; i < arrIds.length; i++) {
            if (objTable.getCell(arrIds[i], "isLeaf") == "true") {
                strTotal += parseFloat(objTable.getCell(arrIds[i], "je"));
                intTotal += parseFloat(objTable.getCell(arrIds[i], "sl"));
            }
        }
        if (arrIds.length == 0) {
            $("#" + strTableP0212).footerData("set", { khmc: "合计",sl:"&nbsp;", je: "&nbsp;" }, false);
        }
        else {
            $("#" + strTableP0212).footerData("set", { khmc: "合计",sl:intTotal, je: strTotal }, true);
        }
    }
    else {  //"&nbsp;"
        var arrIDLen = $("#" + strTableP0212).getDataIDs();
        if (arrIDLen.length == 0) {
            $("#" + strTableP0212).footerData("set", { rn: "合计", sl: "&nbsp;", je: "&nbsp;" }, false);
        }
        else {
            $("#" + strTableP0212).footerData("set", { rn: "合计", sl: intNumSl, je: intNumJe }, true);
        }
    }
    intNumJe = null;
    intNumSl = null;
    objTable = null;
    arrIds = null;
    strTotal = null;
    intTotal = null;
    arrIDLen = null;
}
//改变tab标签的名称，切换iframe中的内容。
function showDetail() {
    try {
        if (intSelRowIDP0212 > 0) {
            //改变标签的Name 
            $(window.parent.document).find("#span_212").text("销售退货明细");
            //选择的日期
            $(window.parent.document).find("#idIframe-21202").attr("dname", objWherejson.dname);
            $(window.parent.document).find("#idIframe-21202").attr("sdate", objWherejson.sdate);
            $(window.parent.document).find("#idIframe-21202").attr("edate", objWherejson.edate);
            //供应商代码
            $(window.parent.document).find("#idIframe-21202").attr("khdm", objSelRowP0212.khdm ? objSelRowP0212.khdm : objWherejson.khdm);
            $(window.parent.document).find("#idIframe-21202").attr("khmc", objSelRowP0212.khdm ? objSelRowP0212.khmc : objWherejson.khmc);
            //药品代码
            $(window.parent.document).find("#idIframe-21202").attr("spdm", objSelRowP0212.spdm ? objSelRowP0212.spdm : objWherejson.spdm);
            //改变标签Iframe的Src；
            $(window.parent.document).find("#idIframe-212").css("display", "none");
            $(window.parent.document).find("#idIframe-21202").css("display", "");
            $(window.parent.document).find("#idIframe-21202").attr("src", "02/p021202.htm");
            $(window.parent.document).find("#idIframe-21202").load(function () {
                var iHeight = window.parent.document.documentElement.clientHeight - window.parent._iFrmHeight;
                $(this).height(iHeight);
            });

        }
        else {
            window.parent.cihoAlert(msg.other.selDataRow);
        }
    } catch (e) { }
}

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

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

//客户下拉框信息的加载
function loadKhData_P0212() {
    $("#cmbCustmoerP0212").combobox({
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbCustmoerP0212").combobox("getText") == "") {
                $("#cmbCustmoerP0212").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbCustmoerP0212").combobox("getText") == "全部客户") {
                $("#cmbCustmoerP0212").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchData_P0212();
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });

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

//药品下拉框信息的加载
function loadYpData_P0212() {
    $("#cmbYpP0212").combobox({
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbYpP0212").combobox("getText") == "") {
                $("#cmbYpP0212").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbYpP0212").combobox("getText") == "全部药品") {
                $("#cmbYpP0212").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchData_P0212(); 
        }
    });

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

//sl格式化Count
function formatterP0212Count(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = "&nbsp;";
    }
    return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
}

//dj,je格式化money
function formatterP0212Money(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = "&nbsp;";
    }
    return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
}

//szbl格式化
function formatterP0212Percent(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = "&nbsp;";
    }
    return (parseFloat(cellvalue) * 100).toFixed(window.parent.strPercentPlace) + "%";
}

//按xx查询
function searchType_P0212(strValue) {
    try {
        //按客户查询
        if (strValue == "按客户") {
            strTableP0212 = "Tp0212";
            $("#divTP0212").css("display", "");
            $("#divTP021201").css("display", "none");
        }
        //按药品查询
        else if (strValue == "按药品") {
            strTableP0212 = "Tp021201";
            $("#divTP0212").css("display", "none");
            $("#divTP021201").css("display", "");
        }
        /*---------------------设置默认值------------------------*/
        objSelRowP0212 = {};
        intSelRowIDP0212 = "";
        searchData_P0212();
        //createTable_P0212();
        resizeGrid(strTableP0212, true, false);
        resizeGridScroll(strTableP0212);
    } catch (e) { }
}

//导出方法
function toExcelMethod() {
    makeExportData(strTableP0212, "销售退货统计");
}