<?php

namespace App\Models;


use Illuminate\Database\Eloquent\Model;

class Device extends Model
{
    protected $table = 'devices';
    protected $fillable = ['serial', 'brand', 'color', 'notice', 'photo', 'state', 'type', 'extentions', 'done_notice'];

    public function setBrandAttribute($value)
    {
        if(is_null($value))
            $this->attributes['brand'] = 'غير محدد';
        else
            $this->attributes['brand'] = $value;
    }
    public function setColorAttribute($value)
    {
        if(is_null($value))
            $this->attributes['color'] = 'غير محدد';
        else $this->attributes['color'] = $value;
    }
    public function setNoticeAttribute($value)
    {
        if(is_null($value))
            $this->attributes['notice'] = 'غير محدد';
        else $this->attributes['notice'] = $value;
    }
    public function setPhotoAttribute($value)
    {
        if(is_null($value))
            $this->attributes['photo'] = '#';
        else $this->attributes['photo'] = $value;
    }

    public function getType(){
        if($this->type === 1) return 'computer';
        else return 'mobile';
    }

    public function customer(){
        return $this->belongsTo(Customer::class, 'id', 'device_id');
    }


    public function setExtentionsAttribute($value)
    {
        $this->attributes['extentions'] = json_encode($value);
    }

    public function getExtentionsAttribute($value)
    {
        return $this->attributes['extentions'] = json_decode($value);
    }
}
