<?php
if (Auth('admin')->User()->dashboard_style == 'light') {
    $text = 'dark';
    $bg = 'light';
} else {
    $bg = 'dark';
    $text = 'light';
}
?>
@extends('layouts.app')

@section('title', $title ?? 'Contact Message Details')

@section('content')
    @include('admin.topmenu')
    @include('admin.sidebar')
    <div class="main-panel">
        <div class="content">
            <div class="page-inner">
                <p>
                    <a href="{{ route('admin.contact-messages.index') }}">
                        <i class="p-2 rounded-lg fa fa-arrow-circle-left fa-2x bg-light"></i>
                    </a>
                </p>

                <div class="mt-2 mb-4">
                    <h1 class="title1 text-{{ $text }}">{{ $title ?? 'Contact Message Details' }}</h1>
                    <p class="text-muted mt-1">Message ID: CM-{{ str_pad($contactMessage->id, 6, '0', STR_PAD_LEFT) }}</p>
                </div>

                <x-danger-alert />
                <x-success-alert />

                <!-- Status and Actions -->
                <div class="row mb-4">
                    <div class="col-md-8">
                        <div class="card border-left-{{ $contactMessage->status_color }} shadow">
                            <div class="card-body">
                                <div class="row align-items-center">
                                    <div class="col">
                                        <h5 class="font-weight-bold mb-1 text-{{ $text }}">
                                            Message Status:
                                            <span class="badge badge-{{ $contactMessage->status_color }} badge-lg">
                                                @if($contactMessage->status === 'unread')
                                                    <i class="fas fa-envelope mr-1"></i>
                                                @elseif($contactMessage->status === 'read')
                                                    <i class="fas fa-envelope-open mr-1"></i>
                                                @elseif($contactMessage->status === 'replied')
                                                    <i class="fas fa-reply mr-1"></i>
                                                @elseif($contactMessage->status === 'archived')
                                                    <i class="fas fa-archive mr-1"></i>
                                                @endif
                                                {{ $contactMessage->status_text }}
                                            </span>
                                        </h5>
                                        <p class="text-muted mb-0">Received: {{ $contactMessage->formatted_created_at }}</p>
                                    </div>
                                    <div class="col-auto">
                                        <div class="btn-group">
                                            @if($contactMessage->status !== 'read')
                                                <form method="POST" action="{{ route('admin.contact-messages.update-status', $contactMessage) }}" class="d-inline mr-2">
                                                    @csrf
                                                    @method('PATCH')
                                                    <input type="hidden" name="status" value="read">
                                                    <button type="submit" class="btn btn-success">
                                                        <i class="fas fa-check mr-1"></i>Mark as Read
                                                    </button>
                                                </form>
                                            @endif

                                            @if($contactMessage->status !== 'replied')
                                                <form method="POST" action="{{ route('admin.contact-messages.update-status', $contactMessage) }}" class="d-inline mr-2">
                                                    @csrf
                                                    @method('PATCH')
                                                    <input type="hidden" name="status" value="replied">
                                                    <button type="submit" class="btn btn-primary">
                                                        <i class="fas fa-reply mr-1"></i>Mark as Replied
                                                    </button>
                                                </form>
                                            @endif

                                            @if($contactMessage->status !== 'archived')
                                                <form method="POST" action="{{ route('admin.contact-messages.update-status', $contactMessage) }}" class="d-inline">
                                                    @csrf
                                                    @method('PATCH')
                                                    <input type="hidden" name="status" value="archived">
                                                    <button type="submit" class="btn btn-secondary">
                                                        <i class="fas fa-archive mr-1"></i>Archive Message
                                                    </button>
                                                </form>
                                            @endif
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="card border-left-danger shadow">
                            <div class="card-body text-center">
                                <h6 class="font-weight-bold text-danger mb-2">Danger Zone</h6>
                                <form method="POST" action="{{ route('admin.contact-messages.destroy', $contactMessage) }}">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="btn btn-danger btn-sm"
                                            onclick="return confirm('Are you sure you want to delete this message? This action cannot be undone.')">
                                        <i class="fas fa-trash mr-1"></i>Delete Message
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <!-- Sender Information -->
                    <div class="col-xl-6 mb-4">
                        <div class="card shadow">
                            <div class="card-header bg-primary text-white">
                                <h6 class="m-0 font-weight-bold">
                                    <i class="fas fa-user mr-2"></i>Sender Information
                                </h6>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-sm-6">
                                        <strong class="text-{{ $text }}">Full Name:</strong><br>
                                        <span class="text-primary font-weight-bold">{{ $contactMessage->fname }}</span>
                                    </div>
                                    <div class="col-sm-6">
                                        <strong class="text-{{ $text }}">Email:</strong><br>
                                        <a href="mailto:{{ $contactMessage->email }}" class="text-decoration-none">
                                            <i class="fas fa-envelope mr-1"></i>{{ $contactMessage->email }}
                                        </a>
                                    </div>
                                </div>
                                <hr>
                                <div class="row">
                                    <div class="col-sm-6">
                                        <strong class="text-{{ $text }}">Phone:</strong><br>
                                        @if($contactMessage->phone)
                                            <a href="tel:{{ $contactMessage->phone }}" class="text-decoration-none">
                                                <i class="fas fa-phone mr-1"></i>{{ $contactMessage->phone }}
                                            </a>
                                        @else
                                            <span class="text-muted">Not provided</span>
                                        @endif
                                    </div>
                                    <div class="col-sm-6">
                                        <strong class="text-{{ $text }}">Contact Type:</strong><br>
                                        @if($contactMessage->service_branch)
                                            <span class="badge badge-primary">Military Personnel</span>
                                        @else
                                            <span class="badge badge-secondary">Civilian</span>
                                        @endif
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Military Information -->
                    <div class="col-xl-6 mb-4">
                        <div class="card shadow">
                            <div class="card-header bg-info text-white">
                                <h6 class="m-0 font-weight-bold">
                                    <i class="fas fa-star mr-2"></i>Military Information
                                </h6>
                            </div>
                            <div class="card-body">
                                @if($contactMessage->service_branch || $contactMessage->rank)
                                    <div class="row">
                                        <div class="col-sm-6">
                                            <strong class="text-{{ $text }}">Service Branch:</strong><br>
                                            @if($contactMessage->service_branch)
                                                <span class="badge badge-primary">{{ $contactMessage->service_branch }}</span>
                                            @else
                                                <span class="text-muted">Not specified</span>
                                            @endif
                                        </div>
                                        <div class="col-sm-6">
                                            <strong class="text-{{ $text }}">Rank:</strong><br>
                                            @if($contactMessage->rank)
                                                <span class="badge badge-secondary">{{ $contactMessage->rank }}</span>
                                            @else
                                                <span class="text-muted">Not specified</span>
                                            @endif
                                        </div>
                                    </div>
                                @else
                                    <div class="text-center py-3">
                                        <i class="fas fa-user-tie fa-2x text-gray-300 mb-2"></i>
                                        <p class="text-muted mb-0">Civilian Contact - No military information provided</p>
                                    </div>
                                @endif
                            </div>
                        </div>
                    </div>

                    <!-- Message Subject -->
                    <div class="col-xl-12 mb-4">
                        <div class="card shadow">
                            <div class="card-header bg-warning text-white">
                                <h6 class="m-0 font-weight-bold">
                                    <i class="fas fa-tag mr-2"></i>Message Subject
                                </h6>
                            </div>
                            <div class="card-body">
                                <h5 class="text-{{ $text }} font-weight-bold mb-0">{{ $contactMessage->subject }}</h5>
                            </div>
                        </div>
                    </div>

                    <!-- Message Content -->
                    <div class="col-xl-12 mb-4">
                        <div class="card shadow">
                            <div class="card-header bg-dark text-white">
                                <h6 class="m-0 font-weight-bold">
                                    <i class="fas fa-comment-alt mr-2"></i>Message Content
                                </h6>
                            </div>
                            <div class="card-body">
                                <div class="bg-light p-4 rounded border-left-primary">
                                    <p class="text-{{ $text }} mb-0" style="white-space: pre-wrap; line-height: 1.6;">{{ $contactMessage->message }}</p>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Quick Actions -->
                    <div class="col-xl-12 mb-4">
                        <div class="card shadow">
                            <div class="card-header bg-success text-white">
                                <h6 class="m-0 font-weight-bold">
                                    <i class="fas fa-tools mr-2"></i>Quick Actions
                                </h6>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-4 text-center mb-3">
                                        <a href="mailto:{{ $contactMessage->email }}?subject=Re: {{ $contactMessage->subject }}"
                                           class="btn btn-primary btn-lg btn-block">
                                            <i class="fas fa-reply mr-2"></i>Reply via Email
                                        </a>
                                    </div>
                                    @if($contactMessage->phone)
                                        <div class="col-md-4 text-center mb-3">
                                            <a href="tel:{{ $contactMessage->phone }}"
                                               class="btn btn-success btn-lg btn-block">
                                                <i class="fas fa-phone mr-2"></i>Call Contact
                                            </a>
                                        </div>
                                    @endif
                                    <div class="col-md-4 text-center mb-3">
                                        <button type="button" class="btn btn-info btn-lg btn-block" onclick="copyContactInfo()">
                                            <i class="fas fa-copy mr-2"></i>Copy Contact Info
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

