@extends('layouts.main')

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

@section('content')
    <div class="w-full grid gap-5 lg:gap-7.5 mx-auto">
        @if(isset($jenisLegalitasJaminan->id))
            <form action="{{ route('basicdata.jenis-legalitas-jaminan.update', $jenisLegalitasJaminan->id) }}" method="POST">
                <input type="hidden" name="id" value="{{ $jenisLegalitasJaminan->id }}">
                @method('PUT')
                @else
                    <form method="POST" action="{{ route('basicdata.jenis-legalitas-jaminan.store') }}">
                        @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($jenisLegalitasJaminan->id) ? 'Edit' : 'Tambah' }} Jenis Legalitas Jaminan
                                </h3>
                                <div class="flex items-center gap-2">
                                    <a href="{{ route('basicdata.jenis-legalitas-jaminan.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">
                                @if(isset($jenisLegalitasJaminan->id))
                                    <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 readonly class="input border-warning bg-warning-light  @error('code') border-danger bg-danger-light @enderror" type="text" name="code" value="{{ $jenisLegalitasJaminan->code ?? '' }}">
                                            @error('code')
                                            <em class="alert text-danger text-sm">{{ $message }}</em>
                                            @enderror
                                        </div>
                                    </div>
                                @endif
                                <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="{{ $jenisLegalitasJaminan->name ?? '' }}">
                                        @error('name')
                                        <em class="alert text-danger text-sm">{{ $message }}</em>
                                        @enderror
                                    </div>
                                </div>

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

                                <!-- Tambahkan inputan custom_field_type -->
                                <div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                                    <label class="form-label max-w-56">
                                        Custom Field Type
                                    </label>
                                    <div class="flex flex-wrap items-baseline w-full">
                                        <select class="input tomselect @error('custom_field_type') border-danger bg-danger-light @enderror" name="custom_field_type">
                                            <option value="">Pilih Tipe</option>
                                            <option value="text" {{ (isset($jenisLegalitasJaminan) && $jenisLegalitasJaminan->custom_field_type == 'text') ? 'selected' : '' }}>Text</option>
                                            <option value="number" {{ (isset($jenisLegalitasJaminan) && $jenisLegalitasJaminan->custom_field_type == 'number') ? 'selected' : '' }}>Number</option>
                                            <option value="date" {{ (isset($jenisLegalitasJaminan) && $jenisLegalitasJaminan->custom_field_type == 'date') ? 'selected' : '' }}>Date</option>
                                        </select>
                                        @error('custom_field_type')
                                        <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">
                                        Custom Fields
                                    </label>
                                    <div class="grid grid-cols-3 lg:grid-cols-4 w-full gap-2.5">
                                        @foreach($customFields as $customField)
                                            <label class="switch">
                                                @if ( isset($jenisLegalitasJaminan) && !empty($jenisLegalitasJaminan->custom_fields))
                                                    <input type="checkbox" @if (in_array($customField->id, $jenisLegalitasJaminan->custom_fields))
                                                        {{ 'checked' }}
                                                    @endif
                                                    value="{{ $customField->id }}" name="custom_fields[]"/>
                                                @else
                                                    <input type="checkbox" value="{{ $customField->id }}"
                                                           name="custom_fields[]"/>
                                                @endif
                                                <span class="switch-label">
                                                    {{ $customField->urutan_prioritas.'. '.$customField->label }}
                                                </span>
                                            </label>
                                        @endforeach
                                    </div>
                                </div>


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