@extends('layouts.app')

@section('title', $notice->title)

@section('content')
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-md-12">
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ route('notices.index') }}">নোটিশ</a></li>
                    <li class="breadcrumb-item active">{{ Str::limit($notice->title, 50) }}</li>
                </ol>
            </nav>
            
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h2 class="mb-1">{{ $notice->title }}</h2>
                    <div class="d-flex align-items-center gap-2">
                        @if($notice->pin_to_top)
                        <span class="badge bg-warning">
                            <i class="fas fa-thumbtack"></i> পিন করা
                        </span>
                        @endif
                        @if($notice->is_important)
                        <span class="badge bg-danger">
                            <i class="fas fa-exclamation-triangle"></i> গুরুত্বপূর্ণ
                        </span>
                        @endif
                        @if($notice->priority == 'urgent')
                        <span class="badge bg-danger">জরুরি</span>
                        @elseif($notice->priority == 'high')
                        <span class="badge bg-warning">উচ্চ অগ্রাধিকার</span>
                        @endif
                        
                        <span class="badge bg-info">
                            {{ $notice->notice_type_bangla }}
                        </span>
                        
                        <span class="badge 
                            @if($notice->status == 'published') bg-success
                            @elseif($notice->status == 'draft') bg-secondary
                            @else bg-warning @endif">
                            {{ $notice->status_bangla }}
                        </span>
                    </div>
                </div>
                
                <div class="btn-group">
                    <a href="{{ route('notices.print', $notice->id) }}" class="btn btn-outline-primary" target="_blank" title="প্রিন্ট">
                        <i class="fas fa-print"></i>
                    </a>
                    @if(auth()->user()->user_type == 'admin' || auth()->user()->user_type == 'committee')
                    <a href="{{ route('notices.edit', $notice->id) }}" class="btn btn-outline-primary" title="সম্পাদনা">
                        <i class="fas fa-edit"></i>
                    </a>
                    
                    <div class="dropdown">
                        <button class="btn btn-outline-secondary dropdown-toggle" type="button" 
                                data-bs-toggle="dropdown" title="স্ট্যাটাস পরিবর্তন">
                            <i class="fas fa-cog"></i>
                        </button>
                        <ul class="dropdown-menu">
                            @if($notice->status != 'published')
                            <li>
                                <form action="{{ route('notices.change-status', $notice->id) }}" method="POST" class="d-inline">
                                    @csrf
                                    <input type="hidden" name="status" value="published">
                                    <button type="submit" class="dropdown-item">
                                        <i class="fas fa-check-circle text-success"></i> প্রকাশ করুন
                                    </button>
                                </form>
                            </li>
                            @endif
                            @if($notice->status != 'draft')
                            <li>
                                <form action="{{ route('notices.change-status', $notice->id) }}" method="POST" class="d-inline">
                                    @csrf
                                    <input type="hidden" name="status" value="draft">
                                    <button type="submit" class="dropdown-item">
                                        <i class="fas fa-edit text-secondary"></i> খসড়া করুন
                                    </button>
                                </form>
                            </li>
                            @endif
                            @if($notice->status != 'expired')
                            <li>
                                <form action="{{ route('notices.change-status', $notice->id) }}" method="POST" class="d-inline">
                                    @csrf
                                    <input type="hidden" name="status" value="expired">
                                    <button type="submit" class="dropdown-item">
                                        <i class="fas fa-archive text-warning"></i> মেয়াদোত্তীর্ণ করুন
                                    </button>
                                </form>
                            </li>
                            @endif
                        </ul>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-8">
            <!-- নোটিশ কন্টেন্ট -->
            <div class="card mb-4">
                <div class="card-body">
                    <div class="notice-content">
                        {!! nl2br(e($notice->content)) !!}
                    </div>
                    
                    <!-- সংযুক্তি -->
                    @if($notice->attachment)
                    <div class="mt-4 pt-4 border-top">
                        <h5><i class="fas fa-paperclip"></i> সংযুক্ত ফাইল</h5>
                        <div class="d-flex align-items-center gap-2 p-3 bg-light rounded">
                            <span style="font-size: 24px;">
                                {{ $notice->attachment_icon }}
                            </span>
                            <div class="flex-grow-1">
                                <div class="fw-bold">{{ $notice->attachment_name ?? 'ফাইল' }}</div>
                                <small class="text-muted">
                                    @if(file_exists(storage_path('app/public/' . $notice->attachment)))
                                        {{ round(filesize(storage_path('app/public/' . $notice->attachment)) / 1024, 2) }} KB
                                    @endif
                                </small>
                            </div>
                            <a href="{{ Storage::url($notice->attachment) }}" 
                               class="btn btn-sm btn-primary" target="_blank" download>
                                <i class="fas fa-download"></i> ডাউনলোড
                            </a>
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>

        <div class="col-md-4">
            <!-- সাইডবার তথ্য -->
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">নোটিশ তথ্য</h5>
                </div>
                <div class="card-body">
                    <table class="table table-sm table-borderless">
                        <tr>
                            <td width="40%"><strong><i class="fas fa-user"></i> তৈরিকারী</strong></td>
                            <td>{{ $notice->creator->name ?? 'নাম নেই' }}</td>
                        </tr>
                        <tr>
                            <td><strong><i class="fas fa-calendar"></i> তৈরির তারিখ</strong></td>
                            <td>{{ $notice->created_at->format('d/m/Y h:i A') }}</td>
                        </tr>
                        <tr>
                            <td><strong><i class="fas fa-calendar-check"></i> প্রকাশের তারিখ</strong></td>
                            <td>
                                @if($notice->publish_date)
                                {{ $notice->publish_date->format('d/m/Y h:i A') }}
                                @else
                                <span class="text-muted">নির্ধারিত নয়</span>
                                @endif
                            </td>
                        </tr>
                        <tr>
                            <td><strong><i class="fas fa-clock"></i> মেয়াদ শেষ</strong></td>
                            <td>
                                @if($notice->expiry_date)
                                {{ $notice->expiry_date->format('d/m/Y h:i A') }}
                                @if($notice->expiry_date < now())
                                <span class="badge bg-danger ms-2">মেয়াদোত্তীর্ণ</span>
                                @endif
                                @else
                                <span class="text-muted">নির্ধারিত নয়</span>
                                @endif
                            </td>
                        </tr>
                        <tr>
                            <td><strong><i class="fas fa-users"></i> টার্গেট</strong></td>
                            <td>
                                {{ $notice->target_audience_bangla }}
                                @if($notice->target_audience == 'specific' && $notice->specific_members)
                                    <br>
                                    <small class="text-muted">
                                        <?php 
                                            $specificIds = json_decode($notice->specific_members, true);
                                            $specificUsers = App\Models\User::whereIn('id', $specificIds)->get();
                                        ?>
                                        @foreach($specificUsers as $user)
                                            {{ $user->name }}@if(!$loop->last), @endif
                                        @endforeach
                                    </small>
                                @endif
                            </td>
                        </tr>
                        <tr>
                            <td><strong><i class="fas fa-eye"></i> ভিউ</strong></td>
                            <td>{{ $notice->views }} বার</td>
                        </tr>
                        <tr>
                            <td><strong><i class="fas fa-history"></i> সর্বশেষ আপডেট</strong></td>
                            <td>{{ $notice->updated_at->format('d/m/Y h:i A') }}</td>
                        </tr>
                    </table>
                </div>
            </div>

            <!-- কার্যক্রম -->
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">কার্যক্রম</h5>
                </div>
                <div class="card-body">
                    <div class="d-grid gap-2">
                        @if(auth()->user()->user_type == 'admin' || auth()->user()->user_type == 'committee')
                        <a href="{{ route('notices.edit', $notice->id) }}" class="btn btn-primary">
                            <i class="fas fa-edit"></i> নোটিশ সম্পাদনা
                        </a>
                        
                        <form action="{{ route('notices.destroy', $notice->id) }}" method="POST" class="d-inline" id="deleteForm">
                            @csrf
                            @method('DELETE')
                            <button type="button" class="btn btn-danger w-100" 
                                    onclick="if(confirm('আপনি কি নিশ্চিত?')) document.getElementById('deleteForm').submit()">
                                <i class="fas fa-trash"></i> নোটিশ ডিলিট
                            </button>
                        </form>
                        @endif
                        
                        <a href="{{ route('notices.index') }}" class="btn btn-outline-secondary">
                            <i class="fas fa-arrow-left"></i> নোটিশ তালিকায় ফিরে যান
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('styles')
<style>
.notice-content {
    font-size: 16px;
    line-height: 1.8;
    white-space: pre-wrap;
}
.notice-content img {
    max-width: 100%;
    height: auto;
    border-radius: 8px;
    margin: 10px 0;
}
</style>
@endpush