@extends('admin.layouts.app')

@section('title', 'Settings')
@section('page-title', 'Settings')
@section('page-description', 'Manage system settings')

@section('content')

<style>
    .settings-page {
        max-width: 1080px;
        margin: 0 auto;
        padding-bottom: 30px;
    }

    .settings-hero {
        margin-bottom: 20px;
        padding: 22px 24px;
        border: 1px solid #e5eaf1;
        border-radius: 14px;
        background: linear-gradient(135deg, #ffffff 0%, #f8faff 100%);
        box-shadow: 0 4px 18px rgba(23, 32, 51, .04);
    }

    .settings-hero-inner {
        display: flex;
        align-items: center;
        gap: 14px;
    }

    .hero-icon {
        width: 46px;
        height: 46px;
        border-radius: 12px;
        background: #eef4ff;
        color: #315cdb;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 21px;
        flex-shrink: 0;
    }

    .settings-hero h2 {
        margin: 0;
        color: #172033;
        font-size: 20px;
        line-height: 1.25;
        font-weight: 750;
    }

    .settings-hero p {
        margin: 5px 0 0;
        color: #707b8d;
        font-size: 13px;
    }

    .settings-card {
        background: #fff;
        border: 1px solid #e5eaf1;
        border-radius: 14px;
        overflow: hidden;
        margin-bottom: 18px;
        box-shadow: 0 4px 18px rgba(23, 32, 51, .035);
    }

    .settings-header {
        padding: 20px 24px;
        border-bottom: 1px solid #edf0f4;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 20px;
    }

    .setting-title-wrap {
        display: flex;
        align-items: center;
        gap: 13px;
    }

    .setting-icon {
        width: 42px;
        height: 42px;
        border-radius: 11px;
        background: #eef4ff;
        color: #315cdb;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 20px;
        flex-shrink: 0;
    }

    .settings-header h3 {
        margin: 0;
        font-size: 17px;
        font-weight: 700;
        color: #172033;
    }

    .settings-header p {
        margin: 5px 0 0;
        font-size: 12px;
        color: #7b8494;
        line-height: 1.5;
    }

    .settings-body {
        padding: 24px;
    }

    .settings-grid {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 22px;
    }

    .setting-group {
        display: flex;
        flex-direction: column;
    }

    .setting-label {
        margin-bottom: 8px;
        font-size: 12px;
        font-weight: 700;
        color: #273142;
    }

    .setting-input,
    .setting-select {
        width: 100%;
        height: 45px;
        padding: 0 13px;
        border: 1px solid #d9dee7;
        border-radius: 8px;
        background: #fff;
        color: #172033;
        font-size: 14px;
        outline: none;
        box-sizing: border-box;
        transition: border-color .15s, box-shadow .15s;
    }

    .setting-input:hover,
    .setting-select:hover {
        border-color: #c7cfdb;
    }

    .setting-input:focus,
    .setting-select:focus {
        border-color: #315cdb;
        box-shadow: 0 0 0 3px rgba(49, 92, 219, .10);
    }

    .setting-help {
        margin-top: 6px;
        font-size: 11px;
        color: #8a94a3;
        line-height: 1.5;
    }

    .switch-row {
        min-height: 45px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 15px;
        padding: 0 13px;
        border: 1px solid #d9dee7;
        border-radius: 8px;
        box-sizing: border-box;
    }

    .switch-text {
        font-size: 13px;
        color: #374151;
    }

    .switch {
        position: relative;
        width: 44px;
        height: 24px;
        flex-shrink: 0;
    }

    .switch input {
        opacity: 0;
        width: 0;
        height: 0;
    }

    .slider {
        position: absolute;
        cursor: pointer;
        inset: 0;
        background: #cbd5e1;
        border-radius: 20px;
        transition: .2s;
    }

    .slider:before {
        content: "";
        position: absolute;
        width: 18px;
        height: 18px;
        left: 3px;
        top: 3px;
        background: #fff;
        border-radius: 50%;
        transition: .2s;
        box-shadow: 0 2px 5px rgba(0,0,0,.15);
    }

    .switch input:checked + .slider {
        background: #315cdb;
    }

    .switch input:checked + .slider:before {
        transform: translateX(20px);
    }

    .settings-divider {
        margin: 26px 0 22px;
        border: 0;
        border-top: 1px solid #edf0f4;
    }

    .preview-box {
        display: grid;
        grid-template-columns: minmax(0, 1fr) 250px;
        gap: 20px;
        align-items: stretch;
    }

    .preview-copy {
        padding: 18px;
        border: 1px solid #e8edf3;
        border-radius: 10px;
        background: #f8fafc;
    }

    .preview-title {
        margin-bottom: 6px;
        font-size: 12px;
        font-weight: 700;
        color: #273142;
        text-transform: uppercase;
        letter-spacing: .03em;
    }

    .preview-note {
        margin-top: 0;
        font-size: 11px;
        color: #7b8494;
        line-height: 1.55;
    }

    .amount-preview {
        min-height: 78px;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 15px 18px;
        border: 1px solid #dfe6ef;
        border-radius: 10px;
        background: #fff;
        color: #172033;
        font-size: 27px;
        font-weight: 750;
        box-sizing: border-box;
        white-space: nowrap;
    }

    .settings-actions {
        margin-top: 24px;
        padding-top: 20px;
        border-top: 1px solid #edf0f4;
        display: flex;
        justify-content: flex-end;
    }

    .save-btn {
        height: 43px;
        padding: 0 22px;
        border: 0;
        border-radius: 8px;
        background: #315cdb;
        color: #fff;
        font-size: 13px;
        font-weight: 700;
        cursor: pointer;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        box-sizing: border-box;
        transition: background .15s, transform .1s;
    }

    .save-btn:hover {
        background: #274dbd;
        color: #fff;
    }

    .save-btn:active {
        transform: translateY(1px);
    }

    .alert {
        margin-bottom: 18px;
        padding: 12px 15px;
        border-radius: 9px;
        font-size: 13px;
    }

    .alert-success {
        background: #ecfdf3;
        border: 1px solid #bbf7d0;
        color: #166534;
    }

    .alert-danger {
        background: #fef2f2;
        border: 1px solid #fecaca;
        color: #991b1b;
    }

    .alert-danger ul {
        margin: 0;
        padding-left: 18px;
    }

    @media (max-width: 820px) {
        .preview-box {
            grid-template-columns: 1fr;
        }
    }

    @media (max-width: 700px) {

        .settings-page {
            width: 100%;
        }

        .settings-hero {
            padding: 18px;
        }

        .settings-header {
            padding: 18px;
            align-items: flex-start;
        }

        .settings-body {
            padding: 18px;
        }

        .settings-grid {
            grid-template-columns: 1fr;
        }

        .settings-actions {
            justify-content: stretch;
        }

        .save-btn {
            width: 100%;
        }

        .amount-preview {
            font-size: 24px;
        }
    }
</style>


<div class="settings-page">

    {{-- =========================================================
         PAGE HEADER
    ========================================================== --}}

    <div class="settings-hero">

        <div class="settings-hero-inner">

            <div class="hero-icon">
                ⚙️
            </div>

            <div>

                <h2>
                    System Settings
                </h2>

                <p>
                    Control currency and amount display preferences
                    used across the application.
                </p>

            </div>

        </div>

    </div>


    {{-- =========================================================
         SUCCESS MESSAGE
    ========================================================== --}}

    @if(session('success'))

        <div class="alert alert-success">
            {{ session('success') }}
        </div>

    @endif


    {{-- =========================================================
         VALIDATION ERRORS
    ========================================================== --}}

    @if($errors->any())

        <div class="alert alert-danger">

            <strong>
                Please check the following:
            </strong>

            <ul>

                @foreach($errors->all() as $error)

                    <li>
                        {{ $error }}
                    </li>

                @endforeach

            </ul>

        </div>

    @endif


    {{-- =========================================================
         CURRENCY & AMOUNT SETTINGS
    ========================================================== --}}

    <div class="settings-card">

        <div class="settings-header">

            <div class="setting-title-wrap">

                <div class="setting-icon">
                    💰
                </div>

                <div>

                    <h3>
                        Currency & Amount Settings
                    </h3>

                    <p>
                        Configure how currency and amounts are displayed
                        throughout the system.
                    </p>

                </div>

            </div>

        </div>


        <div class="settings-body">

            <form
                method="POST"
                action="{{ route('admin.system-settings.update') }}"
            >

                @csrf
                @method('PUT')


                {{-- =================================================
                     SETTINGS GRID
                ================================================== --}}

                <div class="settings-grid">

                    {{-- Currency Code --}}
                    <div class="setting-group">

                        <label class="setting-label">
                            Currency Code
                        </label>

                        <input
                            type="text"
                            name="currency_code"
                            class="setting-input"
                            value="{{ old('currency_code', $settings->currency_code ?? 'INR') }}"
                            placeholder="INR"
                            maxlength="10"
                            required
                        >

                        <div class="setting-help">
                            Example: INR, USD, AED, EUR, GBP
                        </div>

                    </div>


                    {{-- Currency Symbol --}}
                    <div class="setting-group">

                        <label class="setting-label">
                            Currency Symbol
                        </label>

                        <input
                            type="text"
                            name="currency_symbol"
                            class="setting-input"
                            value="{{ old('currency_symbol', $settings->currency_symbol ?? '₹') }}"
                            placeholder="₹"
                            maxlength="10"
                            required
                        >

                        <div class="setting-help">
                            Example: ₹, $, €, £, د.إ
                        </div>

                    </div>


                    {{-- Decimal Places --}}
                    <div class="setting-group">

                        <label class="setting-label">
                            Decimal Places
                        </label>

                        <select
                            name="currency_decimal_places"
                            class="setting-select"
                            required
                        >

                            @for($i = 0; $i <= 4; $i++)

                                <option
                                    value="{{ $i }}"
                                    {{ (int) old(
                                        'currency_decimal_places',
                                        $settings->currency_decimal_places ?? 2
                                    ) === $i ? 'selected' : '' }}
                                >

                                    {{ $i }}

                                    {{ $i == 1 ? 'decimal' : 'decimals' }}

                                </option>

                            @endfor

                        </select>

                        <div class="setting-help">
                            Example: 2 decimals → ₹500.00
                        </div>

                    </div>


                    {{-- Round Amount --}}
                    <div class="setting-group">

                        <label class="setting-label">
                            Round Amount Display
                        </label>

                        <div class="switch-row">

                            <span class="switch-text">
                                Enable amount rounding
                            </span>

                            <label class="switch">

                                {{-- Hidden value ensures false is submitted --}}
                                <input
                                    type="hidden"
                                    name="currency_round_display"
                                    value="0"
                                >

                                <input
                                    type="checkbox"
                                    id="currency_round_display"
                                    name="currency_round_display"
                                    value="1"
                                    {{ old(
                                        'currency_round_display',
                                        $settings->currency_round_display ?? false
                                    ) ? 'checked' : '' }}
                                >

                                <span class="slider"></span>

                            </label>

                        </div>

                        <div class="setting-help">
                            Example: ₹16.01 → ₹16 when enabled.
                        </div>

                    </div>

                </div>


                {{-- =================================================
                     PREVIEW
                ================================================== --}}

                <hr class="settings-divider">


                <div class="preview-box">

                    <div class="preview-copy">

                        <div class="preview-title">
                            Amount Preview
                        </div>

                        <div class="preview-note">

                            This preview updates instantly based on the
                            currency symbol, decimal places and rounding
                            option selected above.

                            It does not change any loan or payment
                            calculation.

                        </div>

                    </div>


                    <div
                        id="amountPreview"
                        class="amount-preview"
                    >
                        {{ $settings->currency_symbol ?? '₹' }}500.00
                    </div>

                </div>


                {{-- =================================================
                     SAVE BUTTON
                ================================================== --}}

                <div class="settings-actions">

                    <button
                        type="submit"
                        class="save-btn"
                    >
                        Save Currency Settings
                    </button>

                </div>

            </form>

        </div>

    </div>

</div>


<script>
document.addEventListener('DOMContentLoaded', function () {

    const symbolInput = document.querySelector(
        'input[name="currency_symbol"]'
    );

    const decimalSelect = document.querySelector(
        'select[name="currency_decimal_places"]'
    );

    const roundCheckbox = document.querySelector(
        '#currency_round_display'
    );

    const preview = document.querySelector(
        '#amountPreview'
    );


    function updatePreview() {

        if (
            !symbolInput ||
            !decimalSelect ||
            !roundCheckbox ||
            !preview
        ) {
            return;
        }


        let symbol = symbolInput.value.trim();

        if (!symbol) {
            symbol = '₹';
        }


        let decimals = parseInt(
            decimalSelect.value || 2,
            10
        );


        let amount = 500;


        if (roundCheckbox.checked) {
            amount = Math.round(amount);
        }


        preview.textContent =
            symbol + Number(amount).toFixed(decimals);

    }


    if (symbolInput) {

        symbolInput.addEventListener(
            'input',
            updatePreview
        );

    }


    if (decimalSelect) {

        decimalSelect.addEventListener(
            'change',
            updatePreview
        );

    }


    if (roundCheckbox) {

        roundCheckbox.addEventListener(
            'change',
            updatePreview
        );

    }


    updatePreview();

});
</script>

@endsection