@extends('layouts.admin')

@section('content')
<div class="card">
    <form class="" id="sort_customers" action="{{ route('admin.whatsapp.logs') }}" method="GET">
        <div class="card-header">
            <div class="col">
                <h5 class="mb-0 h6">WhatsApp Logs</h5>
            </div>
        </div>      

        <div class="card-body">
            <div class="row g-3 mb-4">
                <div class="col-md-3">
                    <div class="alert alert-primary mb-0">
                        <strong>Total Messages:</strong> {{ $utilizationSummary['total_messages'] ?? 0 }}
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="alert alert-success mb-0">
                        <strong>Sent:</strong> {{ $utilizationSummary['sent_messages'] ?? 0 }}
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="alert alert-warning mb-0">
                        <strong>Utility Credits Used:</strong> {{ $utilizationSummary['utility_credits_used'] ?? 0 }}
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="alert alert-danger mb-0">
                        <strong>Marketing Credits Used:</strong> {{ $utilizationSummary['marketing_credits_used'] ?? 0 }}
                    </div>
                </div>
            </div>

            <div class="mb-4">
                <h6 class="mb-2">Utilization Analysis (By Template)</h6>
                <table class="table table-sm table-bordered">
                    <thead>
                        <tr>
                            <th>Template</th>
                            <th>Category</th>
                            <th>Sent</th>
                            <th>Failed</th>
                            <th>Credit / Message</th>
                            <th>Credits Used</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($utilizationByTemplate ?? [] as $row)
                            <tr>
                                <td>{{ $row->identifier }}</td>
                                <td>
                                    @if($row->category === 'utility')
                                        <span class="badge bg-success">Utility</span>
                                    @else
                                        <span class="badge bg-danger">Marketing</span>
                                    @endif
                                </td>
                                <td>{{ $row->sent_messages }}</td>
                                <td>{{ $row->failed_messages }}</td>
                                <td>{{ $row->credits_per_message }}</td>
                                <td>{{ $row->credits_used }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="text-muted">No utilization data found.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <table id="whatsappLogsTable"  class="table aiz-table mb-0">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Template Identifier</th>
                        <th>Phone</th>
                        <th>Order Code</th>
                        <th>Date</th>
                        <th>Status</th>
                        <th>Action </th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($logs as $index => $log)
                        <tr>
                            <td>{{ $index + 1 }}</td>
                            <td><i class="fa fa-ban text-danger" aria-hidden="true"></i> {{ $log->identifier }}</td>
                            <td>{{ $log->phone }}</td>
                            <td>{{ $log->order_code }}</td>
                           <td>{{ \Carbon\Carbon::parse($log->created_at)->format('Y-m-d H:i:s') }}</td>
                            <td>
                                @if($log->status === 1)
                                    <span class="badge bg-success">sent</span>
                                @else
                                    <span class="badge bg-warning">Failed</span>
                                @endif
                            </td>
                           <td>
                                <a href="{{ route('admin.whatsapp.logs.detail', ['id' => $log->id]) }}" class="btn btn-primary btn-icon btn-circle btn-sm" title="View Details">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>

            <!-- Pagination - Right Aligned -->
            <div class="pagination-wrapper mt-4">
                <div class="d-flex justify-content-end align-items-center gap-3 flex-wrap">
                    <div class="pagination-info-text">
                        Showing <strong>{{ $logs->firstItem() ?? 0 }}</strong>–<strong>{{ $logs->lastItem() ?? 0 }}</strong> 
                        of <strong>{{ $logs->total() }}</strong> logs
                    </div>
                    @if($logs->hasPages())
                    <div class="pagination-controls">
                        {{ $logs->links('vendor.pagination.admin-categories') }}
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </form>
</div>
@endsection