<?php

namespace App\Models;

use App\Facades\CommonService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;

class LotteryMember extends BaseModel
{
    use HasFactory;

    public $fillable = ['user_id', 'lottery_id', 'pic', 'name', 'mobile', 'award_id', 'code','index'];

    protected function mobile(): Attribute
    {
        if (request()->routeIs('h5.*')) {
            return Attribute::make(
                get: fn ($value) => CommonService::hidePhone($value),
            );
        }
        return Attribute::make(
            get: fn ($value) => $value,
        );
    }

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