<?php

namespace App\Models;

use App\ModelsNotificationImage;
use Illuminate\Database\Eloquent\Model;

class Notification extends Model
{
    //
    const NOT_PUSH = 0;
    const PUSHED = 1;

    const NOT_READ = 0;
    const READ = 1;

    protected $fillable = [
        'to_user',
        'hostel_id',
        'room_id',
        'content',
        'title',
        'status',
        'is_read',
        'user_id',
        'payload',
        'created_at',
        'image',
        'updated_at',
        'is_display',
        'is_owner_send',
        'owner_notification_id'
    ];

    protected $dates = [
        'created_at',
        'updated_at'
    ];

	public function getImageAttribute()
	{
		$image = $this->attributes['image'];

		if(empty($image))
		{
			return '/frontend3/assets/img/avatar_admin@1x.png';
		}

		return $image;
	}

	public function hostel()
    {
        return $this->belongsTo(Hostel::class);
    }

    public function room()
    {
        return $this->belongsTo(Room::class);
    }

    public function imagesOwner()
    {
        return $this->hasMany(NotificationImage::class, 'notification_id', 'owner_notification_id');
    }

	public function getStatusTextHtmlAttribute()
	{
		$type = $this->attributes['status'];

		if ($type == Notification::PUSHED) {
			return '<label class="label label-success">Đã gửi</label>';
		} else if ($type == Notification::NOT_PUSH) {
			return '<label class="label label-danger">Chưa gửi</label>';
		}
		return 'Chưa rõ';
	}

	public function scopePublish($q)
    {
        $q->where('notifications.is_display', true);
    }

    public function scopeMessage($q)
    {
        $q->where('notifications.is_display', false);
    }
}
