@extends('admin.admin_dashboard')
@section('customcss')
<!-- Datatable -->
<link href="{{asset('../asset/vendor/datatables/css/jquery.dataTables.min.css')}}" rel="stylesheet">
<!-- Custom Stylesheet -->
<link href="{{asset('../asset/vendor/jquery-nice-select/css/nice-select.css"')}}" rel="stylesheet">
@endsection

@section('admin')
  <div class="content-body">
    <div class="container-fluid">
      <div class="row page-titles">
        <h4>Time Slots</h4>
      </div>

      <div class="row">
        <!-- Bulk Add Form -->
        <div class="col-md-12">
          <div class="card p-3">
            <h5>Generate Time Slots</h5>
            <form method="POST" action="{{ route('admin.timeslots.store') }}">
              @csrf
              <div class="row mb-3">
                <div class="col-md-4 mb-2">
                  <label class="form-label">Start Time</label>
                  <input type="time" id="baseStart" class="form-control" required>
                </div>
                <div class="col-md-4 mb-2">
                  <label class="form-label">Duration (minutes)</label>
                  <input type="text" id="duration" class="form-control" value="30" required>
                </div>
                <div class="col-md-4 mb-2">
                  <label class="form-label">Number of Periods</label>
                  <input type="text" id="numPeriods" class="form-control" value="7" required>
                </div>
                <div class="col-md-12 mb-2">
                  <button type="button" class="btn btn-info w-100" id="generateBtn">Generate Slots</button>
                </div>
              </div>

              <div id="timeslot-rows"></div>

              <button class="btn btn-primary w-100 mt-3 d-none" id="saveBtn">Save All</button>
            </form>
          </div>
        </div>

        <!-- Slot List -->
        <div class="col-md-12">
          <div class="card p-3">
            <div class="table-responsive">
              <table id="example" class="display" style="min-width: 845px">
                <thead>
                  <tr>
                    <th>#</th>
                    <th>Name</th>
                    <th>Time</th>
                    <th>Shift</th>
                    <th>Routine Type</th>
                    <th>Routine Section</th>
                    <th>Status</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  @foreach($slots as $slot)
                    <tr>
                      <td>{{ $slot->id }}</td>
                      <td>{{ $slot->name }}</td>
                      <td>{{ \Carbon\Carbon::parse($slot->start_time)->format('H:i') }} -
                          {{ \Carbon\Carbon::parse($slot->end_time)->format('H:i') }}
                      </td>
                      <td>{{ $slot->shift?->name ?? '-' }}</td>
                      <td>{{ $slot->routineType?->name ?? '-' }}</td>
                      <td>{{ $slot->routineSection?->name ?? 'All' }}</td>
                      <td>
                        <form method="POST" action="{{ route('admin.timeslots.toggle', $slot) }}">
                          @csrf @method('PATCH')
                          <button class="btn btn-{{ $slot->status ? 'success' : 'secondary' }} btn-sm">
                            {{ $slot->status ? 'Active' : 'Inactive' }}
                          </button>
                        </form>
                      </td>
                      <td class="d-flex gap-1">
                        <!-- Edit -->
                        <button class="btn btn-warning btn-sm" data-bs-toggle="modal"
                          data-bs-target="#editSlotModal{{ $slot->id }}">Edit</button>
                        <!-- Delete -->
                        <form method="POST" action="{{ route('admin.timeslots.destroy', $slot) }}"
                          onsubmit="return confirm('Delete?')">
                          @csrf @method('DELETE')
                          <button class="btn btn-danger btn-sm">Delete</button>
                        </form>
                      </td>
                    </tr>

                    <!-- Edit Modal -->
                    <div class="modal fade" id="editSlotModal{{ $slot->id }}" tabindex="-1">
                      <div class="modal-dialog">
                        <div class="modal-content">
                          <div class="modal-header">
                            <h5 class="modal-title">Edit Time Slot</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                          </div>
                          <form method="POST" action="{{ route('admin.timeslots.update', $slot) }}">
                            @csrf @method('PUT')
                            <div class="modal-body">
                              <div class="mb-2">
                                <label class="form-label">Name</label>
                                <input name="name" class="form-control" value="{{ $slot->name }}" required>
                              </div>
                              <div class="mb-2">
                                <label class="form-label">Start</label>
                                <input type="time" name="start_time" class="form-control"
                                  value="{{ \Carbon\Carbon::parse($slot->start_time)->format('H:i') }}" required>
                              </div>
                              <div class="mb-2">
                                <label class="form-label">End</label>
                                <input type="time" name="end_time" class="form-control"
                                  value="{{ \Carbon\Carbon::parse($slot->end_time)->format('H:i') }}" required>
                              </div>
                              <div class="mb-3">
                                <label class="form-label">Shift</label>
                                <select name="shift_id" class="form-control" required>
                                  <option value="">-- Select Shift --</option>
                                  @foreach($shifts as $shift)
                                    <option value="{{ $shift->id }}" @if($slot->shift_id == $shift->id) selected @endif>
                                      {{ $shift->name }}
                                    </option>
                                  @endforeach
                                </select>
                              </div>
                              <div class="mb-3">
                                <label class="form-label">Routine Type</label>
                                <select name="routine_type_id" class="form-control" required>
                                  <option value="">-- Select Routine Type --</option>
                                  @foreach($routineTypes as $rt)
                                    <option value="{{ $rt->id }}" @if($slot->routine_type_id == $rt->id) selected @endif>
                                      {{ $rt->name }}
                                    </option>
                                  @endforeach
                                </select>
                              </div>
                              <div class="mb-3">
                                <label class="form-label">Routine Section <small class="text-muted">(Optional)</small></label>
                                <select name="routine_section_id" class="form-control">
                                  <option value="">-- All Sections --</option>
                                  @foreach($routineSections as $rs)
                                    <option value="{{ $rs->id }}" @if($slot->routine_section_id == $rs->id) selected @endif>
                                      {{ $rs->name }}
                                    </option>
                                  @endforeach
                                </select>
                              </div>
                            </div>
                            <div class="modal-footer">
                              <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                              <button type="submit" class="btn btn-primary">Update</button>
                            </div>
                          </form>
                        </div>
                      </div>
                    </div>
                  @endforeach
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

 
@endsection
@section('customjs')
<!-- Datatable -->
<script src="{{asset('../asset/vendor/datatables/js/jquery.dataTables.min.js')}}"></script>
<script src="{{asset('../asset/js/plugins-init/datatables.init.js')}}"></script>

