<?php
    
namespace App\Http\Controllers;
     
use Illuminate\Http\Request;
use Session;
use Stripe;
use App\Models\User;
use App\Models\Product;
use App\Models\Order;
use App\Models\ProductCoupon;
use App\Models\BusinessInformation;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

use App\Traits\OrderTrait;
use Mail;

class PaymentController extends Controller
{
    use OrderTrait;

    /**
     * success response method.
     *
     * @return \Illuminate\Http\Response
     */

    public function stripe(Request $request)
    {
        $business_info = BusinessInformation::where('client_id',Auth()->User()->id)
        ->get();
        foreach($business_info as $business)
        $checkcoupon = ProductCoupon::select()
                                     ->join('business_information','business_information.id','=','product_coupons.business_id')
                                     ->where('product_coupons.business_id','=',$business->id)
                                     ->where('business_information.closerid','!=','8888')
                                     ->exists();
        if($checkcoupon)
        {
            $business_info = BusinessInformation::select()
            ->join('product_coupons','product_coupons.business_id','=','business_information.id')
            ->join('products','products.id','=','product_coupons.product_id')
            ->join('coupons','coupons.id','=','product_coupons.coupon_id')
            ->join('users','users.id','=','coupons.closer_id')
            ->join('closers','closers.user_id','=','users.id')
            ->where('business_information.id','=',$business->id)
            ->select(
                'business_information.id AS bid',
                'business_information.client_id AS client_id',
                'business_information.bname AS bname',
                'business_information.bemail AS bemail',
                'business_information.bphone AS bphone',
                'business_information.website_url AS website',
                'business_information.baddress AS baddress',
                'business_information.working_hours AS workinghours',
                'business_information.description AS bdesc',
                'business_information.radius_address AS radius_address',
                'business_information.radius AS radius',
                'business_information.radius_image',
                'business_information.services',
                'business_information.closerid AS bcloserid',
                'business_information.category AS category',
                'business_information.timezone',
                'products.price AS oldprice',
                'product_coupons.id AS couponid',
                'product_coupons.new_discount_price AS newprice',
                'products.productname',
                'products.productdescription',
                'coupons.code',
                'coupons.discount_price',
                'closers.closer_name',
                )
            ->get();
            // dd($business_info);
            $product = $this->get_single_product($request->packageid);
        if(BusinessInformation::where('client_id',Auth()->User()->id)->exists())
        {
           $status =  Order::where('client_id',Auth()->user()->id)->get();
           if(Order::where('client_id',Auth()->User()->id)->exists()){
            foreach($status as $stat)
            {
                $statuss = $stat->order_status;
            }
            if($statuss == 'pending')
            {      
                session()->put('paymentpending','order saved & payment pending');
                return view('stripe')->with(['data'=>$business_info, 'product'=>$product]);
            }
            else{            
                return view('stripe')->with(['data'=>$business_info, 'product'=>$product]);
            }  
           }
           else{    
            return view('stripe')->with(['data'=>$business_info, 'product'=>$product]);
           }            
        }
        else{
            session()->put('infopending','please add your business information to complete your business profile');
            return view('home');
        }
        }
        else{

            $business_info = BusinessInformation::where('client_id',Auth()->User()->id)
            ->join('users','users.id','=','business_information.client_id')
            ->join('products','products.id','=','business_information.pid')
            ->join('closers','closers.id','=','business_information.closerid')
             ->select('business_information.bname AS bname',
             'business_information.bemail AS bemail',
             'business_information.bphone AS bphone', 
             'business_information.baddress AS baddress',
             'business_information.website_url AS website', 
             'business_information.radius_address',
             'business_information.description AS bdesc',
             'business_information.radius AS radius',
             'closers.closer_name AS closer_name',
             'business_information.services AS services',
             'products.productname AS productname', 
             'products.price AS oldprice',
             'products.productdescription',
             'users.first_name',
             'users.last_name',
             'users.email',
             'users.phone')
             ->get();

             $product = $this->get_single_product($request->packageid);
             if(BusinessInformation::where('client_id',Auth()->User()->id)->exists())
             {
                $status =  Order::where('client_id',Auth()->user()->id)->get();
                if(Order::where('client_id',Auth()->User()->id)->exists()){
                 foreach($status as $stat)
                 {
                     $statuss = $stat->order_status;
                 }
                 if($statuss == 'pending')
                 {      
                     session()->put('paymentpending','order saved & payment pending');
                     return view('stripe')->with(['data'=>$business_info, 'product'=>$product]);
                 }
                 else{            
                     return view('stripe')->with(['data'=>$business_info, 'product'=>$product]);
                 }  
                }
                else{    
                 return view('stripe')->with(['data'=>$business_info, 'product'=>$product]);
                }            
             }
             else{
                 session()->put('infopending','please add your business information to complete your business profile');
                 return view('home');
             }
        } 
    }
    
    /**
     * success response method.
     *
     * @return \Illuminate\Http\Response
     */
   /**

 * success response method.

 *

 * @return \Illuminate\Http\Response

 */

public function stripePost(Request $request)

{
    
    try{

    Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));

  

