<?php

namespace App\DataTables;

use App\Models\Payement;
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 PayementDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        $query = Payement::with(['boardingPass','booking']);
        return datatables()
            ->eloquent($query)
            ->filterColumn('subject', function($query, $search) {
                $query->whereHas('booking',function($query) use($search){
                    $query->where('number','like','%'.$search.'%');
                });
               
                $query->orWhereHas('boardingPass.ticket',function($query) use($search){
                    $query->where('ticket_number','like','%'.$search.'%');
                });
               
             })
            ->addColumn('subject', function ($v) {
                if ($v->booking) return 'Booking: '.$v->booking->number;
                if ($v->boardingPass&&$v->boardingPass->ticket) return 'Boarding Pass: '.$v->boardingPass->ticket->ticket_number;
            })
            ->addColumn('amount', function ($payement) {
                return formatPrice($payement->amount);
            })
            ->addColumn('created_at', function ($account) {
                return $account->created_at->format('Y-m-d h:i');
            })
            ->addColumn('status', function ($payement) {
                return view('components.badge',['status'=> $payement->status]);
            })
            ->addColumn('action', function($payement){
                $action ='';
               if($payement->status == Payement::STATUS_PENDING){
                 $action = view('components.link', ['href' => route('payements.update', $payement->id),'class'=>'payement-link btn btn-success btn-sm','icon'=>'dollar-sign','label'=>'Payement']);
               }else{
                $action = view('components.link', ['href' => route('payements.show', $payement->id),'class'=>'print-link btn btn-info btn-sm','icon'=>'print','label'=>'Print']);
               }
               $action .= view('components.browse-link', ['id'=>$payement->id]);
               return $action;
            });
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Payement $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Payement $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('payement-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('subject')->title(__('Subject')),
            Column::make('amount')->title(__('Amount')),
            Column::make('status')->title(__('Status')),
            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 'Payement_' . date('YmdHis');
    }
}
