"use strict";
appCore.controller("RequisitionDashboardCtrl", ["$state", "$scope", "$http", "$timeout", "$uibModal", "vServices", 
function ($state, $scope, $http, $timeout, $uibModal, vServices) {
    $scope.params = $state.params;
    $scope.$root.page.title = "Template";
    $scope.params = $state.params;
    $scope.currentUser = $scope.$root.gUser;
    $scope.myself_id = $scope.$root.gUser.id;
    $scope.dTable = null;
    $scope.dTable2 = null;
    $scope.heroesList = [];
    $scope.inactiveRequisitions = [];
    $scope.raised_by=0;

    // $scope.$root.page.title = "Manage " + $state.params.type;

    $scope.page = {
            allow: false,
            name: "Requisition",
            type: "Dashboard ",
            icon: "fa-users", 
            color: "purple"
        };

    $scope.requisitionList = [];



    $scope.clickToApprove = (d) => {
        vServices.callApi("requi/changeReqApprovalStatus?id=" + d, "GET" , {})
        .then(function (res) {
            if (res) toastr.success(res, "Success!", {timeOut: 5000});
            $state.go($state.current, {}, {reload: true});
        });
    };
    
    $scope.req = {};

    $scope.deleteRequisition = (d) => {
        vServices.callApi("requi/deleteRequisition", "POST" , {'req_id':d})
        .then(function (res) {
            if (res) toastr.success(res, "Success!", {timeOut: 5000});
            $state.go($state.current, {}, {reload: true});
        });
    };

    $scope.cloneReq = (d) => {
        vServices.callApi("requi/getRequisitionRow?id=" + d, "GET" , {})
        .then(function (res) {
            $scope.req = res;
            // console.log($scope.req);
            if (res) toastr.success(res, "Success!", {timeOut: 5000});
            $state.go($state.current, {}, {reload: true});
        });
    };
    $scope.cloneRequisition = (reqRow) => {
        // console.log(reqRow);
        $scope.req = reqRow;
        $state.go('requisitionAdd', {}, {reload: true}); 
    };
    
    /*19Sep23---------------------------*/ 
    $scope.statusList = [];
    $scope.allRequisitionList = [];
     /*------------------------------------------ */
     $scope.filterByStatus = (status_id) => {
        if($scope.dTable)$scope.dTable.destroy();
        var tmp = $scope.allRequisitionList;
        $scope.requisitionList = [];
        if(status_id==0){
            $scope.requisitionList =  tmp;
            // $scope.$root.callDataTable(".requisitionList", 1000);
            $scope.$root.callDataTable(".requisitionList", 1000, (t) => {
                $scope.dTable = t;
            }, {orientation: "landscape"});
        }else{           
            $scope.requisitionList = tmp.filter(x=>x.req_status==status_id);
            // $scope.$root.callDataTable(".requisitionList", 1000);
            $scope.$root.callDataTable(".requisitionList", 1000, (t) => {
                $scope.dTable = t;
            }, {orientation: "landscape"});
        }

    }
    /*End-19Sep23---------------------------*/
    
    $scope.filterByRequirement = (raised_by) => {
        if($scope.dTable)$scope.dTable.destroy();
        var tmp = $scope.allRequisitionList;
        $scope.requisitionList = [];
        if(raised_by==0){
            $scope.requisitionList =  tmp;
            // $scope.$root.callDataTable(".requisitionList", 1000);
            $scope.$root.callDataTable(".requisitionList", 1000, (t) => {
                $scope.dTable = t;
            }, {orientation: "landscape"});
        }else{           
            $scope.requisitionList = tmp.filter(x=>x.on_behalf_of==raised_by);
            // $scope.$root.callDataTable(".requisitionList", 1000);
            $scope.$root.callDataTable(".requisitionList", 1000, (t) => {
                $scope.dTable = t;
            }, {orientation: "landscape"});
        }

    }
    
  

    const init = function () {
        if($scope.dTable)$scope.dTable.destroy();
        /*19Sep23---------------------------*/ 
        vServices.callApi("requi/getReqStatusList/", "GET", {}).then((res) => {
            // $scope.statusList = res.filter(x=>x.id!=1 && x.id!=2);
            var abc = res;
            abc.unshift({id:'0',status_name:'All'});
            $scope.statusList = abc;
        });
        /*End-19Sep23---------------------------*/ 

        $scope.myself_id = $scope.$root.gUser.id;
        vServices.callApi("requi/allRequisitionByHero/", "GET", {}).then(res => {
            if (res) {
                vServices.callApi("requi/getAllReqAuthority/", "GET", {}).then(res2 => {
                    if (res2) {
                        res.forEach(element => {
                            var tempArray = []; 
                            res2.forEach(item => {
                                if (item.req_id==element.id) {
                                    tempArray.push(item);
                                }
                            });
                            element.req_auth=tempArray;
                        });
                    }
                });

                // $scope.allRequisitionList = JSON.parse(JSON.stringify(res));
                $scope.allRequisitionList = res.filter(x=>x.req_status!=4 && x.req_status!=5);
                $scope.requisitionList =  res.filter(x=>x.req_status!=4 && x.req_status!=5);
                $scope.inactiveRequisitions = res.filter(x=>x.req_status==4 || x.req_status==5);
                $scope.$root.callDataTable(".requisitionList", 1000, (t) => {
                    $scope.dTable = t;
                }, {orientation: "landscape"});

                $scope.$root.callDataTable(".inactiveRequisitions", 1000, (t) => {
                    $scope.dTable = t;
                }, {orientation: "landscape"});
            }
        });

        vServices.callApi("requi/listHeroesRaised", "GET", {}).then(function (res) {
            if (res && res.length > 0) {
                res.forEach(i=>{
                    i.fullname = i.emp_first_name + ' ' + i.emp_last_name;
                })
                $scope.heroesList = res;
            }
        });
    };
    init();

    /* Model code start*/
    $scope.openHeroLookup = (obj) => {
        // console.log(obj);
        let modalInstance = $uibModal.open({
            animation: true,
            ariaLabelledBy: "modal-title",
            ariaDescribedBy: "modal-body",
            templateUrl: "HeroLookup.html",
            controller: "HeroLookupDashCtrl",
            controllerAs: "$scope",
            size: "lg",//sm md lg
            backdrop: "static",
            resolve: {
                data: () => {
                    return {
                        hero: obj,
                    };
                }
            }
        });
        modalInstance.result.then((data) => {
            $scope.deleteHero(data);
        }, () => {
            // toastr.warning("Hero Lookup has been canceled", "Cancel");
        });
    };
    /* Model code end*/

    /* Model code start*/
    $scope.changeExternalStatus = (obj) => {
        // console.log(obj);
        let modalInstance = $uibModal.open({
            animation: true,
            ariaLabelledBy: "modal-title",
            ariaDescribedBy: "modal-body",
            templateUrl: "changeExternalStatus.html",
            controller: "ChangeExternalStatusCtrl",
            controllerAs: "$scope",
            size: "lg",//sm md lg
            backdrop: "static",
            resolve: {
                data: () => {
                    return {
                        hero: obj,
                    };
                }
            }
        });
        modalInstance.result.then((data) => {
            $scope.deleteHero(data);
        }, () => {
            // toastr.warning("Hero Lookup has been canceled", "Cancel");
        });
    };
    /* Model code end*/

    /*--------------------------------------------*/
    /*-Model For Pool Manager Status Change-*/
    $scope.statusChangeByPool = (obj) => {
        // console.log(obj);
        let modalInstance = $uibModal.open({
            animation: true,
            ariaLabelledBy: "modal-title",
            ariaDescribedBy: "modal-body",
            templateUrl: "statusChangeByPool.html",
            controller: "StatusChangeByPool",
            controllerAs: "$scope",
            size: "lg",//sm md lg
            backdrop: "static",
            resolve: {
                data: () => {
                    return {
                        hero: obj,
                    };
                }
            }
        });
        modalInstance.result.then((data) => {
            $scope.deleteHero(data);
        }, () => {
            // toastr.warning("Hero Lookup has been canceled", "Cancel");
        });
    };
    /*-End Model For Pool Manager Status Change-*/
}
])
/*Status Change By Pool Manager */ 
.controller("StatusChangeByPool", function ($scope, $uibModalInstance, data, vServices) {
            $scope.data = data;
            $scope.cancel = () => $uibModalInstance.dismiss("cancel");
            $scope.req = {id :$scope.data.hero.id, req_status :'3', sourcing_status :'internal'};
            $scope.statusList = [];
            vServices.callApi("requi/getReqStatusList/", "GET", {}).then((res) => {
                // $scope.statusList = res.filter(x=>x.id!=1 && x.id!=2);
                $scope.statusList = res.filter(x=>x.id!=1);
            });

            $scope.clickToChangeHeroLookup = (d) => {
                $scope.req.id = $scope.data.hero.id;
                // console.log($scope.req,$scope.data);
                vServices.callApi("requi/changeHeroLookUpStatus", "POST" , $scope.req)
                .then(res=>{
                    if (res){
                        toastr.success(res, "Success!");
                        $uibModalInstance.dismiss("cancel");
                    }
                });
            };
           
            $scope.clickToChangeStatusByPool = (d) => {
                // $scope.req.id = $scope.data.hero.id;
                // console.log(d);
                // console.log($scope.req,$scope.data);
                $scope.req.closing_date = $scope.$root.convertDate2DateTimeUTC($scope.req.closing_date);

                vServices.callApi("requi/clickToChangeStatusByPool", "POST" , $scope.req)
                .then(res=>{
                    if (res){
                        toastr.success(res, "Success!");
                        $uibModalInstance.dismiss("cancel");
                        $state.go('requisitionDashboard', {}, {reload: true});
                    }
                });
            };
})
.controller("HeroLookupDashCtrl", function ($scope, $uibModalInstance, data, vServices) {
            $scope.data = data;
            $scope.cancel = () => $uibModalInstance.dismiss("cancel");
            $scope.req = {id :$scope.data.hero.id, req_status :'3', sourcing_status :'internal'};
            $scope.statusList = [];
            $scope.heroes = [];
            $scope.hero = {heroes:''};
            
            vServices.callApi("requi/getAllHeroes", "GET", {}).then(function (res) {
                if (res && res.length > 0) {
                    res.sort((a, b) => {
                    let prop = "emp_first_name";
                    return a[prop] > b[prop] ? 1 : a[prop] < b[prop] ? -1 : 0;
                    });
                    $scope.heroes = res.filter(x=>x.emp_status=='active');
                }
            })

            vServices.callApi("requi/getReqStatusList/", "GET", {}).then((res) => {
                // $scope.statusList = res.filter(x=>x.id!=1 && x.id!=2);
                $scope.statusList = res.filter(x=>x.id!=1);
            });

            $scope.clickToChangeHeroLookup = (d) => {
                $scope.req.id = $scope.data.hero.id;
                // console.log($scope.req,$scope.data);
                vServices.callApi("requi/changeHeroLookUpStatus", "POST" , $scope.req)
                .then(res=>{
                    if (res){
                        toastr.success(res, "Success!");
                        $uibModalInstance.dismiss("cancel");
                        $state.go('requisitionDashboard', {}, {reload: true});

                    }
                });
            };
})
.controller("ChangeExternalStatusCtrl", function ($scope, $uibModalInstance, data, vServices) {
    $scope.data = data;
    $scope.cancel = () => $uibModalInstance.dismiss("cancel");
    $scope.req = {id :$scope.data.hero.id, req_status :'3', sourcing_status :'internal'};
    $scope.statusList = [];
    $scope.heroes = [];
    $scope.hero = {heroes:''};
    
    vServices.callApi("requi/getAllHeroes", "GET", {}).then(function (res) {
        if (res && res.length > 0) {
            res.sort((a, b) => {
            let prop = "emp_first_name";
            return a[prop] > b[prop] ? 1 : a[prop] < b[prop] ? -1 : 0;
            });
            $scope.heroes = res.filter(x=>x.emp_status=='active');
        }
    })

    vServices.callApi("requi/getReqStatusList/", "GET", {}).then((res) => {
        $scope.statusList = res.filter(x=>x.id!=1);
    });

    $scope.changeExtStatus = (d) => {
        $scope.req.id = $scope.data.hero.id;
        $scope.req.closing_date = moment($scope.req.closing_date).format('DD-MM-YYYY');
        vServices.callApi("requi/changeExtStatus", "POST" , $scope.req)
        .then(res=>{
            if (res){
                toastr.success(res, "Success!");
                $uibModalInstance.dismiss("cancel");
            }
        });
    };

    
});



