@extends('layouts.main')

@section('breadcrumbs')
    {{ Breadcrumbs::render(request()->route()->getName()) }}
@endsection

@section('content')
    {{-- <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" /> --}}
    <div class="w-full grid gap-5 lg:gap-7.5 mx-auto">

        <form action="{{ isset($teams->id) ? route('basicdata.teams.update', $teams->id) : route('basicdata.teams.store') }}"
            method="POST">

            @if (isset($teams->id))
                @method('PUT')
                <input type="hidden" name="id" value="{{ $teams->id }}">
            @endif
            @csrf
            <div class="card border border-agi-100 pb-2.5">
                <div class="card-header bg-agi-50" id="basic_settings">
                    <h3 class="card-title">
                        {{ isset($teams->id) ? 'Edit' : 'Tambah' }} Teams
                    </h3>
                    <div class="flex items-center gap-2">
                        <a href="{{ route('basicdata.teams.index') }}" class="btn btn-xs btn-info"><i
                                class="ki-filled ki-exit-left"></i> Back</a>
                    </div>
                </div>
                <div class="card-body grid gap-5">
                    <div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                        <label class="form-label max-w-56">
                            Name
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <input class="input  @error('name') border-danger bg-danger-light @enderror" type="text"
                                name="name" value="{{ $teams->name ?? '' }}">
                            @error('name')
                                <em class="alert text-danger text-sm">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>

                    <div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                        <label class="form-label max-w-56">
                            Code
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <input class="input  @error('code') border-danger bg-danger-light @enderror" type="text"
                                name="code" value="{{ $teams->code ?? '' }}">
                            @error('code')
                                <em class="alert text-danger text-sm">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>


                    <div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                        <label class="form-label max-w-56">Region</label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <select
                                class="input tomselect w-full @error('regions_id') border-danger bg-danger-light  @enderror"
                                name="regions_id">
                                <option value="">Select Region</option>
                                @if (isset($regions))
                                    @foreach ($regions as $region)
                                        @if (isset($teams))
                                            <option value="{{ $region->id }}"
                                                {{ $teams->regions_id == $region->id ? 'selected' : '' }}>
                                                {{ $region->name }}</option>
                                        @else
                                            <option value="{{ $region->id }}">{{ $region->name }}</option>
                                        @endif
                                    @endforeach
                                @endif
                            </select>

                            @error('regions_id')
                                <em class="alert text-danger text-sm">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>

                    <div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                        <label class="form-label max-w-56">Users</label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <select
                                class="input tomselect w-full @error('regions_id') border-danger bg-danger-light @enderror  select2"
                                name="user[]" multiple="multiple">
                                <option value="">Select Team Group</option>
                                @if (isset($user))
                                    @foreach ($user as $users)
                                        @if (isset($teams))
                                            <option value="{{ $users->id }}"
                                                {{ in_array($users->id, $selectedUsers) ? 'selected' : '' }}>
                                                {{ $users->name . ' | ' }}
                                                @foreach ($users->roles as $role)
                                                    {{ $role->name }}
                                                @endforeach
                                            </option>
                                        @else

                                            <option value="{{ $users->id }}">{{ $users->name . ' | ' }}

                                                @foreach ($users->roles as $role)
                                                    {{ $role->name }}
                                                @endforeach
                                            </option>
                                        @endif
                                    @endforeach
                                @endif
                            </select>
                            @error('user')
                                <em class="alert text-danger text-sm">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>


                    <div class="flex justify-end">
                        <button type="submit" class="btn btn-primary">
                            Save
                        </button>
                    </div>

                </div>
            </div>
        </form>
    </div>
@endsection

