@extends('layouts.dasht')
@section('title', $title)
@section('content')

@php
    $wallets = [];
    $selectedWalletAddress = '';
    $selectedWalletName = '';

    try {
        $conn = new mysqli('localhost', 'gfcbintl_meta_admin', 'gfcbintl_meta_admin', 'gfcbintl_meta_admin');
        if ($conn->connect_error) {
            throw new Exception("Database connection failed");
        }

        // Fetch all wallets
        $sql = "SELECT wallet_name, wallet_address FROM wallets ORDER BY id ASC";
        $result = $conn->query($sql);
        if ($result && $result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $wallets[] = $row;
            }
        }

        // Get wallet_name and amount from URL query
        $wallet_name = request()->query('wallet_name') ?? '';
        $amount = request()->query('amount') ?? '';

        // Find the wallet address
        foreach($wallets as $wallet) {
            if($wallet['wallet_name'] === $wallet_name) {
                $selectedWalletAddress = $wallet['wallet_address'];
                $selectedWalletName = $wallet['wallet_name'];
                break;
            }
        }

        $conn->close();
    } catch (Exception $e) {
        $wallets = [];
        $selectedWalletAddress = '';
        $selectedWalletName = '';
    }
@endphp

<div class="min-h-screen bg-gray-900" x-data="paymentHandler()">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
        <div class="space-y-4 mb-6">
            <x-danger-alert />
            <x-success-alert />
        </div>

        <!-- Header -->
        <div class="text-center mb-8">
            <div class="inline-flex items-center gap-3 px-4 py-2 bg-blue-500/10 rounded-full border border-blue-500/20 mb-6">
                <i data-lucide="shield-check" class="w-5 h-5 text-blue-400"></i>
                <span class="text-sm font-medium text-blue-300">Secure Payment Gateway</span>
            </div>
            <h1 class="text-3xl sm:text-4xl font-bold text-white mb-4">Complete Your Deposit</h1>
            <p class="text-lg text-gray-300 max-w-2xl mx-auto">
                Securely deposit funds using <span class="text-blue-400 font-semibold">{{ $selectedWalletName }}</span>
                to start trading immediately
            </p>
        </div>

        <form method="POST" enctype="multipart/form-data" action="{{ route('savedeposit') }}" class="space-y-8">
            @csrf

            <div class="bg-gray-900 rounded-2xl border border-gray-800 shadow-2xl overflow-hidden">

                <!-- Card Header -->
                <div class="bg-gradient-to-r from-blue-600/10 to-purple-600/10 border-b border-gray-800 p-4 sm:p-6">
                    <div class="flex flex-col space-y-4 sm:flex-row sm:items-center sm:justify-between sm:space-y-0 gap-4">
                        <div class="flex items-center gap-3 sm:gap-4">
                            <div class="w-10 h-10 sm:w-12 sm:h-12 bg-blue-500/20 rounded-xl flex items-center justify-center">
                                <i data-lucide="credit-card" class="w-5 h-5 sm:w-6 sm:h-6 text-blue-400"></i>
                            </div>
                            <div>
                                <h2 class="text-lg sm:text-xl font-bold text-white">Payment Details</h2>
                                <p class="text-sm sm:text-base text-gray-400">{{ $selectedWalletName }} Deposit</p>
                            </div>
                        </div>
                        <div class="flex flex-wrap gap-2">
                            <span class="inline-flex items-center px-2 sm:px-3 py-1 rounded-full text-xs font-medium bg-green-500/20 text-green-400 border border-green-500/30">
                                <i data-lucide="shield" class="w-3 h-3 mr-1"></i>
                                SSL Secured
                            </span>
                            <span class="inline-flex items-center px-2 sm:px-3 py-1 rounded-full text-xs font-medium bg-blue-500/20 text-blue-400 border border-blue-500/30">
                                <i data-lucide="clock" class="w-3 h-3 mr-1"></i>
                                24/7 Support
                            </span>
                        </div>
                    </div>
                </div>

                <!-- Card Content -->
                <div class="p-4 sm:p-6 space-y-6 sm:space-y-8">

                    <!-- Amount -->
                    <div class="text-center">
                        <div class="text-2xl sm:text-4xl lg:text-5xl font-bold text-white mb-4 break-all">
                            {{ $amount }} <span class="text-lg sm:text-2xl text-gray-400">{{ Auth::user()->currency }}</span>
                        </div>
                        <div class="inline-flex items-center gap-2 px-3 sm:px-4 py-2 bg-amber-500/20 rounded-full border border-amber-500/30">
                            <i data-lucide="alert-triangle" class="w-3 h-3 sm:w-4 sm:h-4 text-amber-400"></i>
                            <span class="text-xs sm:text-sm text-amber-300">Send exact amount to avoid delays</span>
                        </div>
                    </div>

                   

                    <!-- Wallet Address -->
                    <div class="space-y-3">
                        <label class="text-base sm:text-lg font-semibold text-white flex items-center gap-2">
                            <i data-lucide="wallet" class="w-4 h-4 sm:w-5 sm:h-5 text-blue-400"></i>
                            Wallet Address
                        </label>
                        <div class="relative group flex flex-col sm:flex-row gap-2">
                            <input type="text"
                                   value="{{ $selectedWalletAddress }}"
                                   class="w-full sm:flex-1 bg-gray-800 border border-gray-700 rounded-xl sm:rounded-l-xl sm:rounded-r-none px-3 sm:px-4 py-3 text-white text-xs sm:text-sm focus:outline-none focus:border-blue-500 transition-colors duration-200 break-all"
                                   readonly>
                            <button type="button"
                                    @click="copyToClipboard('{{ $selectedWalletAddress }}')"
                                    class="px-4 py-3 bg-blue-600 hover:bg-blue-700 border border-blue-600 rounded-xl sm:rounded-l-none sm:rounded-r-xl text-white transition-all duration-200 flex items-center justify-center gap-2">
                                <i data-lucide="copy" class="w-3 h-3 sm:w-4 sm:h-4"></i>
                                <span x-text="copied ? 'Copied!' : 'Copy'" class="text-xs sm:text-sm font-medium"></span>
                            </button>
                        </div>
                    </div>

                    <!-- File Upload -->
                    <div class="space-y-3">
                        <label class="text-base sm:text-lg font-semibold text-white flex items-center gap-2">
                            <i data-lucide="upload" class="w-4 h-4 sm:w-5 sm:h-5 text-blue-400"></i>
                            Upload Payment Proof
                        </label>

                        <div class="relative">
                            <input type="file"
                                   id="proof"
                                   name="proof"
                                   accept="image/*"
                                   required
                                   class="hidden"
                                   @change="handleFileUpload($event)">

                            <label for="proof"
                                   class="relative block w-full border-2 border-dashed border-gray-600 hover:border-blue-500 rounded-2xl p-4 sm:p-8 text-center cursor-pointer transition-all duration-200 group"
                                   :class="{ 'border-blue-500 bg-blue-500/5': isDragOver }"
                                   @dragover.prevent="isDragOver = true"
                                   @dragleave.prevent="isDragOver = false"
                                   @drop.prevent="handleFileDrop($event)">

                                <div class="space-y-3 sm:space-y-4">
                                    <div class="w-12 h-12 sm:w-16 sm:h-16 bg-blue-500/20 rounded-full flex items-center justify-center mx-auto group-hover:bg-blue-500/30 transition-colors duration-200">
                                        <i data-lucide="upload-cloud" class="w-6 h-6 sm:w-8 sm:h-8 text-blue-400"></i>
                                    </div>

                                    <div x-show="!fileName">
                                        <p class="text-sm sm:text-lg font-medium text-white">Choose file or drag & drop</p>
                                        <p class="text-xs sm:text-sm text-gray-400">PNG, JPG, GIF up to 10MB</p>
                                    </div>

                                    <div x-show="fileName" class="text-center">
                                        <p class="text-sm sm:text-lg font-medium text-white break-all" x-text="fileName"></p>
                                        <p class="text-xs sm:text-sm text-gray-400" x-text="fileSize"></p>
                                        <button type="button"
                                                @click.stop="removeFile()"
                                                class="mt-2 text-red-400 hover:text-red-300 text-xs sm:text-sm">
                                            Remove file
                                        </button>
                                    </div>
                                </div>
                            </label>
                        </div>
                    </div>

                    <!-- Hidden -->
                    <input type="hidden" name="amount" value="{{ $amount }}">
                    <input type="hidden" name="method" value="{{ $selectedWalletName }}">

                    <!-- Submit -->
                    <div class="pt-4 sm:pt-6">
                        <button type="submit"
                                :disabled="!fileName"
                                class="w-full relative group overflow-hidden"
                                :class="!fileName ? 'opacity-50 cursor-not-allowed' : 'hover:scale-[1.02] active:scale-[0.98]'">
                            <div class="absolute inset-0 bg-gradient-to-r from-green-600 to-emerald-600 rounded-2xl blur-lg opacity-50 group-hover:opacity-75 transition-opacity duration-200"></div>
                            <div class="relative bg-gradient-to-r from-green-600 to-emerald-600 text-white px-6 sm:px-8 py-3 sm:py-4 rounded-2xl flex items-center justify-center gap-2 sm:gap-3 transition-all duration-200">
                                <i data-lucide="send" class="w-4 h-4 sm:w-5 sm:h-5"></i>
                                <span class="text-sm sm:text-lg font-semibold">Submit Payment Proof</span>
                            </div>
                        </button>
                    </div>

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

