@extends('admin.layouts.app')

@section('title', 'جزئیات باربری')

@section('content')
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-md-6">
            <h2><i class="fas fa-warehouse"></i> جزئیات باربری</h2>
        </div>
        <div class="col-md-6 text-start">
            <a href="{{ route('admin.freight-companies.index') }}" class="btn btn-secondary">
                <i class="fas fa-arrow-right"></i> بازگشت
            </a>
            <a href="{{ route('admin.freight-companies.edit', $freightCompany) }}" class="btn btn-primary">
                <i class="fas fa-edit"></i> ویرایش
            </a>
        </div>
    </div>

    <!-- وضعیت -->
    <div class="card mb-4">
        <div class="card-body">
            <div class="row align-items-center">
                <div class="col-md-8">
                    <h4>{{ $freightCompany->company_name }}</h4>
                    <p class="mb-0">
                        <span class="badge bg-{{ $freightCompany->status_badge }} fs-6">
                            {{ $freightCompany->status_label }}
                        </span>
                        <span class="badge bg-info fs-6 ms-2">{{ $freightCompany->entity_type_label }}</span>
                    </p>
                    @if($freightCompany->rejection_reason)
                        <div class="alert alert-danger mt-3">
                            <strong>دلیل رد:</strong> {{ $freightCompany->rejection_reason }}
                        </div>
                    @endif
                </div>
                <div class="col-md-4 text-start">
                    @if($freightCompany->status === 'pending')
                        <button type="button" class="btn btn-success" onclick="approveCompany()">
                            <i class="fas fa-check"></i> تایید
                        </button>
                        <button type="button" class="btn btn-danger" onclick="rejectCompany()">
                            <i class="fas fa-times"></i> رد
                        </button>
                    @endif
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <!-- اطلاعات اصلی -->
        <div class="col-md-6">
            <div class="card mb-4">
                <div class="card-header">
                    <h5><i class="fas fa-info-circle"></i> اطلاعات اصلی</h5>
                </div>
                <div class="card-body">
                    <table class="table table-borderless">
                        <tr>
                            <th width="40%">نام باربری:</th>
                            <td>{{ $freightCompany->company_name }}</td>
                        </tr>
                        <tr>
                            <th>شماره تماس:</th>
                            <td>{{ $freightCompany->phone }}</td>
                        </tr>
                        <tr>
                            <th>نوع:</th>
                            <td>{{ $freightCompany->entity_type_label }}</td>
                        </tr>
                        <tr>
                            <th>دسته‌بندی خودرو:</th>
                            <td>{{ $freightCompany->vehicle_categories_string }}</td>
                        </tr>
                        <tr>
                            <th>تاریخ ثبت:</th>
                            <td>{{ $freightCompany->created_at->format('Y/m/d H:i') }}</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>

        <!-- آدرس -->
        <div class="col-md-6">
            <div class="card mb-4">
                <div class="card-header">
                    <h5><i class="fas fa-map-marker-alt"></i> آدرس شعبه اصلی</h5>
                </div>
                <div class="card-body">
                    <table class="table table-borderless">
                        <tr>
                            <th width="40%">استان:</th>
                            <td>{{ $freightCompany->province ?? '-' }}</td>
                        </tr>
                        <tr>
                            <th>شهر:</th>
                            <td>{{ $freightCompany->city ?? '-' }}</td>
                        </tr>
                        <tr>
                            <th>کد پستی:</th>
                            <td>{{ $freightCompany->postal_code ?? '-' }}</td>
                        </tr>
                        <tr>
                            <th>تلفن ثابت:</th>
                            <td>{{ $freightCompany->landline ?? '-' }}</td>
                        </tr>
                        <tr>
                            <th>آدرس:</th>
                            <td>{{ $freightCompany->address ?? '-' }}</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- معرفی شرکت -->
    @if($freightCompany->company_description)
        <div class="card mb-4">
            <div class="card-header">
                <h5><i class="fas fa-building"></i> معرفی شرکت</h5>
            </div>
            <div class="card-body">
                <p>{{ $freightCompany->company_description }}</p>
            </div>
        </div>
    @endif

    <!-- مدارک حقیقی -->
    @if($freightCompany->entity_type === 'individual')
        <div class="card mb-4">
            <div class="card-header bg-info text-white">
                <h5><i class="fas fa-file-alt"></i> مدارک حقیقی</h5>
            </div>
            <div class="card-body">
                <div class="row">
                    @if($freightCompany->national_card)
                        <div class="col-md-3 mb-3 text-center">
                            <div class="card">
                                <div class="card-body">
                                    <i class="fas fa-id-card fa-3x text-primary mb-2"></i>
                                    <h6>کارت ملی</h6>
                                    <a href="{{ Storage::url($freightCompany->national_card) }}" target="_blank" class="btn btn-sm btn-outline-primary">
                                        <i class="fas fa-download"></i> مشاهده
                                    </a>
                                </div>
                            </div>
                        </div>
                    @endif

                    @if($freightCompany->birth_certificate)
                        <div class="col-md-3 mb-3 text-center">
                            <div class="card">
                                <div class="card-body">
                                    <i class="fas fa-file-alt fa-3x text-success mb-2"></i>
                                    <h6>شناسنامه</h6>
                                    <a href="{{ Storage::url($freightCompany->birth_certificate) }}" target="_blank" class="btn btn-sm btn-outline-success">
                                        <i class="fas fa-download"></i> مشاهده
                                    </a>
                                </div>
                            </div>
                        </div>
                    @endif

                    @if($freightCompany->photo_3x4)
                        <div class="col-md-3 mb-3 text-center">
                            <div class="card">
                                <div class="card-body">
                                    <i class="fas fa-image fa-3x text-info mb-2"></i>
                                    <h6>عکس 3×4</h6>
                                    <a href="{{ Storage::url($freightCompany->photo_3x4) }}" target="_blank" class="btn btn-sm btn-outline-info">
                                        <i class="fas fa-download"></i> مشاهده
                                    </a>
                                </div>
                            </div>
                        </div>
                    @endif

                    @if($freightCompany->commendation_letter)
                        <div class="col-md-3 mb-3 text-center">
                            <div class="card">
                                <div class="card-body">
                                    <i class="fas fa-certificate fa-3x text-warning mb-2"></i>
                                    <h6>برگه ثنا</h6>
                                    <a href="{{ Storage::url($freightCompany->commendation_letter) }}" target="_blank" class="btn btn-sm btn-outline-warning">
                                        <i class="fas fa-download"></i> مشاهده
                                    </a>
                                </div>
                            </div>
                        </div>
                    @endif

                    @if($freightCompany->activity_license)
                        <div class="col-md-3 mb-3 text-center">
                            <div class="card">
                                <div class="card-body">
                                    <i class="fas fa-file-contract fa-3x text-danger mb-2"></i>
                                    <h6>مجوز فعالیت</h6>
                                    <a href="{{ Storage::url($freightCompany->activity_license) }}" target="_blank" class="btn btn-sm btn-outline-danger">
                                        <i class="fas fa-download"></i> مشاهده
                                    </a>
                                </div>
                            </div>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    @endif

    <!-- مدارک حقوقی -->
    @if($freightCompany->entity_type === 'legal')
        <div class="card mb-4">
            <div class="card-header bg-warning text-dark">
                <h5><i class="fas fa-file-alt"></i> مدارک حقوقی</h5>
            </div>
            <div class="card-body">
                <div class="row">
                    @if($freightCompany->official_newspaper)
                        <div class="col-md-3 mb-3 text-center">
                            <div class="card">
                                <div class="card-body">
                                    <i class="fas fa-newspaper fa-3x text-primary mb-2"></i>
                                    <h6>روزنامه رسمی</h6>
                                    <a href="{{ Storage::url($freightCompany->official_newspaper) }}" target="_blank" class="btn btn-sm btn-outline-primary">
                                        <i class="fas fa-download"></i> مشاهده
                                    </a>
                                </div>
                            </div>
                        </div>
                    @endif

                    @if($freightCompany->latest_changes)
                        <div class="col-md-3 mb-3 text-center">
                            <div class="card">
                                <div class="card-body">
                                    <i class="fas fa-edit fa-3x text-success mb-2"></i>
                                    <h6>آخرین تغییرات</h6>
                                    <a href="{{ Storage::url($freightCompany->latest_changes) }}" target="_blank" class="btn btn-sm btn-outline-success">
                                        <i class="fas fa-download"></i> مشاهده
                                    </a>
                                </div>
                            </div>
                        </div>
                    @endif

                    @if($freightCompany->company_statute)
                        <div class="col-md-3 mb-3 text-center">
                            <div class="card">
                                <div class="card-body">
                                    <i class="fas fa-book fa-3x text-info mb-2"></i>
                                    <h6>اساسنامه</h6>
                                    <a href="{{ Storage::url($freightCompany->company_statute) }}" target="_blank" class="btn btn-sm btn-outline-info">
                                        <i class="fas fa-download"></i> مشاهده
                                    </a>
                                </div>
                            </div>
                        </div>
                    @endif

                    @if($freightCompany->ceo_national_card)
                        <div class="col-md-3 mb-3 text-center">
                            <div class="card">
                                <div class="card-body">
                                    <i class="fas fa-id-card fa-3x text-warning mb-2"></i>
                                    <h6>کارت ملی مدیرعامل</h6>
                                    <a href="{{ Storage::url($freightCompany->ceo_national_card) }}" target="_blank" class="btn btn-sm btn-outline-warning">
                                        <i class="fas fa-download"></i> مشاهده
                                    </a>
                                </div>
                            </div>
                        </div>
                    @endif

                    @if($freightCompany->ceo_birth_certificate)
                        <div class="col-md-3 mb-3 text-center">
                            <div class="card">
                                <div class="card-body">
                                    <i class="fas fa-file-alt fa-3x text-danger mb-2"></i>
                                    <h6>شناسنامه مدیرعامل</h6>
                                    <a href="{{ Storage::url($freightCompany->ceo_birth_certificate) }}" target="_blank" class="btn btn-sm btn-outline-danger">
                                        <i class="fas fa-download"></i> مشاهده
                                    </a>
                                </div>
                            </div>
                        </div>
                    @endif

                    @if($freightCompany->official_introduction)
                        <div class="col-md-3 mb-3 text-center">
                            <div class="card">
                                <div class="card-body">
                                    <i class="fas fa-file-signature fa-3x text-primary mb-2"></i>
                                    <h6>معرفی نامه رسمی</h6>
                                    <a href="{{ Storage::url($freightCompany->official_introduction) }}" target="_blank" class="btn btn-sm btn-outline-primary">
                                        <i class="fas fa-download"></i> مشاهده
                                    </a>
                                </div>
                            </div>
                        </div>
                    @endif

                    @if($freightCompany->attorney_letter)
                        <div class="col-md-3 mb-3 text-center">
                            <div class="card">
                                <div class="card-body">
                                    <i class="fas fa-gavel fa-3x text-success mb-2"></i>
                                    <h6>وکالتنامه</h6>
                                    <a href="{{ Storage::url($freightCompany->attorney_letter) }}" target="_blank" class="btn btn-sm btn-outline-success">
                                        <i class="fas fa-download"></i> مشاهده
                                    </a>
                                </div>
                            </div>
                        </div>
                    @endif

                    @if($freightCompany->valid_activity_license)
                        <div class="col-md-3 mb-3 text-center">
                            <div class="card">
                                <div class="card-body">
                                    <i class="fas fa-file-contract fa-3x text-info mb-2"></i>
                                    <h6>مجوز فعالیت</h6>
                                    <a href="{{ Storage::url($freightCompany->valid_activity_license) }}" target="_blank" class="btn btn-sm btn-outline-info">
                                        <i class="fas fa-download"></i> مشاهده
                                    </a>
                                </div>
                            </div>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    @endif
</div>

<!-- Reject Modal -->
<div class="modal fade" id="rejectModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="{{ route('admin.freight-companies.reject', $freightCompany) }}" method="POST">
                @csrf
                <div class="modal-header">
                    <h5 class="modal-title">رد باربری</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">دلیل رد *</label>
                        <textarea class="form-control" name="rejection_reason" rows="4" required></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">انصراف</button>
                    <button type="submit" class="btn btn-danger">تایید رد</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    function approveCompany() {
        if (confirm('آیا از تایید این باربری اطمینان دارید؟')) {
            const form = document.createElement('form');
            form.method = 'POST';
            form.action = '{{ route("admin.freight-companies.approve", $freightCompany) }}';
            
            const csrfToken = document.createElement('input');
            csrfToken.type = 'hidden';
            csrfToken.name = '_token';
            csrfToken.value = '{{ csrf_token() }}';
            
            form.appendChild(csrfToken);
            document.body.appendChild(form);
            form.submit();
        }
    }

    function rejectCompany() {
        const modal = new bootstrap.Modal(document.getElementById('rejectModal'));
        modal.show();
    }
</script>
@endsection
