<?php

// namespace App\Http;

// use App\Models\contact1;

// use App\Models\contact2;

// use App\Models\contact3;

// use App\Models\chat;

// use App\Models\User;

// use App\Models\Agent;

// use App\Models\AdminLogin;

use Illuminate\Http\Request;

// use Illuminate\Support\Facades\DB;

// use Illuminate\Support\Facades\Auth;

// use Illuminate\Support\Facades\Hash;

// use Illuminate\Support\Facades\Validator;



// class Helper{

    
    
     function AssignOrderView($id,$agentId){

        $id = decrypt($id);
        $agentId = decrypt($agentId);

        $user = Auth::guard('AgentLogin')->user();

        $agent_assinged = DB::connection('mysql')->table('agent_assinged')->where('agent_order_id',$id)->where('agent_id',$agentId)->first();

        $agents = DB::connection('mysql')->table('agents')->where('id',$agentId)->first();

        $product = [];

        //$agent_product_list=[];

        $product_maped = DB::connection('mysql')->table('assigned_agent_product_map')->where('agent_assinged_id',$agent_assinged->id)->get();

        $orders = DB::connection('mysql')->table('orders')->where('id',$agent_assinged->OID)->first();

        $order_details = DB::connection('mysql')->table('order_details')->where('order_id',$orders->id)->get();

        $orders_Msg = DB::connection('mysql')->table('order_messages')->where('order_id',$agent_assinged->OID)->first();

        $agent_product_list = DB::connection('mysql')->table('assigned_agent_product_map')
                ->join('agent_product_list', 'assigned_agent_product_map.product_id', '=', 'agent_product_list.id')
                ->where('agent_assinged_id',$agent_assinged->id)->get();

        //dd($agent_product_list);


            // foreach($product_maped as $product_mapeds){

            //     if($agent_assinged->id == $product_mapeds->agent_assinged_id){

            //         // $products[] = DB::connection('mysql')->table('products')->where('id',$product_mapeds->product_id)->first();

            //          $agent_product_list[] =  DB::connection('mysql')->table('agent_product_list')->where('id',$product_mapeds->product_id)->first();



            //     }

            // }

            $data=compact('agent_assinged','product_maped','orders','order_details','orders_Msg','user','agent_product_list','agents','agentId');



            $agent_assinged=$data['agent_assinged'];

            $product_maped=$data['product_maped'];

            $orders=$data['orders'];

            $order_details=$data['order_details'];

            $orders_Msg=$data['orders_Msg'];

            $user=$data['user'];

            $agent_product_list=$data['agent_product_list'];

            $agents=$data['agents'];

            $agentId=$data['agentId'];

            // return $data['agent_assinged'];

        // return $agent_product_list;

        // return view('OrderDetails',compact('agent_assinged','product_maped','orders','order_details','user','agent_product_list','agents','agentId'));

            return view('admin.order.assign_order_view',compact('agent_assinged','product_maped','orders','order_details','orders_Msg','user','agent_product_list','agents','agentId','id'));

    }



    function UpdateAssignOrderStatus($orderId,$agentID,Request $request){
        
        $orderId = decrypt($orderId);
        
        $agentID = decrypt($agentID);
        
        $extra = $request['extra'];

        $deliVery = $request['deliveryCharge'];

        $reason = $request['reason'];

        $status = $request['status'];

        $track_no = $request['track_no'];

        $tracking_link =  $request['tracking_link'];

        

        $rules = ['status' => 'required'];



        if ($status == 'S') {

            $rules['track_no'] = 'required';

        } elseif (in_array($status, ['H', 'CL'])) {

            $rules['reason'] = 'required';

        }



        $messages = [

            'status.required' => 'Please enter the required field for status.',

            'track_no.required' => 'Please provide the tracking number.',

            'reason.required' => 'Please provide a reason.',

        ];



        $validator = Validator::make($request->all(), $rules, $messages);



        if ($validator->fails()) {

            return redirect()->back()->withErrors($validator)->withInput();

        }



        DB::connection('mysql')->table('agent_assinged')->where('agent_order_id',$orderId)->where('agent_id',$agentID)->update([	

                'extra'=> $extra,

                'Deliver_charge' => $deliVery,

                'reason' => $reason,

                'track_no'=>$track_no,

                'tracking_link' => $tracking_link,

                'status' => $status,

            ]);



      

        return back();

    }    


    




