<?php

namespace App\DataTables;

use Yajra\DataTables\Services\DataTable;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Html\Editor\Editor;
use Yajra\DataTables\Html\Editor\Fields;
use App\Models\User;
use Auth;
use Form;

class UserDataTable extends DataTable
{
  public function __construct(){
    $this->dataTableVariable = 'dataTable_user';
  }

  /**
  * Build DataTable class.
  *
  * @param mixed $query Results from query() method.
  * @return \Yajra\DataTables\DataTableAbstract
  */
  public function dataTable($query)
  {
    return datatables()
    ->eloquent($query)
    ->setRowId('id')
    ->addColumn('action', function($entity){
      return view('user.menu', ['entity' => $entity]);
    })
    ->rawColumns(['action']);
  }


  /**
  * Get query source of dataTable.
  *
  * @param \App\Models\User $model
  * @return \Illuminate\Database\Eloquent\Builder
  */
  public function query(User $model)
  {
    return $model->newQuery();
  }

  /**
  * Optional method if you want to use html builder.
  *
  * @return \Yajra\DataTables\Html\Builder
  */
  public function html()
  {
    $buttons = [];
    if(Auth::user()->can('create-users')){
      array_push(
        $buttons,
        Button::make('create')
        ->editor('editor')
        ->className('btn btn-sm btn-primary')
        ->text('<i class="fas fa-plus"></i> Nuovo utente')
        ->formTitle('Nuovo utente')
        ->formButtons(
          [Button::raw('Annulla')
          ->className('btn btn-secondary ml-2')
          ->actionClose(),
          Button::raw('Salva')
          ->className('btn btn-success ml-2')
          ->actionHandler('create')
        ])
      );
    }

    return $this->builder()
    ->setTableId($this->dataTableVariable)
    ->columns($this->getColumns())
    ->minifiedAjax(route('user.index'), null, [])
    ->language(asset('plugins/datatables/Italian.json'))
    // ->select()
    ->dom('Brtip')
    ->orderBy(1, 'asc')
    ->buttons($buttons)
    ->editor(
      Editor::make()
      ->fields([
        Fields\Text::make('nome', 'Nome'),
        Fields\Text::make('cognome', 'Cognome'),
        Fields\Text::make('email', 'Email'),
        Fields\Password::make('password_new', 'Password (lasciare vuoto per non modificare)'),
        Fields\Password::make('password_new_confirmation', 'Conferma password')
      ])
      ->on('submitComplete', "function( e, mode, action ){}"
        )
      )->initComplete("function(settings, json){
        initComplete();
      }");
    }

    /**
    * Get columns.
    *
    * @return array
    */
    protected function getColumns()
    {
      return [
        Column::make('id')->title('ID')->width('10%')->visible(false),
        Column::make('nome')->title('Nome'),
        Column::make('cognome')->title('Cognome'),
        Column::make('email')->title('Email'),
        Column::computed('action')
        ->exportable(false)
        ->printable(false)
        ->title(' ')
        ->addClass('text-right'),
      ];
    }

  }
