<?php
namespace App\Models;
use App\Models\LhcLottery;
use App\Models\LhcOrderItem;
class LhcOrder extends BaseModel
{
    protected $table = 'lhc_order';
    protected $fillable = ['issue','ordernum','member_id','game','gameplay','number','odds','amount','lottery_status','win_amount','profit_and_loss','is_faker','remark','type'];
    protected $hidden = [];
    
    public $timestamps = true;
    protected $dateFormat = 'U'; // U 代表 UNIX 时间戳（int）

    const STATUS_STAY = 0;  // 待开奖
    const STATUS_LOSS = 1; // 未中奖
    const STATUS_WIN = 2; // 已中奖
    const STATUS_REFUND = 3; // 已退款

    public function lottery()
    {
        return $this->belongsTo(LhcLottery::class, 'issue', 'issue')
            ->select('issue', 'type', 'open_code', 'open_time', 'is_settlement');
    }

    public function items() {
        return $this->hasMany(LhcOrderItem::class, 'ordernum', 'ordernum');
    }

    /**
     * 历史订单主表未写入盈亏时，根据派奖金额和总投注额补齐接口返回值。
     */
    protected function getProfitAndLossAttribute($value): string
    {
        if ($value !== null && $value !== '') {
            return (string)$value;
        }

        $status = (int)($this->attributes['lottery_status'] ?? self::STATUS_STAY);
        if (!in_array($status, [self::STATUS_LOSS, self::STATUS_WIN], true)) {
            return '0.00';
        }

        $winAmount = (string)($this->attributes['win_amount'] ?? 0);
        $betAmount = $this->attributes['total_amount'] ?? $this->attributes['amount'] ?? 0;

        return bcsub($winAmount, (string)$betAmount, 2);
    }

    public static function getRemark($type)
    {
        if ($type == 1) {
            return '新澳门六合彩';
        } elseif ($type == 2) {
            return '香港六合彩';
        } elseif ($type == 3) {
            return '澳门六合彩';
        } elseif ($type == 4) {
            return '极速六合彩';
        } elseif ($type == 5) {
            return '加拿大28';
        } elseif ($type == 6) {
            return '极速28';
        } elseif ($type == 7) {
            return '极速赛车';
        } elseif ($type == 8) {
            return '极速时时彩';
        } elseif ($type == 9) {
            return '极速飞艇';
        } elseif ($type == 10) {
            return 'SG时时彩';
        } elseif ($type == 11) {
            return 'SG飞艇';
        } elseif ($type == 12) {
            return '极速快3';
        } elseif ($type == 13) {
            return 'SG快3';
        }
        return '';
    }
 
}
