<?php

namespace App\Http\Controllers\Member;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
use Session;
use Str;
use Auth;
use Hash;
use Mail;

class WalletController extends Controller
{

    public function addfund()
    {
        $data['member_data'] = DB::table('members')->where('id', Auth::guard('member')->user()->id)->first();
        $data['reports'] = DB::table('payments')->where('user_id', $data['member_data']->id)->where('type', 'Add Fund')->where('user_type', 'Member')->where('status', 0)->get();
        $data['repo_count'] = DB::table('payments')->where('user_id', $data['member_data']->id)->where('type', 'Add Fund')->where('user_type', 'Member')->where('status', 0)->count();

        return view('member.addfund', $data);
    }

    public function afbks()
    {
        $data['member_data'] = DB::table('members')->where('id', Auth::guard('member')->user()->id)->first();
        return view('member.afbks', $data);
    }

    public function afngd()
    {
        $data['member_data'] = DB::table('members')->where('id', Auth::guard('member')->user()->id)->first();
        return view('member.afngd', $data);
    }

    public function afrckt()
    {
        $data['member_data'] = DB::table('members')->where('id', Auth::guard('member')->user()->id)->first();
        return view('member.afrckt', $data);
    }

    public function afupay()
    {
        $data['member_data'] = DB::table('members')->where('id', Auth::guard('member')->user()->id)->first();
        return view('member.afupay', $data);
    }

