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

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

  /**
  * Build DataTable class.
  *
  * @param mixed $query Results from query() method.
  * @return \Yajra\DataTables\DataTableAbstract
  */
  public function dataTable($query)
  {
    return datatables()
    ->collection($this->issues)
    ->setRowId('id')
    ->addColumn('created_on_label', function($entity){
      try{
        return Carbon::parse($entity->created_on)->format('d/m/Y');
      }catch(\Exception $e){

      }
    })
    ->addColumn('action', function($entity){
      $button = "<a class='btn btn-primary' href='".route('issue.show', ['issue_id' => $entity->id])."'><i class='fas fa-chevron-right'></i></a>";
      return $button;
      return view('issue.menu', ['entity' => $entity]);
    });
  }

  /**
  * Optional method if you want to use html builder.
  *
  * @return \Yajra\DataTables\Html\Builder
  */
  public function html()
  {
    $buttons = [];

    return $this->builder()
    ->setTableId($this->dataTableVariable)
    ->columns($this->getColumns())
    // ->minifiedAjax()
    ->responsive()
    ->language(asset('plugins/datatables/Italian.json'))
    ->dom(count($buttons)==0?'rtip':'Brtip')
    ->orderBy(0, 'desc')
    ->pageLength(20)
    ->buttons($buttons);
    }

    /**
    * Get columns.
    *
    * @return array
    */
    protected function getColumns()
    {
      return [
        Column::make('id')->title('#')->responsivePriority(1),
        Column::make('project.name')->title('Cliente'),
        Column::make('created_on')->title('Data')->data('created_on_label'),
        Column::make('subject')->title('Oggetto'),
        Column::make('status.name')->title('Stato'),
        Column::computed('action')
        ->exportable(false)
        ->printable(false)
        ->width('10%')
        ->title(' ')
        ->responsivePriority(2)
        ->addClass('text-right'),
      ];
    }

  }
