<?php

namespace App\DataTables;

use App\Models\Service;
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 ServiceDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        $query = Service::with(['roomType','car','article']);
        return datatables()
            ->eloquent($query)
            ->addColumn('ship', function ($v) {
                return $v->ship->name . '-' . $v->ship->ship_number;
            })
            ->addColumn('destination', function ($v) {
                return $v->origin_port . '-' . $v->destination_port;
            })
            ->addColumn('subject', function ($v) {
                if ($v->roomType) return 'Room: '.$v->roomType->name;
                if ($v->car) return 'Car: '.$v->car->name;
                if ($v->article) return 'Article: '.$v->article->name;
            })
            ->addColumn('price', function ($v) {
                return formatPrice($v->price);
            })
            ->addColumn('price_two', function ($v) {
                return formatPrice($v->price_two);
            })
            ->addColumn('status', function ($car) {
                return  view('components.status', ['status' => $car->status]);
            })
            ->addColumn('action', function ($service) {
                $action = view('components.destroy-link', ['href' => route('services.destroy', $service->id)]);
                if($service->status==true){
                    
                    $action .= '<input  data-id='.$service->id.' type="checkbox" name="my-checkbox" checked data-bootstrap-switch data-off-color="danger" data-on-color="success">'; 
                }else{
                    $action .= '<input  data-id='.$service->id.' type="checkbox" name="my-checkbox"  data-bootstrap-switch data-off-color="danger" data-on-color="success">'; 
                }
                return $action;
            });
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Service $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Service $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('service-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('ship')->title(__('Ship')),
            Column::make('destination')->title(__('Destination')),
            Column::make('subject')->width(150)->title(__('Subject')),
            Column::make('price')->title(__('Price')),
            Column::make('price_two')->title(__('price Two')),
            Column::make('qty')->title(__('Qty')),
            Column::make('status')->title(__('Status')),
            Column::computed('action')->title(__('Action'))
                ->exportable(false)
                ->printable(false)
                ->width(100)
                ->addClass('text-center'),
        ];
    }

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