<?php
namespace App\Models;
use Carbon\Carbon;
use App\Models\Sport;

class Order extends BaseModel
{
    protected $table = 'sport_order';
    protected $fillable = ['user_id', 'order_id', 'issue' ,'is_roll' ,'amount' ,'cny_rate' ,'win_amount','profit_and_loss','is_faker','is_win', 'stattus','settlement_status',
            'pay_status','pay_time','pay_type','return_status','return_apply_time','return_operation_time','failure_msg', 'create_time','remark','settlement_time', 'detail' ];
    
    public $timestamps = true;
    protected $dateFormat = 'U'; // U 代表 UNIX 时间戳（int）

    // 2. 修改创建时间字段名为 create_time
    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'update_time';

    protected function getDetailAttribute($value)
    {
        if ($value) {
            $value = json_decode($value,true);
            if (!empty($value['game_time'])) {
                $value['game_time'] = date('Y-m-d H:i', $value['game_time']);
            } else {
                $value['game_time'] = '';
            } 
            if (!empty($value['fixture_status'])) {
                $value['fixture_status'] = Sport::getFixtureStatus($value['fixture_status']);
            } 
        } else {
            return null;
        }
        return $value;
    }

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

    protected function getReturnApplyTimeAttribute($value): string
    {
        return $value ? date('Y-m-d H:i:s', $value) : '';
    }
    
    protected function getReturnOperationTimeAttribute($value): string
    {
        return $value ? date('Y-m-d H:i:s', $value) : '';
    }
    
    protected function getCreateTimeAttribute($value): string
    {
        return Carbon::parse($value)->timezone('Asia/Shanghai')->format('Y-m-d H:i:s');
    }

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