/*************************
*功能：纯销同期比较
*创建人：张振华
*时间：2010-08-17
*备注：
*************************/

var strTable = "Tp0802";
var objYears = [];
var objType = [{ "dm": "按客户", "mc": "按客户", "selected": true }, { "dm": "按药品", "mc": "按药品" }, { "dm": "按销售员", "mc": "按销售员"}];
var objData = [{ "dm": "1", "mc": "按月", "selected": true }, { "dm": "2", "mc": "按季度"}];

//取年份
function getYearData() {
    objYears = [];
    for (var i = new Date(window.parent._startDate.replace(/-/g, "/")).getFullYear(); i <= new Date(window.parent._endDate.replace(/-/g, "/")).getFullYear() + 2; i++) {
        objYears.push({ "key": i + "", "value": i + "" });
    }
}

//重新刷新
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("cmbSpP0802", data);
        }
        if (strModels[i] == "Tkhxx") {
            data = getComboboxData(strModels[i] + "2");
            data.unshift({ dm: "", mc: "全部客户" });
            reLoadCombobox("cmbKhP0802", data);
        }
        if (strModels[i] == "TZyxx") {
            data = getComboboxData(strModels[i] + "2");
            data.unshift({ dm: "", mc: "全部销售员" });
            data.push({ dm: "-1", mc: "其他销售员" });
            reLoadCombobox("cmbZyP0802", data);
        }
    }
}

//头部搜索信息
function getSearchInfo() {

    //加载一些样式
    $("#cmbSpP0802").combobox();
    $("#cmbKhP0802").combobox();
    $("#cmbZyP0802").combobox();
    $("#cmbDisTpP0802").combobox();
    $("#cmbTypeExtentP0802").combobox();
    $("#cmbDateExtentP0802").combobox();

    //下拉框箭头
    addArrowClass();

    var objKh = getComboboxData("Tkhxx2");
    var objSp = getComboboxData("Tspxx2");
    var objZy = getComboboxData("TZyxx2");
    var objDisTp = [{ "dm": "1", "mc": "按金额", "selected": true }, { "dm": "2", "mc": "按数量"}];

    //类型
    $("#cmbTypeExtentP0802").combobox({
        textField: "mc",
        valueField: "dm",
        width: _iWidth80,
        editable: false,
        onSelect: function (record) {
            createGridP0116("Tp0802", getColName(), getColModel());
            searchDataClickP0802();
        },
        enterDown: function () {
            createGridP0116("Tp0802", getColName(), getColModel());
            searchDataClickP0802();
        }
    });
    $("#cmbTypeExtentP0802").combobox("loadData", objType);

    //快捷日期
    $("#cmbDateExtentP0802").combobox({
        textField: "mc",
        valueField: "dm",
        width: _iWidth80,
        editable: false,
        onSelect: function (record) {
            createGridP0116("Tp0802", getColName(), getColModel());
            searchDataClickP0802();
        },
        enterDown: function () {
            createGridP0116("Tp0802", getColName(), getColModel());
            searchDataClickP0802();
        }
    });
    $("#cmbDateExtentP0802").combobox("loadData", objData);

    //开始时间
    $("#dateStartTimeP0802").combobox({
        textField: "key",
        valueField: "value",
        width: _iWidth65,
        listWidth: _iWidth65,
        panelHeight: 162,
        editable: false
    });
    objYears[0].selected = true;
    $("#dateStartTimeP0802").combobox('loadData', objYears);

    //结束时间
    $("#dateEndTimeP0802").combobox({
        textField: "key",
        valueField: "value",
        width: _iWidth65,
        listWidth: _iWidth65,
        panelHeight: 162,
        editable: false
    });
    $("#dateEndTimeP0802").combobox('loadData', objYears);
    $("#dateEndTimeP0802").combobox('setValue', new Date().getFullYear());

    //按数量/按金额
    $("#cmbDisTpP0802").combobox({
        textField: "mc",
        valueField: "dm",
        width: _iWidth80,
        listWidth: _iWidth80,
        editable: false
    });
    if (objDisTp != null && objDisTp != "{}") {
        $("#cmbDisTpP0802").combobox("loadData", objDisTp);
    }

    //商品
    $("#cmbSpP0802").combobox({
        textField: "mc",
        valueField: "dm",
        width: _iWidth80,
        listWidth: _iWidth80,
        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 });
        $("#cmbSpP0802").combobox("loadData", objSp);
    }

    //客户
    $("#cmbKhP0802").combobox({
        textField: "mc",
        valueField: "dm",
        width: _iWidth80,
        listWidth: _iWidth80,
        onSelect: function (Record) {
            if (Record.dm != "") {
                $(this).combobox("setText", Record.mc.replace(/&nbsp;/g, ""));
            }
        },
        afterChange: function (newValue, newText) {
            if (newValue != "" && newValue != undefined) {
                $(this).combobox("setText", newText.replace("&nbsp;&nbsp;", ""));
            }
        },
        onfocus: function () {
            if ($(this).combobox("getText") == "全部客户") {
                $(this).combobox("setText", "");
            }
        },
        onblur: function () {
            if ($(this).combobox("getText") == "") {
                $(this).combobox("setText", "全部客户");
            }
        },
        enterDown: function () {
            searchDataClickP0802();
        }
    });
    if (objKh != null && objKh != "{}") {
        objKh.unshift({ dm: "", mc: "全部客户", selected: true });
        $("#cmbKhP0802").combobox("loadData", objKh);
    }

    //全部销售员
    $("#cmbZyP0802").combobox({
        textField: "mc",
        valueField: "dm",
        width: _iWidth100,
        listWidth: _iWidth100,
        onfocus: function () {
            if ($(this).combobox("getText") == "全部销售员") {
                $(this).combobox("setText", "");
            }
        },
        onblur: function () {
            if ($(this).combobox("getText") == "") {
                $(this).combobox("setText", "全部销售员");
            }
        },
        enterDown: function () {
            searchDataClickP0802();
        }
    });

    //职员
    if (objZy != null && objZy != "{}") {
        objZy.unshift({ dm: "", mc: "全部销售员", selected: true });
        objZy.push({ dm: "-1", mc: "其他销售员" });
        $("#cmbZyP0802").combobox("loadData", objZy);
    }

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

