<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Blog extends Model {
	//
	const SALE_ONGOING = 1;
	const SALE_END = 0;
	const SALE_SOON = 2;
	const SALE_NO_TIME=-1;
	protected $fillable = [
		'title',
		'content',
		'is_featured',
		'status',
		'meta_keywords',
		'meta_description',
		'meta_title',
		'created_at',
		'updated_at',
		'deleted_at',
		'image',
		'user_id',
		'is_sent',
		'start_date',
		'end_date',
		'is_sale',
        'slug'
	];

	protected $dates = [
		'created_at',
		'updated_at',
		'deleted_at',
		'start_date',
		'end_date'
	];

	public function account() {
		return $this->belongsTo( 'App\User', 'user_id', 'id' );
	}

	public function getSaleStatusAttribute() {
		//1 - đang diễn ra
		// 0 - đã kết thúc
		// 2 - sắp diễn ra
		if ( ! empty( $this->start_date ) && empty( $this->end_date ) ) {
			if ( Carbon::now()->greaterThanOrEqualTo( $this->start_date->startOfDay() ) ) {
				return self::SALE_ONGOING;
			} else {
				return self::SALE_SOON;
			}
		}

		if ( empty( $this->start_date ) && ! empty( $this->end_date ) ) {
			if ( Carbon::now()->lessThanOrEqualTo( $this->end_date->endOfDay() ) ) {
				return self::SALE_ONGOING;
			} else {
				return self::SALE_END;
			}
		}

		if ( ! empty( $this->start_date ) && ! empty( $this->end_date ) ) {
			if ( Carbon::now()->lessThanOrEqualTo( $this->end_date->endOfDay() ) && Carbon::now()->greaterThanOrEqualTo( $this->start_date->startOfDay() ) ) {
				return self::SALE_ONGOING;
			}
			if ( Carbon::now()->lessThan( $this->start_date->startOfDay() ) ) {
				return self::SALE_SOON;
			} else {
				return self::SALE_END;
			}
		}

		return self::SALE_NO_TIME;

	}

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

		if ( $type == true ) {
			return '<label class="label label-success">Đã kích hoạt</label>';
		} else if ( $type == false ) {
			return '<label class="label label-danger">Không kích hoạt</label>';
		}

		return 'Chưa rõ';
	}

	public function getCategoryFirstAttribute() {
		$id   = $this->attributes['id'];
		$cats = \DB::table( 'blog_categories' )->join( 'categories', 'blog_categories.category_id', '=', 'categories.id' )
		           ->where( 'blog_categories.blog_id', $id )->first();
		if ( ! empty( $cats ) ) {
			return $cats;
		}

		return null;
	}

	public function getCategoriesTextAttribute() {
		$id   = $this->attributes['id'];
		$cats = \DB::table( 'blog_categories' )->join( 'categories', 'blog_categories.category_id', '=', 'categories.id' )
		           ->where( 'blog_categories.blog_id', $id )->pluck( 'categories.name' )->toArray();
		$cnt  = count( $cats );
		if ( $cnt == 1 ) {
			return $cats[0];
		} else if ( $cnt < 1 ) {
			return '';
		} else {
			$message = '';
			foreach ( $cats as $cat ) {
				$message .= $cat . ', ';
			}

			$message = rtrim( $message, ', ' );

			return $message;
		}
	}

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

		if ( ! empty( $item ) ) {
			return '<img src="/files/' . $item . '" style="max-width: 100px"/>';
		}

		return '<img src="/frontend3/assets/img/placeholder.png" style="max-width: 100px" />';
	}

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

		if ( ! empty( $item ) ) {
			return '/files/' . $item;
		}

		return '/frontend3/assets/img/placeholder.png';
	}

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

	public function scopeFeatured( $q ) {
		$q->where( 'is_featured', true );
	}
}
