Laravel Orchid

Date : 07-03-2024

its just note that I will understand.<?php


namespace App\Orchid\Layouts\Customers;


use Illuminate\Http\Request;

use Orchid\Screen\Layouts\Listener;

use Orchid\Screen\Repository;

use Orchid\Support\Facades\Layout;

use Orchid\Screen\Fields\Group;

use Orchid\Screen\Fields\RadioButtons;

use Orchid\Screen\Fields\Select;

use App\Models\Jobs;

use Orchid\Screen\Fields\Input;

use App\Orchid\Screens\Customers\CustomerAddScreen;

use Orchid\Screen\Fields\Label;


class CustomerProperyTypeLayout extends Listener

{

    /**

     * List of field names for which values will be listened.

     *

     * @var string[]

     */

    protected $targets = [

        'property_model', 'property_type', 'book_name'

    ];

    /**

     * The screen's layout elements.

     *

     * @return \Orchid\Screen\Layout[]|string[]

     */


    public $propertyModel = [];

    protected function layouts(): iterable

    {

        return [

            Layout::columns([

                Layout::rows([

                    Label::make('book_name') // Changed field name for clarity

                        ->type('text')

                        ->title(__('book_name'))

                        ->value(' ')

                        ->help('This value will become visible after an employee email is selected.')

                        ->max(255)

                        ->required(),

                    Select::make('property_type')

                        ->options($this->getBookOptions()->mapWithKeys(function ($item, $key) {

                            return [$key => $key];

                        }))

                        ->empty('Select a book')

                        ->title('Select Book')

                        ->required(),


                    Select::make('property_model')

                        ->title('Select Chapter')

                        ->options($this->propertyModel)

                        //  ->disabled(empty($projects_from_to))

                        ->hidden()

                        ->required(),


                ]),


            ]),

        ];

    }

    /**

     * Get options for the 'property_type' select field.

     *

     * @return array

     */

    protected function getBookOptions()

    {

        $collection = collect([

            'Moby-Dick' => ['Captain Ahab', 'Ishmael', 'Ashore'],

            'Debt: The First 5000 Years' => ['On The Experience of Moral Confusion', 'The Myth of Barter'],

        ]);


        return $collection;

    }

    /**

     * @param \Orchid\Screen\Repository $repository

     * @param \Illuminate\Http\Request  $request

     *

     * @return \Orchid\Screen\Repository

     */

    public function handle(Repository $repository, Request $request): Repository

    {


        $property_type = $request->input('property_type');

        $result = $this->getBookOptions()->get($property_type);

        $this->propertyModel= $result;

        return $repository

            ->set('book_name', $result[0] ?? '')

            ->set('property_type', $property_type)

            ->set($this->propertyModel, $result);



        $propertyType = $request->input('property_type');


        if ($propertyType) {

            $collection = collect([

                'Moby-Dick' => ['Captain Ahab', 'Ishmael', 'Ashore'],

                'Debt: The First 5000 Years' => ['On The Experience of Moral Confusion', 'The Myth of Barter'],

            ]);


            $propertyModelOptions = $collection->get($propertyType, []);

            $repository->put('property_model', null)->put('property_model', $propertyModelOptions);

        }


        return $repository;

    }

}