<?php

namespace Modules\User\Models;


use App\Models\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Modules\User\Traits\BelongsToUser;

class UserInvite extends Model
{
    use BelongsToUser;

    const STATUS_INIT  = 1;
    const STATUS_ALLOT = 2;
    const STATUS_USED  = 3;

    const  STATUS = [
        self::STATUS_INIT  => '未分配',
        self::STATUS_ALLOT => '已分配',
        self::STATUS_USED  => '已激活',
    ];

    protected $dates = [
        'actived_at',
    ];

    public function activeUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'active_id');
    }

    public function getStatusTextAttribute()
    {
        return self::STATUS[$this->status] ?? '未知';
    }

    /**
     * Notes: 激活
     *
     * @Author: 玄尘
     * @Date: 2022/9/21 10:11
     * @param $user_id
     */
    public function use($user_id)
    {
        $this->update([
            'active_id'  => $user_id,
            'actived_at' => now(),
            'status'     => self::STATUS_USED,
        ]);
    }
}