    public function addfAction(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->all();

            $user_data = DB::table('members')->where('id', Auth::guard('member')->user()->id)->first();
            if (!empty($data['amount']) && $data['amount'] >= 100) {
                $data1['user_id'] = $user_data->id;
                $data1['username'] = $user_data->username;
                $data1['user_type'] = 'Member';
                $data1['type'] = 'Add Fund';
                $data1['user_name'] = $user_data->name;
                $data1['user_mobile'] = $user_data->mobile;
                $data1['payment_method'] = $data['gateway'];
                $data1['account'] = $data['sender'];
                $data1['amount'] = $data['amount'];
                $data1['status'] = 0;
                DB::table('payments')->insert($data1);

                toastr()->success('Add Fund Successful! Please Wait...');
                return redirect('addfund');
            }else{
                toastr()->error('Please, Enter Amount Currectly!');
                return redirect()->back();
            }
        }
    }
    

    public function convert()
    {
        $data['member_data'] = DB::table('members')->where('id', Auth::guard('member')->user()->id)->first();

        return view('member.convert', $data);
    }

    public function cashConvert(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->all();

            $member_data = DB::table('members')->where('id', Auth::guard('member')->user()->id)->first();
            if (!empty($data['amount']) && $data['amount'] >= 100) {
                if ($member_data->balance >= $data['amount']) {
                    if (Hash::check($data['pin'], $member_data->pin)) {  
                        $charge = $data['amount']*10/100;
                        $net_amount = $data['amount']-$charge;
                     
                        $data2['balance'] = $member_data->balance-$data['amount'];
                        $data2['credit'] = $member_data->credit+$net_amount;
                        DB::table('members')->where('id', $member_data->id)->update($data2);

                        $repo1['type']            = 'member';
                        $repo1['bal_type']        = 'Transfer';
                        $repo1['wallet']          = 'Balance';
                        $repo1['action']          = 'Convert';                        
                        $repo1['details']         = 'Earning to Buying Convert';
                        $repo1['bal_out']         = $data['amount'];
                        $repo1['bal']             = $member_data->balance-$data['amount'];   
                        $repo1['user_id']         = $member_data->id;
                        DB::table('reports')->insert($repo1);

                        $repo2['type']            = 'member';
                        $repo2['bal_type']        = 'Receive';
                        $repo2['wallet']          = 'Buying';
                        $repo2['action']          = 'Convert';
                        $repo2['details']         = 'Receive from Earning Wallet';
                        $repo2['bal_in']          = $net_amount;
                        $repo2['bal']             = $member_data->credit+$net_amount;   
                        $repo2['user_id']         = $member_data->id;
                        DB::table('reports')->insert($repo2);

                        toastr()->success('Convert Completed Successfully!');
                        return redirect()->back();
                        
                    }else{
                        toastr()->error('Invalid Security PIN! Reset or Generate Under Settings Section.');
                        return redirect()->back();
                    }                    
                }else{
                    toastr()->error('Insufficient Balance!');
                    return redirect()->back();
                }
            }else{
                toastr()->error('Enter Amount Currectly! (Min 100)');
                return redirect()->back();
            }

        }
    }

    public function ctrans()
    {
        $data['member_data'] = DB::table('members')->where('id', Auth::guard('member')->user()->id)->first();
        $data['members'] = DB::table('members')->orderBy('created_at', 'desc')->get();

        return view('member.ctrans', $data);
    }

    public function cwTrans(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->all();

            if (!empty($data['username'])) {
                $reciv_data = DB::table('members')->where('username', $data['username'])->first();
                
                if (!empty($data['amount']) && $data['amount'] >= 100) {
                    $member_data = DB::table('members')->where('id', Auth::guard('member')->user()->id)->first();

                    if ($member_data->kyc == 1) {
                        if ($member_data->credit >= $data['amount']) {
                            if (Hash::check($data['pin'], $member_data->pin)) {
                                $data1['credit'] = $reciv_data->credit+$data['amount'];
                                DB::table('members')->where('id', $reciv_data->id)->update($data1);
                               
                                $repo1['type'] = 'member';
                                $repo1['bal_type'] = 'Receive';
                                $repo1['wallet'] = 'Buying';
                                $repo1['action'] = 'Receive';
                                $repo1['details'] = 'Receive Buying Balance from ';
                                $repo1['bal_in'] = $data['amount'];
                                $repo1['bal'] = $reciv_data->credit+$data['amount'];
                                $repo1['user_id'] = $reciv_data->id;
                                $repo1['from_user'] = $member_data->username;
                                DB::table('reports')->insert($repo1);

                                $data2['credit'] = $member_data->credit-$data['amount'];
                                DB::table('members')->where('id', $member_data->id)->update($data2);

                                $repo3['type'] = 'member';
                                $repo3['bal_type'] = 'Transfer';
                                $repo3['wallet'] = 'Buying';
                                $repo3['action'] = 'Transfer';
                                $repo3['details'] = 'Transfer Buying Balance to ';
                                $repo3['bal_out'] = $data['amount'];
                                $repo3['bal'] = $member_data->credit-$data['amount'];
                                $repo3['user_id'] = $member_data->id;
                                $repo3['from_user'] = $reciv_data->username;
                                DB::table('reports')->insert($repo3);

                                toastr()->success('Balance Send Successfully!');
                                return redirect()->back();
                               
                            }else{
                                toastr()->error('Invalid Security PIN!');
                                return redirect()->back();
                            }
                        }else{
                            toastr()->error('Insufficient Balance!');
                            return redirect()->back();
                        }
                    }else{
                        toastr()->error('Please, Complete your KYC!');
                        return redirect()->back();
                    }
                }else{
                    toastr()->error('Enter Amount Currectly! (Min: 100)');
                    return redirect()->back();
                }
                
            }else{
                toastr()->error('Select a Member!');
                return redirect()->back();
            }
        }
    }

    public function payout()
    {
        $data['member_data'] = DB::table('members')->where('id', Auth::guard('member')->user()->id)->first();
        $data['payouts'] = DB::table('payments')->where('user_id', $data['member_data']->id)->where('user_type', 'Member')->where('type', 'Payout')->orderBy('created_at', 'desc')->get();
        $data['pay_count'] = DB::table('payments')->where('user_id', $data['member_data']->id)->where('user_type', 'Member')->where('type', 'Payout')->count();
       
        return view('member.payout', $data);
    }

    public function balPayout(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->all();

            $member_data = DB::table('members')->where('id', Auth::guard('member')->user()->id)->first();
            if ($member_data->kyc == 1) {
                if (!empty($data['amount']) && $data['amount'] >= 200) {
                    if ($member_data->balance >= $data['amount']) {
                        if (!empty($data['gateway']) && !empty($data['account'])) {
                            if (Hash::check($data['pin'], $member_data->pin)) {  
                                $charge = $data['amount']*10/100;
                                $net_amount = $data['amount']-$charge;

                                $data1['user_id'] = $member_data->id;
                                $data1['username'] = $member_data->username;
                                $data1['user_type'] = 'Member';
                                $data1['type'] = 'Payout';
                                $data1['user_name'] = $member_data->name;
                                $data1['user_mobile'] = $member_data->mobile;
                                $data1['payment_method'] = $data['gateway'];
                                $data1['account'] = $data['account'];
                                $data1['amount'] = $net_amount;
                                $data1['charge'] = $charge;
                                $data1['status'] = 0;
                                DB::table('payments')->insert($data1);
                                  
                                $data2['balance'] = $member_data->balance-$data['amount'];
                                DB::table('members')->where('id', $member_data->id)->update($data2);

                                $repo1['type']            = 'member';
                                $repo1['bal_type']        = 'Transfer';
                                $repo1['wallet']          = 'Balance';
                                $repo1['action']          = 'Payout';
                                $repo1['details']         = 'Transfer Amount for Payout';
                                $repo1['bal_out']         = $data['amount'];
                                $repo1['bal']             = $member_data->balance-$data['amount'];   
                                $repo1['user_id']         = $member_data->id;
                                DB::table('reports')->insert($repo1);

                                toastr()->success('Payout Completed Successfully!');
                                return redirect()->back(); 
                               
                            }else{
                                toastr()->error('Invalid Security PIN! Reset or Generate Under Settings Section.');
                                return redirect()->back();
                            }
                        }else{
                            toastr()->error('Please, Select Payment Method & Enter Account Number Currectly!');
                            return redirect()->back();
                        }
                    }else{
                        toastr()->error('Insufficient Balance!');
                        return redirect()->back();
                    }
                }else{
                    toastr()->error('Please, Enter amount! (Minimum 200)');
                    return redirect()->back();
                }
            }else{
                toastr()->error('Please, Complete your KYC!');
                return redirect()->back();
            }
        }
    }


}
