@extends('layouts.app')

@section('title', 'دفعات العميل')

@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">
                        @if(isset($customer))
                        دفعات العميل: {{ $customer->name }}
                        @else
                        قائمة دفعات العملاء
                        @endif
                    </h3>
                    <div class="card-tools">
                        @if(isset($customer))
                        <a href="{{ route('customers.show', $customer) }}" class="btn btn-default btn-sm">
                            <i class="fas fa-arrow-right"></i> العودة لتفاصيل العميل
                        </a>
                        @else
                        <a href="{{ route('customers.index') }}" class="btn btn-default btn-sm">
                            <i class="fas fa-arrow-right"></i> العودة لقائمة العملاء
                        </a>
                        @endif
                        
                        @can('create', App\Models\CustomerPayment::class)
                        <a href="{{ route('customer-payments.create', isset($customer) ? ['customer_id' => $customer->id] : []) }}" class="btn btn-primary btn-sm">
                            <i class="fas fa-plus"></i> إضافة دفعة جديدة
                        </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="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    @if(!isset($customer))
                                    <th>العميل</th>
                                    @endif
                                    <th>رقم الدفعة</th>
                                    <th>تاريخ الدفع</th>
                                    <th>رقم الفاتورة</th>
                                    <th>المبلغ</th>
                                    <th>طريقة الدفع</th>
                                    <th>رقم المرجع</th>
                                    <th>الإجراءات</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($payments as $payment)
                                <tr>
                                    <td>{{ $loop->iteration }}</td>
                                    @if(!isset($customer))
                                    <td>
                                        <a href="{{ route('customers.show', $payment->customer) }}">
                                            {{ $payment->customer->name }}
                                        </a>
                                    </td>
                                    @endif
                                    <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">غير مرتبط بفاتورة</span>
                                        @endif
                                    </td>
                                    <td>{{ number_format($payment->amount, 2) }} ريال</td>
                                    <td>
                                        @switch($payment->payment_method)
                                            @case('cash')
                                                <span class="badge badge-success">نقداً</span>
                                                @break
                                            @case('bank_transfer')
                                                <span class="badge badge-info">تحويل بنكي</span>
                                                @break
                                            @case('check')
                                                <span class="badge badge-warning">شيك</span>
                                                @break
                                            @case('credit_card')
                                                <span class="badge badge-primary">بطاقة ائتمان</span>
                                                @break
                                            @case('other')
                                                <span class="badge badge-secondary">أخرى</span>
                                                @break
                                            @default
                                                <span class="badge badge-secondary">{{ $payment->payment_method }}</span>
                                        @endswitch
                                    </td>
                                    <td>{{ $payment->reference_number ?: 'غير محدد' }}</td>
                                    <td>
                                        <div class="btn-group">
                                            @can('view', $payment)
                                            <a href="{{ route('customer-payments.show', $payment) }}" class="btn btn-info btn-sm">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            @endcan
                                            
                                            @can('update', $payment)
                                            <a href="{{ route('customer-payments.edit', $payment) }}" class="btn btn-warning btn-sm">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            @endcan
                                            
                                            @can('delete', $payment)
                                            <form action="{{ route('customer-payments.destroy', $payment) }}" method="POST" class="d-inline">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('هل أنت متأكد من حذف هذه الدفعة؟')">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </form>
                                            @endcan
                                        </div>
                                    </td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="{{ isset($customer) ? '8' : '9' }}" class="text-center">لا يوجد دفعات</td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                    
                    <div class="mt-4">
                        {{ $payments->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
