<?php

namespace App\Http\Controllers;
use App\Models\Closer;
use App\Models\User;
use App\Models\Product;
use App\Models\ProductCoupon;
use App\Models\Coupon;
use App\Models\Order;
use App\Models\Comment;
use App\Models\BusinessInformation;
use DataTables;
use Illuminate\Support\Facades\Hash;
use App\Traits\OrderTrait;
use Illuminate\Support\Facades\Validator;

use Illuminate\Http\Request;

class CloserController extends Controller
{
    use OrderTrait;

    public function index(Request $request){

        if ($request->ajax()) {
            $closercoupon = Coupon::where('closer_id',Auth()->User()->id)
                        ->get(); 
                        return Datatables::of($closercoupon)
                        ->make(true);
        }
        $closer = User::where('id',Auth()->User()->id)->get();
        return view('closer.closerdashboard')->with('closer',$closer);
        
        // $coupon = Closer::select()
        // ->join('users','users.id','=','closers.user_id')
        // ->join('coupons','coupons.closer_id','=','users.id')
        // ->where('closers.id','=','234')
        // ->where('coupons.code','=','GMB-200')
        // ->select([
        // 'coupons.id AS couponid',
        // 'coupons.code AS code',
        // 'coupons.discount_price AS discount_price',
        // 'closers.closer_name AS closer_name',
        // 'closers.id AS closerid',
        // 'users.id AS closer_user_id'
        // ])
        // ->get();
        // dd($coupon);
        
    }

    public function check_closer(Request $request)
    {
        if($request->ajax())
        {
        $output="true";
        $closer = Closer::where('id','=',$request->search)->get();
        if($closer->isNotEmpty())
        {
            foreach($closer as $close)
        return Response()->json([
    'name' => $close->closer_name, 
    'closerid' => $close->id],200);
        }else{
            return Response('closer not found');
        }
        }
    }

    public function add_coupon()
    {
        $closer = User::where('id',Auth()->User()->id)->get();
        return view('closer/addcoupon')->with('closer',$closer);
    }

    public function insert_coupon(Request $request)
    {
       
            $validator = Validator::make($request->all(),[
                'code'=>'required|unique:coupons',
                'discount_price'=>'required',
                'closer_id' => 'required'
            ]);
    
            if ($validator->fails()) {
                return redirect('closer/dashboard')->withErrors($validator)->withInput();
            }
            else{
                $checkcoupon = Coupon::where('code',$request->coupon)->exists();
                if($checkcoupon)
                {
                    return redirect('closer/dashboard')->withErrors($validator)->withInput();   
                }else{
                    $coupondata = [
                        'code' => $request->code,
                        'discount_price' => $request->discount_price,
                        'closer_id' => $request->closer_id
                    ];
                
                    Coupon::create($coupondata);
                    return redirect('closer/dashboard')->with('success','New Coupon Code Added Successfully');
                }
            }
    }

    public function show_orders(Request $request){
        if ($request->ajax()) {
            $orders = Order::select()
                        ->join('business_information', 'business_information.client_id', '=', 'orders.client_id')
                        ->join('users', 'users.id', '=', 'business_information.client_id')
                        ->join('products','products.id','=','business_information.pid')
                        ->get('orders.payment_method','orders.id AS orderid','orders.ordernumber','orders.order_status', 'orders.client_id AS clientid', 'orders.product_id AS productid','orders.created_at','orders.invoice_id','business_information.bname AS bname','business_information.bemail AS bemail','business_information.bphone AS bphone', 'business_information.baddress AS baddress','business_information.website_url', 'business_information.radius_image AS radius_image','business_information.radius_address','business_information.radius','closers.closer_name AS closer_name','business_information.services AS services','products.productname AS productname','products.productdescription','users.first_name','users.last_name');
            // dd($orders);
            return Datatables::of($orders)
            ->addIndexColumn()
            ->addColumn('action', function($row){
                   $btn = '<a href="orderdetails/'.$row->client_id.'" class="edit btn btn-primary text-white btn-sm">View</a>';
                    return $btn;
            })
            ->rawColumns(['action'])
            ->make(true);
        }
        return view('closer.closers_order_list');
    }

    public function orderdetails(Request $request)
    {
        return view('closer.orderdetails');
    }
    public function view_order_detail(Request $request){

        $orderdetail = $this->get_business_order($request->id);

        foreach($orderdetail as $orderid)
        {
            $orderidd = $orderid -> order_id;
        }
        
        $comment = Comment::select()
                    ->join('orders','orders.id','=','comments.order_id')
                    ->join('users','users.id','=','comments.closer_id')
                    ->where('comments.order_id',$orderidd)
                    ->get('orders.id AS orderid','orders.ordernumber','orders.order_status','orders.invoice_id','orders.payment_method','users.first_name','users.last_name','comments.created_at'); 
                    return view('closer.orderdetails')->with(['orderdetail'=>$orderdetail, 'comments' => $comment]);
       }
       

       public function add_comment(Request $request){
   
        $cid = Auth()->User()->id;
        $comment = new Comment;
        $comment->order_id = $request->order_id;
        $comment->closer_id = $cid;
        $comment->comments = $request->comments;

        $comment->save();

        $orderdetail = $this->get_business_order($request->userid);

        foreach($orderdetail as $orderid)
        {
            $orderidd = $orderid -> order_id;
        }
        
        $comments = Comment::select()
                    ->join('orders','orders.id','=','comments.order_id')
                    ->join('users','users.id','=','comments.closer_id')
                    ->where('comments.order_id',$orderidd)
                    ->get('orders.id AS orderid','orders.ordernumber','orders.order_status','orders.invoice_id','orders.payment_method','users.first_name','users.last_name','comments.created_at');
        return response()->json($comments);
        
       }

       public function showcomments(Request $request){

        $orderdetail = $this->get_business_order($request->id);

        foreach($orderdetail as $orderid)
        {
            $orderidd = $orderid -> order_id;
        }
        
        $comments = Comment::select()
                    ->join('orders','orders.id','=','comments.order_id')
                    ->join('users','users.id','=','comments.closer_id')
                    ->where('comments.order_id',$orderidd)
                    ->get('orders.id AS orderid','orders.ordernumber','orders.order_status','orders.invoice_id','orders.payment_method','users.first_name','users.last_name','comments.created_at'); 
                    return response()->json($comments);
       }
}
