@extends('layouts.app')

@section('title', \App\Helpers\TranslationHelper::transFromDatabase('Customers.Details', __('Customer 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('Customers.Details', __('Customer Details')) }}: {{ $customer->name }}</h3>
                    <div class="card-tools">
                        <a href="{{ route('customers.index') }}" class="btn btn-default btn-sm">
                            <i class="fas fa-arrow-right"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('General.BackToList', __('Back to List')) }}
                        </a>
                        @can('update', $customer)
                        <a href="{{ route('customers.edit', $customer) }}" class="btn btn-warning btn-sm">
                            <i class="fas fa-edit"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('General.Edit', __('Edit')) }}
                        </a>
                        @endcan
                    </div>
                </div>
                <div class="card-body">
                    @if(session('success'))
                    <div class="alert alert-success alert-dismissible">
                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                        {{ session('success') }}
                    </div>
                    @endif

                    <div class="row">
                        <div class="col-md-6">
                            <div class="card">
                                <div class="card-header bg-primary">
                                    <h3 class="card-title">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.BasicInfo', __('Basic Information')) }}</h3>
                                </div>
                                <div class="card-body">
                                    <table class="table table-bordered">
                                        <tr>
                                            <th style="width: 30%">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Name', __('Customer Name')) }}</th>
                                            <td>{{ $customer->name }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.ContactPerson', __('Contact Person')) }}</th>
                                            <td>{{ $customer->contact_person ?: \App\Helpers\TranslationHelper::transFromDatabase('Customers.NotSet', __('Not Set')) }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Email', __('Email')) }}</th>
                                            <td>{{ $customer->email ?: \App\Helpers\TranslationHelper::transFromDatabase('Customers.NotSet', __('Not Set')) }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Phone', __('Phone')) }}</th>
                                            <td>{{ $customer->phone ?: \App\Helpers\TranslationHelper::transFromDatabase('Customers.NotSet', __('Not Set')) }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Mobile', __('Mobile')) }}</th>
                                            <td>{{ $customer->mobile ?: \App\Helpers\TranslationHelper::transFromDatabase('Customers.NotSet', __('Not Set')) }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Type', __('Customer Type')) }}</th>
                                            <td>
                                                @if($customer->customer_type == 'individual')
                                                <span class="badge badge-info">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Individual', __('Individual')) }}</span>
                                                @else
                                                <span class="badge badge-primary">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Company', __('Company')) }}</span>
                                                @endif
                                            </td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Category', __('Customer Category')) }}</th>
                                            <td>
                                                @if($customer->category)
                                                <span class="badge badge-secondary">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Category.' . Str::studly($customer->category), __($customer->category)) }}</span>
                                                @else
                                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.NotSet', __('Not Set')) }}
                                                @endif
                                            </td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('General.Status', __('Status')) }}</th>
                                            <td>
                                                @if($customer->status == 'active')
                                                <span class="badge badge-success">{{ \App\Helpers\TranslationHelper::transFromDatabase('Status.Active', __('Active')) }}</span>
                                                @else
                                                <span class="badge badge-danger">{{ \App\Helpers\TranslationHelper::transFromDatabase('Status.Inactive', __('Inactive')) }}</span>
                                                @endif
                                            </td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.CreditLimit', __('Credit Limit')) }}</th>
                                            <td>{{ number_format($customer->credit_limit, 2) }} {{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.SAR', __('SAR')) }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.ChartOfAccount', __('Linked Account')) }}</th>
                                            <td>
                                                @if($customer->chartOfAccount)
                                                    <a href="{{ route('accounting.chart-of-accounts.show', $customer->chartOfAccount->id) }}" class="text-primary font-weight-bold">
                                                        <i class="fas fa-link"></i> {{ $customer->chartOfAccount->code }} - {{ $customer->chartOfAccount->name }}
                                                    </a>
                                                @else
                                                    <span class="text-danger">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.NoAccountLinked', __('No account linked')) }}</span>
                                                @endif
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="card">
                                <div class="card-header bg-info">
                                    <h3 class="card-title">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.AddressContactInfo', __('Address and Contact Information')) }}</h3>
                                </div>
                                <div class="card-body">
                                    <table class="table table-bordered">
                                        <tr>
                                            <th style="width: 30%">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Address', __('Address')) }}</th>
                                            <td>{{ $customer->address ?: \App\Helpers\TranslationHelper::transFromDatabase('Customers.NotSet', __('Not Set')) }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.City', __('City')) }}</th>
                                            <td>{{ $customer->city ?: \App\Helpers\TranslationHelper::transFromDatabase('Customers.NotSet', __('Not Set')) }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.State', __('State')) }}</th>
                                            <td>{{ $customer->state ?: \App\Helpers\TranslationHelper::transFromDatabase('Customers.NotSet', __('Not Set')) }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.PostalCode', __('Postal Code')) }}</th>
                                            <td>{{ $customer->postal_code ?: \App\Helpers\TranslationHelper::transFromDatabase('Customers.NotSet', __('Not Set')) }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Country', __('Country')) }}</th>
                                            <td>{{ $customer->country ?: \App\Helpers\TranslationHelper::transFromDatabase('Customers.NotSet', __('Not Set')) }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.TaxNumber', __('Tax Number')) }}</th>
                                            <td>{{ $customer->tax_number ?: \App\Helpers\TranslationHelper::transFromDatabase('Customers.NotSet', __('Not Set')) }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.CommercialRegister', __('Commercial Register')) }}</th>
                                            <td>{{ $customer->commercial_register ?: \App\Helpers\TranslationHelper::transFromDatabase('Customers.NotSet', __('Not Set')) }}</td>
                                        </tr>
                                        <tr>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Website', __('Website')) }}</th>
                                            <td>
                                                @if($customer->website)
                                                <a href="{{ $customer->website }}" target="_blank">{{ $customer->website }}</a>
                                                @else
                                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.NotSet', __('Not Set')) }}
                                                @endif
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

                    @if($customer->notes)
                    <div class="row mt-3">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header bg-secondary">
                                    <h3 class="card-title">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Notes', __('Notes')) }}</h3>
                                </div>
                                <div class="card-body">
                                    {{ $customer->notes }}
                                </div>
                            </div>
                        </div>
                    </div>
                    @endif

                    <div class="row mt-4">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header bg-warning">
                                    <h3 class="card-title">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Invoices', __('Customer Invoices')) }}</h3>
                                    <div class="card-tools">
                                        @can('create', App\Models\CustomerInvoice::class)
                                        <a href="{{ route('customer-invoices.create', ['customer_id' => $customer->id]) }}" class="btn btn-primary btn-sm">
                                            <i class="fas fa-plus"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.AddInvoice', __('Add New Invoice')) }}
                                        </a>
                                        @endcan
                                        <a href="{{ route('customers.invoices', $customer) }}" class="btn btn-default btn-sm">
                                            <i class="fas fa-list"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.ViewAll', __('View All')) }}
                                        </a>
                                    </div>
                                </div>
                                <div class="card-body">
                                    @if($invoices->count() > 0)
                                    <div class="table-responsive">
                                        <table class="table table-bordered table-striped">
                                            <thead>
                                                <tr>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.InvoiceNumber', __('Invoice Number')) }}</th>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.InvoiceDate', __('Invoice Date')) }}</th>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.DueDate', __('Due Date')) }}</th>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.TotalAmount', __('Total Amount')) }}</th>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.PaidAmount', __('Paid Amount')) }}</th>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.RemainingAmount', __('Remaining Amount')) }}</th>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('General.Status', __('Status')) }}</th>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('General.Actions', __('Actions')) }}</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @foreach($invoices as $invoice)
                                                <tr>
                                                    <td>{{ $invoice->invoice_number }}</td>
                                                    <td>{{ $invoice->invoice_date->format('Y-m-d') }}</td>
                                                    <td>{{ $invoice->due_date ? $invoice->due_date->format('Y-m-d') : '-' }}</td>
                                                    <td>{{ number_format($invoice->total_amount, 2) }} {{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.SAR', __('SAR')) }}</td>
                                                    <td>{{ number_format($invoice->paid_amount, 2) }} {{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.SAR', __('SAR')) }}</td>
                                                    <td>{{ number_format($invoice->remaining_amount, 2) }} {{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.SAR', __('SAR')) }}</td>
                                                    <td>
                                                        @switch($invoice->status)
                                                            @case('draft')
                                                                <span class="badge badge-secondary">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Draft', __('Draft')) }}</span>
                                                                @break
                                                            @case('sent')
                                                                <span class="badge badge-info">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Sent', __('Sent')) }}</span>
                                                                @break
                                                            @case('paid')
                                                                <span class="badge badge-success">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Paid', __('Paid')) }}</span>
                                                                @break
                                                            @case('partially_paid')
                                                                <span class="badge badge-warning">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.PartiallyPaid', __('Partially Paid')) }}</span>
                                                                @break
                                                            @case('overdue')
                                                                <span class="badge badge-danger">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Overdue', __('Overdue')) }}</span>
                                                                @break
                                                            @case('cancelled')
                                                                <span class="badge badge-danger">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Cancelled', __('Cancelled')) }}</span>
                                                                @break
                                                            @default
                                                                <span class="badge badge-secondary">{{ $invoice->status }}</span>
                                                        @endswitch
                                                    </td>
                                                    <td>
                                                        <a href="{{ route('customer-invoices.show', $invoice->id) }}" class="btn btn-info btn-sm">
                                                            <i class="fas fa-eye"></i>
                                                        </a>
                                                    </td>
                                                </tr>
                                                @endforeach
                                            </tbody>
                                        </table>
                                    </div>
                                    @else
                                    <p class="text-center">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.NoInvoices', __('No invoices for this customer')) }}</p>
                                    @endif
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row mt-4">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header bg-danger">
                                    <h3 class="card-title">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Payments', __('Customer Payments')) }}</h3>
                                    <div class="card-tools">
                                        @can('create', App\Models\CustomerPayment::class)
                                        <a href="{{ route('customer-payments.create', ['customer_id' => $customer->id]) }}" class="btn btn-primary btn-sm">
                                            <i class="fas fa-plus"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.AddPayment', __('Add New Payment')) }}
                                        </a>
                                        @endcan
                                        <a href="{{ route('customers.payments', $customer) }}" class="btn btn-default btn-sm">
                                            <i class="fas fa-list"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.ViewAll', __('View All')) }}
                                        </a>
                                    </div>
                                </div>
                                <div class="card-body">
                                    @if($payments->count() > 0)
                                    <div class="table-responsive">
                                        <table class="table table-bordered table-striped">
                                            <thead>
                                                <tr>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.PaymentNumber', __('Payment Number')) }}</th>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.PaymentDate', __('Payment Date')) }}</th>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.InvoiceNumber', __('Invoice Number')) }}</th>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.PaymentAmount', __('Payment Amount')) }}</th>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.PaymentMethod', __('Payment Method')) }}</th>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.ReferenceNumber', __('Reference Number')) }}</th>
                                                    <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('General.Actions', __('Actions')) }}</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @foreach($payments as $payment)
                                                <tr>
                                                    <td>{{ $payment->payment_number }}</td>
                                                    <td>{{ $payment->payment_date->format('Y-m-d') }}</td>
                                                    <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('Customers.NotLinkedToInvoice', __('Not linked to invoice')) }}</span>
                                                        @endif
                                                    </td>
                                                    <td>{{ number_format($payment->amount, 2) }} {{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.SAR', __('SAR')) }}</td>
                                                    <td>
                                                        @switch($payment->payment_method)
                                                            @case('cash')
                                                                <span class="badge badge-success">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Cash', __('Cash')) }}</span>
                                                                @break
                                                            @case('bank_transfer')
                                                                <span class="badge badge-info">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.BankTransfer', __('Bank Transfer')) }}</span>
                                                                @break
                                                            @case('check')
                                                                <span class="badge badge-warning">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Check', __('Check')) }}</span>
                                                                @break
                                                            @case('credit_card')
                                                                <span class="badge badge-primary">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.CreditCard', __('Credit Card')) }}</span>
                                                                @break
                                                            @case('other')
                                                                <span class="badge badge-secondary">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.Other', __('Other')) }}</span>
                                                                @break
                                                            @default
                                                                <span class="badge badge-secondary">{{ $payment->payment_method }}</span>
                                                        @endswitch
                                                    </td>
                                                    <td>{{ $payment->reference_number ?: \App\Helpers\TranslationHelper::transFromDatabase('Customers.NotSet', __('Not Set')) }}</td>
                                                    <td>
                                                        <a href="{{ route('customer-payments.show', $payment) }}" class="btn btn-info btn-sm">
                                                            <i class="fas fa-eye"></i>
                                                        </a>
                                                    </td>
                                                </tr>
                                                @endforeach
                                            </tbody>
                                        </table>
                                    </div>
                                    @else
                                    <p class="text-center">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customers.NoPayments', __('No payments for this customer')) }}</p>
                                    @endif
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
