"use strict";
appCore.controller("ClientAppreciationsAddCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter", function ($state, $scope, $http, $timeout, vServices, filterFilter) {
    $scope.$root.page.title = "Add Appreciations";
    $scope.createButton = {
        heading: "Add Appreciation",
        text: "Create Appreciation",
        icon: "fa-save"
    };
	const isAdmin = $scope.emp_acls.includes(",27,");
	const acls = "," + $scope.$root.gUser.emp_acls + ",";
    $scope.emp_acls = acls;
	
    const stateInfo = $state.current;	
    $scope.param = $state.params;
    $scope.showAll = false;
    $scope.accounts = [];
	$scope.today = moment().format("DD-MM-YYYY");
	
    //$scope.gstatus = [{label: "Select", value: ""}, {label: "Active", value: "active"}, {label: "Inactive", value: "inactive"},];
    $scope.appreciation = { 
       id: $scope.param.id,
        hero_id: $scope.$root.gUser.id,
        project_name : '',
        apricated_on  : $scope.today,
        apricated_by   : '',
        team_members_name   : '',
        notes : '',
        verified  : '0',
		screenshot : '',
        
        
    };
	console.log($state.params.id);
	
     const forFocus = (d) => $(document).find("#" + d).focus();
     $scope.createAppreciation = () => {
       // const mobilePattern = new RegExp("^[0-9()+ -]*$");
         let data = $scope.appreciation;
		let attachment = {'data':data,'image':data.screenshot};
         if (!$state.params.id) {
			
             vServices.callApi("clientappreciations/addca", "POST",data.screenshot,data).then((res) => {
                 if (res && typeof res === 'string') {
					 	 
                     toastr.success(res, "Success!", {timeOut: 5000});
                     $scope.$root.updateMenu();
					 $state.go("HeroAppreciationlList", {}, {reload: true});
					 /* if(isAdmin)
                     $state.go("ClientAppreciationsList", {}, {reload: true});
					 if (!isAdmin && acls.includes(",36,"))
					$state.go("HeroAppreciationlList", {}, {reload: true}); */
					
                 }
             });
         }
         else {
             vServices.callApi("clientappreciations/edit", "POST",data.screenshot,data ).then((res) => {
				// console.log($scope.appreciation);
                 if (res && typeof res === 'string') {
                     toastr.success(res, "Success!", {timeOut: 5000});
                     $state.go("HeroAppreciationlList", {}, {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 Client Appreciations", text: "Edit Client Appreciations", icon: "fa-pen"};
            vServices.callApi("clientappreciations/ca_single?id=" + $scope.param.id, "GET", {}).then((res) => {
				
                 if (res) {
					 
                     $scope.appreciation = res;	
                 }
            });
         }

         console.log($scope.appreciation);

    };
    init();
}
]);
