<?php

namespace App\Livewire\Forms\Tables;

use App\Models\MachineStatus;
use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Builder;
use PowerComponents\LivewirePowerGrid\Button;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Facades\Filter;
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\Traits\WithExport; 
use PowerComponents\LivewirePowerGrid\Components\SetUp\Exportable;

final class MachineStatusTable extends PowerGridComponent
{
    public string $tableName = 'machine-status-table';
    public bool $canDownload;

    public bool $showFilters = false;
    public function boot(): void
    {
        config(['livewire-powergrid.filter' => 'outside']);
        //權限設定
        $this->canDownload=true;
    }

    public function setUp(): array
    {
        if($this->canDownload ){
            $this->showCheckBox();
        }
        $actions = [];
        if($this->canDownload){
            $actions[]=PowerGrid::exportable(fileName: $this->tableName.'-file') 
                ->type(Exportable::TYPE_XLS, Exportable::TYPE_CSV);
        }
        $header = PowerGrid::header()
            ->withoutLoading()
            ->showToggleColumns();

        $header->includeViewOnTop('livewire.header.admin.machine-status');

        $actions[]=$header;
        $actions[]=PowerGrid::footer()->showPerPage()->showRecordCount();     
        return $actions;
    }

    public function datasource(): Builder
    {
        return MachineStatus::query()->latest();
    }

    public function relationSearch(): array
    {
        return [];
    }

    public function fields(): PowerGridFields
    {
        return PowerGrid::fields()
            ->add('id')
            ->add('hostname')
            ->add('ip')
            ->add('cpu')
            ->add('memory')
            ->add('disk')
            ->add('created_at');
    }

    public function columns(): array
    {
        $column=[];
        $column[]=Column::make('Id', 'id');
        $column[]=Column::make(__('machine-status.hostname'), 'hostname')->sortable()->searchable();
        $column[]=Column::make(__('machine-status.Ip'), 'ip')->sortable()->searchable();
        $column[]=Column::make(__('machine-status.Cpu'), 'cpu')->sortable()->searchable();
        $column[]=Column::make(__('machine-status.Memory'), 'memory')->sortable()->searchable();
        $column[]=Column::make(__('machine-status.Disk'), 'disk')->sortable()->searchable();
        $column[]=Column::make(__('machine-status.Created at'), 'created_at')->sortable()->searchable();
        
        return $column;
    }

    public function filters(): array
    {
        return [
            Filter::datetimepicker('created_at'),
            Filter::inputText('hostname')->placeholder(__('machine-status.hostname')),
            Filter::inputText('ip')->placeholder(__('machine-status.Ip')),
        ];
    }

}
