<?php

namespace App\Models;

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

class AgentOrder extends BaseModel
{
    use HasFactory;

    public $fillable = ['user_id', 'trade_no', 'origin_price', 'price', 'pay_type', 'pay_status', 'shop_id', 'sku_id', 'num', 'shop_info', 'share_qrcode', 'name','mobile', 'contract_id', 'from_user_id','is_online', 'contract_id', 'sub_mch_id','freight', 'address', 'remark'];

    public const PAIDSTATUS = "PAID";
    public const UNPAIDSTATUS = "UNPAID";
    public const SENDSTATUS = "SEND";
    public const REFUNDSTATUS = "REFUND";
    public const SIGNEDSTATUS = "SIGNED";

    public const OFFLINE = 0;
    public const ONLINE = 1;

    public const FREEPAY = "free";
    public const WECHATPAY = 'wechat';

    public const OFFLINETYPE = 0;
    public const ONLINETYPE = 1;

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

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

        }
        return $query;
    }

    public function lottoCode()
    {
        return $this->hasOne(LottoCode::class, 'order_id', 'id');
    }

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


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

    public function introduceUser()
    {
        return $this->belongsTo(User::class, 'from_user_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 scopeShop($query, $shop_id=null)
    {
        if ($shop_id) {
            return $query->where('shop_id', $shop_id);
        }
        return $query;
    }

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

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

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

    public function scopePaid($query)
    {
        return $query->where('pay_status', '<>', self::UNPAIDSTATUS);
    }

    public function scopeTime($query)
    {
        $start_time = request()->start_time;
        $end_time = request()->end_time;
        if ($start_time && $end_time) {
            return $query->whereBetween("created_at", [$start_time, $end_time]);
        }
        return $query;
    }

    //介绍人 用户购买活动票记录
    public function recommendUserLog()
    {
        return $this->hasOne(RecommendUser::class, 'user_id', 'user_id');
    }
    //介绍人
    public function recommendUser()
    {
        return $this->hasOneThrough(User::class, RecommendUser::class, 'user_id', 'id', 'user_id', 'recommend_user_id');
    }

    public function agentUser()
    {
        return $this->hasOne(AgentUser::class,'user_id', 'user_id');
    }

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