<?php

namespace App\Livewire;

use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
use App\Models\Sensor;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class SensorTable extends DataTableComponent
{
    protected $model = Sensor::class;
   

    public function configure(): void
    {
        $this->setPrimaryKey('id');
        $this->setPerPageAccepted([5, 10, 25, 50, 100]); // rappasoft hanya boleh 10, 25, 50, jadi wajib set ini!
        $this->setPerPage(5); // set agar default 5!
        $this->setPerPageVisibilityStatus(false); // ini untuk set perpage berapa list bagi admin (ui)
        $this->setColumnSelectStatus(false);
    }

    public function builder(): Builder
    {
        return Sensor::query()
            ->orderBy('created_at', 'desc');
    }

    public function columns(): array
    {
        return [
            Column::make("Id", "id")->deselected()->sortable(),
            Column::make("Cahaya", "ldr_value")
                ->format(function ($value) {
                    return $value == 0 ? 'Terang' : 'Gelap';
                })
                ->sortable(),
            Column::make("Suara", "sound_value")
                ->format(function ($value) {
                    return $value == 0 ? 'Ada suara' : 'Tidak ada suara';
                })
                ->sortable(),
            Column::make("Gerakan", "motion_detected")
                ->format(function ($value) {
                    return $value == 1 ? 'Ada gerakan' : 'Tidak ada gerakan';
                })
                ->sortable(),
            Column::make("Waktu", "created_at")->sortable(),
        ];
    }
}
