"use strict";
appCore.controller("EmailNotificationConfigurationCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", function ($state, $scope, $http, $timeout, vServices) {
    $scope.params = $state.params;
    $scope.$root.page.title = "Template";
    $scope.dataList = [];
    $scope.notificationType = [{'id':1,'value':'Birthday Notification'},
                                {'id':2,'value':'Work Anniversary Notification'},
                                {'id':3,'value':'Certification Expiration'}];
    $scope.customFeild = false;
    
    // Add new Feild On custom Select
    $scope.changeOncustomSelect = function(val){    
        if(val=='custom'){
            $scope.customFeild=true;
        }else{
            $scope.customFeild=false;
        }
    }

    $scope.edit = (dl) => {
        $scope.data = JSON.parse(JSON.stringify(dl));
        $scope.data.days_interval = $scope.data.days_interval ? parseInt($scope.data.days_interval) : 0; 
        $scope.data.cty_location = $scope.data.cty_location.toString();
        $scope.data.email_template_id = $scope.data.email_template_id.toString();
        $scope.data.frequency = $scope.data.frequency.toLowerCase();
        if ($scope.data.notification_type=='Birthday Notification') {
            $scope.data.notification_type='1';
        }
        if ($scope.data.notification_type=='Work Anniversary Notification') {
            $scope.data.notification_type='2';
        }
        if ($scope.data.notification_type=='Certification Expiration') {
            $scope.data.notification_type='3';
        }
        console.log($scope.data);     

        $scope.page.type = "Edit";
        $(document).find("#tag").focus();
        window.screen.width < 768 ? window.scrollTo(0, 0) : null;
        setTimeout(() => {
            $(document).find("#formCard").addClass("highlightme");
            setTimeout(() => {
                $(document).find("#formCard").removeClass("highlightme");
            }, 800);
        }, (window.screen.width < 768 ? 500 : 0));
    };

    // $scope.delete = (d) => {
    //     // console.log(d.id);
    //     vServices.callApi("emailNotificationConfiguration/changeENCStatus/" + d.id , "DELETE" , {}).then(function (res) {
    //         if (res) toastr.success(res, "Success!", {timeOut: 5000});
    //         $state.go($state.current, {}, {reload: true});
    //     });
    // };
    $scope.changeStatus = (d) => {
        vServices.callApi("emailNotificationConfiguration/changeENCStatus/" + d.id +"/"+d.status, "PUT" , {}).then(function (res) {
            if (res) toastr.success(res, "Success!", {timeOut: 5000});
            $state.go($state.current, {}, {reload: true});
        });
    };


    $scope.add = (d) => {
        vServices.callApi("emailNotificationConfiguration/addOrEditENC/" + (d.id ? "edit" : "add"), d.id ? "PUT" : "POST", d).then(function (res) {
            if (res) toastr.success(res, "Success!", {timeOut: 5000});
            $state.go($state.current, {}, {reload: true});
        });
    };



    const init = function () {

        // console.log($scope.notificationType);

            $scope.dataList = [];
            $scope.countryList = [];
            $scope.emailTempList = [];
            vServices.callApi("emailNotificationConfi/getEmailNotificationConfigurationList", "GET", {}).then((res) => {
                res.forEach(element => {
                    if (element.notification_type==1) {
                        element.notification_type='Birthday Notification';
                    }
                    if (element.notification_type==2) {
                        element.notification_type='Work Anniversary Notification';
                    }
                    if (element.notification_type==3) {
                        element.notification_type='Certification Expiration';
                    }
                });
                $scope.dataList = res;

            });
            
            vServices.callApi("CommonController/getCountriesList", "GET", {}).then(function (res) {
                if (res && res.length > 0) {
                    $scope.countryList = res;
                    console.log(res);
                }
            }); 
            vServices.callApi("employee/all_email_templates", "GET", {}).then(function (res) {
                if (res && res.length > 0) {
                    $scope.emailTempList = res;
                }
            }); 
        }
    ;
    init();

}
]);



