"use strict";
appCore.controller("ShoutOutAddCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter", function ($state, $scope, $http, $timeout, vServices, filterFilter) {
    $scope.$root.page.title = "Add Shout Out";
    $scope.createButton = {
        heading: "Add Shout Out",
        text: "Create Shout Out",
        icon: "fa-save"
    };
	const isAdmin = $scope.emp_acls.includes(",27,");
	const acls = "," + $scope.$root.gUser.emp_acls + ",";
    $scope.emp_acls = acls;
	$scope.heroOptions = [];
	
    const stateInfo = $state.current;	
    $scope.param = $state.params;
    $scope.showAll = false;
    $scope.accounts = [];
	$scope.today = moment().format("DD-MM-YYYY");
	
	vServices.callApi("shoutout/getAllHero", "GET", {}).then((res) => {
        if (res && res.length > 0) {
          $scope.heroOptions = res;
          $scope.heroOptions.unshift({"employeeid": 0, "emp_first_name": "Select", "emp_last_name": ""});
        }
	})
    $scope.shouts = { 
		shout_id: $scope.param.id,
        emp_to: '',
        filled_by   : $scope.$root.gUser.id,
        shout_data : '',
		image_url : '',
		created_at  : $scope.today,
    
    };
	console.log($state.params.id);
	
     const forFocus = (d) => $(document).find("#" + d).focus();
     $scope.createAppreciation = () => {
        let data = $scope.shouts;
		let attachment = {'data':data,'image':data.image_url};
         if (!$state.params.id) {
			
             vServices.callApi("shoutout/addshoutout", "POST",data.image_url,data).then((res) => {
                 if (res && typeof res === 'string') {
					 	 
                     toastr.success(res, "Success!", {timeOut: 5000});
                     $scope.$root.updateMenu();
					 $state.go("HeroShoutOutList", {}, {reload: true});
				
                 }
             });
         }
         else {
             vServices.callApi("shoutout/edit", "POST",data.image_url,data ).then((res) => {
                 if (res && typeof res === 'string') {
                     toastr.success(res, "Success!", {timeOut: 5000});
                     $state.go("HeroShoutOutList", {}, {reload: true});
                 } else {
                     toastr.error("Edit not working", "Not Working", {timeOut: 5000});
                 }
             });
         }

     };

    const init = function () {

         if ($scope.param && typeof $scope.param.id !== "undefined") {
			
			
             $scope.createButton = {heading: "Edit Shout Out", text: "Edit Shout Out", icon: "fa-pen"};
            vServices.callApi("shoutout/shout_single?id=" + $scope.param.id, "GET", {}).then((res) => {
				
                 if (res) {
					 
                     $scope.shouts = res;	
                 }
            });
         }
    };
    init();
}
]);
