<?php
namespace App\Models;
use App\Services\IssueService;
class Issue extends BaseModel
{
    protected $table = 'issues';
    protected $fillable = ['issue_no', 'start_time', 'end_time', 'winning_numbers', 'status', 'combo', 'extreme', 'image', 'keno'];
    protected $appends = [
        'award',
        'winning_array',
        'end_timestamp',
    ];

    function getEndTimestampAttribute()
    {
        return strtotime($this->end_time);
    }

    function getWinningArrayAttribute()
    {
        if (!empty($this->winning_numbers)) {
            $winning_numbers = explode(',', $this->winning_numbers);
            $winning_numbers = array_map('intval', $winning_numbers);
            $winning_numbers[] = array_sum($winning_numbers);
            return $winning_numbers;
        }
        return [];
    }

    function getKenoAttribute($value)
    {
        return json_decode($value, true);
    }

    function getAwardAttribute()
    {
        if (!empty($this->winning_numbers)) {
            $winning_numbers = explode(',', $this->winning_numbers);
            $winning_numbers = array_map('intval', $winning_numbers);
            return IssueService::award($winning_numbers);
        }
        return [];
    }


    const STATUS_DRAFT = 0;
    const STATUS_BETTING = 1;
    const STATUS_CLOSE = 2;
    const STATUS_DRAW = 3;
    const STATUS_FAIL = 4;

    public static $STATUS = [
        0 => '草稿',
        1 => '投注中',
        2 => '封盘',
        3 => '开奖',
        4 => '失败',
    ];


    public static function getStatus($val = -1)
    {
        $array = self::$STATUS;
        if ($val < 0) {
            $arr = [];
            foreach ($array as $k => $v) {
                $item = [];
                $item['id'] = $k;
                $item['title'] = $v;
                $arr[] = $item;
            }
            return $arr;
        } else {
            return $array[$val];
        }
    }
}