<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;

use Carbon\Carbon;
use Hash;

class User extends Authenticatable
{
   // use SoftDeletes;
	
	protected $dates = ['deleted_at', 'birthday', 'nauticaltitleexpedicion', 'nauticaltitleexpiration'];
	
	protected $image_fields = ['profile_picture'];
	
	/**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
		'first_name',
		'last_name',
		'fiscal_reason',
		'tradename',
		'cif',
		'fiscal_address',
		'birthday',
		'gender',
		'email',
		'telephone',
		'cellphone',
		'local_address',
		'permanent_address',
		'nauticaltitle',
		'nauticaltitleexpedicion',
		'nauticaltitleexpiration',
		'sendemail',
		'sendsms',
		'role_id',
		'username',
		'password',
		'profile_picture',
		'comment',
		'customer_id'
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
	
	public function customer()
	{
	    return $this->belongsTo('App\Customer');    
	}

	public function incidence()
	{
		return $this->hasOne(Incidence::class);
	}
	
	public function reservation()
	{
		return $this->hasMany(Reservation::class);
	}

	public function credit_user()
	{
		return $this->hasMany(CreditUser::class);
	}

	
	public function documents()
	{
		return $this->hasMany('App\Document');
	}
	
	public function role()
	{
		return $this->belongsTo('App\Role');
	}
	
	public function gender_text()
	{
		return $this->gender == 'M' ? 'Masculino' : 'Femenino';
	}

	public function sendemail_text()
	{
		return $this->sendemail == '0' ? 'No' : 'Sí';
	}

	public function sendsms_text()
	{
		return $this->sendsms == '0' ? 'No' : 'Sí';
	}
	
	public function setBirthdayAttribute($birthday){
		$this->attributes['birthday'] = Carbon::parse($birthday);
	}
	
	public function getBirthdayAttribute($birthday){
		return Carbon::parse($birthday)->format('m/d/Y');
	}
	
	public function setNauticalTitleExpedicionAttribute($nauticaltitleexpedicion){
		$this->attributes['nauticaltitleexpedicion'] = Carbon::parse($nauticaltitleexpedicion);
	}
	
	public function getNauticalTitleExpedicionAttribute($nauticaltitleexpedicion){
		return $nauticaltitleexpedicion ? Carbon::parse($nauticaltitleexpedicion)->format('m/d/Y') : '';
	}
	
	public function setNauticalTitleExpirationAttribute($nauticaltitleexpiration){
		$this->attributes['nauticaltitleexpiration'] = $nauticaltitleexpiration != '' ? Carbon::parse($nauticaltitleexpiration) : NULL;
	}
	
	public function getNauticalTitleExpirationAttribute($nauticaltitleexpiration){
		return $nauticaltitleexpiration != NULL ? Carbon::parse($nauticaltitleexpiration)->format('m/d/Y') : 'present';
	}
	
	public function setPasswordAttribute($value)
	{
		$this->attributes['password'] = empty($value)
		? $this->password
		: Hash::make($value);
	}
	
	public function setProfilePictureAttribute($value)
	{
		
		
		if(empty($value)){
			$this->attributes['profile_picture'] = '';
		}else{
			$ext = $value->getClientOriginalExtension();
		    $destinationPath = public_path('dist/img/users'); // upload path
			$fileName2 = str_random(12).'.'.$ext; // renameing image
			$file = $value->move($destinationPath, $fileName2); // uploading file to given path
			\Image::make($file)->resize(120, null, function ($constraint) {
			$constraint->aspectRatio();
			})->crop(120, 120)->save($file);
            $this->attributes['profile_picture'] = $fileName2;
		}
	}
	
	public function getProfilePictureAttribute($value)
	{
		return $value != NULL ? $value : 'default.jpg';
	}

 	public function getCompleteName()
	{
			return $this->first_name . ' - ' . $this->last_name;
	}
	
	public function isAdmin() {
		info($this->role_id);
		return ($this->role_id == 1);
	}
}
