<?php
namespace BooklyStaffCabinet\Frontend\Modules\StaffServices;

use BooklyStaffCabinet\Lib;
use Bookly\Lib as BooklyLib;

/**
 * Class Ajax
 * @package BooklyStaffCabinet\Frontend\Modules\StaffServices
 */
class Ajax extends BooklyLib\Base\Ajax
{
    /**
     * @inheritdoc
     */
    protected static function permissions()
    {
        return array(
            'getServices' => 'user',
        );
    }

    public static function getServices()
    {
        $staff = BooklyLib\Entities\Staff::query()->where( 'wp_user_id', get_current_user_id() )->findOne();
        $staff_id   = $staff->getId();
        $attributes = array();
        foreach ( (array) self::parameter( 'attributes' ) as $key => $value ) {
            $attributes[ $key ] = array_fill_keys( explode( ',', $value ), true );
        }
        $form = new \Bookly\Backend\Modules\Staff\Forms\StaffServices();
        $form->load( $staff->getId() );

        $html = self::renderTemplate( 'services', compact( 'staff_id', 'form', 'attributes' ), false );
        wp_send_json_success( compact( 'html' ) );
    }
}