@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($jenisJaminan->id))
            <form action="{{ route('basicdata.jenis-jaminan.update', $jenisJaminan->id) }}" method="POST">
                <input type="hidden" name="id" value="{{ $jenisJaminan->id }}">
                @method('PUT')
            @else
                <form method="POST" action="{{ route('basicdata.jenis-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($jenisJaminan->id) ? 'Edit' : 'Tambah' }} Jenis Jaminan
                </h3>
                <div class="flex items-center gap-2">
                    <a href="{{ route('basicdata.jenis-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($jenisJaminan->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="{{ $jenisJaminan->code ?? '' }}">
                            @error('code')
                                <em class="alert text-danger text-sm">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>
                @else
                    <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="{{ $ijin_usaha->code ?? old('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="{{ $jenisJaminan->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">Form Kategori</label>
                    <div class="flex flex-wrap items-baseline w-full">
                        <select
                            class="input tomselect w-full @error('form_kategori') border-danger bg-danger-light @enderror  select2"
                            name="form_kategori[]" multiple="multiple">

                            <option value="">Pilih Form</option>
                            @php
                                $formKategoriOptions = ['tanah', 'bangunan', 'kapal', 'kendaraan', 'mesin', 'pesawat', 'alat-berat', 'apartemen-kantor', 'lingkungan', 'fakta', 'informasi'];
                                $selectedOptions = isset($jenisJaminan->form_kategori) ? json_decode($jenisJaminan->form_kategori, true) : [];
                                $selectedOptions = is_array($selectedOptions) ? $selectedOptions : [];
                            @endphp

                            @foreach ($formKategoriOptions as $item)
                                <option value="{{ $item }}" {{ in_array($item, $selectedOptions) ? 'selected' : '' }}>
                                    {{ $item }}
                                </option>
                            @endforeach
                        </select>
                        @error('form_kategori')
                            <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">
                            Jenis Legalitas Jaminan
                        </label>
                        <div class="grid grid-cols-3 lg:grid-cols-4 w-full gap-2.5">
                            @foreach ($jenisLegalitasJaminan as $row)
                                <label class="switch">
                                    @if ( isset($jenisJaminan) && !empty(json_decode($jenisJaminan->jenis_legalitas_jaminan_id, true)))
                                        <input type="checkbox" @if (in_array($row->code, json_decode($jenisJaminan->jenis_legalitas_jaminan_id, true))) {{ 'checked' }} @endif
                                        value="{{ $row->code }}" name="jenis_legalitas_jaminan_id[]" />
                                    @else
                                        <input type="checkbox" value="{{ $row->code }}"
                                               name="jenis_legalitas_jaminan_id[]" />
                                    @endif
                                    <span class="switch-label">
                                    {{ $row->name }}
                                </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
