@extends('layouts.app')

@section('content')
    <section class="content">
        <div class="card card-primary card-outline card-outline-tabs">
            <div class="card-header p-0 border-bottom-0">
                <ul class="nav nav-tabs" id="custom-tabs-four-tab" role="tablist">
                    <li class="nav-item">
                        <a class="nav-link {{ isset($list) ? 'active' : '' }}"
                            href="{{ route('contacts.index') . qString() }}">
                            <i class="bi bi-list-ol"></i> Contact List
                        </a>
                    </li>
                    @if (isset($show))
                        <li class="nav-item">
                            <a class="nav-link active" href="#" role="tab">
                                <i class="bi bi-eye-fill"></i> Contact Details
                            </a>
                        </li>
                    @endif
                </ul>
            </div>
            
            <div class="card-body">
                <div class="tab-content" id="custom-tabs-four-tabContent">
                    @if (isset($list))
                        <div class="tab-pane fade show active" role="tabpanel">
                            <form method="GET" action="{{ route('contacts.index') }}" class="filter">
                                <div class="row justify-content-end mr-1">
                                    <div class="col-sm-12 col-md-4 col-lg-3">
                                        <div class="form-group input-group-sm">
                                            <input type="text" class="form-control" name="search" value="{{ Request::get('search') }}" placeholder="Write your search text...">
                                        </div>
                                    </div>
                                    <div class="col-sm-12 col-md-4 col-lg-1">
                                        <div class="btn-group" role="group">
                                            <button type="submit" class="btn text-success py-1">
                                                <i class="bi bi-search"></i>
                                            </button>

                                            <a class="btn text-danger py-1" href="{{ route('contacts.index') }}" data-toggle="tooltip" data-placement="top" title="Details">
                                                <i class="bi bi-arrow-repeat"></i>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </form>

                            <div class="card">
                                <div class="card-body p-0">
                                    <div class="table-responsive">
                                        <table class="table table-bordered table-sm mb-0">
                                            <thead>
                                                <tr>
                                                    <th style="width: 10px">SN</th>
                                                    <th>Name</th>
                                                    <th>Email</th>
                                                    <th>Phone</th>
                                                    <th>Subject</th>
                                                    <th>Date</th>
                                                    <th style="width: 70px">Message</th>
                                                    <th style="width: 60px">Action</th>
                                                </tr>
                                            </thead>

                                            <tbody>
                                                @foreach ($records as $record)
                                                    <tr>
                                                        <td style="color: {{ $record->is_view == 'Yes' ? 'gray' : 'black';}};" class="text-center">{{ $loop->iteration }}</td>
                                                        <td style="color: {{ $record->is_view == 'Yes' ? 'gray' : 'black';}};">{{ $record->name }}</td>
                                                        <td style="color: {{ $record->is_view == 'Yes' ? 'gray' : 'black';}};">{{ $record->email }}</td>
                                                        <td style="color: {{ $record->is_view == 'Yes' ? 'gray' : 'black';}};">{{ $record->phone }}</td>
                                                        <td style="color: {{ $record->is_view == 'Yes' ? 'gray' : 'black';}};">{{ excerpt($record->subject, 30) }}</td>
                                                        <td style="color: {{ $record->is_view == 'Yes' ? 'gray' : 'black';}};">{{ $record->created_at->format('d M Y l g:i A') }}</td>
                                                        <td class="text-center">
                                                            <i class="bi bi-chat-left-text-fill info-icon" data-toggle="tooltip" data-placement="top" title="{{ $record->message }}"></i>
                                                        </td>
                                                        <td class="text-center action-button px-0">
                                                            <div class="btn-group" role="group">
                                                                <a href="{{ route('contacts.show', $record->id) . qString() }}" class="btn" data-toggle="tooltip" data-placement="top" title="Details">
                                                                    <i class="bi bi-eye-fill"></i>
                                                                </a>

                                                                <form id="delete-form-{{ $record->id }}" action="{{ route('contacts.destroy', $record->id) }}" method="POST" enctype="multipart/form-data">
                                                                    @csrf
                                                                    @method('DELETE')
                                                                    <button type="button" class="btn delete-button rounded-0" data-record-id="{{ $record->id }}" data-toggle="tooltip" data-placement="top" title="Delete">
                                                                        <i class="bi bi-trash-fill"></i>
                                                                    </button>
                                                                </form>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                @endforeach
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>

                            <div class="d-flex justify-content-center mt-4">{{ $records->links() }}</div>
                        </div>
                    @elseif(isset($show))
                        <div class="tab-pane active" role="tabpanel">
                            <div class="card">
                                <div class="card-body p-0">
                                    <div class="table-responsive">
                                        <table class="table table-bordered table-sm mb-0">
                                            <thead>
                                                <tr>
                                                    <th style="width: 200px">Fields</th>
                                                    <th style="width: 20px">:</th>
                                                    <th>Values</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <tr>
                                                    <th>Name</th>
                                                    <th>:</th>
                                                    <td>{{ $record->name }}</td>
                                                </tr>
                                                <tr>
                                                    <th>Email</th>
                                                    <th>:</th>
                                                    <td>{{ $record->email }}</td>
                                                </tr>
                                                <tr>
                                                    <th>Phone</th>
                                                    <th>:</th>
                                                    <td>{{ $record->phone }}</td>
                                                </tr>
                                                <tr>
                                                    <th>Company Name</th>
                                                    <th>:</th>
                                                    <td>{{ $record->company_name }}</td>
                                                </tr>
                                                <tr>
                                                    <th>Subject</th>
                                                    <th>:</th>
                                                    <td>{{ $record->subject }}</td>
                                                </tr>
                                                <tr>
                                                    <th>Message</th>
                                                    <th>:</th>
                                                    <td>{{ $record->message }}</td>
                                                </tr>
                                                @php
                                                    $attachment = $record->attachment;
                                                    $extension = pathinfo($attachment, PATHINFO_EXTENSION);
                                                    $imageOrPdf = in_array(strtolower($extension), ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp', 'pdf']);
                                                @endphp
                                                
                                                <tr>
                                                    <th>Attachment</th>
                                                    <th>:</th>
                                                    <td>
                                                        @if ($attachment)
                                                            <a href="{{ asset('storage/contact/' . $attachment) }}" target="{{ $imageOrPdf ? '_blank' : '_self' }}" @if (!$imageOrPdf) download @endif class="text-decoration-none">
                                                                @if ($imageOrPdf)
                                                                    <span class="me-2">View</span>
                                                                    <i class="bi bi-eye ms-1"></i>
                                                                @else
                                                                    <span class="me-2">Download</span>
                                                                    <i class="bi bi-download ms-1"></i>
                                                                @endif
                                                            </a>
                                                        @else
                                                            No attachment
                                                        @endif
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <th>Date</th>
                                                    <th>:</th>
                                                    <td>{{ $record->created_at->format('d M Y l g:i A') }}</td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </section>
@endsection