<script src="{{asset('../asset/vendor/jquery-nice-select/js/jquery.nice-select.min.js')}}"></script>

 <script>
    document.getElementById('generateBtn').addEventListener('click', function () {
      let baseStart = document.getElementById('baseStart').value;
      let duration = parseInt(document.getElementById('duration').value);
      let numPeriods = parseInt(document.getElementById('numPeriods').value);

      if (!baseStart || !duration || !numPeriods) {
          alert("Please enter valid start time, duration and number of periods");
          return;
      }

      let container = document.getElementById('timeslot-rows');
      container.innerHTML = "";
      let start = new Date("1970-01-01T" + baseStart + ":00");

      for (let i = 0; i < numPeriods; i++) {
          let end = new Date(start.getTime() + duration * 60000);

          let row = `
          <div class="row timeslot-row mb-2">
              <div class="col-md-2">
                  <input name="slots[${i}][name]" class="form-control" value="Period ${i+1}" required>
              </div>
              <div class="col-md-2">
                  <input type="time" name="slots[${i}][start_time]" class="form-control"
                        value="${start.toTimeString().slice(0,5)}" required>
              </div>
              <div class="col-md-2">
                  <input type="time" name="slots[${i}][end_time]" class="form-control"
                        value="${end.toTimeString().slice(0,5)}" required>
              </div>
              <div class="col-md-3">
                  <select name="slots[${i}][shift_id]" class="form-control" required>
                      <option value="">--Shift--</option>
                      @foreach($shifts as $shift)
                          <option value="{{ $shift->id }}">{{ $shift->name }}</option>
                      @endforeach
                  </select>
              </div>
              <div class="col-md-2">
                  <select name="slots[${i}][routine_type_id]" class="form-control" required>
                      <option value="">--Routine Type--</option>
                      @foreach($routineTypes as $rt)
                          <option value="{{ $rt->id }}">{{ $rt->name }}</option>
                      @endforeach
                  </select>
              </div>
              <div class="col-md-3">
                  <select name="slots[${i}][routine_section_id]" class="form-control">
                      <option value="">--All Sections--</option>
                      @foreach($routineSections as $rs)
                          <option value="{{ $rs->id }}">{{ $rs->name }}</option>
                      @endforeach
                  </select>
              </div>
          </div>`;
          
          container.insertAdjacentHTML('beforeend', row);
          start = end;
      }

      document.getElementById('saveBtn').classList.remove('d-none');
    });
  </script>
@endsection
