<x-app-layout>
    @section('title')
        Appoinments
    @endsection
    @php
    $doctors = \App\Models\Doctor::oldest('name')->get();
    @endphp

    <!-- DataTales Example -->
    <div class="card shadow mb-4">
        <div class="card-header py-3 pl-0 justify-content-between d-flex align-items-center">
            <!--<h6 class="m-0 font-weight-bold text-primary">-->
            <!--     Appoinments-->
            <!--</h6>-->
            <div class="pl-4">
                <a href="{{ route('admin.appoinments.index') }}"
                    class="btn btn-{{ !isset(request()->status) ? 'primary' : 'secondary' }}  btn-sm">All</a>

                <a href="{{ route('admin.appoinments.index', ['status' => 0]) }}"
                    class="btn btn-{{ isset(request()->status) && request()->status == 0 ? 'primary' : 'secondary' }} btn-sm">Pending</a>

                <a href="{{ route('admin.appoinments.index', ['status' => 1]) }}"
                    class="btn btn-{{ isset(request()->status) && request()->status == 1 ? 'primary' : 'secondary' }}  btn-sm">Approved</a>
                <a href="{{ route('admin.appoinments.index', ['status' => 2]) }}"
                    class="btn btn-{{ isset(request()->status) && request()->status == 2 ? 'primary' : 'secondary' }}  btn-sm">Cancelled</a>
            </div>
            <form class="d-flex justify-content-end align-items-center" action="{{ route('admin.appoinments.index') }}" method="get">
            <div class="form-group pl-3">
                <label class="mb-0">Doctor</label>
                <select name="doctor_id" id="doctor_id" class="form-control">
                    <option value="">All Doctor</option>
                    @foreach ($doctors as $service)
                        <option {{request()->doctor_id == $service->id ? 'selected' : ''}} value="{{ $service->id }}">{{ $service->name }}</option>
                    @endforeach
                </select>
            </div>
             <div class="form-group pl-2">
                 <label class="mb-0">From</label>
                <input type="date" name="from"
                class="form-control"
                 value="{{request()->from ? \Carbon\Carbon::parse(request()->from)->format('Y-m-d') : ''}}" />
            </div>
            <div class="form-group pl-2">
                <label class="mb-0">To</label>
                <input type="date" name="to"
                class="form-control"
                 value="{{request()->to ? \Carbon\Carbon::parse(request()->to)->format('Y-m-d') : ''}}" />
            </div>
            <button type="submit" 
                    class="btn btn-success  btn-sm ml-2">Filter</button>
                    <a href="{{route('admin.appoinments.index')}}"
                    class="btn btn-danger  btn-sm ml-2">Reset</a>
            </form>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th>S.No.</th>
                            <th>UUID/Service/Doctor</th>
                            <th>Date/Time</th>
                            <th>Patient</th>
                            <th>Status</th>
                            <th>Updated At</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>

                        @can('Appoinment access')


                            @foreach ($appoinments as $row)
                                @php
                                    if (session()->has($row->uuid) && session()->get($row->uuid) == $row->uuid) {
                                        if (!isset($color)) {
                                            $color = 'text-success';
                                        }
                                        if ($color == 'text-info') {
                                            $color == 'text-success';
                                        } else {
                                            $color = 'text-info';
                                        }
                                    } else {
                                        session()->put($row->uuid, $row->uuid);
                                        if (!isset($color)) {
                                            $color = 'text-info';
                                        } else {
                                            if ($color == 'text-info') {
                                                $color == 'text-success';
                                            } else {
                                                $color = 'text-info';
                                            }
                                        }
                                    }

                                @endphp
                                <tr>
                                    <td>{{ $loop->iteration }}</td>
                                    <td>
                                        <span class="{{ $color }}">{{ $row->uuid }}</span> <br>
                                        {{ $row->doctor->service->name }} <br>
                                        {{ $row->doctor->name }} <br>

                                    </td>
                                    <td>
                                        {{ \Carbon\Carbon::parse($row->apply_date)->format('d-m-Y') }} <br>
                                        {{ $row->time->name }}
                                    </td>
                                    <td>
                                        {{ $row->name }} <br>
                                        {{ $row->phone }}
                                    </td>
                                    <td>
                                        @if ($row->status == 1)
                                            <span class="badge badge-success">Active</span>
                                        @else
                                            <span class="badge badge-danger">Deactive</span>
                                        @endif

                                    <td>{{ $row->updated_at->diffForHumans() }}</td>
                                    <td class="d-flex">
                                        @can('Appoinment edit')
                                            <a href="{{ route('admin.appoinments.edit', $row->id) }}"
                                                class="btn btn-primary btn-sm mr-1">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                        @endcan

                                        @can('Appoinment delete')
                                            <a href="{{ route('admin.appoinments.destroy', $row->id) }}"
                                                class="btn btn-danger btn-sm" id="delete"><i class="fas fa-trash"></i>
                                            </a>
                                        @endcan

                                    </td>
                                </tr>
                            @endforeach


                        @endcan
                    </tbody>
                </table>
            </div>
        </div>
    </div>

</x-app-layout>
