<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class AgentWithdrawal extends Model
{
    /**
     * 数据库连接名
     */
    protected $connection = 'mysql';

    /**
     * 与模型关联的表名
     */
    protected $table = 'webgame.agent_withdrawals';

    /**
     * 可批量赋值的属性
     */
    protected $fillable = [
        'UserID',
        'amount',
        'before_balance',
        'after_balance',
        'status',
        'remarks',
        'order_sn',
        'admin_id',
        'admin_name',
        'process_time'
    ];

    /**
     * 应该转换为日期的属性
     *
     * @var array
     */
    protected $dates = [
        'process_time',
        'created_at',
        'updated_at'
    ];

    /**
     * 状态常量
     */
    const STATUS_PENDING = 0;   // 处理中
    const STATUS_COMPLETED = 1; // 已完成
    const STATUS_REJECTED = 2;  // 已拒绝

    /**
     * 获取状态文本
     *
     * @return string
     */
    public function getStatusTextAttribute()
    {
        switch ($this->status) {
            case self::STATUS_PENDING:
                return 'Pending';
            case self::STATUS_COMPLETED:
                return 'Completed';
            case self::STATUS_REJECTED:
                return 'Rejected';
            default:
                return 'Unknown';
        }
    }

    /**
     * 获取关联的用户信息
     */
    public function userInfo()
    {
        return $this->belongsTo(AgentUserInfo::class, 'UserID', 'UserID');
    }
    
    /**
     * 生成唯一订单号
     *
     * @return string
     */
    public static function generateOrderSn()
    {
        return 'AW' . date('YmdHis') . mt_rand(1000, 9999);
    }
} 