<?php

namespace App\DataTables;

use App\Models\Ticket;
use App\Models\Setting;
use App\Models\Office;
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 TicketDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
         $query = Ticket::with(['segment'])->withoutGlobalScope('status');
        return datatables()
            ->eloquent($query)
            ->filterColumn('cruise', function($query, $search) {
                $query->whereHas('segment.cruise',function($query) use($search){
                    $query->where('cruise_number','like','%'.$search.'%');
                });
            
               
             })
            ->addColumn('booking_number', function ($ticket) {
                return  $ticket->segment->booking->number;
            })
            ->addColumn('office', function ($ticket) {
                return  optional($ticket->segment->booking->office)->name;
            })
            ->addColumn('status', function ($ticket) {
                return  view('components.status', ['status' => $ticket->status]);
            })
            ->addColumn('cruise', function ($ticket) {
                return  $ticket->segment->cruise->cruise_number;
            })
            ->addColumn('phone', function ($ticket) {
                return  $ticket->phone;
            })
            ->addColumn('created_at', function ($ticket) {
                return $ticket->created_at->format('Y-m-d h:i');
            })
            ->addColumn('price', function ($ticket) {
                return formatPrice($ticket->price);
            })
            
            ->addColumn('total', function ($ticket) {
                return formatPrice($ticket->total);
            })
            ->addColumn('status', function ($ticket) {
                if($ticket->hasRetour()){
                    return 'two way';

                }
                else{
                    
                return 'one way';
                }
            })
            ->addColumn('action', function ($ticket) {
                $action = '';
                // $value__refund=Setting::find(7)->value;
                // $value__resiue=Setting::find(8)->value;
                // dd($value__refund,$value__resiue);
                
                //  dd(config('booking.tax_reissue'));

                  $tikects=   Ticket::whereHas('segment.booking', function ($query) {
                        $query->where('office_id', auth()->user()->office->id);
                    })->pluck('ticket_number')->toarray();
                
                    // dd()

                                    
                //   dd( array_values($tikects));
                   $office_id= auth()->user()->office_id;
                
               if($ticket->ticket_status == Ticket::‫‪STATE_OPEN‬‬  && $ticket->old_id == null  && Gate::allows('add_booking')){
                    $date = $ticket->created_at;
                    if ($date->endOfDay() > now() ) {
                               $office=Office::find($office_id);
                                if($office->is_admin ==1)
                                {
                                     if(in_array($ticket->ticket_number ,$tikects))
                                    {
                                            if($ticket->hasRetour() && $ticket->ticket_status =='U'){
                                                  $action='';
                                              }
                                        else{
                                                if($ticket->hasRetour()){
                                                    $action=''; 
                                                }
                                                else{
                                                    
                                                    $action .= view('components.link_modal', ['href' => route('ticket.void', $ticket->ticket_number),'class'=>'refund-link btn btn-danger btn-sm','icon'=>'ban','label'=>'Void','id'=> $ticket->id,'ticket'=>$ticket]);
                                                }
                                        }
                    
                                        
                                    }
                                 
                                }
                                else{
                                      if(in_array($ticket->ticket_number ,$tikects))
                                         {
                                                  
                                            if($ticket->hasRetour() && $ticket->ticket_status =='U'){
                                                $action='';
                                            }
                                            else{
                                              
                                                       if($ticket->hasRetour()){
                                                             $action='';
                                                       }
                                                       else{
                                                           
                                                          $action .= view('components.link_modal', ['href' => route('ticket.void', $ticket->ticket_number),'class'=>'refund-link btn btn-danger btn-sm','icon'=>'ban','label'=>'Void','id'=> $ticket->id,'ticket'=>$ticket]);
                                                       }
                                                 
                                                
                                              }
                                         }
                                         else{
                                               $action='';
                                         }
                                }
                    } else {
                        //if(auth()->id()== 33){
                            $date2 = $date1 = $ticket->segment->cruise->departure_date_time;
                            if($date1->modify('-5 hours')> now())
                            {   
                                $office=Office::find($office_id);
                                if($office->is_admin == 1)
                                {
                                    // dd($ticket);
                                     if( in_array($ticket->ticket_number ,$tikects))
                                    {
                                        if($ticket->hasRetour() && $ticket->ticket_status =='U'){
                                            $action='';
                                        }
                                        else{
                                            
                                             $action .= view('components.link_modal', ['href' => route('ticket.refund', $ticket->ticket_number),'class'=>'refund-link btn btn-danger btn-sm','icon'=>'ban','label'=>'Refund','id'=>$ticket->id ,'ticket'=>$ticket]);
                                        }
                                    }
                                 
                                }
                                else{
                                    if( in_array($ticket->ticket_number ,$tikects))
                                    {
                                         if($ticket->hasRetour() && $ticket->ticket_status =='U'){
                                                $action='';
                                            }
                                            else{
                                                  
                                                 $action .= view('components.link_modal', ['href' => route('ticket.refund', $ticket->ticket_number),'class'=>'refund-link btn btn-danger btn-sm','icon'=>'ban','label'=>'Refund','id'=>$ticket->id,'ticket'=>$ticket]);
                                                     
                                            }
                                    }
                                    else{
                                          $action='';
                                    }
                                }
                        
                            
                                
                            }
                            // if($date2->modify('-1 hour')> now())
                            // {
                              $office=Office::find($office_id);

                                  
                                if($office->is_admin == 1){
                                    
                                  $action .= view('components.link', ['href' => route('tickets.edit', $ticket->id),'class'=>'btn btn-primary btn-sm mx-1','icon'=>'ban','label'=>'Reissue']);
                                }
                                else{
                                    if( in_array($ticket->ticket_number ,$tikects))
                                    {
                                      $action .= view('components.link', ['href' => route('tickets.edit', $ticket->id),'class'=>'btn btn-primary btn-sm mx-1','icon'=>'ban','label'=>'Reissue']);
                                        
                                    }
                                }
                                 }
                                
                            
                        
                       
                    // }
                }elseif($ticket->ticket_status == Ticket::‫‪STATE_OPEN‬‬ && $ticket->old_id !==null && Gate::allows('add_booking')){
                    $date = $ticket->created_at;
                    $date22 = $date11 = $ticket->segment->cruise->departure_date_time;
                    if ($date->endOfDay() < now() ) {
                        // dd(array_values($tikects));
                              $office=Office::find($office_id);
                                if($office->is_admin ==1)
                                {
                                     if( in_array($ticket->ticket_number ,$tikects))
                                    {
                                         if($ticket->hasRetour() && $ticket->ticket_status =='U'){
                                            $action='';
                                        }
                                        else{
                                            
                                           $action .= view('components.link_modal', ['href' => route('ticket.refund', $ticket->ticket_number),'class'=>'refund-link btn btn-danger btn-sm','icon'=>'ban','label'=>'Refund','id'=>$ticket->id,'ticket'=>$ticket]);
                                        }
                                    }
                                    
                                 
                                }
                                else{
                                      if($ticket->hasRetour() && $ticket->ticket_status =='U' ){
                                            $action='';
                                        }
                                        else{
                                                 if(in_array($ticket->ticket_number ,$tikects))
                                               {
                                                 $action .= view('components.link_modal', ['href' => route('ticket.refund', $ticket->ticket_number),'class'=>'refund-link btn btn-danger btn-sm','icon'=>'ban','label'=>'Refund','id'=>$ticket->id,'ticket'=>$ticket]);
                                               }
                                               else{
                                                         $action='';

                                               }
                                          
                                        }
                                }
                        
                                                    
                          if ($date22->modify('-1 hours')> now()){
                                                      $office=Office::find($office_id);


                            if($office->is_admin == 1){
                             $action .= view('components.link', ['href' => route('tickets.edit', $ticket->id),'class'=>'btn btn-primary btn-sm mx-1','icon'=>'ban','label'=>'Reissue']);
                            }
                            else{
                                  if(in_array($ticket->ticket_number ,$tikects))
                                 {
                                     
                                    $action .= view('components.link', ['href' => route('tickets.edit', $ticket->id),'class'=>'btn btn-primary btn-sm mx-1','icon'=>'ban','label'=>'Reissue']);
                                     
                                 }
                            }
                          } 
                        
                        
                    }
                    
                }
                $action .= view('components.browse-link', ['id'=>$ticket->id]);
                if($ticket->ticket_status !='U'){
                     if(in_array($ticket->ticket_number ,$tikects)){
                         
                        $action .= view('components.edit-link', ['href'=>route('edit_passenger', $ticket->id)]);
                     }
                }
                else{
                   $action=''; 
                }
                return $action;
            });
    }


    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Ticket $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Ticket $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('ticket-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('booking_number')->title(__('Booking Number')),
            Column::make('ticket_number')->title(__('Ticket Number')),
            Column::make('office')->title(__('Office')),
            Column::make('cruise')->title(__('Cruise')),
            Column::make('phone')->title(__('Phone')),
            Column::make('ticket_status')->title(__('Status')),
            Column::make('price')->title(__('Price')),
            Column::make('total')->title(__('Total')),
            Column::make('status')->title(__('Tickect Type')),
            Column::make('created_at')->title(__('Created At')),
            Column::computed('action')->title(__('Action'))
                ->exportable(false)
                ->printable(false)
                ->width(280)
                ->addClass('text-center'),
        ];
    }

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