<?php

namespace App\Models;

use App\Constants\Util;

/**
 * @property string $activity_id
 * @property string $finish_time
 * @property string member_id
 * @property string title
 * @property string status
 * @property string sub_title
 * @property string detail_image
 * @property string start_time
 * @property string end_time
 * @property string part_in_time
 * @property $effective_betting_amount
 * @property $gift_amount
 * @property $betting_amount
 *
 */
class ActivityUser extends BaseModel
{
    protected $table = 'activity_user';
    protected $fillable = ['activity_id',
        'finish_time', 'member_id', 'title', 'status', 'sub_title', 'detail_image',
        'start_time', 'end_time', 'part_in_time',
        'effective_betting_amount', 'gift_amount', 'betting_amount',

    ];


    const STATUS_APPLY = 0;
    const STATUS_IN_PROGRESS = 1;
    const STATUS_COMPLETE = 2;


    public function member()
    {
        return $this->belongsTo(User::class, 'member_id', 'member_id')
            ->select(['id', 'member_id', 'username', 'first_name', 'admin_note', 'status', 'phone', 'visitor_id', 'register_ip']);
    }


    protected function getDetailImageAttribute($value)
    {
        return Util::ensureUrl($value);
    }

    protected function setDetailImageAttribute($value)
    {
        $this->attributes['detail_image'] = Util::replacePartInUrl($value);
    }

    protected function getPartInTimeAttribute($value): string
    {
        return date('Y-m-d H:i:s', $value);
    }

    protected function getFinishTimeAttribute($value): string
    {
        if ($value > 1) {
            return date('Y-m-d H:i:s', $value);
        }
        return "";
    }


    protected function getStartTimeAttribute($value): string
    {
        return date('Y-m-d', $value);
    }

    protected function getEndTimeAttribute($value): string
    {
        return date('Y-m-d', $value);
    }
}
