<?php

namespace app\model;
use app\model\Base;
/**
 * @property integer $id 主键(ID) - 无注释
 * @property string $user_id 用户ID
 * @property float $amount 金额
 * @property string $network 网络
 * @property string $address 充值地址
 * @property string $extra 其他参数
 * @property string $from 支付地址
 * @property float $real_amount 实收金额
 * @property string $txid 凭证
 * @property integer $pay_time 支付时间
 * @property integer $confirmations 确认数量
 * @property string $result 结果
 * @property string $reason 原因
 * @property integer $status -1取消,0:创建,1支付中,2完成
 * @property integer $created_at 创建时间
 * @property integer $updated_at 更新时间
 */
class Recharge extends Base
{

    protected function getOptions(): array 
    {
        // 所有的参数配置统一返回
        return array_merge(parent::getOptions(),[
            'append'    =>  ['status_text','remaining_sec','usdt_amount']
        ]);
    }
    function getStatusTextAttr($v,$row){
        if($v){
            return \app\enum\RechargeStatus::tryFromValue($row['status'])->getDescription();
        }
    }
    function getRemainingSecAttr($v,$row){
        $created_at = $row['created_at'];
        if(!$created_at){
            return 0;
        }
        if(false !== strpos($created_at,'-')){
            $created_at = strtotime($created_at);
        }
        $v = $created_at + 900 -time() ;
        return $v <=0 ? 0 : $v;
    }
    function getUsdtAmountAttr($v,$row){
        if(in_array($row['network'],['BEP-20','TRC-20'])){
            $amount = bcdiv($row['amount'],Config('site.money_to_usdt_rate'),4);
            //折扣
            $amount = bcmul($amount,Config('site.usdt_recharge_discount'),4);
            return formatAmount($amount,4);
        }
        return 0;
    }
    function setTransferAtAttr($v){
        if($v && strpos($v,'-')){
            return strtotime($v);
        }
        return $v;
    }
    
    function setCreatedAtAttr($v){
        if($v && strpos($v,'-')){
            return strtotime($v);
        }
        return $v;
    }
    function setUpdatedAtAttr($v){
        if($v && strpos($v,'-')){
            return strtotime($v);
        }
        return $v;
    }
    function getStatusList(){
        return \app\enum\RechargeStatus::toArray();
    }
    
    function getNetworkList(){
        return [
            "BEP-20"=>"BEP-20",
            "TRC-20"=>"TRC-20"
        ];
    }
    
    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id');//->setEagerlyType(0);
    }
}