//搜索数据
function searchDataClickP0802() {

    //客户
    if ($("#cmbKhP0802").combobox('getValue') == undefined) {
        $("#cmbKhP0802").combobox('setValue', ""); 
    }

    //药品
    if ($("#cmbSpP0802").combobox('getValue') == undefined) {
        $("#cmbSpP0802").combobox('setValue', ""); 
    }

    //职员
    if ($("#cmbZyP0802").combobox('getValue') == undefined) {
        $("#cmbZyP0802").combobox('setValue', ""); 
    }

    //单据类型不同模块名称不同
    var strType = $("#cmbTypeExtentP0802").combobox('getValue');
    var strJson = "";

    if (strType == "按客户") {
        strTable = "Tp0802";
    }
    if (strType == "按销售员") {
        strTable = "Tp080202";
    }
    if (strType == "按药品") {
        strTable = "Tp080201";
    }


    //拼接查询字符串
    strJson = "{\"sjtype\":\"" + $("#cmbDateExtentP0802").combobox('getValue') + "\"";

    //是否查看同比
    if ($("#imgSame").attr("alt") == "0") {
        strJson += ",\"compare\":\"false\"";
    }
    else {
        strJson += ",\"compare\":\"true\"";
    }

    strJson += ",\"sdate\":\"" + $("#dateStartTimeP0802").combobox('getValue') + "\",\"edate\":\"" + $("#dateEndTimeP0802").combobox('getValue') + "\"";
    strJson += ",\"bhth\":\"" + $("#chkReturn").attr("checked") + "\"";
    strJson += ",\"khdm\":\"" + $("#cmbKhP0802").combobox('getValue') + "\"";
    strJson += ",\"zydm\":\"" + $("#cmbZyP0802").combobox('getValue') + "\"";
    strJson += ",\"spdm\":\"" + $("#cmbSpP0802").combobox('getValue') + "\"";
    strJson += ",\"flag\":\"" + $("#cmbDisTpP0802").combobox('getValue') + "\"";
    strJson += "}";

    //查询数据
    var objData = ParseData(window.external.DoOptions(strTable, "GetList", strJson));
    if (objData.Records == "{}") {
        objData.Records = [];
    }

    //加载数据
    loadTreeData("Tp0802", objData.Records);

    //重新调整表格
    resizeGridScroll("Tp0802");

}

//创建grid
function createGridP0116(strTableID, objColNames, objColModel) {

    $("#" + strTableID).GridUnload();
    $("#" + strTableID).jqGrid({
        datatype: "local",
        sortname: "id",
        colNames: objColNames,
        colModel: objColModel,
        data: [],
        rowNum: 9999999,
        rownumbers: true,
        autowidth: true,
        rownumWidth: 30,
        gridview: true
    }).parent(".ui-jqgrid-bdiv").siblings();


    $("table.ui-jqgrid-htable th:first", $("#gbox_" + strTableID)).text("序号");
}

