@extends ('layouts.ic')
@section('content')
<div class="container mcp-theme-1">
    <div class="d-flex align-items-baseline mt-3 mb-2">
        <h3 class="font-size-16 font-weight-bold text-dark">{{$customer->client->displayName()}} / {{isset($company) ? $company->name : '(No compamy)'}}</h3>
        <a href="{{config('app.stagfe6_url')}}" class="ml-auto text-nowrap">
            <i class="fa fa-power-off"></i>
            Exit
        </a>
    </div>
    <hr class="mt-0 mb-3">
    <div class="row">
        <div class="col-md-8">
            <div class="mb-3 font-weight-bold">Invoices</div>
            @if(count($customer->invoices))
                <table class="table table-sm table-bordered">
                    <thead>
                    <tr>
                        <th class="border-bottom-0 text-secondary bg-light">Date</th>
                        <th class="border-bottom-0 text-secondary bg-light">Particulars</th>
                        <th class="border-bottom-0 text-secondary bg-light">Total</th>
                        <th class="border-bottom-0 text-secondary bg-light">Paid</th>
                        <th class="border-bottom-0 text-secondary bg-light">Due</th>
                    </tr>
                    </thead>
                    <tbody class="font-weight-normal">
                    <?php
                    $totalAmount = 0;
                    $totalPaid = 0;
                    $totalBalance = 0;
                    ?>
                    @foreach($customer->invoices as $invoice)
                        <tr>
                            <td>{{friendly_date($invoice->created_at)}}</td>
                            <td><a href="{{route('icPayInvoice', ['invoiceUid' => $invoice->uid])}}">{{$invoice->description}}</a></td>
                            <td class="text-dark">${{!is_null($invoice->amount) ? $invoice->amount : 0}}</td>
                            <td class="text-success">${{!is_null($invoice->paid) ? $invoice->paid : 0}}</td>
                            <td class="font-weight-bold text-warning-dark">${{!is_null($invoice->balance) ? $invoice->balance : 0}}</td>
                            <?php
                            $totalAmount += (!is_null($invoice->amount) ? $invoice->amount : 0);
                            $totalPaid += (!is_null($invoice->paid) ? $invoice->paid : 0);
                            $totalBalance += (!is_null($invoice->balance) ? $invoice->balance : 0);
                            ?>
                        </tr>
                    @endforeach
                    <tr>
                        <td class="p-3 text-right" colspan="5">
                            <div class="d-flex justify-content-end align-items-baseline mb-2">
                                <span class="text-secondary">Total Amount: </span>
                                <span class="width-50px text-left text-secondary font-weight-bold pl-2">${{$totalAmount}}</span>
                            </div>
                            <div class="d-flex justify-content-end align-items-baseline mb-2">
                                <span class="text-secondary">Total Paid: </span>
                                <span class="width-50px text-left text-secondary font-weight-bold pl-2">${{$totalPaid}}</span>
                            </div>
                            <div class="d-flex justify-content-end align-items-baseline">
                                <span class="font-weight-bold text-secondary">Total Due: </span>
                                <span class="width-50px text-left font-weight-bold pl-2 font-size-14">${{$totalBalance}}</span>
                            </div>
                        </td>
                    </tr>
                    </tbody>
                </table>
            @else
                <div class="text-secondary mb-3">No invoices yet!</div>
            @endif
            <div class="mb-3 font-weight-bold">Transactions</div>
            @if(count($customer->customerTransactions))
                <table class="table table-sm table-bordered">
                    <thead>
                    <tr>
                        <th class="border-bottom-0 text-secondary bg-light">Date</th>
                        <th class="border-bottom-0 text-secondary bg-light">Type</th>
                        <th class="border-bottom-0 text-secondary bg-light">Remarks</th>
                        <th class="border-bottom-0 text-secondary bg-light">Amount</th>
                        <th class="border-bottom-0 text-secondary bg-light">Starting Balance</th>
                        <th class="border-bottom-0 text-secondary bg-light">Ending Balance</th>
                    </tr>
                    </thead>
                    <tbody class="font-weight-normal">
                    <?php
                    $totalAmount = 0;
                    $totalPaid = 0;
                    $totalBalance = 0;
                    ?>
                    @foreach($customer->customerTransactions as $transaction)
                        <tr>
                            <td>{{friendly_date($transaction->created_at)}}</td>
                            <td class="{{$transaction->plus_or_minus === 'PLUS' ? 'text-success' : 'text-warning-dark'}}">{{$transaction->plus_or_minus === 'PLUS' ? 'Credit' : 'Debit'}}</td>
                            <td>{{ sanitize_state_name($transaction->reason_type)}}</td>
                            <td class="{{$transaction->plus_or_minus === 'PLUS' ? 'text-success' : 'text-warning-dark'}}">${{!is_null($transaction->amount) ? $transaction->amount : 0}}</td>
                            <td>${{!is_null($transaction->starting_balance) ? $transaction->starting_balance : 0}}</td>
                            <td>${{!is_null($transaction->resulting_balance) ? $transaction->resulting_balance : 0}}</td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            @else
                <div class="text-secondary">No transactions yet!</div>
            @endif
        </div>
        @include('app.invoice-center.ic-rhs')
    </div>
</div>
@endsection
