<?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\Role;
use Auth;
use Form;

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

  /**
  * 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('role.menu', ['entity' => $entity]);
    });
  }


  /**
  * Get query source of dataTable.
  *
  * @param \App\Role $model
  * @return \Illuminate\Database\Eloquent\Builder
  */
  public function query(Role $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-roles-permissions')){
      array_push($buttons, Button::make('create')
      ->editor('editor')
      ->className('btn btn-sm btn-primary')
      ->formTitle('Crea nuovo ruolo')
      ->formButtons(
        [Button::raw('Annulla')
        ->className('btn btn-secondary ml-2')
        ->actionClose(),
        Button::raw('Salva')
        ->className('btn btn-success ml-2')
        ->actionHandler('create')]
        )
      ->text('<i class="fas fa-plus"></i> Nuovo ruolo'));
    }

    return $this->builder()
    ->setTableId($this->dataTableVariable)
    ->columns($this->getColumns())
    ->minifiedAjax()
    ->language(asset('plugins/datatables/Italian.json'))
    ->dom(count($buttons)==0?'rtip':'Brtip')
    ->orderBy(1, 'ASC')
    ->buttons($buttons)
    ->editor(
      Editor::make()
      ->fields([
        Fields\Text::make('display_name', 'Nome'),
        Fields\Hidden::make('guard_name')->default('web'),
        Fields\Hidden::make('name')
      ])
      ->on('preSubmit', "function(e, data, action){
        if(action == 'create'){
          data.data[0]['name'] = convertToSlug(data.data[0]['display_name']);
        }
      }")
      ->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('display_name')->title('Nome ruolo'),
        Column::computed('action')
        ->exportable(false)
        ->printable(false)
        ->width('10%')
        ->title(' ')
        ->addClass('text-right'),
      ];
    }

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

  }
