<?php

namespace App\DataTables;

use App\Models\Seats;
use Yajra\DataTables\Services\DataTable;
use Yajra\DataTables\EloquentDataTable;

class SeatsDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        $dataTable = new EloquentDataTable($query);

        $dataTable->addColumn('action', 'seats.datatables_actions');
        $dataTable->addColumn('status', function (Seats $seats) {
            if ($seats->status == 1) {
              return '<span class="badge  bg-label-success">Available</span>';
            }else if ($seats->status == 2) {
              return '<span class="badge  bg-label-danger">Reserved</span>';
            } else {
              return '<span class="badge  bg-label-warning">Inactive</span>';
            }
          })
          ->toJson();
         /*  $dataTable->addColumn('branch_id', function (Seats $seats) {
            return $seats->branch->name??'';
          })
          ->toJson(); */
      $dataTable->rawColumns(['status','action']);
      return $dataTable;
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Seats $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Seats $model)
    {
        return $model->newQuery()->where('branch_id',auth()->user()->branch_id)->orderBy('id','DESC');
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->addAction(['width' => '120px', 'printable' => false])
            ->parameters([
                'dom'       => 'Bfrtip',
                'stateSave' => true,
                'order'     => [[0, 'desc']],
                'initComplete' => 'function(settings, json) {
                    $(".dataTables_wrapper").css("overflow", "auto");
                }',
                'buttons'   => [
                    // Enable Buttons as per your need
//                    ['extend' => 'create', 'className' => 'btn btn-default btn-sm no-corner',],
//                    ['extend' => 'export', 'className' => 'btn btn-default btn-sm no-corner',],
//                    ['extend' => 'print', 'className' => 'btn btn-default btn-sm no-corner',],
//                    ['extend' => 'reset', 'className' => 'btn btn-default btn-sm no-corner',],
//                    ['extend' => 'reload', 'className' => 'btn btn-default btn-sm no-corner',],
                ],
            ]);
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'name',
/*             'branch_id'=>['title'=>'Branch'],
 */            'status'
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename(): string
    {
        return 'seats_datatable_' . time();
    }
}
