"use strict";

appCore.controller("projectWeeklyForecastCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter",
    function ($state, $scope, $http, $timeout, vServices, filterFilter) {
        $scope.$root.page.title = "Project Weekly Timeline";
        $scope.btn = {name: "Set Project Info", icon: "fa-save", screen: "Add"};
        $scope.params = $state.params;
        $scope.projectId = 0;
        $scope.project = {
            id:0,
            proj_id: 0,
            forecast:[
                {profile_id:'0',week_number:1,weekly_rate:1}
            ]
        };
        $scope.forecastProfile=[];

        $scope.addRow = () =>{
            var ind = $scope.project.forecast.length;
            $scope.project.forecast.push({profile_id:'0',week_number:ind+1,weekly_rate:1});
        }

        $scope.removeRow = (index)=>{
            var obj = $scope.project.forecast[index];
            if(obj.id && obj.id!=0){
                vServices.callApi("project/deleteForcast?ids="+obj.id, "GET").then(function (res) {
                    if (res){
                        $scope.project.forecast.splice(index,1);
                    }
                });
            }else{
                $scope.project.forecast.splice(index,1);
            }
        }

        $scope.createProject = () => {
            if($scope.project.forecast[0].id){
                $scope.project.forecast.forEach(r=>{
                    delete r.proj_name;
                    r.profile_id = parseInt(r.profile_id);
                    r.proj_id = parseInt($state.params.projectid);
                    r.updated_at = 'NOW()';
                    r.status = 1;
                    r.updated_by = parseInt($scope.$root.gUser.id);
                })
            }else{
                $scope.project.forecast.forEach(r=>{
                    r.profile_id = parseInt(r.profile_id);
                    r.proj_id = parseInt($state.params.projectid);
                    r.created_at = 'NOW()';
                    r.updated_at = 'NOW()';
                    r.status = 1;
                    r.created_by = parseInt($scope.$root.gUser.id);
                    r.updated_by = parseInt($scope.$root.gUser.id);
                })
            }
            vServices.callApi("project/project_forecast_add", "POST", {'myval':JSON.stringify($scope.project.forecast)}).then(function (res) {
                if (res) {
                    toastr.success(res, "Success!", {timeOut: 5000});
                    window.location.reload();
                }
            });
        };

        $scope.cloneData = () =>{
            const len = $scope.project.forecast.length - 1;
            const lastWeek = $scope.project.forecast[len];
            var forecast = $scope.project.forecast.filter(x=>x.week_number==lastWeek.week_number);
            forecast.forEach(r=>{
                delete r.id;
                delete r.proj_name;
                r.week_number = parseInt(r.week_number) + 1;
                r.profile_id = parseInt(r.profile_id);
                r.proj_id = parseInt($state.params.projectid);
                r.created_at = 'NOW()';
                r.updated_at = 'NOW()';
                r.status = 1;
                r.created_by = parseInt($scope.$root.gUser.id);
                r.updated_by = parseInt($scope.$root.gUser.id);
            })
            vServices.callApi("project/project_forecast_add", "POST", {'myval':JSON.stringify(forecast)}).then(function (res) {
                if (res) {
                    toastr.success(res, "Success!", {timeOut: 5000});
                    window.location.reload();
                }
            });
        }

        const init = function () {
            vServices.callApi("project/project_forecast_dtl?projectid=" + $state.params.projectid, "GET").then(function (res) {
                if (res.length>0){
                    $scope.$root.page.title = "Edit Project Info";
                    $scope.btn = {name: "Edit Project Info", icon: "fa-save", screen: "Edit"};
                    res.forEach(i=>{
                        i.profile_id = i.profile_id.toString();
                        i.weekly_rate = parseInt(i.weekly_rate); 
                    })
                    $scope.project = {
                        id:res[0].id,
                        proj_id: res[0].proj_id,
                        proj_name:res[0].proj_name,
                        forecast : res
                    };
                }else{
                    vServices.callApi("project/fetch_project?proj_id="+$state.params.projectid, "GET").then(function (res) {
                        if (res.length>0){
                            $scope.project.proj_name = res[0].proj_name;
                        }
                    });
                }
            });
            vServices.callApi("project/projectForecastProfile", "GET").then(function (res) {
                if (res.length>0){
                    $scope.forecastProfile = res;
                }
            });
        };
        init();
    }
]);
