@extends('layouts.app')
@section('title',$collateral->exists?'Edit Collateral':'Register Collateral')
@section('content')
<x-ui.form-shell
    :title="$collateral->exists ? 'Edit Collateral' : 'Register Collateral'"
    description="Record the pledged asset, valuation, custody location and supporting evidence. The collateral code is generated by the system."
    :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Collateral','url'=>route('collateral.index')],['label'=>$collateral->exists?'Edit':'Register']]"
>
<form method="POST" enctype="multipart/form-data" action="{{ $collateral->exists ? route('collateral.update',$collateral) : route('collateral.store') }}" class="mf-form">
    @csrf
    @if($collateral->exists)@method('PUT')@endif

    <x-ui.form-section title="Ownership & identification" description="Connect the asset to the correct customer, branch and collateral category.">
        <x-ui.form-grid>
            @if($collateral->exists)
                <div class="mf-col-3"><label class="mf-field-label">Collateral code</label><input class="mf-input" value="{{ $collateral->code }}" readonly><p class="mf-field-help">System generated and cannot be changed.</p></div>
                <div class="mf-col-3"><label class="mf-field-label">Customer</label><input class="mf-input" value="{{ $collateral->customer->full_name }}" readonly></div>
                <div class="mf-col-3"><label class="mf-field-label">Branch</label><input class="mf-input" value="{{ $collateral->branch->name }}" readonly></div>
            @else
                <div class="mf-col-4"><label class="mf-field-label">Customer</label><select name="customer_id" class="mf-input" required><option value="">Select customer</option>@foreach($customers as $customer)<option value="{{ $customer->id }}" @selected((int)old('customer_id')===$customer->id)>{{ $customer->customer_number }} — {{ $customer->full_name }} — {{ $customer->branch->name ?? '' }}</option>@endforeach</select>@error('customer_id')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
                <div class="mf-col-4"><label class="mf-field-label">Branch</label><select name="branch_id" class="mf-input" required><option value="">Select branch</option>@foreach($branches as $branch)<option value="{{ $branch->id }}" @selected((int)old('branch_id')===$branch->id)>{{ $branch->name }}</option>@endforeach</select>@error('branch_id')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
            @endif
            <div class="mf-col-4"><label class="mf-field-label">Category</label><select name="category_id" class="mf-input" required><option value="">Select category</option>@foreach($categories as $category)<option value="{{ $category->id }}" @selected((int)old('category_id',$collateral->category_id)===$category->id)>{{ $category->name }}</option>@endforeach</select>@error('category_id')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
        </x-ui.form-grid>
    </x-ui.form-section>

    <x-ui.form-section title="Item details" description="Describe the physical asset clearly enough that staff can identify it without ambiguity.">
        <x-ui.form-grid>
            <div class="mf-col-4"><label class="mf-field-label">Item</label><input name="item" class="mf-input" value="{{ old('item',$collateral->item) }}" placeholder="e.g. 55-inch Smart TV" required>@error('item')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
            <div class="mf-col-2"><label class="mf-field-label">Brand</label><input name="brand" class="mf-input" value="{{ old('brand',$collateral->brand) }}"></div>
            <div class="mf-col-2"><label class="mf-field-label">Model</label><input name="model" class="mf-input" value="{{ old('model',$collateral->model) }}"></div>
            <div class="mf-col-2"><label class="mf-field-label">Serial number</label><input name="serial_number" class="mf-input" value="{{ old('serial_number',$collateral->serial_number) }}"></div>
            <div class="mf-col-2"><label class="mf-field-label">Colour</label><input name="colour" class="mf-input" value="{{ old('colour',$collateral->colour) }}"></div>
            <div class="mf-col-3"><label class="mf-field-label">Condition</label><input name="condition" class="mf-input" value="{{ old('condition',$collateral->condition) }}" placeholder="Good / Fair / New"></div>
            <div class="mf-col-9"><label class="mf-field-label">Description</label><textarea name="description" class="mf-input" rows="3">{{ old('description',$collateral->description) }}</textarea></div>
        </x-ui.form-grid>
    </x-ui.form-section>

    <x-ui.form-section title="Valuation & receipt" description="Record assessed values and who physically received the collateral.">
        <x-ui.form-grid>
            <div class="mf-col-3"><label class="mf-field-label">Estimated value</label><input name="estimated_value" inputmode="decimal" class="mf-input" value="{{ old('estimated_value',$collateral->estimated_value) }}"></div>
            <div class="mf-col-3"><label class="mf-field-label">Accepted value</label><input name="accepted_value" inputmode="decimal" class="mf-input" value="{{ old('accepted_value',$collateral->accepted_value) }}"></div>
            <div class="mf-col-3"><label class="mf-field-label">Date received</label><input type="date" name="received_at" class="mf-input" value="{{ old('received_at',optional($collateral->received_at)->format('Y-m-d') ?: now()->format('Y-m-d')) }}" required></div>
            <div class="mf-col-3"><label class="mf-field-label">Received by</label><select name="received_by" class="mf-input"><option value="">Current user / unassigned</option>@foreach($receivers as $receiver)<option value="{{ $receiver->id }}" @selected((int)old('received_by',$collateral->received_by)===$receiver->id)>{{ $receiver->name }}</option>@endforeach</select></div>
        </x-ui.form-grid>
    </x-ui.form-section>

    <x-ui.form-section title="Custody & storage" description="Track the exact physical location. Future movements are recorded permanently from the collateral profile.">
        <x-ui.form-grid>
            <div class="mf-col-4"><label class="mf-field-label">Status</label><select name="status" class="mf-input" required @disabled($collateral->exists)>@foreach($statuses as $status)<option value="{{ $status }}" @selected(old('status',$collateral->status ?: 'pending_inspection')===$status)>{{ str($status)->replace('_',' ')->title() }}</option>@endforeach</select>@if($collateral->exists)<input type="hidden" name="status" value="{{ $collateral->status }}"><p class="mf-field-help">Use custody movement on the collateral profile to change status.</p>@endif</div>
            <div class="mf-col-4"><label class="mf-field-label">Store room</label><input name="store_room" class="mf-input" value="{{ old('store_room',$collateral->store_room) }}" @readonly($collateral->exists)></div>
            <div class="mf-col-4"><label class="mf-field-label">Shelf / position</label><input name="shelf" class="mf-input" value="{{ old('shelf',$collateral->shelf) }}" @readonly($collateral->exists)></div>
        </x-ui.form-grid>
    </x-ui.form-section>

    @unless($collateral->exists)
    <x-ui.form-section title="Photos & supporting documents" description="Files are stored privately. You can add more attachments after registration.">
        <x-ui.form-grid>
            <div class="mf-col-6"><label class="mf-field-label">Photos</label><input type="file" name="photos[]" multiple accept="image/png,image/jpeg,image/webp" class="mf-input"><p class="mf-field-help">JPEG, PNG or WebP, up to 8 MB each.</p></div>
            <div class="mf-col-6"><label class="mf-field-label">Documents</label><input type="file" name="documents[]" multiple accept=".pdf,.doc,.docx,image/png,image/jpeg,image/webp" class="mf-input"><p class="mf-field-help">Ownership papers, receipts, valuation reports or other evidence.</p></div>
        </x-ui.form-grid>
    </x-ui.form-section>
    @endunless

    <x-ui.form-actions>
        <a class="mf-btn" href="{{ $collateral->exists ? route('collateral.show',$collateral) : route('collateral.index') }}">Cancel</a>
        <button class="mf-btn mf-btn-primary">{{ $collateral->exists ? 'Save changes' : 'Register collateral' }}</button>
    </x-ui.form-actions>
</form>
</x-ui.form-shell>
@endsection
