@extends('layouts.app')
@section('title', __('Business Settings'))
@section('content')
<form id="business" method="POST" action="{{ route('settings.update') }}" enctype="multipart/form-data" class="card mb-3">
    @csrf @method('PUT')
    <div class="card-body p-4">
        <div class="d-flex justify-content-between align-items-start gap-3 mb-3">
            <div>
                <h6 class="mb-1">{{ __('Business Information') }}</h6>
                <small class="text-muted">{{ __('Used on invoices, reports, receipts, and the menu header.') }}</small>
            </div>
            @if(!empty($settings['logo']))<img src="/{{ $settings['logo'] }}" alt="{{ $settings['business_name'] ?? '' }}" style="height:54px;max-width:140px;object-fit:contain">@endif
        </div>
        <div class="row g-3">
            <div class="col-md-4"><label class="form-label">{{ __('Business Name') }}</label><input name="business_name" class="form-control" value="{{ $settings['business_name'] ?? '' }}" required></div>
            <div class="col-md-4"><label class="form-label">{{ __('Phone') }}</label><input name="business_phone" class="form-control" value="{{ $settings['business_phone'] ?? '' }}"></div>
            <div class="col-md-4"><label class="form-label">{{ __('Email') }}</label><input name="business_email" type="email" class="form-control" value="{{ $settings['business_email'] ?? '' }}"></div>
            <div class="col-md-8"><label class="form-label">{{ __('Address') }}</label><input name="business_address" class="form-control" value="{{ $settings['business_address'] ?? '' }}"></div>
            <div class="col-md-4"><label class="form-label">{{ __('Tax / Registration No.') }}</label><input name="tax_number" class="form-control" value="{{ $settings['tax_number'] ?? '' }}"></div>
            <div class="col-md-6"><label class="form-label">{{ __('Logo') }}</label><input type="file" name="logo" class="form-control" accept="image/*"></div>
            <div class="col-md-6"><label class="form-label">{{ __('Default Invoice Print') }}</label><select name="invoice_size" class="form-select"><option value="a4" @selected(($settings['invoice_size'] ?? 'a4') === 'a4')>{{ __('A4 / Normal Printer') }}</option><option value="thermal" @selected(($settings['invoice_size'] ?? '') === 'thermal')>{{ __('80mm Small Printer') }}</option></select></div>
            <div class="col-md-4"><label class="form-label">{{ __('Invoice Footer') }}</label><textarea name="invoice_footer" class="form-control" rows="3">{{ $settings['invoice_footer'] ?? '' }}</textarea></div>
            <div class="col-md-4"><label class="form-label">{{ __('Invoice Terms') }}</label><textarea name="invoice_terms" class="form-control" rows="3">{{ $settings['invoice_terms'] ?? '' }}</textarea></div>
            <div class="col-md-4"><label class="form-label">{{ __('Report Footer') }}</label><textarea name="report_footer" class="form-control" rows="3">{{ $settings['report_footer'] ?? '' }}</textarea></div>
        </div>
    </div>
    <div class="card-footer bg-white text-end"><button class="btn btn-success">{{ __('Save Settings') }}</button></div>
</form>

<div class="row g-3">
    <div id="currencies" class="col-lg-5">
        <div class="card h-100">
            <div class="card-body p-4">
                <h6>{{ __('Currencies') }}</h6>
                <div class="table-responsive">
                    <table class="table table-sm align-middle">
                        <thead><tr><th>{{ __('Code') }}</th><th>{{ __('Rate') }}</th><th></th></tr></thead>
                        <tbody>
                            @forelse($currencies as $currency)
                                <tr>
                                    <td><strong>{{ $currency->code }}</strong><small class="d-block">{{ $currency->symbol }} {{ $currency->name }}</small></td>
                                    <td>{{ $currency->exchange_rate }}</td>
                                    <td class="text-end">@if($currency->is_default)<span class="badge badge-soft">{{ __('Default') }}</span>@else<form method="POST" action="{{ route('currencies.default', $currency) }}">@csrf @method('PUT')<button class="btn btn-link btn-sm">{{ __('Make default') }}</button></form>@endif</td>
                                </tr>
                            @empty
                                <tr><td colspan="3" class="text-center text-muted">{{ __('No currencies configured.') }}</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
                <form method="POST" action="{{ route('currencies.store') }}" class="row g-2">@csrf
                    <div class="col-4"><input name="code" class="form-control" placeholder="AFN" required></div>
                    <div class="col-3"><input name="symbol" class="form-control" placeholder="؋" required></div>
                    <div class="col-5"><input name="exchange_rate" type="number" step="0.0001" class="form-control" placeholder="{{ __('Rate') }}" required></div>
                    <div class="col-12"><input name="name" class="form-control" placeholder="{{ __('Currency Name') }}" required></div>
                    <div class="col-12"><button class="btn btn-light w-100">{{ __('Add Currency') }}</button></div>
                </form>
            </div>
        </div>
    </div>
    <div id="categories" class="col-lg-7">
        <div class="card h-100">
            <div class="card-body p-4">
                <h6>{{ __('Categories') }}</h6>
                <form method="POST" action="{{ route('categories.store') }}" class="row g-2 mb-3">@csrf
                    <div class="col-md-7"><input name="name" class="form-control" placeholder="{{ __('Category Name') }}" required></div>
                    <div class="col-md-3"><input name="icon" class="form-control" placeholder="fa-box"></div>
                    <div class="col-md-2"><button class="btn btn-success w-100"><i class="fa-solid fa-plus"></i></button></div>
                </form>
                <div class="row g-2">
                    @forelse($categories as $category)
                        <div class="col-md-6">
                            <div class="d-flex justify-content-between align-items-center border rounded-3 px-3 py-2">
                                <span><i class="fa-solid {{ $category->icon ?: 'fa-tag' }} me-2 text-success"></i>{{ $category->name }}</span>
                                <form method="POST" action="{{ route('categories.destroy', $category) }}">@csrf @method('DELETE')<button class="btn btn-link btn-sm text-danger"><i class="fa-solid fa-trash"></i></button></form>
                            </div>
                        </div>
                    @empty
                        <div class="col-12 text-center text-muted py-3">{{ __('No categories configured.') }}</div>
                    @endforelse
                </div>
            </div>
        </div>
    </div>
</div>

<div id="units" class="card mt-3">
    <div class="card-body p-4">
        <h6>{{ __('Units') }}</h6>
        <form method="POST" action="{{ route('units.store') }}" class="row g-2 mb-3">@csrf
            <div class="col-md-5"><input name="name" class="form-control" placeholder="{{ __('Unit Name') }}" required></div>
            <div class="col-md-5"><input name="short_name" class="form-control" placeholder="pc" required></div>
            <div class="col-md-2"><button class="btn btn-success w-100"><i class="fa-solid fa-plus"></i></button></div>
        </form>
        <div class="row g-2">
            @forelse($units as $unit)
                <div class="col-md-4"><div class="d-flex justify-content-between align-items-center border rounded-3 px-3 py-2"><span>{{ $unit->name }} <small class="text-muted">({{ $unit->short_name }})</small></span><form method="POST" action="{{ route('units.destroy', $unit) }}">@csrf @method('DELETE')<button class="btn btn-link btn-sm text-danger"><i class="fa-solid fa-trash"></i></button></form></div></div>
            @empty
                <div class="col-12 text-center text-muted py-3">{{ __('No units configured.') }}</div>
            @endforelse
        </div>
    </div>
</div>
@endsection
