<?php
/**
 * Description of Ajax
 *
 * @author carlos
 */
class ScheduleEmployeeAjax extends ScheduleEmployeeRepository {
    
    public $flashmessenger = null;
    
    public function __construct() {
        if(!$this->flashmessenger instanceof FlashMessenger){
            $this->flashmessenger = new FlashMessenger();
        }
    }
    
    public function getResponse($request, $options) {
        return $this->$request($options);
    }
    
    public function _getTranslation($text){
        $translator = new Translator();
        return $translator->_getTranslation($text);
    }
    
    public function getTranslation($options){
        $msj = $options['msj'];
        
        return array(
            'response'=>true,
            'translation'=>$this->_getTranslation($msj)
                );
    }    
    
    public function saveSchedule($options){       
        $data = array();
        foreach($options['options'] as $key => $option){  //var_dump($option);    
           
            $data[$option['name']] = $option['value'];           
            /*
            if(strpos($option['name'], 'start_hour_')!== false){
                $name = str_replace('start_hour_', '', $option['name']);
                $data[$name]['start_hour'] = $option['value'];
            }

            if(strpos($option['name'], 'start_hour_2_')!== false){
                $name = str_replace('start_hour_2_', '', $option['name']);
                $data[$name]['start_hour_2'] = $option['value'];
            }   
            
            if(strpos($option['name'], 'end_hour_')!== false){
                $name = str_replace('end_hour_', '', $option['name']);
                $data[$name]['end_hour'] = $option['value'];
            }

            if(strpos($option['name'], 'end_hour_2_')!== false){
                $name = str_replace('end_hour_2', '', $option['name']);
                $data[$name]['end_hour_2'] = $option['value'];
            }*/   
        }               
        
        set_error_handler(function ($err_severity, $err_msg, $err_file, $err_line, array $err_context){
            throw new ErrorException( $err_msg, 0, $err_severity, $err_file, $err_line );
        }, E_WARNING);
        
        try{
            /*Validar que sucursal tengo horario asginado*/
            $storeRepo = new StoreRepository();
            if(!$storeRepo->hasScheduled($data['store_id'])){
                $this->flashmessenger->addMessage(array('danger'=>'Sucursal seleccionada no tiene horario asignado.<br/>Asignar horario a sucursal y despues intentar nuevamente.'));
                return array(
                    'response' => null,
                    'msj'=>$this->flashmessenger->getRawMessage(),
                );
            }     
            
            /*Valida que no exista programacion para periodo seleccionado*/
            $scheduleId = $this->existScheduloForThisPeriod($data);
            if($scheduleId){
                $this->flashmessenger->addMessage(array('danger'=>"Ya existe programacion de horarios para este periodo.<br/><a href='ScheduleEmployee.php?action=edit&id={$scheduleId}'>Ver programacion de horarios</a>"));
                return array(
                    'response' => null,
                    'msj'=>$this->flashmessenger->getRawMessage(),
                );
            }
            
            if($this->_saveSchedule($data)){                
                $this->flashmessenger->addMessage(array('success'=>'Periodo para horarios se guardo correctamente.'));
                
                return array(
                    'response' => true,                    
                    'schedule_id'=>$this->getLastInsertId()
                );
            }           
            
            $this->flashmessenger->addMessage(array('danger'=>'Error al intentar guardar periodo para horarios.'));
            return array(
                'response' => null,
                'msj'=>$this->flashmessenger->getRawMessage(),
            );
        } catch (Exception $ex) {
            $this->flashmessenger->addMessage(array('success'=>'Error al guardar periodo para horarios.'));
            restore_error_handler();            
            
             return array(
                'response' => null,
                'msj'=>$this->flashmessenger->getRawMessage(),
            );
        }    
    }
}