@extends('layouts.masterHome2')

@section('content')
<style>
    /* Main Styles */
    .account-statement {
        margin: 2rem;
        font-family: 'Tajawal', Tahoma, Geneva, Verdana, sans-serif;
    }
    
    /* Header Cards */
    .header-card {
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        border-radius: 8px;
        overflow: hidden;
        margin-bottom: 1.5rem;
        
        
        
    }
    
    .header-card .card-header {
        background-color: #2C4E71;
        padding: 5px;
        
        
    }
    
    .header-card .card-title {
        text-align: center;
        font-weight: 300;
        color: #FFF;
        margin: 0;
        font-size: 1.1rem;
        font-size:15px;
        
    }
    
    .header-card .card-body {
        padding: 5px;
        display: flex;
        align-items: center;
        background-color: #f8f9fa;
        font-size:14px;
    }
    
    /* Main Table */
    .statement-table {
        width: 100%;
        border-collapse: collapse;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
    }
    
    .statement-table thead th {
        background-color: #2C4E71;
        color: white;
        padding: 2px;
        text-align: center;
        vertical-align: middle;
        border: 1px solid  #0a3f63;
        font-size:12px
    }
    
    .statement-table tbody td {
        padding: 2px;
        text-align: center;
        border: 1px solid #dee2e6;
        font-size:12px
    }
    
    .statement-table tbody tr {
        background-color: #f8f9fa;
        transition: background-color 0.2s;
    }
    
    .statement-table tbody tr:hover {
        background-color: #e9ecef;
    }
    
    /* Special Cells */
    .final-state-cell {
        font-weight: 700 !important;
        background-color: #e2e6ea !important;
    }
    
    .rejected-item {
        color: #dc3545;
        text-decoration: line-through;
    }
    
    .summary-row {
        background-color: #2C4E71 !important;
        color: white !important;
    }
 
    
    .summary-row th {
        font-weight: 700;
        padding: 3px;
        font-size:12px;
        text-align:center;
    }
    
    /* Responsive Adjustments */
    @media (max-width: 768px) {
        .header-card .card-body {
            flex-direction: column;
            text-align: center;
        }
        
        .statement-table {
            display: block;
            overflow-x: auto;
        }
    }

     @media print{
    @page{ size: A4 landscape; margin: 12mm; }
    body{background:#fff}
    .toolbar{display:none !important}
    .page{box-shadow:none; border-radius:0; margin:0}
    thead{display: table-header-group;}
    tfoot{display: table-footer-group;}
    /* تحسين الألوان في الطباعة */
    *{-webkit-print-color-adjust: exact; print-color-adjust: exact;}
  }
  
</style>

<div class="account-statement">
    <!-- Header Section -->
    <div class="row">
        <!-- Logo Column -->
        <div class="col-md-3 mb-3">
            <a href="{{ url('#') }}" class="brand-link d-block text-center">
                <img src='{{ asset("theme/dist/img/splash.png") }}' style="height: 74px;" alt="Company Logo">
            </a>
        </div>

        <!-- Period Card -->
        <div class="col-md-4 mb-3">
            <div class="header-card">
                <div class="card-header">
                    <h3 class="card-title">الفترة</h3>
                </div>
                <div class="card-body">
                    <span class="period-date">{{ $to }}</span>
                    <span class="px-2 font-weight-bold">الي</span>
                    <span class="period-date">{{ $from }}</span>
                    <span class="px-2 font-weight-bold">بدأ من</span>
                </div>
            </div>
        </div>

        <!-- Client Account Card -->
        <div class="col-md-5 mb-3">
            <div class="header-card">
                <div class="card-header">
                    <h3 class="card-title">كشف حساب عميل</h3>
                </div>
                <div class="card-body">
                    <span class="creation-date">{{ date('Y-m-d') }}</span>
                    <span class="px-2">تاريخ الأنشاء</span>
                    <span class="client-name font-weight-bold">{{ $client->name }}</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Account Statement Table -->
    <div class="card shadow-sm">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="statement-table">
                    <thead>
                        <tr>
                            <th style="width: 100px">الحالة النهائية</th>
                            <th style="width: 100px">الرصيد النهائي</th>
                            <th style="width: 100px">الرصيد السابق</th>
                            <th>مدفوعات</th>
                            <th>خصم مرتجعات</th>
                            <th>خصم نقدي</th>
                            <th>المبلغ</th>
                            <th>السعر</th>
                            <th>الكمية</th>
                            <th>رقم العملية</th>
                            <th>نوع العملية</th>
                            <th>التاريخ</th>
                        </tr>
                    </thead>
                    <tbody>
                        @php
                            $total_amount = 0;
                            $total_price = 0;
                            $total_payments = 0;
                        @endphp

                        @foreach($client_sheets as $client_sheet_item)
                            <!-- Invoice Items -->
                            @foreach($client_sheet_item->items as $key => $item)
                                @php
                                    $total_amount += $item->item_amount;
                                    $total_price += ($item->item_price * $item->item_amount);
                                @endphp
                                <tr>
                                    @if(count($client_sheet_item->payment_methods) == 0 && count($client_sheet_item->items)-1 == $key)
                                        <td class="final-state-cell">{{ $client_sheet_item->final_state }}</td>
                                        <td class="final-state-cell">{{ $client_sheet_item->current }}</td>
                                        <td class="final-state-cell">{{ $client_sheet_item->previos }}</td>
                                    @else
                                        <td>-</td>
                                        <td>-</td>
                                        <td>-</td>
                                    @endif

                                    <td>-</td>
                                    <td>-</td>
                                    <td>-</td>
                                    <td>{{ number_format($item->item_price * $item->item_amount, 2) }}</td>
                                    <td>{{ number_format($item->item_price, 2) }}</td>
                                    <td>{{ $item->item_amount }}</td>
                                    <td>{{ $item->invoice_id }}</td>
                                    <td>فاتورة - {{ $item->sell_store->store->sell_name }}</td>
                                    <td>{{ $item->created_at->format('d/m/Y') }}</td>
                                </tr>
                            @endforeach

                            <!-- Payment Methods -->
                            @foreach($client_sheet_item->payment_methods as $key => $payment)
                                @php
                                    if (!($payment->cheque_id && $payment->cheque->status == "Rejected")) {
                                        $total_payments += $payment->pay;
                                    }
                                @endphp
                                <tr>
                                    @if(count($client_sheet_item->payment_methods)-1 === $key)
                                        <td class="final-state-cell">{{ $client_sheet_item->final_state }}</td>
                                        <td class="final-state-cell">{{ $client_sheet_item->current }}</td>
                                        <td class="final-state-cell">{{ $client_sheet_item->previos }}</td>
                                    @else
                                        <td>-</td>
                                        <td>-</td>
                                        <td>-</td>
                                    @endif

                                    <td @if($payment->cheque_id && $payment->cheque->status == "Rejected") class="rejected-item" @endif>
                                        {{ number_format($payment->pay, 2) }}
                                    </td>
                                    <td>-</td>
                                    <td>-</td>
                                    <td>-</td>
                                    <td>-</td>
                                    <td>-</td>
                                    <td>{{ $payment->receipt_id }}</td>
                                    <td @if($payment->cheque_id && $payment->cheque->status == "Rejected") class="rejected-item" @endif>
                                        سند قبض ف - {{ $payment->payment_method ? $payment->payment_method->name : "" }}
                                    </td>
                                    <td>{{ $payment->created_at->format('d/m/Y') }}</td>
                                </tr>
                            @endforeach

                            <!-- Receipts -->
                            @foreach($client_sheet_item->receipts as $key => $receipt)
                                @php
                                    if (!($receipt->cheque_id && $receipt->cheque->status == "Rejected")) {
                                        $total_payments += $receipt->pay;
                                    }
                                @endphp
                                <tr>
                                    <td class="final-state-cell">{{ $client_sheet_item->final_state }}</td>
                                    <td class="final-state-cell">{{ $client_sheet_item->current }}</td>
                                    <td class="final-state-cell">{{ $client_sheet_item->previos }}</td>
                                    
                                    <td @if($receipt->cheque_id && $receipt->cheque->status == "Rejected") class="rejected-item" @endif>
                                        {{ number_format($receipt->pay, 2) }}
                                    </td>
                                    <td>-</td>
                                    <td>-</td>
                                    <td>-</td>
                                    <td>-</td>
                                    <td>-</td>
                                    <td>{{ $receipt->id }}</td>
                                    <td @if($receipt->cheque_id && $receipt->cheque->status == "Rejected") class="rejected-item" @endif>
                                        سند قبض - {{ $receipt->safe_account_id ? $receipt->safe_account->payment_method->name : "" }}
                                    </td>
                                    <td>{{ $receipt->created_at->format('d/m/Y') }}</td>
                                </tr>
                            @endforeach

                            <!-- Review Items -->
                            @foreach($client_sheet_item->review_items as $key => $item)
                                <tr>
                                    @if(count($client_sheet_item->review_items)-1 == $key)
                                        <td class="final-state-cell">{{ $client_sheet_item->final_state }}</td>
                                        <td class="final-state-cell">{{ $client_sheet_item->current }}</td>
                                        <td class="final-state-cell">{{ $client_sheet_item->previos }}</td>
                                    @else
                                        <td>-</td>
                                        <td>-</td>
                                        <td>-</td>
                                    @endif

                                    <td>-</td>
                                    
                                    @if(count($client_sheet_item->review_items)-1 == $key)
                                        <td>{{ number_format($item->review->total_price, 2) }}</td>
                                    @else
                                        <td>-</td>
                                    @endif

                                    <td>-</td>
                                    <td>{{ number_format($item->price * $item->quantity, 2) }}</td>
                                    <td>{{ number_format($item->price, 2) }}</td>
                                    <td>{{ $item->quantity }}</td>
                                    <td>{{ $item->invoiceitem->invoice_id }}</td>
                                    <td style="color: #dc3545;">
                                        مرتجع - {{ $item->invoiceitem->sell_store->store->sell_name }}
                                    </td>
                                    <td>{{ $item->created_at->format('d/m/Y') }}</td>
                                </tr>
                            @endforeach
                            
                            <!-- discount -->
                            @foreach($client_sheet_item->discounts as $key => $dis)
                                 
                                <tr>
                                    <td class="final-state-cell">{{ $client_sheet_item->final_state }}</td>
                                    <td class="final-state-cell">{{ $client_sheet_item->current }}</td>
                                    <td class="final-state-cell">{{ $client_sheet_item->previos }}</td>
                                    
                                    <td>-</td>
                                    <td>-</td>

                                    <td> {{$dis->value}}</td>
                                    
                                    <td>-</td>
                                    <td>-</td>
                                    <td>-</td>
                                    <td>{{ $dis->id }}</td>
                                    <td >
                                        خصم نقدي  
                                    
                                    </td>
                                    <td>{{ $dis->created_at->format('d/m/Y') }}</td>
                                </tr>
                            @endforeach


                        @endforeach

                        <!-- Summary Row -->
                        @php
                            $final_state = $client_sheets ? $client_sheets[count($client_sheets)-1]->final_state : "";
                            $final_current = $client_sheets ? $client_sheets[count($client_sheets)-1]->current : "";
                        @endphp
                        <tr class="summary-row">
                            <th>{{ $final_state }}</th>
                            <th>{{ $final_current }}</th>
                            <th></th>
                            <th>{{ number_format($total_payments, 2) }}</th>
                            <th></th>
                            <th></th>
                            <th>{{ number_format($total_price, 2) }}</th>
                            <th></th>
                            <th>{{ $total_amount }}</th>
                            <th colspan="3"></th>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection