<?php

namespace App\DataTables;

use App\Models\PipeWgtMoc;
use Illuminate\Database\Eloquent\Builder as QueryBuilder;
use Yajra\DataTables\EloquentDataTable;
use Yajra\DataTables\Html\Builder as HtmlBuilder;
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 PipeWgtMocDataTable extends DataTable
{
    /**
     * Build the DataTable class.
     *
     * @param QueryBuilder<PipeWgtMoc> $query Results from query() method.
     */
    public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))
        ->addColumn('wgtname', function($query){
            // return $query->swgt;
            if($query->swgt == 5){
                return '5S Wgt (kg/m)';
            }elseif($query->swgt == 10){
                return '10S Wgt (kg/m)';
            }elseif($query->swgt == 40){
                return '40S Wgt (kg/m)';
            }elseif($query->swgt == 80){
                return '80S Wgt (kg/m)';
            }elseif($query->swgt == 120){
                return '120S Wgt (kg/m)';
            }elseif($query->swgt == 160){
                return '160S Wgt (kg/m)';
            }else{
                return '0';
            }
        })
        ->addColumn('action', function($query){
            // view Button
            $viewButton = "";
            $viewButton = '<a href="'.route('admin/pipewgtmoc/show',$query->id).'" class="btn btn-info btn-sm"><i class="lni lni-eye"></i></a>';
            // Update Button
            $updateButton = "";
            $updateButton = '<a href="'.route('admin/pipewgtmoc/edit',$query->id).'" class="btn btn-info btn-sm"><i class="lni lni-pencil-alt"></i></a>';
            // Delete Button
            $deleteButton = "";
            $deleteButton = "<a class='delete btn btn-danger btn-sm' title='Delete' data-toggle='tooltip' data-id='".$query->id."'><i class='lni lni-trash'></i></a>";
            return $viewButton." ".$updateButton." ".$deleteButton;
        })
        ->rawColumns(['wgtname','action']);
    }

    /**
     * Get the query source of dataTable.
     *
     * @return QueryBuilder<PipeWgtMoc>
     */
    public function query(PipeWgtMoc $model): QueryBuilder
    {
        return $model->newQuery();
    }

    /**
     * Optional method if you want to use the html builder.
     */
    public function html(): HtmlBuilder
    {
        return $this->builder()
                    ->setTableId('pipewgtmoc-table')
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->orderBy(1)
                    ->selectStyleSingle()
                    ->buttons([
                        // Button::make('excel'),
                        // Button::make('csv'),
                        // Button::make('pdf'),
                        // Button::make('print'),
                        // Button::make('reset'),
                        // Button::make('reload')
                    ]);
    }

    /**
     * Get the dataTable columns definition.
     */
    public function getColumns(): array
    {
        return [
            'nbname' => ['data' => 'nbname', 'name' => 'nbname', 'title' => 'NB Name'],
            'wgtname' => ['data' => 'wgtname', 'name' => 'wgtname', 'title' => 'Wgt Name'],
            'wgt' => ['data' => 'wgt', 'name' => 'wgt', 'title' => 'Wgt (kg/m)'],
            'moc' => ['data' => 'moc', 'name' => 'moc', 'title' => 'WEIGHT FO DIFF MOC'],
            Column::computed('action')
                  ->exportable(false)
                  ->printable(false)
                  ->width(120)
                  ->addClass('text-center'),
            // Column::make('id'),
            // Column::make('add your columns'),
            // Column::make('created_at'),
            // Column::make('updated_at'),
        ];
    }

    /**
     * Get the filename for export.
     */
    protected function filename(): string
    {
        return 'PipeWgtMoc_' . date('YmdHis');
    }
}
