@extends('layout.admin')

@section('content')
@php
    $adminUser = auth('admin')->user();
    $canUpdateUser = $adminUser?->hasAnyProjectPermission(['users', 'users_update']) ?? true;
@endphp
<div class="max-w-lg mx-auto p-6 bg-white rounded shadow-md">
    <h2 class="text-2xl font-bold mb-6 text-gray-800">Update User</h2>

    @if(session('success'))
        <div class="mb-4 p-3 rounded bg-green-50 text-green-700 border border-green-200">
            {{ session('success') }}
        </div>
    @endif

    @if(session('error'))
        <div class="mb-4 p-3 rounded bg-red-50 text-red-700 border border-red-200">
            {{ session('error') }}
        </div>
    @endif

    @unless($canUpdateUser)
        <div class="mb-4 p-3 rounded bg-yellow-50 text-yellow-800 border border-yellow-200">
            আপনার `update` permission নেই। তাই এই page দেখতে পারবেন, কিন্তু save করতে পারবেন না।
        </div>
    @endunless

    <form method="POST" action="{{ route('admin.users.update', $user->id) }}">
        @csrf

        <div class="mb-4">
            <label class="block mb-1 font-medium text-gray-700">Name</label>
            <input type="text" name="name" value="{{ old('name', $user->name) }}"
                   class="w-full border rounded px-3 py-2 focus:ring-2 focus:ring-blue-500" required>
        </div>

        <div class="mb-4">
            <label class="block mb-1 font-medium text-gray-700">Mobile</label>
            <input type="text" name="mobile" value="{{ old('mobile', $user->mobile) }}"
                   class="w-full border rounded px-3 py-2 focus:ring-2 focus:ring-blue-500" required>
        </div>

        <div class="mb-4">
            <label class="block mb-1 font-medium text-gray-700">Email</label>
            <input type="email" name="email" value="{{ old('email', $user->email) }}"
                   class="w-full border rounded px-3 py-2 focus:ring-2 focus:ring-blue-500" required>
        </div>

        <div class="mb-4">
            <label class="block mb-1 font-medium text-gray-700">Referral Code</label>
            <input type="text" name="referral_code" value="{{ old('referral_code', $user->referral_code) }}"
                   class="w-full border rounded px-3 py-2 focus:ring-2 focus:ring-blue-500" required>
        </div>

        <div class="mb-4">
            <label class="block mb-1 font-medium text-gray-700">Referred By</label>
            <input type="text" name="referred_by" value="{{ old('referred_by', $user->referred_by) }}"
                   class="w-full border rounded px-3 py-2 focus:ring-2 focus:ring-blue-500">
        </div>

        <div class="mb-4">
            <label class="block mb-1 font-medium text-gray-700">New Password (optional)</label>
            <input type="password" name="password"
                   class="w-full border rounded px-3 py-2 focus:ring-2 focus:ring-blue-500"
                   placeholder="Leave empty if you do not want to change password">
        </div>

        <div class="mb-4">
            <label class="block mb-1 font-medium text-gray-700">Account Status</label>
            <select name="is_active"
                    class="w-full border rounded px-3 py-2 focus:ring-2 focus:ring-blue-500">
                <option value="1" {{ $user->is_active ? 'selected' : '' }}>Active</option>
                <option value="0" {{ !$user->is_active ? 'selected' : '' }}>Inactive</option>
            </select>
        </div>

        <div class="mb-4">
            <label class="block mb-1 font-medium text-gray-700">Verified At</label>
            <input type="datetime-local" name="activated_at"
                   value="{{ old('activated_at', optional($user->activated_at)->format('Y-m-d\\TH:i')) }}"
                   class="w-full border rounded px-3 py-2 focus:ring-2 focus:ring-blue-500">
        </div>

        <div class="mb-4">
            <label class="block mb-1 font-medium text-gray-700">Typing Task for Upline</label>
            <select name="typing_job_referral_enabled"
                    class="w-full border rounded px-3 py-2 focus:ring-2 focus:ring-blue-500">
                <option value="1" {{ (int) ($user->typing_job_referral_enabled ?? 0) === 1 ? 'selected' : '' }}>Enable</option>
                <option value="0" {{ (int) ($user->typing_job_referral_enabled ?? 0) !== 1 ? 'selected' : '' }}>Disable</option>
            </select>
        </div>

        <div class="mb-4">
            <label class="block mb-1 font-medium text-gray-700">Welcome Offer Count for Upline</label>
            @php
                $welcomeReferralEnabled = ($user->welcome_offer_referral_enabled ?? null) === null
                    ? 1
                    : (int) $user->welcome_offer_referral_enabled;
            @endphp
            <select name="welcome_offer_referral_enabled"
                    class="w-full border rounded px-3 py-2 focus:ring-2 focus:ring-blue-500">
                <option value="1" {{ $welcomeReferralEnabled === 1 ? 'selected' : '' }}>Enable</option>
                <option value="0" {{ $welcomeReferralEnabled !== 1 ? 'selected' : '' }}>Disable</option>
            </select>
        </div>

        <div class="mb-4">
            <label class="block mb-1 font-medium text-gray-700">Leadership Count for Upline</label>
            @php
                $leadershipReferralEnabled = ($user->leadership_referral_enabled ?? null) === null
                    ? 1
                    : (int) $user->leadership_referral_enabled;
            @endphp
            <select name="leadership_referral_enabled"
                    class="w-full border rounded px-3 py-2 focus:ring-2 focus:ring-blue-500">
                <option value="1" {{ $leadershipReferralEnabled === 1 ? 'selected' : '' }}>Enable</option>
                <option value="0" {{ $leadershipReferralEnabled !== 1 ? 'selected' : '' }}>Disable</option>
            </select>
        </div>

        <div class="mb-4">
            <label class="block mb-1 font-medium text-gray-700">Ban Status</label>
            <select name="is_banned"
                    class="w-full border rounded px-3 py-2 focus:ring-2 focus:ring-blue-500">
                <option value="0" {{ !$user->is_banned ? 'selected' : '' }}>Unbanned</option>
                <option value="1" {{ $user->is_banned ? 'selected' : '' }}>Banned</option>
            </select>
        </div>

        <div class="mb-4">
            <label class="block mb-1 font-medium text-gray-700">Ban Reason</label>
            <textarea name="ban_reason" rows="3"
                      class="w-full border rounded p-2 focus:ring-2 focus:ring-red-500"
                      placeholder="Write why this account is banned">{{ old('ban_reason', $user->ban_reason) }}</textarea>
        </div>

        <div class="mb-4">
            <label class="block mb-1 font-medium text-gray-700">Admin Message</label>
            <textarea name="admin_message" rows="3"
                      class="w-full border rounded p-2 focus:ring-2 focus:ring-blue-500">{{ old('admin_message', $user->admin_message) }}</textarea>
        </div>

        @if($canUpdateUser)
            <div class="mb-6">
                <label class="block mb-1 font-medium text-red-600">Security Password</label>
                <input type="password" name="admin_password"
                       class="w-full border rounded px-3 py-2 focus:ring-2 focus:ring-red-500"
                       placeholder="Enter security password to save" required>
                <p class="mt-2 text-xs text-gray-500">Configured security password or your current admin password will work.</p>
            </div>

            <button type="submit"
                    class="w-full bg-blue-600 text-white font-semibold py-2 rounded hover:bg-blue-700 transition">
                Update User
            </button>
        @endif
    </form>
</div>

<script>
    (function () {
        const banSelect = document.querySelector('select[name="is_banned"]');
        const banReason = document.querySelector('textarea[name="ban_reason"]');
        const syncBan = () => {
            if (!banSelect || !banReason) return;
            banReason.required = banSelect.value === '1';
        };

        if (banSelect && banReason) {
            banSelect.addEventListener('change', syncBan);
            syncBan();
        }
    })();
</script>
@endsection
