/**
 * Created by Mai on 2017/4/1.
 */
var ration_ass = {
    createNew: function (project) {
        // 用户定义private方法
        var tools = {};

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

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

        // prototype用于定义public方法
        ration_ass.prototype.loadData = function (datas) {
            this.datas = datas;
        };

        // 提交数据后返回数据处理
        ration_ass.prototype.doAfterUpdate = function(err, data){
            if(!err){
                if(data.updateTpye=='ut_update'){
                    this.refreshAfterUpdate(data);
                }else if(data.updateTpye=='ut_delete'){
                    this.refreshAfterDelete(data);
                } else {
                    this.refreshAfterSave(data);
                }
            }
        };
        ration_ass.prototype.refreshAfterSave=function(data){

        };
        ration_ass.prototype.refreshAfterUpdate=function(data){
            this.updateRation(data.rationID,data.doc);
            //this.updateRationGLJ(data.ration_glj_list);
            zmhs_obj.showAssData();
        };

        ration_ass.prototype.updateRation = function (rationID,doc) {
            var index =  _.findIndex(projectObj.project.Ration.datas,(ration)=>{
                return ration.ID==rationID;
            })
            projectObj.project.Ration.datas[index].rationAssList = doc.rationAssList;
        };

        ration_ass.prototype.updateRationGLJ = function (updateList) {
            for(var i=0;i<updateList.length;i++){
                var index =  _.findIndex(projectObj.project.ration_glj.datas,(glj)=>{
                    return glj.ID==updateList[i].ID;
                })
                projectObj.project.ration_glj.datas[index].quantity=updateList[i].quantity;

                var showIndex =  _.findIndex(gljOprObj.sheetData,(shglj)=>{
                    return shglj.ID==updateList[i].ID;
                })
                gljOprObj.sheetData[showIndex].quantity=updateList[i].quantity;
            }
            sheetCommonObj.showData(gljOprObj.sheet,gljOprObj.setting,gljOprObj.sheetData);
        };

        ration_ass.prototype.refreshAfterDelete=function(data){
            var list = projectObj.project.ration_coe.datas;
            _.remove(list,data.query);
            zmhs_obj.showAssData();
        };
        ration_ass.prototype.CreateNewAss = function (std) {
            var newAssList = []
            if(std.hasOwnProperty('rationAssList')&&std.rationAssList.length>0){
                for(var i=0;i<std.rationAssList.length;i++){
                    var ass = std.rationAssList[i];
                    ass.actualValue = ass.stdValue;
                    newAssList.push(ass);
                }
            }
            return newAssList;
        };
        ration_ass.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;
        };
        ration_ass.prototype.updateActualValue=function(assList,index,newVal,isAdjust = 1){//修改实际值默认打勾
            let me = this;
            var selected = projectObj.project.mainTree.selected;
            let newList = _.cloneDeep(assList);
            if(gljUtil.isDef(newVal))newList[index].actualValue=newVal;
            newList[index].isAdjust = isAdjust;
            var rationname = selected.data.name;
            if(selected.data.hasOwnProperty('caption')){
                 if(isAdjust == 1) newVal =  newList[index].stdValue;//如果是取消打勾，名字恢复成实际值
                 rationname =  selected.data.caption.replace('%s',newVal);
            }
            var query = {
                'ration':{projectID:selected.data.projectID,ID:selected.data.ID},
                'editAss':newList[index],
                'editIndex':index
            };
            var doc ={
                rationAssList:newList,
                name:rationname
            };

            var updateData = this.getUpdateData('ut_update',query,doc);
            $.bootstrapLoading.start();
            CommonAjax.post("/ration/updateRationAss",updateData[0],function (result) {
                //更新缓存
                selected.data.rationAssList = newList;
                $.bootstrapLoading.end();
                zmhs_obj.refreshAfterUpdate(result);
                zmhs_obj.showDatas();
            })
        };
        return new ration_ass(project);
    }

};