<script>
function copyContactInfo() {
    const contactInfo = `
Name: {{ $contactMessage->fname }}
Email: {{ $contactMessage->email }}
Phone: {{ $contactMessage->phone ?? 'Not provided' }}
Subject: {{ $contactMessage->subject }}
@if($contactMessage->service_branch)Service Branch: {{ $contactMessage->service_branch }}@endif
@if($contactMessage->rank)Rank: {{ $contactMessage->rank }}@endif

Message:
{{ $contactMessage->message }}
    `.trim();

    navigator.clipboard.writeText(contactInfo).then(function() {
        Swal.fire({
            icon: 'success',
            title: 'Copied!',
            text: 'Contact information copied to clipboard',
            confirmButtonColor: '#28a745',
            timer: 2000
        });
    }, function(err) {
        Swal.fire({
            icon: 'error',
            title: 'Error!',
            text: 'Could not copy to clipboard',
            confirmButtonColor: '#dc3545'
        });
    });
}
</script>

@if(session('success'))
    <script>
        Swal.fire({
            icon: 'success',
            title: 'Success!',
            text: '{{ session('success') }}',
            confirmButtonColor: '#28a745'
        });
    </script>
@endif

@if(session('error'))
    <script>
        Swal.fire({
            icon: 'error',
            title: 'Error!',
            text: '{{ session('error') }}',
            confirmButtonColor: '#dc3545'
        });
    </script>
@endif
@endsection
