<?php

namespace App\Orchid\Screens\Color;

use App\Models\Color;
use Orchid\Screen\Screen;
use Illuminate\Http\Request;
use Orchid\Screen\Actions\Button;
use Orchid\Support\Facades\Alert;
use App\Orchid\Layouts\Color\ColorCreateLayout;

class ColorCreateScreen extends Screen {
  public $color;

  public function query(Color $color): iterable {
    return [
      'color' => $color,
    ];
  }

  public function name(): ?string {
    return 'Creating a new color';
  }

  public function commandBar(): iterable {
    return [
      Button::make('Create color')
        ->icon('pencil')
        ->method('create')
    ];
  }

  public function layout(): iterable {
    return [
      ColorCreateLayout::class,
    ];
  }

  public function create(Request $request) {

    $request->validate([
      'color.name' => 'required|unique:colors,name,' . $this->color->id,
      'color.code' => 'required|unique:colors,code,' . $this->color->id,
    ]);

    $this->color->name = ucwords($request->input('color.name'));
    $this->color->code = $request->input('color.code');

    $this->color->save();
    Alert::info('You have successfully created the color.');
    return redirect()->route('platform.color.list');
  }
}
