/**
 * Created by CSL on 2017-06-20.
 */
//var ModuleNames = require('./main_consts.js');

var FeeRate = {
    createNew: function (project) {

        function FeeRate(proj){
            this.datas = null;
            this.sourceType = ModuleNames.feeRate;
            proj.registerModule(ModuleNames.feeRate, this);
        };
        FeeRate.prototype.loadData = function (datas) {
            this.datas = datas;
            socketObject.connect();
        };
   /*     FeeRate.prototype.getViewDatas = function(){
            var  rates=[];
            _.forEach(this.datas,function (item) {
                if(item.status == 'activate'){
                    rates= item.rates;
                }
            })
            return rates;
        };*/
        FeeRate.prototype.getActivateFeeRate = function () {
            var feeRate={
                rates:[]
            };
            if(this.datas){
                feeRate = this.datas;
            }
            return feeRate;
        };
        FeeRate.prototype.getActivateFeeRateID = function(){
            var feeRate = this.getActivateFeeRate()
            if(feeRate){
                return feeRate.ID;
            }else {
                return 0;
            }
        };

        FeeRate.prototype.getSubViewData= function(item) {
            var datas = [];
            if(item.hasOwnProperty('subFeeRate')&&item.subFeeRate!=undefined){
                datas = item.subFeeRate.recodes;
            }
            return datas;
        };
        FeeRate.prototype.getValueMap= function(item) {
            var valueMap = {};
            if(item.hasOwnProperty('subFeeRate')&&item.subFeeRate!=undefined){
                var recodes = item.subFeeRate.valueMaps;
                _.forEach(recodes,function (r) {
                    valueMap[r.ID] = r.value;
                })
                return valueMap;
            }else {
                return null;
            }
        };
        FeeRate.prototype.getOptions=function (options,subRates,parentID,itemID) {
            var data = {};
            var list = _.filter(subRates, {'ParentID':parentID});
            if(list&&list.length>0){
                if(parentID==-1){
                    data.ID = itemID+'_'+0;
                }else {
                    data.ID = itemID+'_'+parentID;
                }
                data.name = list[0].type;
                data.optionList=[];
            }
            var nexParent = null;
            _.forEach(list,function (c) {
                var option={};
                option.name = c.name;
                option.value = c.ID;
                option.selected = c.selected;
                data.optionList.push(option);
                if(c.selected&&!c.isLeaf){
                    nexParent = c.ID;
                }
            })
            options.push(data);
            if(nexParent!=null){
                this.getOptions(options,subRates,nexParent,itemID);
            }
        };
        FeeRate.prototype.getUpdateData=function(type,query,doc,callfunction){
            var updateData = [];
            var newobj = {
                'updateType': type,
                'query': query,
            }
            if(doc){
                newobj['doc']=doc;
            }
            if(callfunction){
                newobj['updateFunction']=callfunction;
            }
            updateData.push(newobj);
            return updateData;
        };
        FeeRate.prototype.updateFeeRateByEdit = function(params,feerate){
            var query={
                ID:feerate.feeRateID
            };
            var doc ={
                rateIndex:params.sourceIndex,
                rate:params.dataItem
            }
            this.updateFeeRate(query,doc);
            if(this.ifRateChange(params)){
                this.synchronizeFeeRate();
            }
        };

        FeeRate.prototype.batchUpdateFeeRate = function (items,feerate) {
            var query={
                ID:feerate.feeRateID
            };
            this.updateFeeRate(query,items);
        };
        FeeRate.prototype.updateFeeRate=function (query,doc) {
          var updateData = this.getUpdateData('ut_update',query,doc,'update_rates');
            project.pushNow('updateFeeRate',[this.sourceType],updateData);
            socket.emit('feeRateChangeNotify', this.getActivateFeeRateID());
        };
        FeeRate.prototype.updateStatusBySelected = function (updateTasks) {
            var updateData = [];
            var newobj = {
                'updateType': 'ut_update',
                'tasks': updateTasks,
                'updateFunction':'updateStatusBySelected'
            }
            updateData.push(newobj);
            project.pushNow('updateFeeRate',[this.sourceType],updateData);
        };
        FeeRate.prototype.synchronizeFeeRate = function () {
            var node = project.mainTree.selected;
            if(node){
                if (node.sourceType==='ration' && calcProgramObj.sheet) {
                    calcProgramObj.showData(node);
                }
            }
        };
        FeeRate.prototype.ifRateChange=function (params) {
            if(params.dataItem.rate!=params.oldDataItem.rate){
                return true;
            }else {
                return false;
            }
        };
        FeeRate.prototype.changeFeeRateStandard=function(newVal,callback){
            var me =this;
            var feeRate = this.getActivateFeeRate();
            var data={
                "newLibID": newVal,
                "feeRateFileID":feeRate.ID,
                "feeRateID":feeRate.feeRateID,
                "user_id":userID
            };
            CommonAjax.post('/feeRates/changeFeeRateStandard', data, function (data) {
                if (data) {
                    me.datas.libID=data.libID;
                    me.datas.libName=data.libName;
                    me.datas.feeRateID=data.feeRateID;
                    me.datas.rates=data.rates;
                    socket.emit('feeRateChangeNotify', me.getActivateFeeRateID());
                    callback();
                }
            });
        };
        FeeRate.prototype.checkFeeRateName = function(newVal,callback){
            var me =this;
            var feeRate = this.getActivateFeeRate();
            var data = {
                name:newVal,
                rootProjectID:feeRate.rootProjectID
            }
            CommonAjax.post('/feeRates/checkFeeRateName', data, function (data) {
                callback(data);
            });
        };
        FeeRate.prototype.feeRateFileSaveAs = function (newName) {
            this.datas.name = newName;
            this.datas.ID = uuid.v1();
            this.datas.feeRateID=uuid.v1();
            this.datas.usageProjects=[];
            this.datas.usageProjects.push({name:projectInfoObj.projectInfo.name,ID:projectInfoObj.projectInfo.ID});
            var updateData = this.getUpdateData('ut_update',{projectID:projectInfoObj.projectInfo.ID},this.datas,'feeRateFileSaveAs');
            project.pushNow('updateFeeRate',[this.sourceType],updateData);

            socket.emit('feeRateChangeNotify', this.getActivateFeeRateID());
            return  this.datas;
        };
        FeeRate.prototype.getChangeInfo = function (callback) {
           // var projectID = projectInfoObj.projectInfo.ID;
            var feeRate = this.getActivateFeeRate();
            var  rootProjectID= feeRate.rootProjectID;
            if(!rootProjectID){
                rootProjectID=projectInfoObj.projectInfo.property.rootProjectID;
            }
            var data={
                "user_id":userID,
                "rootProjectID":rootProjectID,
            };
            CommonAjax.post('/feeRates/getChangeInfo', data, function (data) {
                callback(data);
            });
        };

        FeeRate.prototype.changeFeeRateFileFromCurrent = function (newFeeRateFile,callback){
            var me=this;
            var projectID = projectInfoObj.projectInfo.ID;
            var data={
                "projectID": projectID,
                "newFeeRateFile":newFeeRateFile
            };
            CommonAjax.post('/feeRates/changeFeeRateFileFromCurrent', data, function (data) {
                if (data) {
                    me.datas=data;
                    socket.emit('feeRateChangeNotify', me.getActivateFeeRateID());
                    callback();
                }
            });
        };

        FeeRate.prototype.changeFeeRateFileFromOthers=function (feeRateFileID,name,callback) {
            var me = this;
            var projectID = projectInfoObj.projectInfo.ID;
            var data={
                "projectID": projectID,
                "feeRateFileID":feeRateFileID,
                "name":name,
                "rootProjectID":me.getActivateFeeRate().rootProjectID
            };
            CommonAjax.post('/feeRates/changeFeeRateFileFromOthers', data, function (data) {
                if (data) {
                    me.datas=data;
                    socket.emit('feeRateChangeNotify', me.getActivateFeeRateID());
                    callback();
                }
            });

        };

        FeeRate.prototype.getRate = function (fileID, rateID){
            return 1.25;
        };

        var feeRate = new FeeRate(project);
        return feeRate;
    },

};