<?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 ManageController extends Controller
{
   
    public function members()
    {

        $data['members'] = DB::table('members')->orderBy('created_at', 'desc')->get();
        return view('admin.members', $data);
    }

    public function exmem()
    {

        $data['members'] = DB::table('members')->where('club', 1)->orderBy('created_at', 'desc')->get();
        return view('admin.exmem', $data);
    }

    public function mnmem()
    {

        $data['members'] = DB::table('members')->where('club', 2)->orderBy('created_at', 'desc')->get();
        return view('admin.mnmem', $data);
    }

    public function fndmem()
    {

        $data['members'] = DB::table('members')->where('founder', 1)->orderBy('created_at', 'desc')->get();
        return view('admin.fndmem', $data);
    }

    public function blockmembers()
    {

        $data['members'] = DB::table('members')->where('status', 1)->orderBy('created_at', 'desc')->get();
        return view('admin.blockmembers', $data);
    }

    public function memdata()
    {
        $data['members'] = DB::table('members')->orderBy('created_at', 'desc')->get();
        return view('admin.memdata', $data);
    }

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

            if (!empty($data['user_id'])) {
                return redirect('admin/memdata/'.$data['user_id']);
            }else{
                toastr()->error('Please, Select a Member!');
                return redirect()->back();
            }
        }
    }

    public function memdataView($id)
    {
       $data['member_data'] = DB::table('members')->where('id', $id)->first();
       $data['ref'] = DB::table('reports')->where('user_id', $id)->where('type', 'member')->where('wallet', 'bal')->where('bal_type', 'Refer')->where('action', 'Commission')->sum('bal_in');
       $data['prom'] = DB::table('reports')->where('user_id', $id)->where('type', 'member')->where('wallet', 'bal')->where('bal_type', 'Promotion')->where('action', 'Commission')->sum('bal_in');
       $data['pay'] = DB::table('payments')->where('user_id', $id)->where('type', 'payout')->sum('amount');

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

    public function memedit()
    {
        $data['members'] = DB::table('members')->orderBy('created_at', 'desc')->get();
        return view('admin.memedit', $data);
    }

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

            if (!empty($data['user_id'])) {
                return redirect('admin/memedit/'.$data['user_id']);
            }else{
                toastr()->error('Please, Select a Member!');
                return redirect()->back();
            }
        }
    }

    public function memberEdit($id)
    {
        $data['member_data'] = DB::table('members')->where('id', $id)->first();
        $data['country'] = DB::table('countries')->get();

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

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

            $data1['name'] = $data['name'];
            $data1['email'] = $data['email'];
            $data1['country'] = $data['country'];
            $data1['address'] = $data['address'];
            DB::table('members')->where('id', $data['user_id'])->update($data1);

            toastr()->success('Member Info Updated Successfully!');
            return redirect()->back();

        }
    }

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

            if ($data['password'] == $data['conf_password']) {
                $data1['password'] = Hash::make($data['password']);
                DB::table('members')->where('id', $data['user_id'])->update($data1);

                toastr()->success('Password Updated Successfully!');
                return redirect()->back();
            }else{
                toastr()->error('Confirm Password Not Matched!');
                return redirect()->back();
            }                
        }
    }

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

            if ($data['pin'] == $data['conf_pin']) {
                $data1['pin'] = Hash::make($data['pin']);
                DB::table('members')->where('id', $data['user_id'])->update($data1);

                toastr()->success('PIN Generated Successfully!');
                return redirect()->back();
            }else{
                toastr()->error('Confirm PIN Not Matched!');
                return redirect()->back();
            } 
        }
    }

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

            if ($data['pin'] == $data['conf_pin']) {
                $data1['pin'] = Hash::make($data['pin']);
                DB::table('members')->where('id', $data['user_id'])->update($data1);

                toastr()->success('PIN Updated Successfully!');
                return redirect()->back();
            }else{
                toastr()->error('Confirm PIN Not Matched!');
                return redirect()->back();
            }                
        }
    }

    public function block()
    {
        $data['members'] = DB::table('members')->orderBy('created_at', 'desc')->get();
        return view('admin.block', $data);
    }

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

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

                if ($user_data->status == 0) {
                    $data1['status'] = 1;
                }else{
                    $data1['status'] = 0;
                }
                DB::table('members')->where('id', $user_data->id)->update($data1);

                toastr()->success('Status Updated Successfully!');
                return redirect()->back();

            }else{
                toastr()->error('Please, Select a Member!');
                return redirect()->back();
            }
        }
    }

    // KYC ///////////////////////////////////
    public function kycrq()
    {
        $data['members'] = DB::table('members')->where('kyc', 0)->get();
        return view('admin.kycrq', $data);
    }

    public function kycmng($id)
    {
        $data['member'] = DB::table('members')->where('id', $id)->first();
        $data['imgs'] = DB::table('kycimgs')->where('user_id', $id)->get();

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

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

            $admin = DB::table('admins')->where('id', Auth::guard('admin')->user()->id)->first();
            if (Hash::check($data['pin'], $admin->pin)) {
                $user_data = DB::table('members')->where('id', $data['uid'])->first();

                if ($data['status'] == 1) {
                    $data1['kyc'] = 1;

                    //SMS Alert                                            
                    $mob = $user_data->mobile;
                    $url = "http://bulksmsbd.net/api/smsapi";
                    $api_key = "F7isYmkCiQF14G4C6uM6";
                    $senderid = "8809617613059";
                    $number= "$mob";
                    $message = 'Congratulations! Your KYC is Approved. Thanks for Business with 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];
                }else{
                    $data1['kyc'] = null;
                    $data1['remark'] = $data['remark'];
                }
                DB::table('members')->where('id', $user_data->id)->update($data1);

                toastr()->success('Status Updated Successfully!');
                return redirect('admin/kycrq');
            }else{
                toastr()->error('Invalid Admin PIN!');
                return redirect()->back(); 
            }
        }
    }

    public function kycs()
    {
        $data['members'] = DB::table('members')->where('kyc', 1)->get();
        return view('admin.kycs', $data);
    }

    // Orders /////////////////////////////////////////////////////////////

    public function pendOrd()
    {

        $data['orders'] = DB::table('orders')->where('status', 0)->orderBy('created_at', 'asc')->get();

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

    public function paidOrd()
    {

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

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

    public function invoice($id)
    {
        $data['ord_data'] = DB::table('orders')->where('id', $id)->first();
        $data['user_data'] = DB::table('members')->where('username', $data['ord_data']->username)->first();
        $data['pro_data'] = DB::table('orders_products')->where('order_id', $data['ord_data']->id)->get();
        $data['shop'] = DB::table('shops')->where('id', $data['ord_data']->shop_id)->first();

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

    public function cbstatus()
    {
        $data['cbacks'] = DB::table('packs')->orderBy('created_at', 'desc')->get();
        return view('admin.cbstatus', $data);
    }

    public function ordMng($id)
    {

        $data['ord_data'] = DB::table('orders')->where('id', $id)->first();
        
        return view('admin.ordmng', $data);
    }

    public function ordProcess(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)) {
                    $ord_data = DB::table('orders')->where('id', $data['ord_id'])->first();
                    $data1['status'] = 1;
                    DB::table('orders')->where('id', $ord_data->id)->update($data1);

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

    // Vendor /////////////////////////////////////////////////////////////////////
    public function vendors()
    {
        $data['vendors'] = DB::table('vendors')->orderBy('created_at', 'asc')->get();

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

    public function addvendor()
    {
        return view('admin.addvendor');
    }

    public function vendorAbility(Request $request)
    {
        $username       = $request->username;
        if (!empty($username)) {
            $username_result   = DB::table('vendors')->where('username', $username)->first();
            
            if(empty($username_result)){
                echo 'unique';
            }else {
                echo 'not_unique';
            }
        }else{
            echo 'invalid';
        }      
              
    }

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

            $rules = [
                'username' => 'max:20|regex:/^[a-zA-Z0-9]*$/',
            ];
                
            $customMessages = [
                'username.regex' => 'Username: Only letters and numbers allow! No Space or Symbol.',
            ];

            $this->validate($request,$rules,$customMessages);

            if (!empty($data['username'])) {
                $new_user = DB::table('vendors')->where('username', $data['username'])->first();
                if (empty($new_user)) {
                    if (!empty($data['password']) && $data['password'] == $data['confirm_pass']) {

                        $data1['name'] = $data['name'];
                        $data1['username'] = $data['username'];
                        $data1['email'] = $data['email'];
                        $data1['mobile'] = $data['mobile'];
                        $data1['password'] = Hash::make($data['password']);
                        $data1['address'] = $data['address'];
                        $data1['status'] = 0;
                        DB::table('vendors')->insert($data1);

                        //SMS Alert
                        $mob = $data['mobile'];
                        $url = "http://66.45.237.70/api.php";
                        $number= "$mob";
                        $text='Welcome to osbbl.shop! Your Vendor Account Register Successful. Login ID: '.$data['username'].' & Password: '.$data['password'].' Hotline: 01940243031';
                        $dataxx= array(
                        'username'=>"01304098181",
                        'password'=>"NKDTFPYZ",
                        'number'=>"$number",
                        'message'=>"$text"
                        );

                        $ch = curl_init(); // Initialize cURL
                        curl_setopt($ch, CURLOPT_URL,$url);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($dataxx));
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                        $smsresult = curl_exec($ch);
                        $p = explode("|",$smsresult);
                        $sendstatus = $p[0];

                        toastr()->success('Vendor Added Successful!');
                        return redirect()->back();

                    }else{
                        toastr()->error('Please, Enter Password Currectly!');
                        return redirect()->back();
                    }
                    
                }else{
                    toastr()->error('Vendor Exist! Try Another.');
                    return redirect()->back();
                }
            }else{
                toastr()->error('Enter Username!');
                return redirect()->back();
            }

        }
    }

    public function agents()
    {
        $data['agents'] = DB::table('agents')->get();
        return view('admin.agents', $data);
    }

    public function agEdit($id)
    {
        $data['agdata'] = DB::table('agents')->where('id', $id)->first();
        return view('admin.agedit', $data);
    }

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

            $admin = DB::table('admins')->where('id', Auth::guard('admin')->user()->id)->first();
            if ($admin->id == 1) {
                $agdata = DB::table('agents')->where('id', $data['ag_id'])->first();

                $data1['name'] = $data['name'];
                $data1['shop_name'] = $data['shop_name'];
                $data1['email'] = $data['email'];
                $data1['mobile'] = $data['mobile'];
                $data1['address'] = $data['address'];
                $data1['min_ratio'] = $data['percent'];
                $data1['status'] = 0;
                DB::table('agents')->where('id', $agdata->id)->update($data1);

                toastr()->success('Agent Updated Successfully!');
                return redirect('admin/agents');
            }else{
                toastr()->error('Invalid Access!');
                return redirect()->back();
            }
        }
    }

    public function addagent()
    {
        $data['vendors'] = DB::table('vendors')->orderBy('created_at', 'desc')->get();
        return view('admin.addagent', $data);
    }

    public function admagAbility(Request $request)
    {
        $username       = $request->username;
        if (!empty($username)) {
            $username_result   = DB::table('agents')->where('username', $username)->first();
            
            if(empty($username_result)){
                echo 'unique';
            }else {
                echo 'not_unique';
            }
        }else{
            echo 'invalid';
        }      
              
    }

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

            $rules = [
                'username' => 'max:20|regex:/^[a-zA-Z0-9]*$/',
            ];
                
            $customMessages = [
                'username.regex' => 'Username: Only letters and numbers allow! No Space or Symbol.',
            ];

            $this->validate($request,$rules,$customMessages);

            if (!empty($data['username'])) {
                $new_user = DB::table('agents')->where('username', $data['username'])->first();
                if (empty($new_user)) {
                    if (!empty($data['password']) && $data['password'] == $data['confirm_pass']) {

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

                        $data1['name'] = $data['name'];
                        $data1['shop_name'] = $data['shop_name'];
                        $data1['username'] = $data['username'];
                        $data1['email'] = $data['email'];
                        $data1['mobile'] = $data['mobile'];
                        $data1['password'] = Hash::make($data['password']);
                        $data1['address'] = $data['address'];
                        $data1['min_ratio'] = $data['percent'];
                        $data1['vendor_id'] = $vendor->id;
                        $data1['status'] = 0;
                        DB::table('agents')->insert($data1);

                        $data2['agents'] = $vendor->agents+1;
                        DB::table('vendors')->where('id', $vendor->id)->update($data2);

                        //SMS Alert
                        $mob = $data['mobile'];
                        $url = "http://66.45.237.70/api.php";
                        $number= "$mob";
                        $text='Welcome to osbbl.shop! Your Agent Account Register Successful. Login ID: '.$data['username'].' & Password: '.$data['password'].' Hotline: 01940243031';
                        $dataxx= array(
                        'username'=>"01304098181",
                        'password'=>"NKDTFPYZ",
                        'number'=>"$number",
                        'message'=>"$text"
                        );

                        $ch = curl_init(); // Initialize cURL
                        curl_setopt($ch, CURLOPT_URL,$url);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($dataxx));
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                        $smsresult = curl_exec($ch);
                        $p = explode("|",$smsresult);
                        $sendstatus = $p[0];

                        toastr()->success('Agent Added Successful!');
                        return redirect()->back();

                    }else{
                        toastr()->error('Please, Enter Password Currectly!');
                        return redirect()->back();
                    }
                    
                }else{
                    toastr()->error('Agent Exist! Try Another.');
                    return redirect()->back();
                }
            }else{
                toastr()->error('Enter Agent Username!');
                return redirect()->back();
            }

        }
    }

    // Ranks ///////////////////////
    public function agm()
    {
        $data['members'] = DB::table('ranks')->where('rank', 1)->orderBy('created_at', 'desc')->get();
        return view('admin.agm', $data);
    }

    public function dgm()
    {
        $data['members'] = DB::table('ranks')->where('rank', 2)->orderBy('created_at', 'desc')->get();
        return view('admin.dgm', $data);
    }

    public function gm()
    {
        $data['members'] = DB::table('ranks')->where('rank', 3)->orderBy('created_at', 'desc')->get();
        return view('admin.gm', $data);
    }

    public function ed()
    {
        $data['members'] = DB::table('ranks')->where('rank', 4)->orderBy('created_at', 'desc')->get();
        return view('admin.ed', $data);
    }

    public function dirc()
    {
        $data['members'] = DB::table('ranks')->where('rank', 5)->orderBy('created_at', 'desc')->get();
        return view('admin.dirc', $data);
    }

    
}
