"use strict";
appCore.controller("skillsUpdatedCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "$uibModal", "$document",
    function ($state, $scope, $http, $timeout, vServices, $uibModal, $document) {
      $scope.$root.page.title = "Hero Skill Updated";
      $scope.skills = [];
      $scope.unfilledSkills = [];
      $scope.emailtemplates=[];
      $scope.emailtemplateSingle=[];
      $scope.emailTempId = '0';

      $scope.sendReminder = (empId) => {
        const _html = `Do you really want to send mail ?`;
        $scope.$root.prompt(_html, 'Mail operation has been canceled!').then(result => {
          if (result) {
            vServices.callApi("skill/sendSkillReminder?hero="+empId, "GET", {}).then(function (res) {
              if(res){
                toastr.success('Skill Reminder has sent successfully.');
              }
              });
            }
        });
      }

      const init = function () {
        vServices.callApi("skill/getSkillMonthly", "GET", {}).then(function (res) {
          if (res && res.length > 0) {
            $scope.skills = res;
            $scope.skills.forEach(item => {
              item.emp_doj2 = moment(item.emp_doj).format('YYYY-MM-DD HH:mm:ss');
              if(moment(item.updatedAt).format('YYYY-MM-DD') == moment(new Date()).format('YYYY-MM-DD') ){
                item.noOfdays = moment(new Date()).diff(moment(item.updatedAt), 'days');
              }else{
                item.noOfdays = moment(new Date()).diff(moment(item.updatedAt), 'days') + 1;
              }
               
            });
            $scope.$root.callDataTable("#skillReport");
          }
        });
        vServices.callApi("skill/getUnfilledSkills", "GET", {}).then(function (res) {
          if (res && res.length > 0) {
            $scope.unfilledSkills = res;
            $scope.unfilledSkills.forEach(item => {
              item.emp_doj2 = moment(item.emp_doj).format('YYYY-MM-DD HH:mm:ss');
            });
            $scope.$root.callDataTable("#skillUnfilledReport");
          }
        });
      };
	  
      init();

      // Email send to Hero start
      $scope.openEmailPopup = (jsonQ, idx = null) => {
        let modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'sendEmailTemp.html',
            controller: 'sendEmailTempCtrl',
            size: 'xl',//sm md,lg,xl
            controllerAs: "$scope",
            backdrop: "static",
            keyboard  : false,
            resolve: {
                data: () => {
                    return {idx, single: JSON.parse(JSON.stringify(jsonQ))};
                }
            }
        });
        
        modalInstance.result.then(function () {
            toastr.success('Mails Send successfully to heroes.');
        }, () => {
            //$log.info('Modal dismissed at: ' + new Date());
        });
    };
      // Email send to Hero end
    }
  ])

      // Created By Yasin on 28 Feb 2023
      // Purpose: Modal open for meail sending, start
  .controller('sendEmailTempCtrl', function ($scope, $uibModalInstance, data, vServices) {
		$scope.cancel = () => $uibModalInstance.dismiss(false);
		$scope.data = data;// single idx
		$scope.data.sendmail = [];
		$scope.mInfo = {
			head_icon: "fas fa-plus-circle",
			head_title: "Sent Email",
			btn_icon: "fas fa-save",
			btn_title: "Send Mail",
			today: new Date()
		};

        vServices.callApi("/employee/all_email_templates", "GET", {}).then(res => {
            $scope.allMailTemplates = res;
        });

        $scope.selectTemplate = function(val){   
          vServices.callApi("employee/email_template_single/"+val, "GET", $scope.emailtemplates).then(function (res) {
            if (res) {
              $scope.emailtemplateSingle=res;
                // toastr.success(res, "Success!", {timeOut: 5000});
                // $scope.$root.updateMenu();
                // $state.go("emailtemplateList", {}, {reload: true});
            }
        });
      }

		$scope.send = (d) => {
     
          // if($scope.template_id == '' || $scope.template_id == 'undefined'){
          //     toastr.error("Please fill all required fields","Alert");
          //     return false;
          // }else{

              vServices.callApi("/employee/postUpdateSkillMailReminder", "POST", {myval: JSON.stringify(data) , emailTemp: JSON.stringify(d)}).then(data => {
                if(data){
                  toastr.success("Email has been sent successfully.","Success");
                  $uibModalInstance.dismiss(false);
                }
              });
          // }

		}
	})
  // Purpose: Modal open for meail sending, end

