/**
 * Created by zhang on 2020/1/10.
 */
let areaIncreaseFeeObj = {
    setDefaultSetting:function () {
        if(!gljUtil.isDef(projectObj.project.property.areaSetting)){
            let data={
                labour:10,
                material:1.5,
                machine:0,
            };
            projectObj.project.updateProjectProperty(data,"areaSetting")
        }

    },
    casCadeUpdate:async function (node,newval,fieldName) {
        let datas = [];
        let data =  {
            type:node.sourceType,
            data:{ID:node.data.ID}
        };
        setData(data.data,newval,fieldName);
        datas.push(data);
        setChildren(node,newval,datas);//同步设置所有子项
        this.updateAndRefresh(datas);

        function setChildren(pnode,newValue,datas) {//同步设置所有子项
            if(pnode.children.length > 0 && (pnode.children[0].sourceType == ModuleNames.bills || pnode.children[0].sourceType == ModuleNames.ration)){//设置子项
                for(let c of pnode.children){
                    let data =  {
                        type:c.sourceType,
                        data:{ID:c.data.ID}
                    };
                    if(c.data.calcBase&&c.data.calcBase!="") continue;
                    setData(data.data,newval,fieldName);
                    datas.push(data);
                    setChildren(c,newValue,datas)
                }
            }
        }
        function setData(data,avalue,fieldName) {
            data[fieldName] = avalue;
            if(fieldName == "outPutMaxPrice") data.maxPrice = null;
        }
    },
    updateAndRefresh:async function(datas){
        let nodes = await projectObj.project.syncUpdateNodesAndRefresh(datas);
        projectObj.project.calcProgram.calcNodesAndSave(nodes,async function () {
            await OVER_HEIGHT.reCalcOverHeightFee();
            await itemIncreaseFeeObj.calcItemIncreaseFeeByNodes(nodes);
        });
        projectObj.project.projectGLJ.calcQuantity();
        gljOprObj.refreshView();
    },

    confirmAreaIncreaseFeeSetting:async function () {
        let labour = $("#areaIncreaseFee_labour").val();
        let material = $("#areaIncreaseFee_material").val();
        let machine = $("#areaIncreaseFee_machine").val();
        if(!settingNumCheck(labour,"人工")) return;
        if(!settingNumCheck(material,"材料")) return;
        if(!settingNumCheck(machine,"机械")) return;

        let areaSetting = {
            labour:scMathUtil.roundForObj(labour,2),
            material:scMathUtil.roundForObj(material,2),
            machine:scMathUtil.roundForObj(machine,2)
        };
        let tem ={
            type:'project',
            data:{ID:projectObj.project.ID(),"property.areaSetting":areaSetting}
        };
        $('#areaIncreaseFeeDiv').modal('hide');
        let needUpdate = false;
        for(let key in areaSetting){
            if(areaSetting[key] != projectObj.project.property.areaSetting[key]){
                needUpdate = true;
                break;
            }
        }
        if(needUpdate){
            let datas = this.getAreaIncreaseDatas();
            datas.push(tem);
           this.updateAndRefresh(datas);
        }

        function settingNumCheck(value,type) {
            if(!number_util.isNum(value)){
                alert("输入的"+type+"数据类型不对，请重新输入!");
                return   false;
            }
            if(parseFloat(value) < -100){
                alert("输入的"+type+"不能小于负100，请重新输入!");
                return false
            }
            return true;
        }
    },
     getAreaIncreaseDatas:function (withBills = false) {
        let datas = [];
        let rations = projectObj.project.Ration.datas;
        for(let r of rations){
            if(r.areaIncreaseFee == true){
                datas.push({type:"ration",data:{ID:r.ID,areaIncreaseFee:true}})
            }
        }
        if(withBills == true){
            let bills = projectObj.project.Bills.datas;
            for(let b of bills){
                if(b.areaIncreaseFee == true){
                    datas.push({type:"bills",data:{ID:b.ID,areaIncreaseFee:true}})
                }
            }
        }
        return datas;
     },
    cancelAreaIncreaseFee:async function () {
        let datas = this.getAreaIncreaseDatas(true);
        //将值设置为false
        for(let d of datas){
            d.data.areaIncreaseFee = false;
        }
        let nodes = await projectObj.project.syncUpdateNodesAndRefresh(datas);
        projectObj.project.calcProgram.calcNodesAndSave(nodes,async function () {
            await OVER_HEIGHT.reCalcOverHeightFee();
            await itemIncreaseFeeObj.calcItemIncreaseFeeByNodes(nodes);
        });
    }
    
};



$(function () {
    $("#areaIncreaseFeeConfirm").on("click",async function(e){
        areaIncreaseFeeObj.confirmAreaIncreaseFeeSetting();
    });

    $('#areaIncreaseFeeDiv').on('show.bs.modal', function (e) {
        let areaSetting = {
            labour:10,
            material:1.5,
            machine:0,
        };
        if(gljUtil.isDef(projectObj.project.property.areaSetting)) areaSetting = projectObj.project.property.areaSetting;
        $("#areaIncreaseFee_labour").val(areaSetting.labour);
        $("#areaIncreaseFee_material").val(areaSetting.material);
        $("#areaIncreaseFee_machine").val(areaSetting.machine);
    });
});