<?php

namespace App\DataTables;

use App\Models\SecurityManagement\Role;
use Illuminate\Database\Eloquent\Builder as QueryBuilder;
use Yajra\DataTables\EloquentDataTable;
use Yajra\DataTables\Html\Builder as HtmlBuilder;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Services\DataTable;

class RoleDataTable extends DataTable{

    /**
     * Build DataTable class.
     *
     * @param QueryBuilder $query Results from query() method.
     * @return EloquentDataTable
     */
    public function dataTable(QueryBuilder $query): EloquentDataTable{
        return (new EloquentDataTable($query))
            ->addColumn('action', 'role.action')
            ->setRowId('id');
    }

    /**
     * Get query source of dataTable.
     *
     * @param Role $model
     * @return QueryBuilder
     */
    public function query(Role $model): QueryBuilder{
        return $model->newQuery()->latest("updated_at");
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return HtmlBuilder
     */
    public function html(): HtmlBuilder{
        return $this->builder()
                    ->setTableId('role-table')
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    //->dom('Bfrtip')
                    ->orderBy(1)
                    ->selectStyleSingle()
                    ->buttons([
                        Button::make('excel'),
                        Button::make('csv'),
                        Button::make('pdf'),
                        Button::make('print'),
                        Button::make('reset'),
                        Button::make('reload')
                    ]);
    }

    /**
     * Get the dataTable columns definition.
     *
     * @return array
     */
    public function getColumns(): array{
        return [
            Column::computed('action')
                  ->exportable(false)
                  ->printable(false)
                  ->width(60)
                  ->addClass('text-center'),
            Column::make('id'),
            Column::make('add your columns'),
            Column::make('created_at'),
            Column::make('updated_at'),
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename(): string
    {
        return 'Role_' . date('YmdHis');
    }

    /**
     * @inheritDoc
     */
    public function excel()
    {
        // TODO: Implement excel() method.
    }

    /**
     * @inheritDoc
     */
    public function csv()
    {
        // TODO: Implement csv() method.
    }

    /**
     * @inheritDoc
     */
    public function pdf()
    {
        // TODO: Implement pdf() method.
    }
}
