<?php
namespace Dreamfox\DeliveryDate\Ajaxs;

use Dreamfox\DeliveryDate\Abstracts\AbstractAjax;
use Dreamfox\DeliveryDate\Facades\SystemFacade;

class AdminAjax extends AbstractAjax
{

  public function getAjaxEvents()
  {
    return [
      'load_categories',
      'save_settings',
      'save_timeslot_settings',
      'load_timeslot_settings',
    ];
  }

  public function load_categories()
  {
    $this->_verifyAdminRequest();
    $categories = SystemFacade::getInstance()->loadCategories();
    $response = array(
			'message' => 'Updated successfully!',
      'categories' => $categories,
		);
		wp_send_json( $response );
  }

  public function save_settings()
  {
    $this->_verifyAdminRequest();
    $data = $this->_getRequestData();
    SystemFacade::getInstance()->saveSettings($data);
    $response = array(
			'message' => 'Updated successfully!',
		);
		wp_send_json( $response );
  }

  public function save_timeslot_settings()
  {
    $this->_verifyAdminRequest();
    $data = $this->_getRequestData();
    SystemFacade::getInstance()->saveTimeslotSettings( $data );
    wp_send_json( array( 'message' => 'Timeslot settings saved!' ) );
  }

  public function load_timeslot_settings()
  {
    $this->_verifyAdminRequest();
    $settings = SystemFacade::getInstance()->loadTimeslotSettings();
    wp_send_json( $settings->toArray() );
  }
}