//格式化年
function formatterP0116Years(cellValue, option, row) {
    if (cellValue == "同比→") {
        return "<font color='#996600'>同比→</font>";
    }
    else {
        return cellValue;
    }
}

//格式化金额
function formatterP0116Money(cellValue, option, row) {

    if (isNaN(parseFloat(cellValue))) {
        return "&nbsp;";
    }
    if (row.years == "同比→") {

        if (parseFloat(cellValue) > 0) {
            return "<font color='green'>" + parseFloat((parseFloat(cellValue) * 100)).toFixed(window.parent.strPercentPlace) + "%↑</font>";
        }
        else if (parseFloat(cellValue) == 0) {
            return "<center>—</center>";;
        }
        else {
            return "<font color='#996600'>" + parseFloat((parseFloat(cellValue) * 100)).toFixed(window.parent.strPercentPlace) + "%↓</font>";
        }
    }
    else {
        if ($("#cmbDisTpP0802").combobox("getValue") == "1") {
            return parseFloat(cellValue).toFixed(window.parent.strMoneyPlace);
        }
        else {
            return parseFloat(cellValue).toFixed(window.parent.strCountPlace);
        }
    }
}

//配置名称
function getColName() {

    //类型，时间
    var strDataType = $("#cmbDateExtentP0802").combobox('getValue');
    var strType = $("#cmbTypeExtentP0802").combobox('getValue');

    if (strType == "按药品") {
        //按药品
        if (strDataType == "1") {
            //按月
            return ["药品", "药品", "年度", "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"];
        }
        else {
            //按季度
            return ["药品", "药品", "年度", "第一季度", "第二季度", "第三季度", "第四季度"];
        }
    }
    else if (strType == "按客户") {
        //按客户
        if (strDataType == "1") {
            //按月
            return ["客户名称", "客户名称", "年度", "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"];
        }
        else {
            //按季度
            return ["客户名称", "客户名称", "年度", "第一季度", "第二季度", "第三季度", "第四季度"];
        }
    }
    else if (strType == "按销售员") {
        //按销售员
        if (strDataType == "1") {
            //按月
            return ["销售员", "销售员", "年度", "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"];
        }
        else {
            //按季度
            return ["销售员", "销售员", "年度", "第一季度", "第二季度", "第三季度", "第四季度"];
        }
    }
}

