<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use App\Traits\SortableTraitPinned;
use Illuminate\Database\Eloquent\Model;
use Spatie\EloquentSortable\Sortable;

class DistProduct extends Model
{
	use HasDateTimeFormatter;
    use SortableTraitPinned;
    protected $table = 'dist_product';

    // 可选：你可以在这里自定义排序配置
    public $sortable = [
        'order_column_name' => 'order',  // 排序字段
        'sort_when_creating' => true,    // 创建时自动排序
    ];

    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
        'parameters' => 'json', // 将 attributes 字段转换为数组
    ];

    protected $fillable = [
        'id',
        'title',
        'keywords',
        'description',
        'sku',
        'category_id',
        'issuance_date',
        'order',
        'enabled',
        'content',
        'parameters',
        'created_at',
        'updated_at',
        'is_pinned',
        'dist_id',
        'seo_title',
        'seo_keywords',
        'seo_description',
        'slug',
        'status',
        'review_reply',
        'album_id',

    ];


    public function distProductCategory()
    {
        return $this->hasOne(DistProductCategory::class,'id','category_id');
    }

    // 一对多关联
    public function images()
    {
        return $this->hasMany(DistProductImage::class, 'product_id')->orderBy('order', 'asc')->orderBy('id', 'asc');
    }



}
