<?php

namespace App\DataTables;

use App\Models\Room;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Html\Editor\Editor;
use Yajra\DataTables\Html\Editor\Fields;
use Yajra\DataTables\Services\DataTable;

class RoomDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            ->addColumn('status', function($room){
                return  view('components.status', ['status'=>$room->status]);
             })
             ->addColumn('ship', function($room){
                return  $room->ship->name;
             })
             ->addColumn('type', function($room){
                return  $room->type->name;
             })
            ->addColumn('action', function ($room) {
                $acrtion = view('components.edit-link', ['href' => route('rooms.show', $room->id)]);
                $acrtion .= view('components.destroy-link', ['href' => route('rooms.destroy', $room->id)]);
                return $acrtion;
            });
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Room $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Room $model)
    {
        return $model->newQuery();
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
                    ->setTableId('room-table')
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->dom('Bfrtip')
                    ->orderBy(1)
                    ->buttons(
                        Button::make('export'),
                        Button::make('print'),
                        Button::make('reset'),
                        Button::make('reload')
                    );
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            Column::make('room_number')->title(__('Room Number')),
            Column::make('ship')->title(__('Ship')),
            Column::make('type')->title(__('Type')),
            Column::make('code')->title(__('Code')),
            Column::make('status')->title(__('Status')),
            Column::computed('action')->title(__('Action'))
            ->exroomable(false)
            ->printable(false)
            ->width(150)
            ->addClass('text-center'),
        ];
    }

    /**
     * Get filename for exroom.
     *
     * @return string
     */
    protected function filename()
    {
        return 'Room_' . date('YmdHis');
    }
}