    $customer = Stripe\Customer::create(array(

            "address" => [

                    "line1" => $request->line1,

                    "postal_code" => $request->postal_code,

                    "city" => $request->city,

                    "state" => $request->state,

                    "country" => $request->country,

                ],

            "email" => $request->email,

            "name" => $request->name,

            "source" => $request->stripeToken

         ));

  

    Stripe\Charge::create ([

            "amount" => 10 * 100,

            "currency" => "usd",

            "customer" => $customer->id,

            "description" => "Payment from myaio portal",

            "shipping" => [

              "name" => "Jenny Rosen",

              "address" => [

                "line1" => "510 Townsend St",

                "postal_code" => "98140",

                "city" => "San Francisco",

                "state" => "CA",

                "country" => "US",

              ],

            ]

    ]); 
   
        if(Order::where('client_id',$request->client_id)->exists())
        {
            $status = 'Paid';
            
        Order::where('client_id',$request->client_id)->update(['order_Status'=>$status]);
        if(BusinessInformation::where('client_id',Auth()->User()->id)->exists())
        {   
            $business_info = $this->get_order_invoice();
            foreach($business_info as $business)
                    {
                        $orderno = $business->ordernumber;
                        $orderstat = $business->order_status;
                        $bname = $business->bname;
                        $bemail = $business->bemail;
                        $bphone = $business->bphone;
                        $baddress = $business->baddress;
                        $radius_address = $business->radius_address;
                        $radius = $business->radius;
                        $firstname = $business->first_name;
                        $lastname = $business->last_name;
                        $phone = $business->phone;
                        $email = $business->email;
                        $pname = $business->productname;
                        $pdesc = $business->productdescription;
                        $price = $business->price; 

                    }

                    $maildata = [
                       'ordernumber'=>$orderno,
                       'status'=> $orderstat,
                       'bname'=>$bname ,
                       'bemail'=>$bemail,
                       'bphone'=>  $bphone,
                       'baddress'=>  $baddress,
                       'radiusaddress'=>  $radius_address,
                       'radius'=>  $radius,
                       'first_name'=>$firstname,
                       'last_name'=>$lastname,
                       'phone'=>$phone,
                       'email'=>$email,
                       'pname'=>$pname,
                       'pdesc'=>$pdesc,
                       'price'=>$price,
                    ];
                    $useremail['to'] = $email; 
                    Mail::send('email',$maildata,function($messages) use ($useremail){
                        $messages->to($useremail['to']);
                        $messages->subject('Pending Order Payment Recieved');
                    });

            Session::flush('success', 'Payment successful!');
            return view('orderinvoice')->with(['order'=>$business_info]);
        }
        else{
            session()->put('infopending','please add your business information to complete your business profile');
            return view('home');
        }
        
        }
    else{
        $validator = Validator::make($request->all(),[
            'client_id'=>'required|unique:orders',
           'product_id'=>'required',
        ]);
    
        if ($validator->fails()){
            return redirect('/');
        }
            else{
                $order = [
                    'client_id'=>$request->client_id,
                    'product_id'=>$request->product_id,
                    'ordernumber'=>Str::random(5),
                    'order_status' => 'Paid'
                ];
                Order::create($order); 
                if(BusinessInformation::where('client_id',Auth()->User()->id)->exists())
                {   
                    $business_info = $this->get_order_invoice();                    
                    foreach($business_info as $business)
                    {
                        $orderno = $business->ordernumber;
                        $orderstat = $business->order_status;
                        $bname = $business->bname;
                        $bemail = $business->bemail;
                        $bphone = $business->bphone;
                        $baddress = $business->baddress;
                        $radius_address = $business->radius_address;
                        $radius = $business->radius;
                        $firstname = $business->first_name;
                        $lastname = $business->last_name;
                        $phone = $business->phone;
                        $email = $business->email;
                        $pname = $business->productname;
                        $pdesc = $business->productdescription;
                        $price = $business->price;
                        $closername = $business->closer_name;
                        $service = $business->services;
                    }

                    $maildata = [
                       'ordernumber'=>$orderno,
                       'status'=> $orderstat,
                       'bname'=>$bname ,
                       'bemail'=>$bemail,
                       'bphone'=>  $bphone,
                       'baddress'=>  $baddress,
                       'radiusaddress'=>  $radius_address,
                       'radius'=>  $radius,
                       'first_name'=>$firstname,
                       'last_name'=>$lastname,
                       'phone'=>$phone,
                       'email'=>$email,
                       'pname'=>$pname,
                       'pdesc'=>$pdesc,
                       'price'=>$price,
                       'service' => $service,
                       'closername' => $closername,
                    ];
                    $useremail['to'] = $email; 
                    Mail::send('email',$maildata,function($messages) use ($useremail){
                        $messages->to($useremail['to']);
                        $messages->subject('New Order Recieved');
                    });
                    Session::flush('success', 'Payment successful!');
                    return view('orderinvoice')->with(['order'=>$business_info]);
                }
                else{
                    session()->put('infopending','please add your business information to complete your business profile');
                    dd('error');
                    return view('home');
                }
            }
    }    

    }
    catch (\Exception $ex) {
        return $ex->getMessage();
    }

}
}