"use strict";

appCore.controller("MySkillsAddCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter", function ($state, $scope, $http, $timeout, vServices, filterFilter) {
    $scope.params = $state.params;
    $scope.$root.page.title = "Update My Skills";
    $scope.btn = { text: "Update My Skills" };
    $scope.selectSkills = [];
    $scope.editSkillsEnable = false;
    $scope.oldSkills = [];
    $scope.experienceLevelOptions = [
        { "explvlid": " ", "explvltext": "Select" },
        { "explvlid": "knowledge", "explvltext": "Knowledge" },
        { "explvlid": "hands on experience", "explvltext": "Hands-on Experience" },
        { "explvlid": "theoretical knowledge", "explvltext": "Theoretical Knowledge" },
        { "explvlid": "beginner hands on", "explvltext": "Beginner Hands on" },
        { "explvlid": "intermediate hands on", "explvltext": "Intermediate Hands on" },
        { "explvlid": "advanced hands on", "explvltext": "Advanced Hands on" }
    ]
    $scope.editSkillsEnableChange = () => {
        $scope.editSkillsEnable = true;
    };
    $scope.exp = { emp_yoe: 0, emp_relevant_yoe: 0 };
    $scope.selSklGroup = '0';
    $scope.tempArr = [];
    $scope.sklGroupList = [];
    $scope.dtable = null;
    $scope.save = () => {
        // console.log($scope.skillList);
        var dataRes = new Set();
        $scope.tempArr.forEach(i=>{
            $scope.skillList.forEach(j=>{
                if(i.skill_id == j.skill_id){
                    dataRes.add(j);
                }else{
                    dataRes.add(i);
                }
            })
        })
        const arrayList = [...dataRes];
        // console.log(arrayList);
        // toastr.success("We are working on it, it will live shortly.");
        // let isUpdateMonths = $scope.skillList.filter((o) => !o.skill_experience_months && o.skill_experience_level);
        // if (isUpdateMonths.length > 0) {
        //     toastr.error("Please update months.");
        // } else {
        // let dataResult = $scope.skillList.filter((o) => o.skill_experience_months && o.skill_experience_level);
        // console.log(dataResult);
        if (arrayList) {
            let dataResult = arrayList.filter((o) => o.skill_experience_months && o.skill_experience_level);
            vServices.callApi("employee/editMySkills", "PUT", { myval: JSON.stringify(dataResult) }).then((res) => {
                if (res) {
                    toastr.success(res, "Message");
                }
                $state.go("mySkillsAdd", {}, { reload: true });
            });
        }
    }, (e) => {
        console.log(e);
    };
    $scope.updateExperience = () => {
        if ($scope.exp) {
            vServices.callApi("employee/editMyExperience", "PUT", $scope.exp).then((res) => {
                if (res) {
                    toastr.success(res, "Message");
                }
                $state.go("mySkillsAdd", {}, { reload: true });
            });
        }
    }, (e) => {
        console.log(e);
    };

    $scope.filterSklGroup = (val) => {
        if ($scope.dtable) $scope.dtable.destroy();
        var tmpArr = $scope.tempArr;
        if (val != 0) {
            $scope.skillList = tmpArr.filter(x => x.skl_grp_id == val);
        } else {
            $scope.skillList = tmpArr;
        }
        $scope.$root.callDataTable("#masterList", 500, (t) => {
            $scope.dtable = t;
        }, { orientation: "landscape" });
    }

    $scope.removeMyskill = (dl) => {
        var rep = confirm("are you sure want to remove this skill?");
        if (rep) {
            vServices.callApi("employee/removeMySkill?empSkillId=" + dl, "GET").then((res) => {
                if (res) {
                    toastr.success(res, "Message");
                }
                $state.go("mySkillsAdd", {}, { reload: true });
            });
        }
    }
    const init = () => {
        $scope.skillList = [];
        $scope.params.employeeid = $scope.$root.gUser.id;
        vServices.callApi("employee/allSkills", "GET", {}).then(function (res) {
            if (res && res.length > 0) {
                vServices.callApi("employee/" + $scope.$root.gUser.id, "GET", {}).then((res) => {
                    if (res) {
                        $scope.exp.emp_yoe = res.emp_yoe;
                        $scope.exp.emp_relevant_yoe = res.emp_relevant_yoe;
                    }
                });
                let mySkillData = [];
                vServices.callApi("employee/mySkills", "GET", {}).then(function (res1) {
                    if (res1 && res1.length > 0)
                        res1.forEach(o => {
                            mySkillData[o.skill_id] = { employeeskillid: o.employeeskillid, skill_experience_level: o.skill_experience_level, skill_experience_months: o.skill_experience_months, updated_skill: o.updated_skill }
                        });
                    res.forEach((o, i) => {
                        if (mySkillData[o.skillid]) {
                            $scope.oldSkills.push({ employeeskillid: mySkillData[o.skillid]['employeeskillid'], updated_skill: mySkillData[o.skillid]['updated_skill'], name: o.skl_name, skillgroupname: o.sg_name, skl_grp_id: o.skillgroupid, skill_experience_level: mySkillData[o.skillid].skill_experience_level, skill_experience_months: parseInt(mySkillData[o.skillid].skill_experience_months) });
                            $scope.skillList.push({ skill_id: o.skillid, name: o.skl_name, desc: o.skl_description, skillgroupname: o.sg_name, skl_grp_id: o.skillgroupid, skill_experience_level: mySkillData[o.skillid].skill_experience_level, skill_experience_months: parseInt(mySkillData[o.skillid].skill_experience_months) });
                        }
                        else
                            $scope.skillList.push({ skill_id: o.skillid, name: o.skl_name, skillgroupname: o.sg_name, desc: o.skl_description, skl_grp_id: o.skillgroupid });
                    });
                    $scope.tempArr = $scope.skillList;
                    $scope.sklGroupList = $scope.skillList.map(x => {
                        return { skl_grp_id: x.skl_grp_id, skillgroupname: x.skillgroupname };
                    });
                    $scope.sklGroupList[0]['skl_grp_id'] = '0';
                    $scope.sklGroupList[0]['skillgroupname'] = 'Select Skill Group';
                });

                $scope.$root.callDataTable("#masterList", 500);
                $scope.$root.callDataTable("#myskills", 500);
            }
        });
    };
    init();

}])
    ;
