"use strict";
appCore.controller("RequisitionHeroDetailsCtrl", ["$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.$root.page.title = "Manage " + $state.params.type;

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

    $scope.lookupHerosList = [];
    $scope.reqRow = {};

    $scope.deleteHero = function (reqId) {
        let data = {req_id:reqId};
        vServices.callApi("requi/deleteHero", "POST", data).then((res) => {
            if (res) 
            toastr.success(res, "Success!", {timeOut: 5000});
            location.reload();
        });
    };

    const init = function () {
        vServices.callApi("requi/getReqHerosList?id="+$scope.req_id, "GET", {}).then((res) => {
            $scope.lookupHerosList = res;
            });

            vServices.callApi("requi/allRequisitionByHero/", "GET", {}).then(res => {
               let row = res.filter(em => em.id == $scope.req_id);
               $scope.reqRow = row[0]
                // console.log($scope.reqRow);
            });

        };

    init();

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

}
])
.controller("HeroMoreHeroesCtrl", function ($uibModalInstance, data, $scope,vServices) {
    $scope.data = data;
    $scope.statusList = [];
    $scope.heroes = [];
    $scope.selectedHeroes = $scope.data.heroes ? $scope.data.heroes.map(i=>parseInt(i.hero_id)) : [];
    $scope.hero = {heroes:''};

    const init = function () {
        vServices.callApi("requi/getAllHeroes", "GET", {}).then(function (res) {
            if (res && res.length > 0) {
                var selHeroes = $scope.selectedHeroes;
                //   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=>!selHeroes.includes(x.id));
            }
        })

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

    //$uibModalInstance.close(data.hero.employeeid);
    $scope.cancel = () => $uibModalInstance.dismiss("cancel");
    $scope.req = {id :$scope.data.req_id};
    /*Hero Lookup */ 
    $scope.clickToChangeHeroLookup = (d) => {
        console.log( $scope.req);
        vServices.callApi("requi/addMoreHeroes", "POST" , $scope.req)
        .then(function (res) {
            if (res) toastr.success(res, "Success!", {timeOut: 5000});
            $uibModalInstance.dismiss();
            location.reload();
            // $state.go('requisitionHeroDetails', {}, {reload: true});          
            // $state.go($state.current, {}, {reload: true});
            // $uibModalInstance.close();
        });
    };
});