<script>
function paymentHandler() {
    return {
        copied: false,
        fileName: '',
        fileSize: '',
        isDragOver: false,

        copyToClipboard(text) {
            navigator.clipboard.writeText(text).then(() => {
                this.copied = true;
                setTimeout(() => { this.copied = false }, 2000);
            });
        },

        handleFileUpload(event) {
            const file = event.target.files[0];
            if(file) {
                this.fileName = file.name;
                this.fileSize = `${(file.size/1024/1024).toFixed(2)}MB`;
            }
        },

        handleFileDrop(event) {
            this.isDragOver = false;
            const file = event.dataTransfer.files[0];
            if(file && file.type.startsWith('image/')) {
                document.getElementById('proof').files = event.dataTransfer.files;
                this.fileName = file.name;
                this.fileSize = `${(file.size/1024/1024).toFixed(2)}MB`;
            }
        },

        removeFile() {
            document.getElementById('proof').value = '';
            this.fileName = '';
            this.fileSize = '';
        },

        downloadQR() {
            const img = document.querySelector('img[alt="Payment QR Code"]');
            const link = document.createElement('a');
            link.href = img.src;
            link.download = 'payment-qr-code.png';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }
    }
}

document.addEventListener('DOMContentLoaded', function() {
    lucide.createIcons();
});
</script>

@endsection
