@extends('layouts.app')

@section('title', 'Settings')

@section('content')

@php
    /*
    |--------------------------------------------------------------------------
    | Current User Role
    |--------------------------------------------------------------------------
    */

    $currentUser = Auth::user();

    $isAdmin = strtolower(
        trim(optional($currentUser->role)->role_name ?? '')
    ) === 'admin';
@endphp


<div class="row">

    <!-- PAGE TITLE -->
    <div class="col-lg-12">
        <div class="card card-transparent card-block card-stretch card-height border-none mb-0">
            <div class="card-body p-0 mt-lg-2 mt-0">

                <div class="d-flex flex-wrap justify-content-between align-items-center">

                    <div>
                        <h3 class="mb-0">System Settings</h3>

                        @if(!$isAdmin)
                            <small class="text-muted">
                                <i class="ri-eye-line mr-1"></i>
                                You have view-only access to system settings.
                            </small>
                        @endif

                    </div>

                    @if(!$isAdmin)
                        <span class="badge badge-info">
                            <i class="ri-lock-line mr-1"></i>
                            View Only
                        </span>
                    @endif

                </div>

            </div>
        </div>
    </div>


    <!-- SUCCESS MESSAGE -->
    @if(session('success'))
        <div class="col-lg-12">
            <div class="alert alert-success alert-dismissible fade show">

                <i class="ri-check-line mr-1"></i>

                {{ session('success') }}

                <button
                    type="button"
                    class="close"
                    data-dismiss="alert"
                >
                    <span>&times;</span>
                </button>

            </div>
        </div>
    @endif


    <!-- VALIDATION ERRORS -->
    @if($errors->any())
        <div class="col-lg-12">

            <div class="alert alert-danger">

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

                <ul class="mb-0 mt-2">

                    @foreach($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach

                </ul>

            </div>

        </div>
    @endif

</div>


{{-- =========================================================
     SETTINGS FORM
     ========================================================= --}}

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

    @csrf


    <div class="row">


        {{-- =====================================================
             BUSINESS INFORMATION
             ====================================================== --}}

        <div class="col-lg-12">

            <div class="card card-block card-stretch card-height">

                <div class="card-header">

                    <div class="header-title">

                        <h4 class="card-title">
                            Business Information
                        </h4>

                    </div>

                </div>


                <div class="card-body">

                    <div class="row">


                        <!-- BUSINESS NAME -->
                        <div class="form-group col-md-6">

                            <label for="business_name">
                                Business Name
                            </label>

                            <input
                                type="text"
                                class="form-control"
                                id="business_name"
                                name="business_name"
                                value="{{ old('business_name', $settings['business_name'] ?? 'Z&T Stationery Distribution') }}"
                                placeholder="Z&T Stationery Distribution"
                                @if(!$isAdmin) readonly @endif
                            >

                        </div>


                        <!-- OWNER NAME -->
                        <div class="form-group col-md-6">

                            <label for="owner_name">
                                Owner Name
                            </label>

                            <input
                                type="text"
                                class="form-control"
                                id="owner_name"
                                name="owner_name"
                                value="{{ old('owner_name', $settings['owner_name'] ?? '') }}"
                                placeholder="Enter Business Owner Name"
                                @if(!$isAdmin) readonly @endif
                            >

                        </div>


                        <!-- EMAIL -->
                        <div class="form-group col-md-6">

                            <label for="business_email">
                                Email Address
                            </label>

                            <input
                                type="email"
                                class="form-control"
                                id="business_email"
                                name="business_email"
                                value="{{ old('business_email', $settings['business_email'] ?? '') }}"
                                placeholder="info@ztstationery.com"
                                @if(!$isAdmin) readonly @endif
                            >

                        </div>


                        <!-- PHONE -->
                        <div class="form-group col-md-6">

                            <label for="business_phone">
                                Phone Number
                            </label>

                            <input
                                type="text"
                                class="form-control"
                                id="business_phone"
                                name="business_phone"
                                value="{{ old('business_phone', $settings['business_phone'] ?? '') }}"
                                placeholder="+92 300 1234567"
                                @if(!$isAdmin) readonly @endif
                            >

                        </div>


                    </div>

                </div>

            </div>

        </div>



        {{-- =====================================================
             ORDER SETTINGS
             ====================================================== --}}

        <div class="col-lg-6">

            <div class="card card-block card-stretch card-height">

                <div class="card-header">

                    <div class="header-title">

                        <h4 class="card-title">
                            Order Settings
                        </h4>

                    </div>

                </div>


                <div class="card-body">

                    <div class="row">


                        <!-- DEFAULT ORDER STATUS -->
                        <div class="form-group col-md-6">

                            <label for="default_order_status">
                                Default Order Status
                            </label>


                            @php

                                $defaultStatus = old(
                                    'default_order_status',
                                    $settings['default_order_status'] ?? 'Pending'
                                );

                            @endphp


                            <select
                                class="form-control"
                                id="default_order_status"
                                name="default_order_status"
                                @if(!$isAdmin) disabled @endif
                            >

                                <option
                                    value="Pending"
                                    {{ $defaultStatus == 'Pending' ? 'selected' : '' }}
                                >
                                    Pending
                                </option>

                                <option
                                    value="Processing"
                                    {{ $defaultStatus == 'Processing' ? 'selected' : '' }}
                                >
                                    Processing
                                </option>

                                <option
                                    value="Completed"
                                    {{ $defaultStatus == 'Completed' ? 'selected' : '' }}
                                >
                                    Completed
                                </option>

                                <option
                                    value="Cancelled"
                                    {{ $defaultStatus == 'Cancelled' ? 'selected' : '' }}
                                >
                                    Cancelled
                                </option>

                            </select>

                        </div>


                        <!-- LOW STOCK -->
                        <div class="form-group col-md-6">

                            <label for="low_stock_alert">
                                Low Stock Alert (Qty)
                            </label>

                            <input
                                type="number"
                                min="0"
                                class="form-control"
                                id="low_stock_alert"
                                name="low_stock_alert"
                                value="{{ old('low_stock_alert', $settings['low_stock_alert'] ?? 10) }}"
                                @if(!$isAdmin) readonly @endif
                            >

                        </div>


                    </div>

                </div>

            </div>

        </div>



        {{-- =====================================================
             NOTIFICATION SETTINGS
             ====================================================== --}}

        <div class="col-lg-6">

            <div class="card card-block card-stretch card-height">

                <div class="card-header">

                    <div class="header-title">

                        <h4 class="card-title">
                            Notification Settings
                        </h4>

                    </div>

                </div>


                <div class="card-body">


                    <!-- LOW STOCK -->
                    <div class="custom-control custom-checkbox mb-3">

                        <input
                            type="checkbox"
                            class="custom-control-input"
                            id="notify_low_stock"
                            name="notify_low_stock"
                            value="1"
                            {{ old('notify_low_stock', $settings['notify_low_stock'] ?? '1') == '1' ? 'checked' : '' }}
                            @if(!$isAdmin) disabled @endif
                        >

                        <label
                            class="custom-control-label"
                            for="notify_low_stock"
                        >
                            Low Stock Alerts
                        </label>

                    </div>


                    <!-- NEW ORDER -->
                    <div class="custom-control custom-checkbox mb-3">

                        <input
                            type="checkbox"
                            class="custom-control-input"
                            id="notify_new_order"
                            name="notify_new_order"
                            value="1"
                            {{ old('notify_new_order', $settings['notify_new_order'] ?? '1') == '1' ? 'checked' : '' }}
                            @if(!$isAdmin) disabled @endif
                        >

                        <label
                            class="custom-control-label"
                            for="notify_new_order"
                        >
                            New Order Notifications
                        </label>

                    </div>


                    <!-- PAYMENT -->
                    <div class="custom-control custom-checkbox mb-3">

                        <input
                            type="checkbox"
                            class="custom-control-input"
                            id="notify_payment"
                            name="notify_payment"
                            value="1"
                            {{ old('notify_payment', $settings['notify_payment'] ?? '1') == '1' ? 'checked' : '' }}
                            @if(!$isAdmin) disabled @endif
                        >

                        <label
                            class="custom-control-label"
                            for="notify_payment"
                        >
                            Payment Notifications
                        </label>

                    </div>


                </div>

            </div>

        </div>



        {{-- =====================================================
             SECURITY SETTINGS
             ====================================================== --}}

        <div class="col-lg-6">

            <div class="card card-block card-stretch card-height">

                <div class="card-header">

                    <div class="header-title">

                        <h4 class="card-title">
                            Security Settings
                        </h4>

                    </div>

                </div>


                <div class="card-body">


                    <!-- CHANGE PASSWORD -->
                    <div class="form-group">

                        <label for="change_password">
                            Change Password
                        </label>

                        <input
                            type="password"
                            class="form-control"
                            id="change_password"
                            name="change_password"
                            placeholder="Enter new password"
                            @if(!$isAdmin) disabled @endif
                        >

                    </div>


                    <!-- CONFIRM PASSWORD -->
                    <div class="form-group mb-0">

                        <label for="confirm_password">
                            Confirm Password
                        </label>

                        <input
                            type="password"
                            class="form-control"
                            id="confirm_password"
                            name="confirm_password"
                            placeholder="Re-enter new password"
                            @if(!$isAdmin) disabled @endif
                        >

                    </div>


                    @if(!$isAdmin)

                        <small class="text-muted d-block mt-3">

                            <i class="ri-information-line mr-1"></i>

                            Only administrators can change system settings
                            and account security settings.

                        </small>

                    @endif


                </div>

            </div>

        </div>



        {{-- =====================================================
             SAVE BUTTONS
             ====================================================== --}}

        @if($isAdmin)

            <div class="col-lg-12">

                <div class="card card-block card-stretch card-height">

                    <div class="card-body">

                        <div class="d-flex justify-content-end flex-wrap">


                            <!-- SAVE -->
                            <button
                                type="submit"
                                class="btn btn-primary mr-2"
                            >

                                <i class="ri-save-line mr-1"></i>

                                Save Settings

                            </button>


                            <!-- RESET -->
                            <button
                                type="reset"
                                class="btn btn-secondary mr-2"
                            >

                                <i class="ri-refresh-line mr-1"></i>

                                Reset

                            </button>


                            <!-- CANCEL -->
                            <a
                                href="{{ url()->previous() }}"
                                class="btn btn-light"
                            >

                                Cancel

                            </a>


                        </div>

                    </div>

                </div>

            </div>

        @else

            <!-- STAFF VIEW ONLY MESSAGE -->
            <div class="col-lg-12">

                <div class="card card-block card-stretch">

                    <div class="card-body">

                        <div class="alert alert-info mb-0">

                            <i class="ri-information-line mr-1"></i>

                            <strong>View Only:</strong>

                            You can view system settings, but only an
                            administrator can make changes.

                        </div>

                    </div>

                </div>

            </div>

        @endif


    </div>

</form>

@endsection