<?php

namespace App\DataTables;

use App\Models\SegmentService;
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 SegmentServiceDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        $query = SegmentService::with(['segment']);
        return datatables()
        ->eloquent($query)
        ->filterColumn('subject', function($query, $search) {
            if($search == 'car'){
                $query->whereNotNull('car_id');
            } 
        })
        ->filterColumn('cruise', function($query, $search) {
            $query->whereHas('segment.cruise',function($query) use($search){
                $query->where('cruise_number','like',$search);
            });
        })
        ->addColumn('cruise', function ($service) {
            return  $service->segment->cruise->cruise_number;
        })
        ->addColumn('booking_number', function ($service) {
            return  $service->segment->booking->number;
        })
        ->addColumn('office', function ($service) {
            return  $service->segment->booking->office->name;
        })
        ->addColumn('subject', function ($v) {
            if ($v->car) return 'Car: '.$v->car->name;
            if ($v->article) return 'Article: '.$v->article->name;
        })
        ->addColumn('created_at', function ($service) {
            return $service->created_at->format('Y-m-d h:i');
        })
        ->addColumn('price', function ($service) {
            return formatPrice($service->price);
        })
        ->addColumn('action', function ($service) {
            $action = '';
            if($service->car)$action = view('components.link', ['href' => route('segment-services.print', $service->id),'class'=>'print-link btn btn-info btn-sm','icon'=>'print']);

            return $action;
        });
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\SegmentService $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(SegmentService $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('segmentservice-table')
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->dom('Bfrtip')
                    ->orderBy(0)
                    ->buttons(
                        Button::make('export'),
                        Button::make('print'),
                        Button::make('reset'),
                        Button::make('reload')
                    );
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            Column::make('id')->title(__('Id')),
             Column::make('cruise')->title(__('Cruise')),
            Column::make('booking_number')->title(__('Booking Number')),
            Column::make('ticket_number')->title(__('Ticket Number')),
            Column::make('office')->title(__('Office')),
            Column::make('subject')->width(150)->title(__('Subject'))->searchable(true),
            Column::make('price')->title(__('Price')),
            Column::make('created_at')->title(__('Created At')),
            Column::computed('action')->title(__('Action'))
                ->exportable(false)
                ->printable(false)
                ->width(150)
                ->addClass('text-center'),
        ];
    }

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