"use strict";

appCore.controller("MyProfileAddCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter", function ($state, $scope, $http, $timeout, vServices, filterFilter) {
    $scope.params = $state.params;
    $scope.$root.page.title = "Edit My Profile";
    $scope.btn = {text: "Edit My Profile"};
    // $scope.myProfileImage = null;
    $scope.designationOptions = [];
    $scope.managerOptions = [];
    $scope.locationOptions = [
        {value: "Noida-India", label: "Noida-India"},
        {value: "Bangalore-India", label: "Bangalore-India"},
        {value: "Phoenix-USA", label: "Phoenix-USA"},
        {value: "Dallas-USA", label: "Dallas-USA"},
        {value: "Melbourne-Australia", label: "Melbourne-Australia"}
    ];
    $scope.skillsOptions = ["SQL", "JAVA", "JavaScript", "Microsoft C#", "Linux", ".NET Programming", "Oracle", "C++", "Python", "XML", "Angular", "Nodejs"];
    $scope.departmentOptions = [
        {value: "", label: "Select"},
        {value: "Accounts", label: "Accounts"},
        {value: "Admin", label: "Admin"},
        {value: "Analyst", label: "Analyst"},
        {value: "Development", label: "Development"},
        {value: "Design", label: "Design"},
        {value: "HR", label: "HR"},
        {value: "IT", label: "IT"},
        {value: "Operations", label: "Operations"},
        {value: "Project Management", label: "Project Management"},
        {value: "Quality", label: "Quality"},
        {value: "Software Testing", label: "Software Testing"},
        {value: "Sales & Marketing", label: "Sales & Marketing"},
        {value: "Technical", label: "Technical"},
    ];
    $scope.gstatus = [{label: "Active", value: "active"}, {label: "Inactive", value: "inactive"}];
    $scope.hero = {
        emp_nick_name: "",
        emp_first_name: "",
        emp_middle_name: "",
        emp_last_name: "",
        emp_code: null,
        emp_doj: $scope.today,
        emp_email: "",
        emp_mobile: "",
        emp_marital_status: "",
        emp_location: "Noida-India",
        emp_designation: 0,
        emp_department: $scope.departmentOptions[0].value,
        emp_gender: "",
        emp_status: "active",
        emp_yoe: 0,
        emp_relevant_yoe: 0,
        emp_skype: "",
        emp_skills: [],
        emp_pool: 8,
        emp_lm: 0,
        emp_pm: 0,
        is_admin: false,
        profile_img: null
    };
    $scope.selectSkills = [];
    $scope.save = () => {
        var file = {'file': $scope.hero.profile_img};
        if (!file) {
            toastr.error('no file to upload....');
        }
        else {
            vServices.callApi("employee/updateProfilePic", "POST", {}, file).then((res) => {
                // console.log(res)
                if (res) {
                    toastr.success('Profile Image updated', 'Profile ');
                    $scope.$root.gUser.profile_image = res;
                }
                // $state.go("dashboard", {}, {reload: true});
            });
        }
    }, (e) => {
        console.log(e);
    };

    const init = () => {
        vServices.callApi("employee/manager/all", "GET", {}).then((res) => {
            if (res && res.length > 0) {
                $scope.managerOptions = res;
                $scope.managerOptions.unshift({"employeeid": 0, "emp_first_name": "Select", "emp_last_name": ""});
            }
            $scope.btn = {text: "Edit Hero"};
            vServices.callApi("employee/" + $scope.$root.gUser.id, "GET", {}).then((res) => {
                if (res) {
                    $scope.hero = res;
                    for (let i = 0; i < $scope.managerOptions.length; i++) {
                        if ($scope.hero.emp_lm && $scope.managerOptions[i].employeeid === $scope.hero.emp_lm) {
                            $scope.hero.emp_lm = $scope.managerOptions[i].employeeid;
                        }
                        if ($scope.hero.emp_pm && $scope.managerOptions[i].employeeid === $scope.hero.emp_pm) {
                            $scope.hero.emp_pm = $scope.managerOptions[i].employeeid;
                        }

                        if (i === $scope.managerOptions.length - 1) {
                            $scope.hero.emp_lm = $scope.hero.emp_lm ? $scope.hero.emp_lm : $scope.managerOptions[0].employeeid;
                            $scope.hero.emp_pm = $scope.hero.emp_pm ? $scope.hero.emp_pm : $scope.managerOptions[0].employeeid;
                        }
                    }

                    for (let i = 0; i < $scope.designationOptions.length; i++) {
                        if ($scope.designationOptions[i].desg_name === $scope.hero.emp_designation) {
                            $scope.hero.emp_designation = $scope.designationOptions[i].id;
                        }
                    }
                    if ($scope.hero.emp_skills) $scope.selectSkills = $scope.hero.emp_skills.indexOf(",") > -1 ? $scope.hero.emp_skills.split(",").map(o => {
                        o = {text: o};
                    }) : [{text: $scope.hero.emp_skills}];
                }
            });

        });
    };
    init();

}])
;
