<?php

namespace App\Models;

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

class ActivityShareRefund extends BaseModel
{
    use HasFactory;

    public $fillable = ['user_id', 'order_id', 'target_order_id', 'trade_no', 'refund_trade_no', 'amount', 'is_hook', 'err_message', 'status','total_amount','sub_mch_id','reason', 'type','admin_id','check','log_id'];

    public const SHARETYPE = "SHARE"; //推荐退款
    public const ADMINTYPE = "ADMIN"; //直接退款

    public const AGREESTATUS = 1; //同意
    public const AUDITSTATUS = 0;  //待审核
    public const REFUSESTATUS = -1; //拒绝

    const CHECK_TRUE = 0; //需要审核
    const CHECK_FALSE = 1; //不需要审核
    public function scopeStatus($query)
    {
        $status = request()->status;
        if (is_numeric($status)) {
            return $query->where('status', $status);
        }
    }

    public function scopeKeyword($query)
    {
        $keyword = request()->keyword;
        if ($keyword) {
            return $query->whereHas('user',function ($query) use ($keyword){
                $query->where('name','like','%'.$keyword.'%')->orWhere('mobile','like','%'.$keyword.'%');
            });
        }
        return $query;
    }

    public function targetOrder()
    {
        return $this->belongsTo(ActivityOrder::class, 'target_order_id', 'id');
    }

    public function order()
    {
        return $this->belongsTo(ActivityOrder::class, 'order_id', 'id');
    }

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

    public function admin()
    {
        return $this->belongsTo(Admin::class, 'admin_id', 'id');
    }
}
