@extends('layouts.main-app')

@php
    $request = request();
@endphp 

@section('content')
<form action="{{ route('withdraws.withdraws.money') }}" method="post" enctype="multipart/form-data">
    @csrf
    <div class="row justify-content-center gy-4">
        <div class="col-12 mt-4">
            <div class="page-heading mb-4">
                <h3 class="mb-2">{{ __($pageTitle) }}</h3>
                <p>
                    @lang('Take control of your withdrawals with our comprehensive withdraw page, Optimize your earnings and choose the best withdrawal method for you.')
                </p>
            </div>
            <hr>
        </div>
        <div class="col-12 mt-0">
        <div class="card">
            <div class="card-body">
                <div class="row">
                    <div class="col-md-8 withdraw-detail-border">
                        <div class="withdraw-detail">
                            <h3 class="text-muted title">@lang('Your current balance is') 
                                <span class="text--success withdraw-detail__balance">
                                    {{ showAmount($balance) }}
                                </span>
                            </h3>
                        </div>
                    </div>
                </div>
            </div>
        </div>
          </div>
        <div class="col-12 ">
            <div class="card custom--card style--two">
                <div class="card-header card-title mb-0 bg-white">
                    <h5>@lang('Withdraw Request')</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>@lang('Method')</label>
                                <select class="form-select form--control" name="network" required>
                                    <option value="">@lang('Select One')</option>
                             <option value="TRC20">Tron (TRC20)</option>

                                </select>
                                <p>
                                    <small class="fst-italic">
                                        <i class="las la-info-circle"></i> 
                                        @lang('Withdraw Time'): daily
                                    </small>
                                </p>
                                <p><small class="fst-italic"><i class="las la-info-circle"></i><span>Conversion Rate:</span> <span>1 USD = <span class="rate">{{ $finalRate }}</span>  <span class="base-currency">{{ strtoupper(auth()->user()->currency) }}</span></span></small></p>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="form-label">@lang('Amount')</label>
                                <div class="input--group">
                                    <input type="number" step="any" name="amount" value="0" class="form-control form--control" required>
                                    <span class="input-group--text">{{ strtoupper(auth()->user()->currency) }}</span>
                                </div>
                                <p>
                                    <small class="fst-italic">
                                        <i class="las la-info-circle"></i> 
                                        @lang('Withdraw Charge'): {{ strtoupper(auth()->user()->currency) }} <span class="charge"> 0.00</span> | @lang('Receivable Amount'): USD <span class="receivable"> 0.00</span>
                                    </small>
                                </p>
                            </div>
                        </div>
                        <div class="col-md-12">
            <div class="form-group">
                <div class="justify-content-between d-flex flex-wrap">
                    <label class="form-label required" for="mobile_number">
                        Address 
                    </label>
                                    </div>
                                    <input type="text" class="form-control form--control" name="address" required="">
                            </div>
        </div>
                                <div class="col-md-12">
    <div class="form-group">
        <label class="col-form-label">@lang('Google Authenticator Code')</label>
        
        @if(auth()->user()->ts == 'Y')
            <div class="alert alert-success p-2">
                ✅ @lang('Please enter the correct 6-digit code.')
            </div>
            <input type="text" class="form--control border-success" name="g2fa_code" required placeholder="Enter 6-digit code">
        @else
            <div class="alert alert-warning p-2">
                ⚠️ @lang('2FA is disabled. Please enable it first to change your password.')
            </div>
            <input type="text" class="form--control border-warning" name="g2fa_code" disabled placeholder="2FA Required">
        @endif
    </div>
</div>
                        <div class="col-12">
                            <div class="withdraw_form"></div>
                            <button type="submit" class="btn btn--base w-100 mt-3">@lang('Submit')</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
@endsection

@push('script')
    <script>
        "use strict";
(function($) {
    $(document).ready(function() {
        let rate = parseFloat($('.rate').text()); // Get conversion rate
        let charge = 0.00; 

        $('input[name="amount"]').on('input', function() {
            let usdAmount = parseFloat($(this).val()) || 0;
            let bdtAmount = usdAmount / rate; // Convert USD to BDT
            let receivableAmount = Math.max(bdtAmount - charge, 0); // Deduct charge
            
            $('.charge').text(charge.toFixed(2)); // Show charge
            $('.receivable').text(receivableAmount.toFixed(2)); // Show final amount
        });
    });
})(jQuery);
    </script>
@endpush
