<?php

namespace App\DataTables;

use App\Models\Cruise;
use Illuminate\Support\Facades\Gate;
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 CruisesDataTable 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('cruise',function($cruise){
                return $cruise->name.'-'.$cruise->cruise_number;
            })
            ->addColumn('route',function($cruise){
                return $cruise->route;
            })
            ->addColumn('departure_date',function($cruise){
                return $cruise->departure_date_time->format('Y-m-d h:i a');
            })
            ->addColumn('arrival_date',function($cruise){
                return $cruise->arrival_date_time->format('Y-m-d h:i a');
            })
            ->addColumn('status',function($cruise){
                return  view('components.status', ['status'=>$cruise->status]);
            })
            ->addColumn('action', function($cruise){
                $action = '';
                if(Gate::allows('add_boarding')&& $cruise->status == "1"){
                    $action .= view('components.link', ['href'=>route('boarding-pass.index',['id'=>$cruise->id]), 'class'=>'btn btn-primary btn-sm', 'icon'=>'bed','label'=>"boarding"]);
                }
                if(Gate::allows('check_in')){
                    $action .= view('components.link', ['href'=>route('check_in.index',['cruise'=>$cruise->id]), 'class'=>'mx-1 btn btn-success btn-sm', 'icon'=>'check','label'=>"checkIn"]);
                }
                if(Gate::allows('add_cruise')){
                    $action .= view('components.link', ['href'=>route('check_out',['cruise'=>$cruise->id]), 'class'=>'mx-1 btn btn-danger btn-sm', 'icon'=>'check','label'=>"checkOut"]);
                 }
                if(Gate::allows('all_permissions')){
                    $action .= view('components.edit-link', ['href'=>route('cruises.show',$cruise->id)]);
                    $action .= view('components.destroy-link', ['href'=>route('cruises.destroy',$cruise->id)]);
                    $action .= view('components.browse-link', ['href'=>route('cruises.show',$cruise->id)]);
                }
                
                return $action;
                
            });
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Cruise $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Cruise $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('cruises-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('cruise')->title(__('Cruise')),
            Column::make('route')->title(__('Route')),
            Column::make('departure_date')->title(__('Departure Date')),
            Column::make('arrival_date')->title(__('Arrival Date')),
            Column::make('status')->title(__('Status')),
            Column::computed('action')->title(__('Action'))
            ->exportable(false)
            ->printable(false)
            ->width(250)
            ->addClass('text-center'),
        ];
    }

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