@extends('layouts.app')
@section('title', 'Schedule')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
    <h5 class="mb-0 fw-bold">Automatic Schedule</h5>
    <div class="text-secondary" style="font-size:.8rem">
        <i class="bi bi-clock me-1"></i>Server time: {{ now()->format('H:i') }} ({{ now()->timezoneName }})
    </div>
</div>

<div class="row g-3">
@foreach($devices as $device)
@php $sch = $device->schedule; @endphp
<div class="col-lg-6">
<div class="card">
    <div class="card-header d-flex align-items-center justify-content-between">
        <div>
            <span class="fw-semibold">{{ $device->name }}</span>
            <code class="ms-2" style="font-size:.75rem;color:#7dd3fc">{{ $device->ip_address }}</code>
        </div>
        <div class="d-flex align-items-center gap-2">
            @if($sch && $sch->enabled)
                <span class="badge bg-success">Enabled</span>
                <span class="badge badge-{{ $sch->currentMode() }}">Now: {{ strtoupper($sch->currentMode()) }}</span>
            @else
                <span class="badge bg-secondary">Disabled</span>
            @endif
        </div>
    </div>
    <div class="card-body">
        <form method="POST" action="{{ route('lis.schedule.update', $device) }}">
            @csrf @method('PUT')

            <div class="form-check form-switch mb-3">
                <input class="form-check-input" type="checkbox" name="enabled" id="en{{ $device->id }}"
                       {{ $sch?->enabled ? 'checked' : '' }}>
                <label class="form-check-label text-secondary" for="en{{ $device->id }}">Enable automatic schedule</label>
            </div>

            <div class="mb-3">
                <label class="form-label text-secondary" style="font-size:.8rem">WEEKDAY — BUKA Window 1 (Mon–Fri)</label>
                <div class="d-flex gap-2 align-items-center">
                    <input type="time" name="weekday_green_start_1" class="form-control form-control-sm"
                           value="{{ $sch?->weekday_green_start_1 ?? '06:00' }}">
                    <span class="text-secondary">to</span>
                    <input type="time" name="weekday_green_end_1" class="form-control form-control-sm"
                           value="{{ $sch?->weekday_green_end_1 ?? '10:00' }}">
                </div>
            </div>

            <div class="mb-3">
                <label class="form-label text-secondary" style="font-size:.8rem">WEEKDAY — BUKA Window 2 (Mon–Fri)</label>
                <div class="d-flex gap-2 align-items-center">
                    <input type="time" name="weekday_green_start_2" class="form-control form-control-sm"
                           value="{{ $sch?->weekday_green_start_2 ?? '16:00' }}">
                    <span class="text-secondary">to</span>
                    <input type="time" name="weekday_green_end_2" class="form-control form-control-sm"
                           value="{{ $sch?->weekday_green_end_2 ?? '20:00' }}">
                </div>
            </div>

            <div class="mb-3">
                <label class="form-label text-secondary" style="font-size:.8rem">WEEKEND — BUKA Window (Sat–Sun)</label>
                <div class="d-flex gap-2 align-items-center">
                    <input type="time" name="weekend_green_start" class="form-control form-control-sm"
                           value="{{ $sch?->weekend_green_start ?? '10:00' }}">
                    <span class="text-secondary">to</span>
                    <input type="time" name="weekend_green_end" class="form-control form-control-sm"
                           value="{{ $sch?->weekend_green_end ?? '22:00' }}">
                </div>
            </div>

            <div class="mb-4">
                <label class="form-label text-secondary" style="font-size:.8rem">Re-push interval (hours)</label>
                <input type="number" name="repush_hours" class="form-control form-control-sm" style="max-width:100px"
                       value="{{ $sch?->repush_hours ?? 6 }}" min="1" max="24">
                <div class="text-secondary mt-1" style="font-size:.75rem">
                    Outside green windows → TUTUP. Last auto: {{ $sch?->last_auto_sent_at?->diffForHumans() ?? 'Never' }}
                </div>
            </div>

            <div class="d-flex gap-2">
                <button type="submit" class="btn btn-primary btn-sm px-3">
                    <i class="bi bi-floppy me-1"></i>Save
                </button>
                <a href="{{ route('lis.schedule.run', $device) }}"
                   class="btn btn-outline-secondary btn-sm"
                   onclick="return confirm('Run schedule now for {{ $device->name }}?')"
                   data-method="post">
                    <i class="bi bi-play-fill me-1"></i>Run Now
                </a>
            </div>
        </form>

        {{-- Run Now is POST, need a separate form --}}
    </div>
</div>
</div>
@endforeach

@if($devices->isEmpty())
<div class="col-12">
    <div class="card">
        <div class="card-body text-center py-5 text-secondary">
            No active devices. <a href="{{ route('lis.devices.create') }}">Add a device first →</a>
        </div>
    </div>
</div>
@endif
</div>

{{-- Run-now forms (POST) hidden --}}
@foreach($devices as $device)
<form id="run-form-{{ $device->id }}" method="POST" action="{{ route('lis.schedule.run', $device) }}" class="d-none">
    @csrf
</form>
@endforeach
@endsection

@push('scripts')
<script>
document.querySelectorAll('[data-method=post]').forEach(a => {
    a.addEventListener('click', e => {
        if (!confirm('Run schedule now for this device?')) { e.preventDefault(); return; }
        e.preventDefault();
        const id = a.href.match(/devices\/(\d+)\/run/) || a.href.match(/schedule\/(\d+)\/run/);
        // extract device id from URL
        const m = a.href.match(/\/(\d+)\/run/);
        if (m) document.getElementById('run-form-' + m[1]).submit();
    });
});
</script>
@endpush
