<?php
namespace App\Http\Controllers\Api;
use App\Models\Setting;
use App\Models\ShareConfig;
use App\Models\User;
use App\Models\UserShare;
use App\Models\UserVip;
use App\Models\UserWithdraw;
use EasyWeChat\Factory;
use function Symfony\Component\Translation\t;

class UserController extends Controller
{
    public function get()
    {
        return out();
    }


    // 购买记录
    public function memberRecord()
    {
        $user = auth('api')->user();

        $record = UserVip::where('user_id',$user['id'])->first()->toArray();
        if(empty($record)){
            return out();
        }

        return out([
            'order_fee' => $record['order_fee'],
            'buy_time' => $record['created_at'],
        ]);
    }

    // 余额
    public function overage()
    {
        $user = auth('api')->user();

        $user = User::where('user_id',$user['id'])->first()->toArray();

        return out([
            'overage' => $user['overage'],
        ]);
    }

    // 收益
    public function income()
    {
        $user = auth('api')->user();

        $builder = UserShare::with(['child'])->where('user_id',$user['id']);

        $data = $builder->paginate($req['per_page'] ?? 15);
        $total_num = $builder->sum('id');
        $custom = collect([
            'total_num'   => $total_num,
        ]);
        $data = $custom->merge($data);

        return out($data);
    }

    // 购买会员
    public function buy()
    {
        $user = auth('api')->user();
        $shareConfig = Setting::first()->toArray();

        $orderId = get_order_id($user['id']);
        \DB::beginTransaction();
        $app = Factory::payment(config('wechat.payment.default'));
        $jssdk = $app->jssdk;

        try {

            $order = UserVip::where('user_id', $user['id'])->first();
            if(empty($order) || empty($order->prepay_id)){
                $order = UserVip::create([
                    'order_id' => $orderId,
                    'user_id' => $user['id'],
                    'order_fee' => $shareConfig['member_price']
                ]);
                $payment = $app->order->unify([
                    'body' => '三哥桥梁-会员充值',
                    'out_trade_no' => $orderId,
                    'total_fee' => $shareConfig['member_price']*100, // 分
                    'notify_url' => 'https://pay.weixin.qq.com/wxpay/pay.action', // 支付结果通知网址，如果不设置则会使用配置里的默认地址
                    'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
                    'openid' => $user['openid'],
                ]);

                if($payment['return_code'] != "SUCCESS" || $payment['result_code'] != 'SUCCESS'){
                    throw new \Exception($payment['return_msg']);
                }

                $order->prepay_id = $payment['prepay_id'];
                $order->save();
            }

            if($order->status == 0){
                $data = $jssdk->bridgeConfig($order->prepay_id,false);
            }else{
                throw new \Exception('您已经是会员了');
            }

            \DB::commit();
        }catch (\Exception $ex){
            \DB::rollBack();
            return out([],10012,$ex->getMessage());
        }

        return out($data);
    }
}
