/*************************
*功能：采购同期比较
*创建人：张振华
*时间：2010-08-15
*备注：此时间是加注释时间
*************************/

//表格table
var strTableP0116 = "Tp0116";

//年份
var objYears = [];

//取年份
function getYearData() {
    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("cmbSpP0116", data);
        }
        if (strModels[i] == "Tgysxx") {
            data = getComboboxData(strModels[i] + "2");
            data.unshift({ dm: "", mc: "全部供应商" });
            reLoadCombobox("cmbProviderP0116", data);
        }
    }
}

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

    //设置默认combobox
    $("#dateStartTimeP0116").combobox();
    $("#dateEndTimeP0116").combobox();
    $("#cmbProviderP0116").combobox();
    $("#cmbTypeExtentP0116").combobox();
    $("#cmbDateExtentP0116").combobox();
    $("#cmbSpP0116").combobox();

    //下拉框箭头
    addArrowClass();

    //供应商，商品数据
    var objGys = getComboboxData("Tgysxx2");
    var objSp = getComboboxData("Tspxx2");
    var objType = [{ "dm": "1", "mc": "按供应商", "selected": true }, { "dm": "2", "mc": "按药品"}];
    var objDate = [{ "dm": "1", "mc": "按月", "selected": true }, { "dm": "2", "mc": "按季度"}];

    //按供应商
    $("#cmbTypeExtentP0116").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        textField: "mc",
        valueField: "dm"
    });
    $("#cmbTypeExtentP0116").combobox('loadData', objType);

    //按时间选择
    $("#cmbDateExtentP0116").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        textField: "mc",
        valueField: "dm"
    });
    $("#cmbDateExtentP0116").combobox('loadData', objDate);

    //开始日期
    $("#dateStartTimeP0116").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        textField: "key",
        valueField: "value",
        editable: false
    });
    objYears[0].selected = true;
    $("#dateStartTimeP0116").combobox('loadData', objYears);

    //结束日期
    $("#dateEndTimeP0116").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        textField: "key",
        valueField: "value",
        editable: false
    });
    $("#dateEndTimeP0116").combobox('loadData', objYears);
    $("#dateEndTimeP0116").combobox('setValue', new Date().getFullYear());

    //全部药品
    $("#cmbSpP0116").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", "全部药品");
            }
        }
    });
    if (objSp != null && objSp != "{}") {
        objSp.unshift({ dm: "", mc: "全部药品", selected: true });
        $("#cmbSpP0116").combobox("loadData", objSp);
    }

    //全部供应商
    $("#cmbProviderP0116").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", "全部供应商");
            }
        }
    });
    if (objGys != null && objGys != "{}") {
        objGys.unshift({ dm: "", mc: "全部供应商", selected: true });
        $("#cmbProviderP0116").combobox("loadData", objGys);
    }

    //按供应商，按药品
    $("#cmbTypeExtentP0116").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        editable: false,
        afterChange: function () {
            createGridP0116(strTableP0116, getColName(), getColModel());
            searchDataClick_P0116();
        }
    });

    //快捷日期
    $("#cmbDateExtentP0116").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        editable: false,
        afterChange:function(){
             createGridP0116(strTableP0116, getColName(), getColModel());
            searchDataClick_P0116();
        }
    });

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

//查询数据
function searchDataClick_P0116() {

    //传到后台查询数据
    var strJson = "";
    //查询出来的数据
    var objData;

    //取全部供应商
    if ($("#cmbProviderP0116").combobox('getValue') == undefined) {
        $("#cmbProviderP0116").combobox("setValue", "");
    }

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

    //拼接json
    strJson = "{\"lxtype\":\"" + $("#cmbTypeExtentP0116").combobox('getValue') + "\",\"sjtype\":\"" + $("#cmbDateExtentP0116").combobox('getValue') + "\"";

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

    //供应商，药品
    strJson += ",\"sdate\":\"" + $("#dateStartTimeP0116").combobox('getText') + "\",\"edate\":\"" + $("#dateEndTimeP0116").combobox('getText') + "\"";
    strJson += ",\"gysdm\":\"" + $("#cmbProviderP0116").combobox('getValue') + "\"";
    strJson += ",\"spdm\":\"" + $("#cmbSpP0116").combobox('getValue') + "\"";
    strJson += "}";

    //查询数据
    objData = ParseData(window.external.DoOptions(strTableP0116, "GetList", strJson));

    //加载数据
    if (objData.Records == "{}") {
        objData.Records = [];
    }
    loadTreeData(strTableP0116, objData.Records);

    //调整滚动条
    resizeGridScroll(strTableP0116);

}

//格式化年份
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 {
        return parseFloat(cellValue).toFixed(window.parent.strMoneyPlace);
    }

}

//创建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
    });

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

//配置名称
function getColName() {

    var strDataType = $("#cmbDateExtentP0116").combobox('getValue');
    var strType = $("#cmbTypeExtentP0116").combobox('getValue');
    if (strType == "2") {
        //按药品
        if (strDataType == "1") {
            //按月
            return ["药品", "药品", "年度", "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"];
        }
        else {
            //按季度
            return ["药品", "药品", "年度", "第一季度", "第二季度", "第三季度", "第四季度"];
        }
    }
    else {
        //按供应商
        if (strDataType == "1") {
            //按月
            return ["供应商名称", "供应商名称", "年度", "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"];
        }
        else {
            //按季度
            return ["供应商名称", "供应商名称", "年度", "第一季度", "第二季度", "第三季度", "第四季度"];
        }

    }
}

//列配置
function getColModel() {

    var strType = $("#cmbTypeExtentP0116").combobox('getValue');
    var strDataType = $("#cmbDateExtentP0116").combobox('getValue');

    var objColModel = [];
    if (strType == "1") {
        //按供应商
        if (strDataType == "1") {
            //按月
            objColModel.push({ "sortable": false, "index": "gysdm", "hidden": true, "width": 80, "name": "gysdm", "align": "left" });
            objColModel.push({ "sortable": false, "index": "gysmc", "hidden": false, "width": 80, "name": "gysmc", "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": "gysdm", "hidden": true, "width": 80, "name": "gysdm", "align": "left" });
            objColModel.push({ "sortable": false, "index": "gysmc", "hidden": false, "width": 80, "name": "gysmc", "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 (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_je", "hidden": false, "width": 80, "name": "1_je", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "2_je", "hidden": false, "width": 80, "name": "2_je", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "3_je", "hidden": false, "width": 80, "name": "3_je", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "4_je", "hidden": false, "width": 80, "name": "4_je", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "5_je", "hidden": false, "width": 80, "name": "5_je", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "6_je", "hidden": false, "width": 80, "name": "6_je", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "7_je", "hidden": false, "width": 80, "name": "7_je", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "8_je", "hidden": false, "width": 80, "name": "8_je", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "9_je", "hidden": false, "width": 80, "name": "9_je", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "10_je", "hidden": false, "width": 80, "name": "10_je", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "11_je", "hidden": false, "width": 80, "name": "11_je", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "12_je", "hidden": false, "width": 80, "name": "12_je", "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": "left" });
            objColModel.push({ "sortable": false, "index": "1_je", "hidden": false, "width": 80, "name": "1_je", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "2_je", "hidden": false, "width": 80, "name": "2_je", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "3_je", "hidden": false, "width": 80, "name": "3_je", "formatter": formatterP0116Money, "align": "right" });
            objColModel.push({ "sortable": false, "index": "4_je", "hidden": false, "width": 80, "name": "4_je", "formatter": formatterP0116Money, "align": "right" });
        }
    }
    return objColModel;
}

