<?php

namespace App\DataTables;

use App\Models\Booking;
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 BookingDataTable 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('created_at', function($booking){
                return $booking->created_at->format('Y-m-d h:i');
             })
             ->addColumn('total', function($booking){
                return formatPrice($booking->total);
             })
            ->addColumn('action', function ($booking) {
                $action = view('components.link', ['href' => route('booking.print', $booking->id),'class'=>'print-link btn btn-info btn-sm','icon'=>'print']);
                  
              if (($booking->status == Booking::‫‪STATE_OPEN‬‬ && $booking->status !== Booking::STATE_‫‪EXCHANGED‬‬) && Gate::allows('add_booking') ) {
                    $date = $booking->created_at;
                    $bookings_ids=Booking::where('office_id',auth()->user()->office_id)->pluck('id')->toarray();
                     $booking_refunds= $booking->tickets->pluck('ticket_status')->toarray();
                    if ($date->endOfDay() > now() ) {
                         if(in_array($booking->id,$bookings_ids) &&!in_array('R',$booking_refunds))
                        { 
                           
                            $action .= view('components.link_booking_modal', ['href' => route('booking.refund', $booking->id),'class'=>'refund-link btn btn-danger btn-sm','icon'=>'ban','label'=>'Void','id'=> $booking->id,'booking'=>$booking]);
                        }
                    } 
                  
                   
                }
                return $action;
            });
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Booking $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Booking $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('booking-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('number')->title(__('Number')),
            Column::make('total')->title(__('total')),
            Column::make('created_at')->title(__('created At')),
            Column::computed('action')->title(__('Action'))
            ->exportable(false)
            ->printable(false)
            ->width(200)
            ->addClass('text-center'),
        ];
    }

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