<?php
namespace Lycoreco\Apps\Admin\Controllers;

use Lycoreco\Includes\Model\ValidationError;
use Lycoreco\Apps\Recipes\Models\{
    RecipeModel,
    CategoryModel
};

class AdminRecipeController extends Abstract\AdminSingleController
{
    protected $model_сlass_name = "Lycoreco\Apps\Recipes\Models\RecipeModel";
    protected $field_title = 'field_title';
    protected $verbose_name = 'recipe';
    protected $object_router_name = 'admin:recipe';
    protected $component_widgets = ['the_recipe_author', 'the_recipe_ingredients'];

    protected $fields = array(
        [
            'model_field' => 'title',
            'input_type' => 'text',
            'input_attrs' => ['required']
        ], 
        [
            'model_field' => 'instruction',
            'input_type' => 'textarea',
            'input_attrs' => ['required']
        ], 
        [
            'model_field' => 'image_url',
            'input_type'  => 'image',
            'input_label' => 'Image',
        ],
        [
            'model_field' => 'estimated_time',
            'input_type' => 'number',
            'input_attrs' => ['required'],
            'input_label' => 'Estimated time (min)'
        ], 
        [
            'model_field' => 'estimated_price',
            'input_type' => 'number',
            'input_attrs' => ['required'],
            'input_label' => 'Estimated price ($)'
        ],
        [
            'model_field' => 'status',
            'input_type' => 'select',
            'input_attrs' => ['required'],
            'input_values' => RecipeModel::STATUS
        ],
        [
            'model_field' => 'created_at',
            'input_type' => 'text',
            'dynamic_save'  => false,
            'input_label' => 'Created at',
            'input_attrs' => ['disabled']
        ]
    );
    protected function before_save(&$object)
    {
        if($this->is_new)
        {
            $object->field_author_id = CURRENT_USER->get_id();
        }
    }

    public function __construct($is_new = false) {
        parent::__construct($is_new);
        $this->fields[] = [
            'model_field' => 'category_id',
            'input_type' => 'select',
            'input_label' => 'Categories',
            'input_attrs' => ['required'],
            'input_values' => CategoryModel::get_cat_values()
        ];
    } 
}