<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Order extends Model
{
    //use soft delete
    use SoftDeletes;

    protected $fillable = [
        'token',
        'qr_code',
        'qr_image',
        'user_status',
        'status',
        'note',
        'user_id',
    ];

    public function getdateAttribute()
    {
        $date = date('Y ',strtotime($this->created_at));
        $date .= __('web.'.date('F',strtotime($this->created_at)));
        $date .= date(' d ',strtotime($this->created_at));
        return $date;
    }
    public function getaccountTypeAttribute()
    {
        if($this->user_status == 'visitor')
        {
            return 'visitor';
        }
        if(($this->user ? $this->user->role : 'user' ) == 'company')
        {
            return 'company';
        }
        return 'user';
    }

    public function user()
    {
        if($this->user_status == 'visitor')
        {
            return $this->belongsTo(Visitor::class,'user_id')->withTrashed();
        }
        return $this->belongsTo(User::class,'user_id')->withTrashed();
    }
    
    public function sumProducts()
    {
        $count = OrderProduct::where('order_id',$this->id)->sum('count');
        return $count;
    }
    
    public function city()
    {
        return $this->belongsTo(City::class)->withTrashed();
    }
    public function product()
    {
        return $this->belongsTo(Product::class)->withTrashed();
    }
    public function payment()
    {
        return $this->hasOne(OrderPayment::class);
    }
    public function shipping()
    {
        return $this->hasOne(OrderShipping::class);
    }
    public function address()
    {
        return $this->hasOne(OrderAddress::class);
    }
}
