<?php
namespace App\Http\Controllers\Api\v1;

use Illuminate\Http\Request;
use App\Manager;
use App\Feedback;

/**
 * CmsController Class Doc Comment
 *
 * @category Class
 * @package  Api
 * @author   Ketan Patel - ketan.patel@brainvire.com
 *
*/
class WidgetController extends Controller
{

    public function __construct()
    {
     //$this->middleware('oauth', ['except' => ['index']]);
     //$this->middleware('auth:api', ['except' => ['index']]);
    }
    /**
     * Get the specified resource from storage.
     *
     * @param  int $slug
     * @return \Illuminate\Http\Response
     */
    public function show()
    {
        $managerId   = filter_input(INPUT_GET, 'managerId');
        $feedback    = filter_input(INPUT_GET, 'feedback');
        $label    = filter_input(INPUT_GET, 'label');
        
        $manager = Manager::where('user_id', $managerId)->get()->first();
        $html    = '';
        $html2   = '';
        if (!empty($manager)) {
            $orgId = $manager->organization_id;
            $results   = Feedback::select(
                'id',
                'customer_name',
                'profile_pic',
                'feedback',
                'rating',
                'email',
                'created',
                'user_id'   
            )->take($feedback)
             ->where('organization_id', $orgId)
             ->where('feedback', '<>', '')
             ->orderBy('id', 'desc')
             ->get()
             ->toArray();
            foreach ($results as $k=>$v) {
                    
                    $html.= "<div>";
                    $html.= "<p>".$v['customer_name']."</p>";
                    $html.= '<p>'.$v['feedback'].'</p>';
                    $html.= '<p>'.$v['rating'].'/5</p>';
                  //      $html.= '<p>Email:'.$v['email'].'</p>';
                   // $date = new \DateTime($v['created']);
                  //  $ddf  = $date->format('Y-m-d');
                  //  $html.= '<p>'.$ddf.'</p>';
                    $html.= "</div>";
                    $html2.=$this->getHtmlHorizontal($v,$k,$results,$label);
                
            }
            $html_array=array('vertical'=>$html,'horizontal'=>'<div class="script_testimonial">'.$html2."</div>");
            echo json_encode($html_array);
        }
    }
    public function get_profile_pic($obj){
        if(isset ($obj['profile_pic']) && $obj['profile_pic'] != NULL){
            $profile = DOMAIN_URL.'/customers/'.$obj['user_id'].'/'.$obj['profile_pic'];
        }else{
            $profile=DOMAIN_URL.'/profile.jpeg';
        }
        return $profile;
    }
    public function getHtmlHorizontal($v,$k,$results,$label){
        //make html for horizontal
        $profile_pic=$this->get_profile_pic($v);
        $html2='';
        $html2.='<div class="slideshow-container"><div class="col-md-12 col-xs-12">';
        $html2.='<div class="mySlides slick-track">
            <span class="subtitle"></span>
            <h3>'.$label.'</h3>
            <figure><img src="'.$profile_pic.'" alt=""></figure>
            <figcaption>'.$v['feedback'].'</figcaption>
               <div class="clientName">'.$v['customer_name'].'</div>
               </div></div></div>';
        if(count($results)-1 == $k && count($results) > 1 ){
            $html2.='<div class="dot-container">';
            for($i=1;$i<=count($results);$i++){
                $html2.='<span class="dot" onclick="currentSlide('.$i.')"></span>';
            }
            $html2.='</div>';
        }
         if(count($results)-1==$k && count($results) > 1 ){
                          //  $html2.='<a class="prev" onclick="plusSlides(-1)">❮</a>
                         //   <a class="next" onclick="plusSlides(1)">❯</a>';
                        }
        return $html2; 
    }
}
