<?php

namespace App\DataTables;

use App\Models\Office;
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 OfficesDataTable 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('image', function($office){
                return view('components.image', ['src'=>$office->public_image]);
             })
             ->addColumn('action', function($role){
                 $acrtion = view('components.edit-link', ['href'=>route('offices.edit',$role->id)]);
                 $acrtion .= view('components.destroy_office', ['href'=>route('office.destroy',$role->id) ,'id'=>$role->id]);
                 return $acrtion;
             });
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Office $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Office $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('offices-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('id')->title(__('Id')),
            Column::make('image')->title(__('Image')),
            Column::make('name')->title(__('Name')),
            Column::make('email')->title(__('Email')),
            Column::make('phone')->title(__('phone')),
            Column::make('booking_commission')->title(__('Booking Commission')),
            Column::make('exchange_commission')->title(__('Exchange Commission')),
            Column::make('canellation_commission')->title(__('Canellation Commission')),
            Column::computed('action')->title(__('Action'))
            ->exportable(false)
            ->printable(false)
            ->width(150)
            ->addClass('text-center'),
        ];
    }

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