@extends('layouts.base_portal')
@section('inner_content')
    <div class="container">
        <div class="card mb-4">
            <div class="card-body">
                <h5 class="card-title">Statement: {{ $statement->date_from->format('M d, Y') }} - {{ $statement->date_to->format('M d, Y') }}</h5>
                <p class="card-text">Account: {{ $statement->account->name }} ({{ $statement->account->number }})</p>
{{--                <p class="card-text">Total Debit: {{ number_format($statement->total_debit, 2) }}</p>--}}
{{--                <p class="card-text">Total Credit: {{ number_format($statement->total_credit, 2) }}</p>--}}
                <p class="card-text">Beginning Balance: {{ number_format($statement->begin_balance, 2) }}</p>
                <p class="card-text">Ending Balance: {{ number_format($statement->end_balance, 2) }}</p>
            </div>
        </div>
        <div class="mt-3 mb-3">
            <a href="{{ route('statements.download', $statement->id) }}" class="btn btn-primary">Download Statement</a>
        </div>
        <div class="card">
            <div class="card-body">
                <h5 class="card-title">Transactions</h5>
                <form action="{{ route('statements.show', $statement->id) }}" method="get">
                    <div class="row">
                        <div class="col-sm-4 mb-3">
                            <label for="transaction_filter">Transaction:</label>
                            <input type="text" name="transaction_filter" id="transaction_filter" class="form-control" value="{{ request()->get('transaction_filter') }}">
                        </div>
                        <div class="col-sm-4 mb-3">
                            <label for="from_amount_filter">From Amount:</label>
                            <input type="number" step="0.01" name="from_amount_filter" id="from_amount_filter" class="form-control" value="{{ request()->get('from_amount_filter') }}">
                        </div>
                        <div class="col-sm-4 mb-3">
                            <label for="to_amount_filter">To Amount:</label>
                            <input type="number" step="0.01" name="to_amount_filter" id="to_amount_filter" class="form-control" value="{{ request()->get('to_amount_filter') }}">
                        </div>
                    </div>
                    <button type="submit" class="btn btn-primary">Filter</button>
                </form>
                <table class="table table-hover mt-4">
                    <thead>
                    <tr>
                        <th>Date</th>
                        <th>time</th>
                        <th>From</th>
                        <th>Type</th>
                        <th>Payment Method</th>
                        <th class="text-right">Debit</th>
                        <th class="text-right">Credit</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach ($transactions as $transaction)
                        @php
                        $from = $transaction->transaction_description_2;
                        $type = '';
                        $paymentMethod = '';

                        if(!$transaction->transaction_description_2){
                            $from = $transaction->transaction_description;
                        }

                        if(str_contains($transaction->transaction_description_3, 'FPX')){
                            $paymentMethod = 'FPX';
                        }

                        if(str_contains($transaction->transaction_description_3, 'A/C')){
                            $paymentMethod = 'Bank Transfer';
                        }

                        if(str_contains($transaction->transaction_description_3, 'IBFT')){
                            $paymentMethod = 'Bank Transfer';
                        }

                        if(str_contains($transaction->transaction_description_3, 'FOREIGN TT')){
                            $paymentMethod = 'International Transfer';
                        }

                        if($transaction->amount > 0) {
                            $type = 'Sale';
                        }

                        if($transaction->amount < 0 && str_contains($transaction->transaction_description_3, 'ESI')) {
                            $type = 'Statutory Payment';
                        }

                        if($transaction->amount < 0 && str_contains($transaction->transaction_description_3, 'ESI')) {
                            $type = 'Statutory Payment';
                        }

                        if($transaction->amount < 0 && str_contains($transaction->transaction_description, 'DR DUITNOW S/CHRG')) {
                            $type = 'Bank Charge';
                            $paymentMethod = 'Bank Deduction';

                        }

                        if($transaction->amount < 0 && str_contains($transaction->transaction_description, 'CMS - DR CORP CHG')) {
                            $type = 'Card Payment';
                            $paymentMethod = 'Card';
                        }

                        if($transaction->amount < 0 && str_contains($transaction->transaction_description_3, 'ELECTRONIC')) {
                            $type = 'Electric Bill';
                        }

                        @endphp
                        <tr>
                            <td>{{ $transaction->posting_date->format('d-m-Y') }}</td>
                            <td>{{ $transaction->posting_date->format('g:i A') }}</td>
                            <td>{{ $from }}</td>
                            <td>{{ $type }}</td>
                            <td>{{ $paymentMethod }}</td>
                            <td class="text-right">{{ $transaction->amount < 0 ? number_format($transaction->amount, 2) : 0.00}}</td>
                            <td class="text-right">{{ $transaction->amount > 0 ? number_format($transaction->amount, 2) : 0.00}}</td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        <div class="mt-3">
            {{ $transactions->appends(request()->query())->links() }}
        </div>
    </div>
@endsection

