<?php
namespace Modules\Cart\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    use SoftDeletes;
    protected $table = 'product';
    protected $guarded = ['id'];

    protected $fillable = [
        'sku',
        'barcode',
        'name',
        'slug',
        'image_url',
        'price',
        'high_price',
        'add_shipping_fee',
        'weight',
        'status',
        'description',
        'content',
        'note',
        'inventory',
        'brand_id',
        'status_out_stock',
        'pod_parent_id',
        "approve_advertising",
        'created_at',
        'updated_at',
        'gtin'
    ];

    protected $appends = ['url'];

    public function categories()
    {
        return $this->belongsToMany(Category::class, 'product_n_category')->withPivot(['sorder'])->withTimestamps();
    }

    public function getUrlAttribute () {
        $retval = isset($_SERVER['PREFIX_URL']) ? $_SERVER['PREFIX_URL'] : '';
        return clroute($retval . "/" . (!empty($this->slug) ? $this->slug : "san-pham") . "-p" . $this->id);
    }

    protected static function boot()
    {
        parent::boot();
    }

}
