@extends('layouts.base_pdf')

@section('inner_content')

    <br>
    <htmlpageheader name="page-header">
        <br><br>
        <div class="separator"><strong><i>{{ $company->reference }}</i></strong></div>
    </htmlpageheader>

    <table>
        <!-- Header Section -->
        <tr>
            <td class="header-logo">
                <img src="{{ asset('images/ri_1.png') }}" alt="logo" id="logo" class="logo">
            </td>
            <td class="header-cief-address">
                <span class="company-name"><strong>CIEF WORLDWIDE SDN BHD</strong></span>
                <span class="company-reg">(1134596-M)</span><br>
                No. 72-3, Jalan Jalil 1,<br>
                The Earth Bukit Jalil,<br>
                57000 Kuala Lumpur<br>
                Tel: 03-8082 1252
            </td>
            <td class="header-details">
                <div class="title right" style="font-size: 1.7em !important;"><strong>Account<br>Statement<br>Transactions</strong></div>
                <div>&nbsp;</div>
            </td>
        </tr>
        <tr>
            <td colspan="3" class="address">
                <div class="label">{{ $company->name }}</div>
                @php
                    $billingAddress = $company->addresses()->where('billing', '=', true)->first();
                @endphp
                <div class="address">
                    {{ $billingAddress->street_one }}
                    {{ $billingAddress->street_two }},
                    {{ $billingAddress->district()->first()->name }},
                    {{ $billingAddress->postcode }}
                    {{ $billingAddress->state()->first()->name }},
                    {{ $billingAddress->country()->first()->name }}
                </div>
                <div>Phone: {{ $company->contacts()->first()->phone }}</div>
            </td>
        </tr>
    </table>

    <br>

    <table class="line-table" style="overflow: wrap" autosize="1">
    <!-- Table Header -->
    <thead>
        <tr>
            <th width="5%">No</th>
            <th class="stock-code" width="15%">Date</th>
            <th class="description">Description</th>
            <th width="15%">Incoming</th>
            <th width="15%">Outgoing</th>
            <th width="20%">Balance (RM)</th>
        </tr>
    </thead>
    <tbody>
    @php
            $roundOff = 2;
            if ($isPrecise) {
                $roundOff = 5;
            }
            $current_running_balance = 0;
        @endphp
        @foreach ($transactions as $key => $transaction)
        @php
            $description = "";
            $incoming = "";
            $outgoing = "";
            $marking = "";

            switch ($transaction->type) {
                case 1:
                    if ($transaction->owner_type === "App\Models\Booking") {
                        $description = "Payment <br>" . $transaction->bill_no;
                        $marking = $transaction->owner->marking;
                    } else {
                        $description = "Payment <br>" . $transaction->bill_no;
                        $marking = App\Models\Transaction::where('payment_reference', $transaction->bill_no)->first()->owner->marking;
                    }
                    $outgoing = number_format(trim($transaction->amount), $roundOff);
                    break;
                case 2:
                    $description = "Invoice <br>" . $transaction->bill_no;
                    $incoming = number_format((trim($transaction->amount) / $transaction->currency_rate + $transaction->service_charge), $roundOff);
                    $marking = $transaction->owner->marking;
                    break;
                case 5:
                    $description = "Top Up <br>" . $transaction->payment_reference;
                    $incoming = number_format(trim($transaction->amount), $roundOff);
                    break;
                case 9:
                    $description = "Credit Note <br>" . $transaction->payment_reference;
                    $incoming = number_format(trim($transaction->amount), $roundOff);
                    break;
            }

            $current_running_balance = floatval($current_running_balance) + floatval($incoming) - floatval($outgoing);
            $current_running_balance = number_format($current_running_balance, $roundOff);

        @endphp
        <tr>
            <td width="5%" class="center top">{{ $key + 1 }}</td>
            <td class="stock-code top" width="15%" style="text-align: center;">{{ \Carbon\Carbon::parse($transaction->created_at)->format('Y-m-d') }}</td>
            <td class="description">{!! $description !!} <br> {{$marking}}</td>
            <td width="15%" class="center top" style="text-align: center;">
                {{ $incoming }}
            </td>
            <td width="15%" class="center top" style="text-align: center;">
                {{ $outgoing }}
            </td>
            <td width="20%" class="center top" style="text-align: center;">
                {{ $current_running_balance }}
            </td>
        </tr>
        @endforeach
    </tbody>
</table>

    <br>
    <htmlpagefooter name="page-footer">
        <table width="100%">
            <tr>
                <td style="text-align: right; ">This is generated by computer. No signature required.</td>
                <td style="text-align: right; ">Page {PAGENO} of {nbpg}</td>
            </tr>
        </table>
    </htmlpagefooter>
@endsection
