<?php

namespace app\index\controller;

use app\admin\model\BlackIp;
use app\admin\model\MallOrder;
use app\BaseController;
use Kaadon\Uuid\Uuids;
use think\Exception;
use think\facade\Env;
use think\response\Redirect;

class Index extends BaseController
{
    /**
     * @throws \think\Exception
     */
    public function index()
    {
        if ($this->request->isPost()){
            $param = $this->request->post();
            $ip = $this->request->ip();
            $black = BlackIp::where('ip',$ip)->find();
            if (!empty($black)){
                return view('error');
                exit();
                return error('您的IP已被禁止充值，如有疑问请联系管理员');
            }
            $rid = Uuids::getUuid4();
            $data = [
                'money'=>$param['amount'],
                'rid'=>$rid,
                'start_time'=>time(),
                'end_time'=>time()+180,
                'ip'=> $ip,
            ];
            (new MallOrder())->save($data);
            return redirect('index/index/reloads?rid='.$rid);
        }else{
            return view();
        }
    }
    public function reloads()
    {
        return view();
    }
    public function status()
    {
        $rid = $this->request->param('rid');
        $data = MallOrder::where('rid',$rid)->find();
        if (empty($data))
            return error();
        $status = match ($data['status']) {
            1 => 'yellow',
            2 => 'red',
            default => 'blue',
        };
        return success([
            'status'=>$status,
            'color'=>$status,
            'url'=>$data['url']
        ]);
    }
}