<?php

namespace App\Http\Controllers\Admin;

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

class PaymentController extends Controller
{
    public function balreq()
    {
        $data['payments'] = DB::table('payments')->where('user_type', 'Member')->where('type', 'Add Fund')->orderBy('created_at', 'desc')->get();
        return view('admin.balreq', $data);
    }

    public function afMang($id)
    {
        $data['pay_data'] = DB::table('payments')->where('id', $id)->first();
        return view('admin.afmang', $data);
    }

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

            if (!empty($data['amount'])) {
                $admin = DB::table('admins')->where('id', 1)->first();
                if (Hash::check($data['pin'], $admin->pin)) {
                    $pay_data = DB::table('payments')->where('id', $data['pay_id'])->first();
                    
                    $data1['status'] = $data['status'];
                    $data1['amount'] = $data['amount'];
                     DB::table('payments')->where('id', $pay_data->id)->update($data1);

                    if ($data['status'] == 1) {
                        $user_data = DB::table('members')->where('id', $pay_data->user_id)->first();

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

                        $repo1['type'] = 'member';
                        $repo1['bal_type'] = 'Receive';
                        $repo1['wallet'] = 'Buying';
                        $repo1['action'] = 'Add Fund';
                        $repo1['details'] = 'Receive by Add Fund';
                        $repo1['bal_in'] = $data['amount'];
                        $repo1['bal'] = $user_data->credit+$data['amount'];
                        $repo1['user_id'] = $user_data->id;
                        DB::table('reports')->insert($repo1);
                    }                      

                    toastr()->success('Payment Completed Successfully!!');
                    return redirect('admin/balreq');
                }else{
                   toastr()->error('Invalid Admin PIN!');
                    return redirect()->back(); 
                }
            }else{
                toastr()->error('Enter Amount!');
                return redirect()->back();
            }
        }
    }

    public function balsend()
    {
        $admin = DB::table('admins')->where('id', Auth::guard('admin')->user()->id)->first();

        if ($admin->id <= 2) {
            $data['members'] = DB::table('members')->get();

            return view('admin.balsend', $data);
        }else{
            return redirect('admin/dashboard');
        }
        
    }

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

            if (!empty($data['username'])) {
                $user_data = DB::table('members')->where('username', $data['username'])->first();
                
                if (!empty($data['amount']) && $data['amount'] >= 1) {
                    $admin = DB::table('admins')->where('id', Auth::guard('admin')->user()->id)->first();
                    if (Hash::check($data['pin'], $admin->pin)) {
                        $data1['credit'] = $user_data->credit+$data['amount'];
                        DB::table('members')->where('id', $user_data->id)->update($data1);

                        $repo1['type'] = 'member';
                        $repo1['bal_type'] = 'Receive';
                        $repo1['wallet'] = 'Buying';
                        $repo1['action'] = 'Receive';
                        $repo1['details'] = 'Receive Balance from ';
                        $repo1['bal_in'] = $data['amount'];
                        $repo1['bal'] = $user_data->credit+$data['amount'];
                        $repo1['user_id'] = $user_data->id;
                        $repo1['from_user'] = 'Admin';
                        DB::table('reports')->insert($repo1);

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

                        toastr()->success('Cash Send Successful!');
                        return redirect()->back();
                        
                    }else{
                        toastr()->error('Invalid Admin PIN!');
                        return redirect()->back();
                    }
                }else{
                    toastr()->error('Enter Amount Currectly!');
                    return redirect()->back();
                }
                
            }else{
                toastr()->error('Select a Vendor!');
                return redirect()->back();
            }
        }
    }


    public function pendpay()
    {

        $data['payout'] = DB::table('payments')->where('type', 'Payout')->where('status', 0)->orderBy('created_at', 'desc')->get();

        return view('admin.pendpay', $data);
    }

    public function payMang($id)
    {

        $data['pay_data'] = DB::table('payments')->where('id', $id)->first();

        return view('admin.paymng', $data);
    }

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

            if (!empty($data['status'])) {
                $admin = DB::table('admins')->where('id', Auth::guard('admin')->user()->id)->first();
                if (Hash::check($data['pin'], $admin->pin)) {
                    $pay_data = DB::table('payments')->where('id', $data['pay_id'])->first();
                    $data1['status'] = 1;
                    $data1['trx_id'] = $data['trxid'];
                    DB::table('payments')->where('id', $pay_data->id)->update($data1);

                    //SMS Alert                                            
                    $mob = $pay_data->user_mobile;
                    $url = "http://bulksmsbd.net/api/smsapi";
                    $api_key = "F7isYmkCiQF14G4C6uM6";
                    $senderid = "8809617613059";
                    $number= "$mob";
                    $message = 'Dear, '.$pay_data->username.' '.$pay_data->amount.' TK Paid in your '.$pay_data->payment_method.' from Earning BD Shop.';
                 
                    $datax = [
                        "api_key" => $api_key,
                        "senderid" => $senderid,
                        "number" => $number,
                        "message" => $message
                    ];
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $datax);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                    $smsresult = curl_exec($ch);
                    $p = explode("|",$smsresult);
                    $sendstatus = $p[0];

                    toastr()->success('Payment Completed Successfully!!');
                    return redirect('admin/pendpay');
                }else{
                   toastr()->error('Invalid Admin PIN!');
                    return redirect()->back(); 
                }
            }else{
                toastr()->error('Select Payout Status!');
                return redirect()->back();
            }
        }
    }


    public function paidpay()
    {

        $data['payout'] = DB::table('payments')->where('type', 'payout')->where('status', 1)->orderBy('created_at', 'desc')->get();

        return view('admin.paidpay', $data);
    }
}