//列配置
function getColModel() {

    //类型，时间
    var strType = $("#cmbTypeExtentP0802").combobox('getValue');
    var strDataType = $("#cmbDateExtentP0802").combobox('getValue');

    var objColModel = [];
    if (strType == "按客户") {
        //按供应商
        if (strDataType == "1") {
            //按月
            objColModel.push({ "sortable": false, "index": "khdm", "hidden": true, "width": 80, "name": "khdm", "align": "left" });
            objColModel.push({ "sortable": false, "index": "khmc", "hidden": false, "width": 80, "name": "khmc", "align": "left" });
            objColModel.push({ "sortable": false, "index": "years", "hidden": false, "width": 80, "name": "years", "formatter": formatterP0116Years, "align": "left" });
            objColModel.push({ "sortable": false, "index": "1", "hidden": false, "width": 80, "name": "1", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "2", "hidden": false, "width": 80, "name": "2", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "3", "hidden": false, "width": 80, "name": "3", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "4", "hidden": false, "width": 80, "name": "4", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "5", "hidden": false, "width": 80, "name": "5", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "6", "hidden": false, "width": 80, "name": "6", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "7", "hidden": false, "width": 80, "name": "7", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "8", "hidden": false, "width": 80, "name": "8", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "9", "hidden": false, "width": 80, "name": "9", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "10", "hidden": false, "width": 80, "name": "10", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "11", "hidden": false, "width": 80, "name": "11", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "12", "hidden": false, "width": 80, "name": "12", "formatter": formatterP0116Money, "align": "right" });

        } 
        else {
            //按季度
            objColModel.push({ "sortable": false, "index": "khdm", "hidden": true, "width": 80, "name": "khdm", "align": "left" });
            objColModel.push({ "sortable": false, "index": "khmc", "hidden": false, "width": 80, "name": "khmc", "align": "left" });
            objColModel.push({ "sortable": false, "index": "years", "hidden": false, "width": 80, "name": "years", "formatter": formatterP0116Years, "align": "left" });
            objColModel.push({ "sortable": false, "index": "1", "hidden": false, "width": 80, "name": "1", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "2", "hidden": false, "width": 80, "name": "2", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "3", "hidden": false, "width": 80, "name": "3", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "4", "hidden": false, "width": 80, "name": "4", "formatter": formatterP0116Money, "align": "right" });
        }

    }
    else if (strType == "按药品") {
        //按药品
        if (strDataType == "1") {
            //按月
            objColModel.push({ "sortable": false, "index": "spdm", "hidden": true, "width": 80, "name": "spdm", "align": "left" });
            objColModel.push({ "sortable": false, "index": "spmc", "hidden": false, "width": 80, "name": "spmc", "align": "left" });
            objColModel.push({ "sortable": false, "index": "years", "hidden": false, "width": 80, "name": "years", "formatter": formatterP0116Years, "align": "left" });
            objColModel.push({ "sortable": false, "index": "1", "hidden": false, "width": 80, "name": "1", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "2", "hidden": false, "width": 80, "name": "2", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "3", "hidden": false, "width": 80, "name": "3", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "4", "hidden": false, "width": 80, "name": "4", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "5", "hidden": false, "width": 80, "name": "5", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "6", "hidden": false, "width": 80, "name": "6", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "7", "hidden": false, "width": 80, "name": "7", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "8", "hidden": false, "width": 80, "name": "8", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "9", "hidden": false, "width": 80, "name": "9", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "10", "hidden": false, "width": 80, "name": "10", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "11", "hidden": false, "width": 80, "name": "11", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "12", "hidden": false, "width": 80, "name": "12", "formatter": formatterP0116Money, "align": "right" });
        } 
        else {
            //按季度
            objColModel.push({ "sortable": false, "index": "spdm", "hidden": true, "width": 80, "name": "spdm", "align": "left" });
            objColModel.push({ "sortable": false, "index": "spmc", "hidden": false, "width": 80, "name": "spmc", "align": "left" });
            objColModel.push({ "sortable": false, "index": "years", "hidden": false, "width": 80, "name": "years", "formatter": formatterP0116Years, "align": "right" });
            objColModel.push({ "sortable": false, "index": "1", "hidden": false, "width": 80, "name": "1", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "2", "hidden": false, "width": 80, "name": "2", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "3", "hidden": false, "width": 80, "name": "3", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "4", "hidden": false, "width": 80, "name": "4", "formatter": formatterP0116Money, "align": "right" });
        }
    }
    else if (strType == "按销售员") {
        //按销售员
        if (strDataType == "1") {
            //按月
            objColModel.push({ "sortable": false, "index": "zydm", "hidden": true, "width": 80, "name": "zydm", "align": "left" });
            objColModel.push({ "sortable": false, "index": "zymc", "hidden": false, "width": 80, "name": "zymc", "align": "left" });
            objColModel.push({ "sortable": false, "index": "years", "hidden": false, "width": 80, "name": "years", "formatter": formatterP0116Years, "align": "left" });
            objColModel.push({ "sortable": false, "index": "1", "hidden": false, "width": 80, "name": "1", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "2", "hidden": false, "width": 80, "name": "2", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "3", "hidden": false, "width": 80, "name": "3", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "4", "hidden": false, "width": 80, "name": "4", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "5", "hidden": false, "width": 80, "name": "5", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "6", "hidden": false, "width": 80, "name": "6", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "7", "hidden": false, "width": 80, "name": "7", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "8", "hidden": false, "width": 80, "name": "8", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "9", "hidden": false, "width": 80, "name": "9", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "10", "hidden": false, "width": 80, "name": "10", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "11", "hidden": false, "width": 80, "name": "11", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "12", "hidden": false, "width": 80, "name": "12", "formatter": formatterP0116Money, "align": "right" });

        } 
        else {
            //按季度
            objColModel.push({ "sortable": false, "index": "zydm", "hidden": true, "width": 80, "name": "zydm", "align": "left" });
            objColModel.push({ "sortable": false, "index": "zymc", "hidden": false, "width": 80, "name": "zymc", "align": "left" });
            objColModel.push({ "sortable": false, "index": "years", "hidden": false, "width": 80, "name": "years", "formatter": formatterP0116Years, "align": "left" });
            objColModel.push({ "sortable": false, "index": "1", "hidden": false, "width": 80, "name": "1", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "2", "hidden": false, "width": 80, "name": "2", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "3", "hidden": false, "width": 80, "name": "3", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "4", "hidden": false, "width": 80, "name": "4", "formatter": formatterP0116Money, "align": "right" });
        }
    }

    //返回列
    return objColModel;
}