<?php

namespace App\DataTables;

use App\Models\Ticket;
use App\Models\TicketReport;
use Carbon\Carbon;
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 TicketReportDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        $from = Carbon::parse(sprintf(
            '%s-%s-01',
            request()->query('y', Carbon::now()->year),
            request()->query('m', Carbon::now()->month)
        ));
        $to      = clone $from;
        $to->day = $to->daysInMonth;
        $tickets = Ticket::with('segment.booking')->whereBetween('created_at', [$from, $to]);

        return datatables()
            ->eloquent($tickets)
            ->addColumn('booking_number', function ($ticket) {
                return  $ticket->segment->booking->number;
            })
            ->addColumn('full_name', function ($ticket) {
                return  $ticket->full_name;
            })
            ->addColumn('office', function ($ticket) {
                return  $ticket->segment->booking->office->name;
            })
            ->addColumn('created_at', function ($ticket) {
                return $ticket->created_at->format('Y-m-d');
            })
            ->addColumn('price', function ($ticket) {
                return formatPrice($ticket->price);
            })
            ->addColumn('total', function ($ticket) {
                //return formatPrice($ticket->price);
            });
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\TicketReport $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('ticketreport-table')
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->dom('Bfrtip')
                    ->orderBy(1)
                    ->buttons(
                        Button::make('create'),
                        Button::make('export'),
                        Button::make('print'),
                        Button::make('reset'),
                        Button::make('reload')
                    );
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            Column::make('created_at')->title(__('Date')),
            Column::make('booking_number')->title(__('Booking Number')),
            Column::make('ticket_number')->title(__('Ticket Number')),
            Column::make('full_name')->title(__('Name Passenger')),
            Column::make('office')->title(__('Office')),
            Column::make('ticket_status')->title(__('Status')),
            Column::make('price')->title(__('Price')), 
            Column::make('total')->title(__('Total')),  
        ];
    }

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