<?php

namespace App\DataTables;

use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\Rule;
use Yajra\DataTables\DataTablesEditor;
use Hash;

class UserDataTableEditor extends DataTablesEditor
{
  protected $model = User::class;

  /**
  * Get create action validation rules.
  *
  * @return array
  */
  public function createRules()
  {
    return [
      'nome'  => 'required',
      'email' => 'required|email|unique:users,email'
    ];
  }

  /**
  * Get edit action validation rules.
  *
  * @param Model $model
  * @return array
  */
  public function editRules(Model $model)
  {
    return [
      'nome'  => 'required',
      'email' => 'required|email|unique:users,email,'.$model->id,
      'password_new' => 'nullable|confirmed|min:6'
    ];
  }

  /**
  * Get remove action validation rules.
  *
  * @param Model $model
  * @return array
  */
  public function removeRules(Model $model)
  {
    return [];
  }

  public function creating(Model $model, array $data)
  {
    $data['password'] = Hash::make($data['password_new']);
    return $data;
  }

  public function updating(Model $model, array $data)
  {
    //reset password
    if($data['password_new'] != null && $data['password_new'] != ''){
      $data['password'] = Hash::make($data['password_new']);
    }

    return $data;
  }
}
