<?php
$customerFinancialTransactions = $customer->customerFinancialTransactions;
?>
<div id="generic-customer-invoices" class="">
    <div class="d-flex align-items-end pb-1">
        <h4 class="font-weight-bold m-0">
            Customer Financial Transactions
        </h4>
    </div>
    @if (!count($customerFinancialTransactions))
    <div class="border p-2">This customer does not have any financial transactions yet.</div>
    @else
    <table class="table table-sm table-bordered table-striped m-0">
        <thead>
            <tr>
                <th class="border-bottom-0">Created At</th>
                <th class="border-bottom-0">Amount</th>
                <th class="border-bottom-0">Is Captured?</th>
                <th class="border-bottom-0"></th>
            </tr>
        </thead>
        <tbody>
            @foreach ($customerFinancialTransactions as $record)
            <tr>
                <td>{{ friendly_date($record->created_at) }}</td>
                <td>${{ friendly_money($record->amount) }}</td>
                <td>{{ $record->is_captured ? 'YES':'NO' }}</td>
                <td>
                    @if($record->was_refunded)
                    <?php
                        $refundDetails = json_decode($record->refund_detail_json);
                        $refundedAmount = floatval($refundDetails->amount)/100;
                    ?>
                    <div class="d-flex flex-column">
                        <span><b>Refunded:</b> ${{ friendly_money($refundedAmount) }}</span>
                        <span><b>Reason:</b> {{ $refundDetails->reason }}</span>
                    </div>
                    @elseif($record->is_captured)
                    <div moe class="mr-2">
                        <a href="" start show>Refund</a>
                        <form url="/api/financialTransaction/refund" class="mcp-theme-1" right>
                            <input type="hidden" name="uid" value="{{ $record->uid }}">
                            <div class="mb-2">
                                <label class="text-sm text-secondary mb-1">Amount</label>
                                <input type="text" name="amount" autocomplete="off" class="form-control form-control-sm" value="{{ $record->amount }}" required>
                            </div>
                            <div class="mb-2">
                                <label class="text-sm text-secondary mb-1">Description</label>
                                <textarea rows="2" name="description" autocomplete="off" class="form-control form-control-sm" required></textarea>
                            </div>
                            <div>
                                <button submit class="btn btn-sm btn-primary mr-2">Refund</button>
                                <button cancel class="btn btn-sm btn-default border">Cancel</button>
                            </div>
                        </form>
                    </div>
                    @else
                    <div moe class="mr-2">
                        <a href="" start show>Capture</a>
                        <form url="/api/financialTransaction/captureAuthorizedTransaction" class="mcp-theme-1" right>
                            <p class="mb-2 text-nowrap">Are you sure?</p>
                            <input type="hidden" name="uid" value="{{ $record->uid }}">
                            <div>
                                <button submit class="btn btn-sm btn-primary mr-2">Capture</button>
                                <button cancel class="btn btn-sm btn-default border">Cancel</button>
                            </div>
                        </form>
                    </div>
                    @endif
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
    @endif
</div>