<?php

namespace App\DataTables;

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

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Refund $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Refund $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('refund-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 'Refund_' . date('YmdHis');
    }
}
