"use strict";
appCore.controller("heroesBySkillsCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "$uibModal", "$document",
    function ($state, $scope, $http, $timeout, vServices, $uibModal, $document) {
        $scope.heroesDatatable = [];
        $scope.$root.page.title = "List of Heroes By Skills";
        $scope.heroes = [];
        $scope.allHeroes = [];
        $scope.heroId = 0;
        $scope.searchSkill = '';
        $scope.selectLevel = '1';
        $scope.revenueCenter = 0;
        $scope.revenueCenterList = [];
        $scope.filter = {
            hero: 0,
            rc: 0
        };
        $scope.skillsMatrix = [];

        const init = function () {
            // $scope.showLoader = true;
            // vServices.callApi("employee/heroesBySkills", "GET", {}).then((res) => {
            //     if (res) {
            //         $scope.showLoader = false;
            //         $scope.heroes = res;
            //         $scope.$root.callDataTable("#heroSkills", 500);
            //     }
            // });

            vServices.callApi("employee/limited", "GET", {}).then(res => convertDataForProject(res));

            vServices.callApi("/account/all_revenue_centers", "GET", {}).then(function (res) {
                if (res && res.length > 0) {
                    $scope.revenueCenterList = res;
                    $scope.revenueCenterList.unshift({ "revenuecenterid": 0, "rc_name": " Select All", "rc_status": 'active' });
                }
            });

            vServices.callApi("/employee/skillMatrix", "GET", {}).then(function (res) {
                if (res) {
                    $scope.skillsMatrix = res;
                    $scope.$root.callDataTable("#skillsMatrix", 500);
                }
            });
        };

        const convertDataForProject = res => {
            if (res) {
                $scope.allHeroes = [];
                $scope.allHeroes.push({ id: 0, name: " Select Hero", emp_status: 'active' });
                res = res.filter(emp => emp.emp_status === 'active');
                res.map((o) => $scope.allHeroes.push({ id: o.employeeid, name: o.emp_first_name + (o.emp_middle_name ? ' ' + o.emp_middle_name + ' ' : ' ') + o.emp_last_name + ' (' + o.emp_code + ')', emp_status: o.emp_status }));
                $scope.allHeroes = $scope.$root.sort($scope.allHeroes, 'name');
            }
        };

        $scope.searchRC_OR_Hero = (filter) => {
            $scope.showLoader = true;
            $scope.heroes = [];
            vServices.callApi("employee/heroesBySkills?heroId=" + filter.hero + '&rc=' + filter.rc, "GET", {}).then((res) => {
                if (res) {
                    $scope.showLoader = false;
                    $scope.heroes = res;
                    $scope.$root.callDataTable("#heroSkills", 500);
                }
            });
        }

        $scope.searchSkillButton = (searchSkill, selectLevel) => {
            $scope.heroes = [];
            $scope.showLoader = true;
            if (selectLevel == '1' && searchSkill != '') {
                vServices.callApi("employee/heroesBySkills?heroId=" + $scope.filter.hero + '&rc=' + $scope.filter.rc, "GET", {}).then((res) => {
                    if (res) {
                        $scope.showLoader = false;
                        $scope.heroes = res.filter(o => o.skill.toLowerCase().includes(searchSkill.toLowerCase()));
                        $scope.$root.callDataTable("#heroSkills", 500);
                    }
                });

            }
            if (selectLevel == '2' && searchSkill != '') {
                vServices.callApi("employee/heroesBySkills?heroId=" + $scope.filter.hero + '&rc=' + $scope.filter.rc, "GET", {}).then((res) => {
                    if (res) {
                        $scope.showLoader = false;
                        $scope.heroes = res.filter(o => o.skill.toLowerCase().includes(searchSkill.toLowerCase()) && o.exp_level == 'knowledge');
                        $scope.$root.callDataTable("#heroSkills", 500);
                    }
                });
            }
            if (selectLevel == '3' && searchSkill != '') {
                vServices.callApi("employee/heroesBySkills?heroId=" + $scope.filter.hero + '&rc=' + $scope.filter.rc, "GET", {}).then((res) => {
                    if (res) {
                        $scope.showLoader = false;
                        $scope.heroes = res.filter(o => o.skill.toLowerCase().includes(searchSkill.toLowerCase()) && o.exp_level == 'hands on experience');
                        $scope.$root.callDataTable("#heroSkills", 500);
                    }
                });
            }
            if (selectLevel == '1' && searchSkill == '') {
                vServices.callApi("employee/heroesBySkills?heroId=" + $scope.filter.hero + '&rc=' + $scope.filter.rc, "GET", {}).then((res) => {
                    if (res) {
                        $scope.showLoader = false;
                        $scope.heroes = res;
                        $scope.$root.callDataTable("#heroSkills", 500);
                    }
                });
            }
            if (selectLevel == '2' && searchSkill == '') {
                vServices.callApi("employee/heroesBySkills?heroId=" + $scope.filter.hero + '&rc=' + $scope.filter.rc, "GET", {}).then((res) => {
                    if (res) {
                        $scope.showLoader = false;
                        $scope.heroes = res.filter(o => o.exp_level == 'knowledge');
                        $scope.$root.callDataTable("#heroSkills", 500);
                    }
                });
            }
            if (selectLevel == '3' && searchSkill == '') {
                vServices.callApi("employee/heroesBySkills?heroId=" + $scope.filter.hero + '&rc=' + $scope.filter.rc, "GET", {}).then((res) => {
                    if (res) {
                        $scope.showLoader = false;
                        $scope.heroes = res.filter(o => o.exp_level == 'hands on experience');
                        $scope.$root.callDataTable("#heroSkills", 500);
                    }
                });
            }
        }

        init();

    }
])
