@extends('master')
@section('body')

<!-- Dashboard Section -->
<div class="container py-5">
    <div class="row g-4">
        <!-- Profile + Payment -->
        <form action='{{route("payment")}}' method="post" onsubmit="return validatePayment()">
            @csrf
            <div class="col-12">
                <div class="card card-custom p-4">
                    <div class="row align-items-center">
                        <div class="col-md-8 d-flex align-items-center gap-4">
                            <div class="avatar-circle"><i class="fa-solid fa-user"></i></div>
                            <div>
                                <h5 class="mb-0">{{ Session::get('name') }}</h5>
                                <div class="text-muted small">Customer ID: {{ $customer->cus_id}}</div>
                                <div class="text-muted small">Customer IP: {{ $customer->ip}}</div>
                                <div class="text-muted small">Location: {{$customer->ag_office_address}}</div>
                                <div class="text-muted small">Phone: {{$customer->ag_mobile_no}}</div>
                                @if($customer->ag_status == 1)
                                <span class="badge bg-success mt-2">Active</span>
                                @elseif($customer->ag_status == 2)
                                <span class="badge bg-success mt-2">Active</span>
                                @elseif($customer->ag_status == 0)
                                <span class="badge bg-danger mt-2">Inactive</span>
                                @endif
                            </div>
                        </div>
                        <div class="col-md-4 text-end">
                            <div class="d-flex justify-content-end align-items-center gap-2 mb-2">
                                <img src="https://download.logo.wine/logo/BKash/BKash-Logo.wine.png" class="bkash-logo" alt="bKash">
                                <span class="fw-semibold text-danger">Payment</span>
                            </div>
                            @php
                            $dueadvance = $billinng->dueadvance;
                            $monthlyBill = $billinng->monthlybill;
                            if ($dueadvance < 0) {
                                $due=0;
                                $advance=abs($dueadvance);
                                }else{
                                $due=$dueadvance;
                                }
                                @endphp
                                <h5 class="mb-2">Bill Amount:</h5>
                                <input type="number" class="my-3 border-1" style="width: 100px;" id="amount" name="amount" value="{{$due}}"><br>
                                @if($dueadvance < 0)
                                    <p class="text-success fw-bold">Advance Amount: {{$advance}}</p>
                                    @elseif($dueadvance == 0)
                                    <p class="text-success fw-bold">bill Amount : 0</p>
                                    @else
                                    <p class="text-danger fw-bold">Due Amount: {{$due}}</p>
                                    @endif
                                    <input type="hidden" name="customer_id" value="{{$customer->ag_id}}">
                                    <button class="btn btn-success btn-pay">Pay Now</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
        <!-- Notice -->
        <div class="col-12">
            <h5 class="section-title">Notice</h5>
            @if(Session::has('wrong'))
            <div class="alert alert-danger" role="alert">
                <p class="mb-0"> {{ Session::get('wrong') }}</p>
            </div>
            @elseif(Session::has('success'))
            <div class="alert alert-success" role="alert">
                <p class="mb-0">{{ Session::get('success') }}</p>
            </div>
            @endif
        </div>
    </div>
</div>
<div class="container py-5">
    <div class="row g-4">
        <!-- Profile + Payment -->
        <div class="col-12">
            <div class="card card-custom p-4">
                <div class="row align-items-center">
                    <table class="table">
                        <thead>
                            <tr>
                                <th scope="col">#</th>
                                <th scope="col">Date</th>
                                <th scope="col">Description</th>
                                <th scope="col">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($accountStatement as $statement)
                            <tr>
                                <th scope="row">{{$loop->iteration}}</th>
                                <td>{{$statement->entry_date}}</td>
                                <td>{{$statement->acc_description}}</td>
                                <td>{{$statement->acc_amount}}</td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    function validatePayment() {
        let amount = parseFloat(document.getElementById('amount').value); // 400
        let dueAdvance = {{ $dueadvance }}; //400
        let monthlyBill = {{ $monthlyBill }}; // 500

        if(dueAdvance > amount && amount < monthlyBill) {
            alert("The payment amount must be equal to or greater than the monthly bill ({{ $monthlyBill }} BDT)");
            return false;
        }
        return true;
    }
</script>

@endsection

