<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ShopOrder extends BaseModel
{
    use HasFactory;

    public $fillable = ['user_id', "shop_id", "sku_id", "shop_info", "origin_price", "price", "num", "pay_type", "pay_status", "problem_status", "trade_no", "name", "mobile","address","express_type","express","express_trade_no","ship_fee"];

    public const FREEPAYTYPE = "free";
    public const WECHATPAYTYPE = "wechat";

    public const UNPAIDSTATUS = "UNPAID";
    public const PAIDSTATUS = "PAID";
    public const SENDSTATUS = "SEND";
    public const REFUNDSTATUS = "REFUND";
    public const SIGNEDSTATUS = "SIGNED";
    public const PROBLEMSTATUS = "PROBLEM";
    public const NO_PROCESSED_STATUS = "NO_PROCESSED";
    public const PROCESSED_STATUS = "PROCESSED";
    public const BEING_PROCESSED_STATUS = "BEING_PROCESSED";

    public const OFFLINETYPE = "OFFLINE";
    public const ONLINETYPE = "ONLINE";

    protected $casts = [
        'shop_info' => 'array',
    ];


    public function scopePaidStatus($query)
    {
        return $query->whereNotIn("pay_status", [self::UNPAIDSTATUS]);
    }

    public function scopeOwner($query, $user_id=null)
    {
        if ($user_id) {
            return $query->where('user_id', $user_id);
        }
        return $query;
    }

    public function scopeShop($query, $shop_id=null)
    {
        if ($shop_id) {
            return $query->where('shop_id', $shop_id);
        }
        return $query;
    }


    public function status()
    {
        return $this->hasMany(ShopOrderStatus::class, 'order_id', 'id');
    }

    public function scopeStatus($query)
    {
        $pay_status = request()->pay_status;
        if ($pay_status) {
            return $query->where("pay_status", $pay_status);
        }else {
            return $query->where("pay_status", "<>", self::UNPAIDSTATUS);
        }
//        return $query;
    }

    public function scopeProblem($query)
    {
        $problem_status = request()->problem_status;
        if ($problem_status) {
            return $query->where("problem_status", $problem_status);
        }
        return $query;
    }

    public function scopeKeyword($query)
    {
        $keyword = request()->keyword;
        if ($keyword) {
            return $query->where(function ($sql) use ($keyword) {
                $sql->where("mobile", "like", "%$keyword%")
                    ->orWhere("name", "like", "%$keyword%")
                    ->orWhere("trade_no", "like", "%$keyword%")
                    ->orWhereHas('shop', function ($query) use($keyword){
                        $query->where("title", 'like', "%$keyword%");
                    });
            });
        }
        return $query;
    }

    public function shop()
    {
        return $this->belongsTo(Shop::class, 'shop_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function refundOrders()
    {
        return $this->hasMany(ShopRefundOrder::class, 'order_id', 'id');
    }

    //介绍人
    public function recommendUser()
    {
        return $this->hasOneThrough(User::class, RecommendUser::class, 'user_id', 'id', 'user_id', 'recommend_user_id')->selectRaw("ufutx_users.id, ufutx_users.name, ufutx_users.mobile");
    }

    public function comments()
    {
        return $this->hasMany(ShopOrderComment::class, 'order_id', 'id');
    }
}
