<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\SupportRequest;
use App\Http\Controllers\SupportApi;
use Crypt;
use Auth;
class SupportController extends Controller
{
    //

    public static $response ;
    public static $base_url;
    public static $id;
    public static $chat;
    public static $key;
    
    public function __construct()
    {
        SupportController::$response = null;
        SupportController::$chat = null;
        SupportController::$base_url="http://192.168.0.108:1010/api/";

        SupportController::$id=Crypt::encrypt(1);
        SupportController::$key=Crypt::encrypt("RVusQH6OVIuVfid82LVBQMR+Rfm6nexg5aliPodo1kE=");

    }
    public function index()
    {   

        return view('support.manage_support',["response"=>$this->getAllComplaint()]);
    }

    public function storeComplaint(SupportRequest $request){

        $url= SupportController::$base_url."storecomplaint";

    	  $form_collect   = $request->input();



        $form_collect["file_data"] = base64_encode($request->file('file_data'));
           
        
        $client = new \GuzzleHttp\Client();


        $request = $client->post($url,['form_params'=>
                                [
                                "employee_name"=>$form_collect['employee_name'],
                                "title"=>$form_collect["title"],
                                "priority"=>$form_collect["priority"],
                                "description"=>$form_collect["description"],
                                "file_data"=>$form_collect["file_data"],
                                "project_id"=> SupportController::$id,
                                "user_id"=>Crypt::encrypt(Auth::user()->id),
                                "key"=>SupportController::$key


                            ]]);





     
     
        parent::successMessage("Complaint has been submitted");
           return redirect()->back();
    }

    public function getAllComplaint(){
       
        $url= SupportController::$base_url."getcomplaint/";


        $client = new \GuzzleHttp\Client();

        $request = $client->get($url.SupportController::$id);



        $response = json_decode($request->getBody(),true);

     

           return $response;

    }

    public function updateComplaint(SupportRequest $request){

        $url= SupportController::$base_url."updatecomplaint";

        $form_collect   = $request->input();

        $client = new \GuzzleHttp\Client();

        $request = $client->post($url,['form_params'=>
                                [
                                "employee_name"=>$form_collect['employee_name'],
                                "title"=>$form_collect["title"],
                                "priority"=>$form_collect["priority"],
                                "description"=>$form_collect["description"],
                                "file_data"=>$form_collect["file_data"],
                                "id"=>$form_collect["id"],



                            ]]);





      
        parent::successMessage("Complaint has been updated");

           return redirect()->back();

    }


    public function deleteComplaint($id){

        if(isset($id)){
            $url= SupportController::$base_url."deletecomplaint/";

            $client  = new \GuzzleHttp\Client();

            $request = $client->get($url.$id);
        
            $response = json_decode($request->getBody(),true);

                    parent::successMessage("Complaint has been deleted");

           return redirect()->back();
           }
        else{
            return true;
        }   

    }

    public function chatComplaint($id){

        $url= SupportController::$base_url."getchat/";        

        $client = new \GuzzleHttp\Client();

        $request = $client->get($url.SupportController::$id);



        $response = json_decode($request->getBody(),true);

        

        

        return view("support.manage_response",['complaints'=>$response,"chat"=>SupportController::$chat]);
    }


    public function getComments($id){

        $url= SupportController::$base_url."getcomment/";   


        $client = new \GuzzleHttp\Client();

        $request = $client->get($url.$id);



        $response = json_decode($request->getBody(),true);

        return $response;




        

    }

 
public function chatComment($chat,$client_comment){
   if(isset($client_comment)){
        
      
      $url= SupportController::$base_url."storecommentclient/";
      $client = new \GuzzleHttp\Client();
      $request = $client->get($url.$chat.'/'.$client_comment);
      $response = json_decode($request->getBody(),true);
      $view =null;



   foreach($response as $rows){
                   if(isset($rows['client_response'])){
                   $view.='<tr>
                       <td>
                       <div class="row">
                       <div class="col-md-2 col-lg-2 col-sm-2">
                           <span class="avatar" style="background-image: url(./demo/faces/male/9.jpg)"></span>
                       </div>';
                   $view.='<div class="col-md-10 col-lg-10 col-sm-10" style="text-align:left">'.$rows['client_response'].'</div>
                   </td><tr>';
                   }
                   if(isset($rows['gis_response'])){
                       $view.='<tr>
                       <td>
                       <div class="row">
                       <div class="col-md-10 col-lg-10 col-sm-10" style="text-align:right">'.$rows['gis_response'].'</div>
                       ';
                   $view.='<div class="col-md-2 col-lg-2 col-sm-2">
                           <span class="avatar" style="background-image: url(./demo/faces/male/9.jpg)"></span>
                       </div>
                   </td><tr>';


               }

           }


      return $view;
    }
      }



}
