@extends('layouts.base_portal')
@section('inner_content')
    <div class="row">
        <div class="col bg-white p-t-15 p-b-15">
            <div class="row no-margin">
                <div class="col-12">
                    <form method="post" >
                        @csrf
                        <div class="row">
                            <div class="col">
                                <input class="form-control" type="text" name="marking" placeholder="Marking" value="{{$marking}}">
                            </div>
                            <div class="col">
                                <input class="form-control" type="text" name="customer_email" placeholder="Email" value="{{$email}}">
                            </div>
                            <div class="col">
                                <input class="form-control" type="text" name="booking_reference" placeholder="Booking Reference" value="{{$bookingReference}}">
                            </div>
                            <div class="col-auto">
                                <button class="btn btn-complete" type="submit">Search</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col">
            <main>
                @if($company)
                    @php
                        $employees = $company->employees;
                        $primaryEmployee = $employees->first();
                        $identification = $company->documents->whereIn('document_type', \App\Classes\ValueObjects\Constants\DocumentType::IDENTIFICATION_DOCUMENTS)->first()
                    @endphp
                    <section id="customer-account" class="m-b-50">
                        <h3>Customer Account</h3>
                        <p>Marking: <span><a href="{{route('customer.profile', $company->reference)}}" target="_blank">{{$company->reference}}</a></span></p>
                        <p>Account Type: <span>{{$company->type === 1 ? 'Business' : 'Personal'}}</span></p>
                        @if($company->type === 1)<p>Company's Name: <span>{{$company->name}}</span></p>@endif
                        <p>Customer's Name: <span>{{$employees->pluck('name')->implode(', ')}}</span></p>
                        <p>Email: <span>{{$employees->pluck('email')->implode(', ')}}</span></p>
                        <p>Phone: <span>{{$company->contacts->pluck('phone')->implode(', ')}}</span></p>
                        <p>Registration Date: <span>{{$company->created_at->format('d-m-Y')}}</span></p>
                    </section>
                    <section id="customer-verification" class="m-b-50">
                        <h3>Customer Verification</h3>
                        <p>Email Verification Status: <span>{{ $primaryEmployee->status === 2 ? 'Verified' : 'Pending Verification'}}</span></p>
                        <p>Identification Verification Status: <span>{{$identification ? ($identification->status === 2 ? 'Verified' : 'Pending Verification') : 'Not Submitted'}}</span></p>
                    </section>
                    @if(!$booking)
                    <section>
                        <h3>Customer Bookings</h3>
                            @php
                                $bookings = $company->bookings()->whereIn('status', [2, 3])->get();
                            @endphp
                            <section>
                                @foreach($bookings as $booking)
                                    @php
                                        $payments = $booking->transactions()->where('type', \App\Classes\ValueObjects\Constants\TransactionType::PAYMENT)->get();
                                        $purchaseOrder = $booking->transactions()->where('type', \App\Classes\ValueObjects\Constants\TransactionType::PURCHASE_ORDER)->first();
                                    @endphp
                                    <section class="m-b-50">
                                        <h5 class="bold">Booking Reference: {{$booking->marking}}</h5>
                                        <p>Amount: <span>{{$booking->fix_amount.' '.$booking->fixedCurrency->short_code}}</span></p>
                                        <p>Status: <span>{{$booking->status === 3 ? 'Complete' : 'In Progress'}}</span></p>
                                        <p>Purchase Order Status: <span>{{$purchaseOrder ? ($purchaseOrder->status === 3 ? 'Approved' : ($purchaseOrder->status === 1 ? 'Pending Approval' : 'Incomplete Submission')) : 'Pending Submission'}}</span></p>
                                        @if($payments)<p class="m-t-35 bold">Payment History:</p>@endif
                                        @php $i = 1; @endphp
                                        @foreach($payments as $payment)
                                            @php
                                                $bill = $payment->transactions()->where('type', \App\Classes\ValueObjects\Constants\TransactionType::BILL)->first();
                                                $transferProof = null;
                                                $status = 'Pending Submission';

                                                if($payment->status === 1) {
                                                    $status = 'Pending Approval';
                                                }

                                                if($payment->status === 2) {
                                                    $status = 'Pending Confirmation';
                                                }

                                                if(in_array($payment->status, [4, 5])) {
                                                    $status = 'Rejected/Failed Payment';
                                                }

                                                if($bill) {
                                                    if($bill->status === 1) {
                                                        $status = 'Pending Transfer Proof';
                                                    }

                                                    if(in_array($bill->status, [2, 3])) {
                                                        $status = 'Transfer Complete';
                                                        $transferProof = $bill->documents()->first();
                                                    }
                                                }

                                            @endphp
                                            <section class="m-b-35">
                                                <p>{{$i++}}.</p>
                                                <p>Amount: <span>{{$payment->original_amount.' '.$payment->original_currency->short_code}}</span></p>
                                                <p>Status: <span>{{$status}}</span></p>
                                                <p>Payment Date: <span>{{$payment->created_at->format('d-m-Y')}}</span></p>
                                                @if($bill)
                                                    <p>Supplier: <span>{{$bill->issuerCompany->name}}</span></p>
                                                    <p>Supplier Order Date: <span>{{$bill->created_at->format('d-m-Y')}}</span></p>
                                                    @if($transferProof)<p>Transfer Proof Upload Date: <span>{{$transferProof->created_at->format('d-m-Y')}}</span></p>@endif
                                                @endif
                                            </section>
                                        @endforeach
                                    </section>
                                @endforeach
                            </section>
                    </section>
                    @endif
                @endif
                @if($booking)
                    @php
                        $payments = $booking->transactions()->where('type', \App\Classes\ValueObjects\Constants\TransactionType::PAYMENT)->get();
                        $purchaseOrder = $booking->transactions()->where('type', \App\Classes\ValueObjects\Constants\TransactionType::PURCHASE_ORDER)->first();
                    @endphp
                    <section>
                        <h5 class="bold">Booking Reference: {{$booking->marking}}</h5>
                        <p>Amount: <span>{{$booking->fix_amount.' '.$booking->fixedCurrency->short_code}}</span></p>
                        <p>Status: <span>{{$booking->status === 3 ? 'Complete' : 'In Progress'}}</span></p>
                        <p>Purchase Order Status: <span>{{$purchaseOrder ? ($purchaseOrder->status === 3 ? 'Approved' : ($purchaseOrder->status === 1 ? 'Pending Approval' : 'Incomplete Submission')) : 'Pending Submission'}}</span></p>
                        @if($payments)<p class="m-t-35 bold">Payment History:</p>@endif
                        @php $i = 1; @endphp
                        @foreach($payments as $payment)
                            @php
                                $bill = $payment->transactions()->where('type', \App\Classes\ValueObjects\Constants\TransactionType::BILL)->first();
                                $transferProof = null;

                                $status = 'Pending Submission';

                                if($payment->status === 1) {
                                    $status = 'Pending Approval';
                                }

                                if($payment->status === 2) {
                                    $status = 'Pending Confirmation';
                                }

                                if(in_array($payment->status, [4, 5])) {
                                    $status = 'Rejected/Failed Payment';
                                }

                                if($bill) {
                                    if($bill->status === 1) {
                                        $status = 'Pending Transfer Proof';
                                    }

                                    if(in_array($bill->status, [2, 3])) {
                                        $status = 'Transfer Complete';
                                        $transferProof = $bill->documents()->first();
                                    }
                                }

                            @endphp
                            <section class="m-b-35">
                                <p>{{$i++}}.</p>
                                <p>Amount: <span>{{$payment->original_amount.' '.$payment->original_currency->short_code}}</span></p>
                                <p>Status: <span>{{$status}}</span></p>
                                <p>Payment Date: <span>{{$payment->created_at->format('d-m-Y')}}</span></p>
                                @if($bill)
                                    <p>Supplier: <span>{{$bill->issuerCompany->name}}</span></p>
                                    <p>Supplier Order Date: <span>{{$bill->created_at->format('d-m-Y')}}</span></p>
                                    @if($transferProof)<p>Transfer Proof Upload Date: <span>{{$transferProof->created_at->format('d-m-Y')}}</span></p>@endif
                                @endif
                            </section>
                        @endforeach
                    </section>
                @endif
            </main>
        </div>
    </div>
@endsection
