@extends('layouts.app')

@section('title', \App\Helpers\TranslationHelper::transFromDatabase('Payment Details', __('Payment Details')))

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">{{ \App\Helpers\TranslationHelper::transFromDatabase('Payment Details', __('Payment Details')) }} #{{ $payment->payment_number }}</h3>
                    <div class="card-tools">
                        <a href="{{ route('customer-payments.index') }}" class="btn btn-sm btn-secondary">
                            <i class="fas fa-arrow-right"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('Return to Payments', __('Return to Payments')) }}
                        </a>
                        
                        @can('customer-payments.edit')
                        <a href="{{ route('customer-payments.edit', $payment) }}" class="btn btn-sm btn-primary">
                            <i class="fas fa-edit"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('Edit', __('Edit')) }}
                        </a>
                        @endcan
                    </div>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="card">
                                <div class="card-header bg-light">
                                    <h5 class="mb-0">{{ \App\Helpers\TranslationHelper::transFromDatabase('Payment Information', __('Payment Information')) }}</h5>
                                </div>
                                <div class="card-body">
                                    <table class="table table-bordered">
                                        <tr>
                                            <th style="width: 30%">{{ \App\Helpers\TranslationHelper::transFromDatabase('Payment Number', __('Payment Number')) }}</th>
                                            <td>{{ $payment->payment_number }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Payment Date', __('Payment Date')) }}</th>
                                            <td>{{ $payment->payment_date->format('Y-m-d') }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Payment Amount', __('Payment Amount')) }}</th>
                                            <td>{{ number_format($payment->amount, 2) }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Payment Method', __('Payment Method')) }}</th>
                                            <td>
                                                @if($payment->payment_method == 'cash')
                                                    <span class="badge badge-success">{{ \App\Helpers\TranslationHelper::transFromDatabase('Cash', __('Cash')) }}</span>
                                                @elseif($payment->payment_method == 'bank_transfer')
                                                    <span class="badge badge-info">{{ \App\Helpers\TranslationHelper::transFromDatabase('Bank Transfer', __('Bank Transfer')) }}</span>
                                                @elseif($payment->payment_method == 'check')
                                                    <span class="badge badge-primary">{{ \App\Helpers\TranslationHelper::transFromDatabase('Check', __('Check')) }}</span>
                                                @elseif($payment->payment_method == 'credit_card')
                                                    <span class="badge badge-warning">{{ \App\Helpers\TranslationHelper::transFromDatabase('Credit Card', __('Credit Card')) }}</span>
                                                @else
                                                    <span class="badge badge-secondary">{{ \App\Helpers\TranslationHelper::transFromDatabase('Other', __('Other')) }}</span>
                                                @endif
                                            </td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Reference Number', __('Reference Number')) }}</th>
                                            <td>{{ $payment->reference_number ?: \App\Helpers\TranslationHelper::transFromDatabase('Not Set', __('Not Set')) }}</td>
                                        </tr>
                                        @if($payment->payment_method == 'cash' && $payment->treasury)
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Treasury', __('Treasury')) }}</th>
                                            <td><i class="fas fa-vault text-success"></i> {{ $payment->treasury->name }}</td>
                                        </tr>
                                        @elseif(in_array($payment->payment_method, ['bank_transfer', 'check', 'credit_card']) && $payment->bankAccount)
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Bank Account', __('Bank Account')) }}</th>
                                            <td>
                                                <i class="fas fa-university text-info"></i> 
                                                {{ $payment->bankAccount->bank->name ?? '' }} - {{ $payment->bankAccount->account_number }}
                                            </td>
                                        </tr>
                                        @endif
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Attachment', __('Attachment')) }}</th>
                                            <td>
                                                @if($payment->attachment)
                                                    <a href="{{ asset('storage/' . $payment->attachment) }}" target="_blank" class="btn btn-sm btn-info">
                                                        <i class="fas fa-file"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('View Attachment', __('View Attachment')) }}
                                                    </a>
                                                @else
                                                    <span class="text-muted">{{ \App\Helpers\TranslationHelper::transFromDatabase('No Attachment', __('No Attachment')) }}</span>
                                                @endif
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="card">
                                <div class="card-header bg-light">
                                    <h5 class="mb-0">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customer and Invoice Information', __('Customer and Invoice Information')) }}</h5>
                                </div>
                                <div class="card-body">
                                    <table class="table table-bordered">
                                        <tr>
                                            <th style="width: 30%">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customer Name', __('Customer Name')) }}</th>
                                            <td>
                                                <a href="{{ route('customers.show', $payment->customer) }}">
                                                    {{ $payment->customer->name }}
                                                </a>
                                            </td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Contact Person', __('Contact Person')) }}</th>
                                            <td>{{ $payment->customer->contact_person }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Email', __('Email')) }}</th>
                                            <td>{{ $payment->customer->email }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Phone', __('Phone')) }}</th>
                                            <td>{{ $payment->customer->phone }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Invoice Number', __('Invoice Number')) }}</th>
                                            <td>
                                                @if($payment->invoice)
                                                    <a href="{{ route('customer-invoices.show', $payment->invoice) }}">
                                                        {{ $payment->invoice->invoice_number }}
                                                    </a>
                                                @else
                                                    <span class="text-muted">{{ \App\Helpers\TranslationHelper::transFromDatabase('No Related Invoice', __('No Related Invoice')) }}</span>
                                                @endif
                                            </td>
                                        </tr>
                                        @if($payment->invoice)
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Invoice Status', __('Invoice Status')) }}</th>
                                            <td>
                                                @if($payment->invoice->status == 'draft')
                                                    <span class="badge badge-secondary">{{ \App\Helpers\TranslationHelper::transFromDatabase('Draft', __('Draft')) }}</span>
                                                @elseif($payment->invoice->status == 'sent')
                                                    <span class="badge badge-info">{{ \App\Helpers\TranslationHelper::transFromDatabase('Sent', __('Sent')) }}</span>
                                                @elseif($payment->invoice->status == 'paid')
                                                    <span class="badge badge-success">{{ \App\Helpers\TranslationHelper::transFromDatabase('Paid', __('Paid')) }}</span>
                                                @elseif($payment->invoice->status == 'partially_paid')
                                                    <span class="badge badge-warning">{{ \App\Helpers\TranslationHelper::transFromDatabase('Partially Paid', __('Partially Paid')) }}</span>
                                                @elseif($payment->invoice->status == 'overdue')
                                                    <span class="badge badge-danger">{{ \App\Helpers\TranslationHelper::transFromDatabase('Overdue', __('Overdue')) }}</span>
                                                @elseif($payment->invoice->status == 'cancelled')
                                                    <span class="badge badge-dark">{{ \App\Helpers\TranslationHelper::transFromDatabase('Cancelled', __('Cancelled')) }}</span>
                                                @endif
                                            </td>
                                        </tr>
                                        @endif
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    @if($payment->notes)
                    <div class="row mt-4">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header bg-light">
                                    <h5 class="mb-0">{{ \App\Helpers\TranslationHelper::transFromDatabase('Notes', __('Notes')) }}</h5>
                                </div>
                                <div class="card-body">
                                    {{ $payment->notes }}
                                </div>
                            </div>
                        </div>
                    </div>
                    @endif
                    
                    @if($payment->invoice)
                    <div class="row mt-4">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header bg-light">
                                    <h5 class="mb-0">{{ \App\Helpers\TranslationHelper::transFromDatabase('Invoice Information', __('Invoice Information')) }}</h5>
                                </div>
                                <div class="card-body">
                                    <div class="row">
                                        <div class="col-md-6">
                                            <table class="table table-bordered">
                                                <tr>
                                                    <th style="width: 40%">{{ \App\Helpers\TranslationHelper::transFromDatabase('Total Invoice Amount', __('Total Invoice Amount')) }}</th>
                                                    <td>{{ number_format($payment->invoice->total_amount, 2) }}</td>
                                                </tr>
                                                <tr>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Paid Amount', __('Paid Amount')) }}</th>
                                                    <td>{{ number_format($payment->invoice->paid_amount, 2) }}</td>
                                                </tr>
                                                <tr>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Remaining Amount', __('Remaining Amount')) }}</th>
                                                    <td>{{ number_format($payment->invoice->remaining_amount, 2) }}</td>
                                                </tr>
                                            </table>
                                        </div>
                                        <div class="col-md-6">
                                            <table class="table table-bordered">
                                                <tr>
                                                    <th style="width: 40%">{{ \App\Helpers\TranslationHelper::transFromDatabase('Payment Percentage', __('Payment Percentage')) }}</th>
                                                    <td>
                                                        @php
                                                            $percentage = $payment->invoice->total_amount > 0 ? ($payment->invoice->paid_amount / $payment->invoice->total_amount) * 100 : 0;
                                                        @endphp
                                                        <div class="progress">
                                                            <div class="progress-bar bg-success" role="progressbar" style="width: {{ $percentage }}%" aria-valuenow="{{ $percentage }}" aria-valuemin="0" aria-valuema="100">{{ number_format($percentage, 1) }}%</div>
                                                        </div>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Contribution Of This Payment', __('Contribution Of This Payment')) }}</th>
                                                    <td>
                                                        @php
                                                            $contribution = $payment->invoice->total_amount > 0 ? ($payment->amount / $payment->invoice->total_amount) * 100 : 0;
                                                        @endphp
                                                        {{ number_format($contribution, 1) }}% {{ \App\Helpers\TranslationHelper::transFromDatabase('From Total Invoice', __('From Total Invoice')) }}
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
