<?php

namespace App\DataTables;

use App\Models\Port;
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 PortDataTable 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($port){
                return  view('components.status', ['status'=>$port->status]);
             })
            ->addColumn('action', function ($port) {
                $acrtion = view('components.edit-link', ['href' => route('ports.show', $port->code)]);
                $acrtion .= view('components.destroy-link', ['href' => route('ports.destroy', $port->code)]);
                return $acrtion;
            });
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Port $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Port $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('port-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('code')->title(__('Code')),
            Column::make('name')->title(__('Name')),
            Column::make('country')->title(__('Country')),
            Column::make('status')->title(__('Status')),
            Column::computed('action')->title(__('Action'))
            ->exportable(false)
            ->printable(false)
            ->width(150)
            ->addClass('text-center'),
        ];
    }

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