/**
 * Standard Ration Lib
 * Created by Mai on 2017/5/16.
 */
/*var rationChapterSpread, sectionRationsSpread;*/

var rationLibObj = {
    rationChapterSpread: null,
    sectionRationsSpread: null,
    checkSpread: function () {
        if (!this.rationChapterSpread) {
            this.rationChapterSpread = SheetDataHelper.createNewSpread($('#stdRationChapter')[0]);
        }
        if (!this.sectionRationsSpread) {
            this.sectionRationsSpread = SheetDataHelper.createNewSpread($('#stdSectionRations')[0]);
        }
    },
    loadStdRationLibs: function () {
        CommonAjax.postRationLib('/rationRepository/api/getRationDisplayNames', {user_id: userID}, function (datas) {
            var select = $('#stdRationLibSelect');
            select.empty();
            datas.forEach(function (data) {
                select.append($('<option>').val(data.ID).text(data.dispName));
            });
            if (select[0].options.length !== 0) {
                rationLibObj.loadStdRation(select.val());
            }
        }, function () {
            $('#stdRationLibSelect').empty();
        });
    },
    loadStdRation: (function () {
        var rationChapterTreeSetting = {
            "emptyRows":0,
            "headRows":1,
            "headRowHeight":[30],
            "defaultRowHeight": 21,
            "treeCol": 0,
            "cols":[{
                "width":300,
                "readOnly": true,
                "head":{
                    "titleNames":["名称"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"name",
                    "vAlign":1,
                    "hAlign":0,
                    "font":"Arial"
                }
            }]
        };
        var showRationChapterTree = function (datas) {
            var rationChapterTree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: false});
            var rationChapterTreeController = TREE_SHEET_CONTROLLER.createNew(rationChapterTree, rationLibObj.rationChapterSpread.getActiveSheet(), rationChapterTreeSetting);
            rationChapterTree.loadDatas(datas);
            rationChapterTreeController.showTreeData();

            rationChapterTreeController.bind(TREE_SHEET_CONTROLLER.eventName.treeSelectedChanged, function (node) {
                rationLibObj.loadSectionRations(node.getID());
            });

            if (rationChapterTree.firstNode()) {
                rationLibObj.loadSectionRations(rationChapterTree.firstNode().getID());
            } else {
                rationLibObj.loadSectionRations();
            };
        };

        var LoadData = function (rationLibID) {
            CommonAjax.postRationLib('/rationRepository/api/getRationTree', {userId: userID, rationRepositoryId: rationLibID}, function (datas) {
                showRationChapterTree(datas);
            }, function () {
                showRationChapterTree([]);
            });
        };

        return LoadData;
    })(),
    loadSectionRations: (function () {
        var sectionRationsSetting = {
            "emptyRows":3,
            "headRows":1,
            "headRowHeight":[20],
            "defaultRowHeight": 21,
            "cols":[{
                "width":100,
                "readOnly": true,
                "head":{
                    "titleNames":["编码"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"code",
                    "vAlign":1,
                    "hAlign":0,
                    "font":"Arial"
                }
            }, {
                "width":180,
                "readOnly": true,
                "head":{
                    "titleNames":["名称"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"name",
                    "vAlign":1,
                    "hAlign":0,
                    "font":"Arial"
                }
            }, {
                "width":50,
                "readOnly":true,
                "head":{
                    "titleNames":["单位"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"unit",
                    "vAlign":1,
                    "hAlign":1,
                    "font":"Arial"
                }
            }, {
                "width":80,
                "readOnly":true,
                "head":{
                    "titleNames":["基价"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"basePrice",
                    "vAlign":1,
                    "hAlign":2,
                    "font":"Arial"
                }
            }]
        };
        var showDatas = function (datas) {
            SheetDataHelper.loadSheetHeader(sectionRationsSetting, rationLibObj.sectionRationsSpread.getActiveSheet());
            SheetDataHelper.loadSheetData(sectionRationsSetting, rationLibObj.sectionRationsSpread.getActiveSheet(), datas);
        }
        var LoadData = function (sectionID) {
            if (sectionID) {
                CommonAjax.postRationLib('/rationRepository/api/getRationItems', {userId: userID, sectionID: sectionID}, function (datas) {
                    showDatas(datas);
                }, function () {
                    showDatas([]);
                });
            } else {
                showDatas([]);
            }
        };
        return LoadData;
    })()
};

$('#stdRationTab').on('shown.bs.tab', function (e) {
    var select = $('#stdRationLibSelect');
    rationLibObj.checkSpread();
    if (select[0].options.length === 0) {
        rationLibObj.loadStdRationLibs();
    };
});
$('#stdRationLibSelect').change(function () {
    var select = $(this);
    if (this.children.length !== 0) {
        rationLibObj.loadStdRation(select.val());
    }
});
