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

use Lycoreco\Apps\Recipes\Models\{
    IngredientInRecipeModel,
    IngredientModel
};

class IngredientRecipeRelController extends Abstract\AdminSingleController
{
    protected $model_сlass_name = "Lycoreco\Apps\Recipes\Models\IngredientInRecipeModel";
    protected $field_title = 'ingredient_name';
    protected $object_router_name = 'admin:ing-cat-rel';
    protected $verbose_name = "ingredient in recipe";
    protected $component_widgets = ['the_recipe_ingredients_relation'];
    protected $fields = array(
        [
            'model_field' => 'recipe_id',
            'input_type'  => 'number',
            'dynamic_save' => false,
            'input_label' => 'Recipe id',
            'input_attrs' => ['required', 'disabled']
        ],
        [
            'model_field' => 'amount',
            'input_type'  => 'text',
            'input_attrs' => ['required']
        ],
    );
    protected function get_model() 
    {
        $model = parent::get_model();

        if($this->context['is_new'])
            $model->field_recipe_id = (int)$this->url_context['url_1'];

        return $model;
    }
    public function __construct($is_new = false) {
        parent::__construct($is_new);
        
        $this->fields[] = [
            'model_field' => 'ingredient_id',
            'input_type' => 'select',
            'input_label' => 'Ingedient',
            'input_attrs' => ['required'],
            'input_values' => IngredientModel::get_ing_values()
        ];
    } 
}