@extends('layouts.admin')

@section('title', 'Settings')

@section('content')
    <section class="dashboard-head">
        <div>
            <span class="eyebrow">Store</span>
            <h1>Settings</h1>
        </div>
    </section>

    @if (session('status'))
        <p class="form-status">{{ session('status') }}</p>
    @endif

    <section class="settings-layout">
        <form method="POST" action="{{ route('admin.settings.update') }}" class="admin-form" enctype="multipart/form-data">
            @csrf

            <article class="settings-card">
                <div class="settings-card-head">
                    <div>
                        <span class="eyebrow">Media</span>
                        <h2>Product Images</h2>
                    </div>
                </div>
                <div class="settings-media-row">
                    <label>
                        <span>Product fallback image</span>
                        <input name="product_fallback_image" type="file" accept="image/jpeg,image/png,image/webp">
                    </label>

                    @if ($fallbackImage)
                        <div class="settings-preview">
                            <img src="{{ \App\Support\PublicStorage::url($fallbackImage) }}" alt="Product fallback image">
                            <span>Current fallback</span>
                        </div>
                    @endif
                </div>
            </article>

            <article class="settings-card">
                <div class="settings-card-head">
                    <div>
                        <span class="eyebrow">Pricing</span>
                        <h2>Currency & Shipping</h2>
                    </div>
                </div>
                <div class="form-grid two-columns">
                    <label>
                        <span>Currency symbol</span>
                        <input name="currency_symbol" value="{{ old('currency_symbol', $currencySymbol) }}" required>
                    </label>

                    <label>
                        <span>Currency position</span>
                        <select name="currency_position" required>
                            <option value="left" @selected(old('currency_position', $currencyPosition) === 'left')>Left - Rs 1,000</option>
                            <option value="right" @selected(old('currency_position', $currencyPosition) === 'right')>Right - 1,000 Rs</option>
                        </select>
                    </label>

                    <label>
                        <span>Shipping charge</span>
                        <input name="shipping_charge" type="number" min="0" step="0.01" value="{{ old('shipping_charge', $shippingCharge) }}" required>
                    </label>

                    <label>
                        <span>Free shipping minimum</span>
                        <input name="free_shipping_minimum" type="number" min="0" step="0.01" value="{{ old('free_shipping_minimum', $freeShippingMinimum) }}" required>
                    </label>
                </div>
            </article>

            <article class="settings-card">
                <div class="settings-card-head">
                    <div>
                        <span class="eyebrow">Notifications</span>
                        <h2>Order Emails</h2>
                    </div>
                    <button class="button ghost" type="submit" form="settings-test-email-form">Send test email</button>
                </div>
                <div class="form-grid two-columns">
                    <label>
                        <span>Admin Order Email 1</span>
                        <input name="admin_order_email_1" type="email" value="{{ old('admin_order_email_1', $adminOrderEmails[0] ?? '') }}" placeholder="orders@example.com">
                    </label>

                    <label>
                        <span>Admin Order Email 2</span>
                        <input name="admin_order_email_2" type="email" value="{{ old('admin_order_email_2', $adminOrderEmails[1] ?? '') }}" placeholder="owner@example.com">
                    </label>
                </div>
            </article>

            <article class="settings-card">
                <div class="settings-card-head">
                    <div>
                        <span class="eyebrow">SEO</span>
                        <h2>Sitemap</h2>
                    </div>
                    <button class="button ghost" type="submit" form="settings-sitemap-form">Generate sitemap.xml</button>
                </div>
                <label class="checkbox-label">
                    <input name="sitemap_enabled" type="checkbox" value="1" @checked(old('sitemap_enabled', $sitemapEnabled))>
                    <span>Enable dynamic sitemap at /sitemap.xml</span>
                </label>
            </article>

            <article class="settings-card">
                <div class="settings-card-head">
                    <div>
                        <span class="eyebrow">Payments</span>
                        <h2>Payment Methods</h2>
                    </div>
                </div>
                <div class="form-grid two-columns">
                    <label class="checkbox-label">
                        <input name="ibft_enabled" type="checkbox" value="1" @checked(old('ibft_enabled', $ibftEnabled))>
                        <span>Enable IBFT bank transfer</span>
                    </label>

                    <label class="checkbox-label">
                        <input name="cod_enabled" type="checkbox" value="1" @checked(old('cod_enabled', $codEnabled))>
                        <span>Enable Cash on Delivery</span>
                    </label>
                </div>

                <label>
                    <span>IBFT IBAN / Account No</span>
                    <input name="ibft_iban" value="{{ old('ibft_iban', $ibft['iban']) }}" placeholder="PK00 BANK 0000 0000 0000 0000">
                </label>

                <div class="form-grid two-columns">
                    <label>
                        <span>IBFT Account Title</span>
                        <input name="ibft_account_title" value="{{ old('ibft_account_title', $ibft['account_title']) }}">
                    </label>

                    <label>
                        <span>IBFT Bank Name</span>
                        <input name="ibft_bank_name" value="{{ old('ibft_bank_name', $ibft['bank_name']) }}">
                    </label>
                </div>
            </article>

            @if ($errors->any())
                <p class="form-error">{{ $errors->first() }}</p>
            @endif

            <div class="settings-actions">
                <button class="button primary" type="submit">Save settings</button>
            </div>
        </form>

        <form id="settings-test-email-form" method="POST" action="{{ route('admin.settings.test-email') }}">
            @csrf
        </form>
        <form id="settings-sitemap-form" method="POST" action="{{ route('admin.settings.generate-sitemap') }}">
            @csrf
        </form>
    </section>
@endsection
