@extends('admin.masterAdmin')

@section('content')
    @php
        $message = 'Commitments'; // default fallback
        if ($type == 1) {
            $message = 'Our Commitments';
        } elseif ($type == 4) {
            $message = 'Our Values';
        }
    @endphp

    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="mb-0">{{ $message }}</h5>
            @if ($type == 1)
                <a href="{{ route('commitment.create') }}" class="btn btn-sm btn-primary">
                    <i class="fa fa-plus-circle mr-1"></i> Add Commitment
                </a>
            @elseif ($type == 4)
                <a href="{{ route('values.create') }}" class="btn btn-sm btn-primary">
                    <i class="fa fa-plus-circle mr-1"></i> Add Value
                </a>
            @endif
        </div>

        <div class="card-body">

            @include('components.alert')

            <div class="table-responsive">
                <table class="table table-bordered w-100" id="dt-responsive">
                    <thead class="thead-light">
                        <tr>
                            <th>SL</th>
                            <th>Date</th>
                            <th>Name</th>
                            <th>Description</th>
                            <th>Image</th>
                            <th>Action</th>
                        </tr>
                    </thead>

                    <tbody>
                        @forelse ($commitments as $commitment)
                            <tr>
                                <td>{{ $loop->iteration }}</td>
                                <td>{{ $commitment->created_at->format('Y-m-d') }}</td>
                                <td>{{ $commitment->name }}</td>
                                <td>{{ Str::limit($commitment->description, 50) }}</td>
                                <td>
                                    @if ($commitment->image && file_exists(public_path($commitment->image)))
                                        <img src="{{ asset($commitment->image) }}" width="80" alt="Image">
                                    @else
                                        <img src="{{ asset('images/no-image.png') }}" width="80" alt="No Image">
                                    @endif
                                </td>
                                @if ($type == 1)
                                    <td>
                                        <a href="{{ route('commitment.edit', $commitment->id) }}"
                                            class="btn btn-sm btn-primary">
                                            <i class="fa fa-edit"></i>
                                        </a>
                                        <form action="{{ route('commitment.destroy', $commitment->id) }}" method="POST"
                                            class="d-inline-block"
                                            onsubmit="return confirm('Are you sure you want to delete this doctor?')">
                                            @csrf
                                            @method('DELETE')
                                            <button class="btn btn-sm btn-danger">
                                                <i class="fa fa-trash"></i>
                                            </button>
                                        </form>
                                    </td>
                                @elseif ($type == 4)
                                    <td>
                                        <a href="{{ route('values.edit', $commitment->id) }}"
                                            class="btn btn-sm btn-primary">
                                            <i class="fa fa-edit"></i>
                                        </a>
                                        <form action="{{ route('values.destroy', $commitment->id) }}" method="POST"
                                            class="d-inline-block"
                                            onsubmit="return confirm('Are you sure you want to delete this value?')">
                                            @csrf
                                            @method('DELETE')
                                            <button class="btn btn-sm btn-danger">
                                                <i class="fa fa-trash"></i>
                                            </button>
                                        </form>
                                    </td>
                                @endif
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="text-center">No {{ $message }} found.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection
