/**
 * Created by Mai on 2017/4/1.
 */

var Ration = {
    createNew: function (project) {
        // 用户定义private方法
        var tools = {};

        // 所有通过this访问的属性，都不应在此单元外部进行写入操作
        var ration = function (proj) {
            this.project = proj;
            this.datas = null;

            var sourceType = ModuleNames.ration;
            this.getSourceType = function () {
                return sourceType;
            }
            proj.registerModule(ModuleNames.ration, this);
        };

        // 从后台获取数据
        /*ration.prototype.pullData = function (){
            this.project.pullData(
                '/ration/getData',
                {projectID: this.project.ID},
                function(result){
                    if (result.error ===0){
                        this.loadDatas(result.data);
                    }
                    else {
                        // to do: 错误处理需要细化
                        alert(result.message);
                    }
                },
                function (){}//to do: 错误处理需要细化
            )
        };*/
        // prototype用于定义public方法
        ration.prototype.loadData = function (datas) {
            this.datas = datas;
            // generate Fees & Flags Index，For View & Calculate
            this.datas.forEach(function (data) {
                data.FeesIndex = {};
                data.fees.forEach(function (fee) {
                    data.FeesIndex[fee.fieldName] = fee;
                });
                data.FlagsIndex = {};
                data.flags.forEach(function (flag) {
                    data.FlagsIndex[flag.fieldName] = flag;
                });
            });
        };

        // 提交数据后的错误处理方法
        ration.prototype.doAfterUpdate = function(err, data){
            // to do
        };

        return new ration(project);
    }
};
