<?php

namespace App\Models;

use App\Models\traits\SetConnection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Model;

class PortalStatusArticle extends Model
{
    use SetConnection;

    protected $fillable = ['article_id', 'portal_id', 'portal_user', 'portal_type', 'created_at'];

    public static function getDataById($id, $type = null)
    {
        $user = Auth::user();
        $shop = MstStore::getStoreCode($user->store_id);

        $query = PortalStatusArticle::query();
        $query->orderBy('updated_at');
        $query->where('article_id', '=', $id);
        if($type != null){
            $query->where('portal_type', '=', $type);
            if($type == 1){
                $query->where('portal_user', '=', $shop->suumo_id);
            }
            if($type == 2){
                $query->where('portal_user', '=', $shop->homes_id);
            }
            if($type == 3){
                $query->where('portal_user', '=', $shop->athome_id);
            }
            $result = $query->first();
        }else{
            $result = $query->get();
        }

        return $result;
    }
}
