<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Http\Requests\Api\CommentRequest;
use App\Http\Requests\Api\ContactingRequest;
use App\Http\Requests\Api\FavoriteRequest;

use App\Comment;
use App\Category;
use App\Contact;
use App\ModelDescription;
use App\Language;
use App\WebsiteVisitor;
use App\ProductPhoto;
use App\CategoryDescription;
use App\Models;
use App\Product;
use App\Device;
use App\Writer;
use App\Follow;
use App\WriterDescription;
use App\ProductDescription;
use App\Favorite;
use App\Page;
use App\Day;
use App\Setting;
use App\News;
use App\Gallery;
use App\NewDescription;
use App\PageDescription;
use App\Video;
use App\VideoDescription;

class MainController extends Controller
{
    public function banners(){
        $arr = array();
        $banners = Gallery::whereNotIn('id',['2','1'])->get();
        foreach ($banners as $key => $row) {
            $arr[$key]['id'] = $row['id'];
            $arr[$key]['image'] = asset('').$row['image'];
            $arr[$key]['link'] = $row['link'];
        }
        return response()->json($arr, 200);
    }
    public function videos(Request $request){
        $lang = Language::where('local',$request->header('language'))->first();
        $lang_id = $lang->id;
        $arr = array();
        $videos = Video::all();
        foreach ($videos as $key => $row) {
            $arr[$key]['id'] = $row['id'];
            $arr[$key]['image'] = asset('').$row['image'];
            $arr[$key]['video'] = $row['video'];
            $name = VideoDescription::where('video_id',$row['id'])->where('language_id',$lang_id)->first();
             $arr[$key]['name'] =$name['name']; 
        }
        return response()->json($arr, 200);
    }

    public function settings(){
        $email = Setting::where('key','email')->first();
        $phone = Setting::where('key','phone')->first();
        $address = Setting::where('key','address')->first();
        $youtube = Setting::where('key','youtube')->first();
        $twitter = Setting::where('key','twitter')->first();
        $facebook = Setting::where('key','facebook')->first();
        $whatsapp = Setting::where('key','whatsapp')->first();
        $context = Setting::where('key','context')->first();
        $banner = Gallery::where('id','2')->first();
        $link = Gallery::where('id','2')->first();
        
        $arr = array();
        $arr['email']= $email->value;
        $arr['phone']= $phone->value;
        $arr['address']= $address->value;
        $arr['youtube']= $youtube->value;
        $arr['twitter']= $twitter->value;
        $arr['facebook']= $facebook->value;
        $arr['whatsapp']= $whatsapp->value;
        $arr['context']= $context->value;
        if($banner->show == 'yes')
        {
            $arr['banners'][0]['banner']= asset('').$banner->image;
            $arr['banners'][0]['link']= $link->link;
        }
        else{
            $arr['banners'][0]['banner']= null;
            $arr['banners'][0]['link']= null;
        }
        
        
        return response()->json($arr, 200);
    }
    public function download(Request $request){
        $daily = Day::where('date',date('Y-m-d'))->first();
        if(!$daily){
            return response()->json(['data'=>'Not Exist'], 200); 
        }
          $file= public_path(). $daily->file;

    $headers = array(
              'Content-Type: application/pdf',
            );
 //return \Response::download($file, 'Daily.pdf', $headers);
  
 return response()->json(['data'=>asset(''). $daily->file], 200);
    }
    public function home(Request $request){
        $lang = Language::where('local',$request->header('language'))->first();
        $lang_id = $lang->id;
        //dd($request->device_token);
        if(!is_null($request->ip) && !is_null($request->device_token)){
            $device = Device::where('ip',$request->ip)->first();
            if(!$device){
                Device::create(['device_token'=>$request->device_token,'ip'=>$request->ip]);
            }
            else{
                $device->update(['device_token'=>$request->device_token]);
            }
        }
         
         $products = Product::whereNotIn('id',fake_ids())->where('show','yes')->orderBy('id','DESC')->skip($request->paginate*10)->take(10)->get();
         $counter = Product::whereNotIn('id',fake_ids())->where('show','yes')->orderBy('id','DESC')->get();
         $max = ceil(count($counter)/10)-1;
         $arr = array();
         WebsiteVisitor::create();
        foreach($products as $key=>$product)
         {
         $productname = ProductDescription::where('product_id',$product['id'])->where('language_id',$lang_id)->first();
         $arr[$key]['id'] = $product['id']; 
         $arr[$key]['name'] = $productname['name']; 
          if(!is_null($product->model_id)){
         $modelname = ModelDescription::where('model_id',$product['model_id'])->where('language_id',$lang_id)->first();
         $arr[$key]['model'] = $modelname['name']; 
         
         }else{
         $categoryname = CategoryDescription::where('category_id',$product['category_id'])->where('language_id',$lang_id)->first();
             $arr[$key]['model'] =$categoryname['name']; 
             
             
         }
         $arr[$key]['color'] = $product->category->color; 
         $arr[$key]['image'] = is_null($product['image'])? null : asset('').$product['image']; 
         
        $now = time(); 
        $your_date = strtotime($product['created_at']);
        $diff = $now - $your_date;
         
        if($lang->local == 'ar'){
                $since = 'منذ';
            }
            else{
                $since = 'since';
            }
        if($diff < 60) //seconds
        {
            if($lang->local == 'ar'){
                $obj = 'ثانية';
            }
            else{
                $obj = 'seconds';
            }
            $time = $since.' '.round($diff ) . ' '. $obj;
        }
        else{
            if($diff < (60*60) && $diff >= 60) //minutes
            {
                if($lang->local == 'ar')
                {
                $obj = 'دقائق';
                }
                else{
                $obj = 'minutes';
                }
            $time = $since.' '.round($diff / (60  )) . ' '. $obj;
            }
            elseif($diff < (60*60*24) && $diff >= (60*60)) //hours
            {
                if($lang->local == 'ar')
                {
                $obj = 'ساعة';
                }
                else{
                $obj = 'hours';
                }
            $time = $since.' '.round($diff / (60 *60 )) . ' '. $obj;
                
            }
            elseif($diff < (60*60*24*30) && $diff >= (60*60*24)) //days
            {
                if($lang->local == 'ar')
                {
                $obj = 'يوم';
                }
                else{
                $obj = 'days';
                }
            $time = $since.' '.round($diff / (60 *60 *24)) . ' '. $obj;
                
            }
            elseif($diff < (60*60*24*30*12) && $diff >= (60*60*24*30)) //months
            {
                if($lang->local == 'ar')
                {
                $obj = 'شهر';
                }
                else{
                $obj = 'months';
                }
            $time = $since.' '.round($diff / (60 *60 *24*30)) . ' '. $obj;
                
            }
            elseif($diff >= (60*60*24*30*12) ) //years
            {
                if($lang->local == 'ar')
                {
                $obj = 'سنة';
                }
                else{
                $obj = 'years';
                }
            $time = $since.' '.round($diff / (60*60*24*30*12 )) . ' '. $obj;
                
            }
            
        }
        $arr[$key]['time'] = $time;
        
        }
         return response()->json(['max'=>$max,'data'=>$arr], 200);
    }
    public function pages(Request $request,$id){
        $lang = Language::where('local',$request->header('language'))->first();
        $lang_id = $lang->id;
         $arr = array();
        
        $page = Page::find($id);
        $pagedescription = PageDescription::where('language_id',$lang_id)->where('page_id',$id)->first();
        $arr['image'] = asset('').$page->image;
        $arr['title']=$pagedescription['title'];
        $arr['description']=$pagedescription['description'];
        return response()->json($arr, 200); 
         
        
            
             
         
    }
     public function page(Request $request){
         $lang = Language::where('local',$request->header('language'))->first();
        $lang_id = $lang->id;
         $arr = array();
     $pages = Page::cursor();
             foreach($pages as $key=>$row){
                 $pagedescription = PageDescription::where('language_id',$lang_id)->where('page_id',$row->id)->first();
                 $arr[$key]['image'] = asset('').$row->image;
        $arr[$key]['title']=$pagedescription['title'];
        $arr[$key]['description']=$pagedescription['description'];
        
             }
             return response()->json($arr, 200); 
     }
    
    public function favorite(Request $request){
        $device = Device::where('ip',$request->header('ip'))->first();
        $lang = Language::where('local',$request->header('language'))->first();
        $lang_id = $lang->id;
        
        if(!$device){
           $device = Device::create(['ip'=>$request->header('ip')]);
        }
        $device_id = $device->id;
        if(isset($request->product_id) && !is_null($request->product_id)){
        $fav = Favorite::where('product_id',$request->product_id)->where('device_id',$device_id)->first();
        if(!$fav){
        Favorite::create([
            'product_id'=>$request->product_id,
            'device_id'=>$device_id,
            ]);
        }
        else{
            $fav->delete();
        }
        }
        else{
            $fav = Favorite::where('device_id',$device_id)->delete();
        }
        
        
        $arr = array();
        
        $fav = Favorite::where('device_id',$device_id)->pluck('product_id');
        $products = Product::whereNotIn('id',fake_ids())->where('show','yes')->whereIn('id',$fav)->get();
        foreach($products as $key=>$product){
              $productname = ProductDescription::where('product_id',$product['id'])->where('language_id',$lang_id)->first();
         $arr[$key]['id'] = $product['id']; 
         $arr[$key]['name'] = $productname['name']; 
         if(!is_null($product->model_id)){
         $modelname = ModelDescription::where('model_id',$product['model_id'])->where('language_id',$lang_id)->first();
         $arr[$key]['model'] = $modelname['name']; 
         
         }else{
         $categoryname = CategoryDescription::where('category_id',$product['category_id'])->where('language_id',$lang_id)->first();
             $arr[$key]['model'] =$categoryname['name']; 
             
             
         }
         $arr[$key]['color'] = $product->category->color; 
         $arr[$key]['image'] =is_null($product['image'])? null : asset('').$product['image']; 
         
       $now = time(); 
        $your_date = strtotime($product['created_at']);
        $diff = $now - $your_date;
         
        if($lang->local  == 'ar'){
                $since = 'منذ';
            }
            else{
                $since = 'since';
            }
        if($diff < 60) //seconds
        {
            if($lang->local  == 'ar'){
                $obj = 'ثانية';
            }
            else{
                $obj = 'seconds';
            }
            $time = $since.' '.round($diff ) . ' '. $obj;
        }
        else{
            if($diff < (60*60) && $diff >= 60) //minutes
            {
                if($lang->local  == 'ar')
                {
                $obj = 'دقائق';
                }
                else{
                $obj = 'minutes';
                }
            $time = $since.' '.round($diff / (60  )) . ' '. $obj;
            }
            elseif($diff < (60*60*24) && $diff >= (60*60)) //hours
            {
                if($lang->local  == 'ar')
                {
                $obj = 'ساعة';
                }
                else{
                $obj = 'hours';
                }
            $time = $since.' '.round($diff / (60 *60 )) . ' '. $obj;
                
            }
            elseif($diff < (60*60*24*30) && $diff >= (60*60*24)) //days
            {
                if($lang->local  == 'ar')
                {
                $obj = 'يوم';
                }
                else{
                $obj = 'days';
                }
            $time = $since.' '.round($diff / (60 *60 *24)) . ' '. $obj;
                
            }
            elseif($diff < (60*60*24*30*12) && $diff >= (60*60*24*30)) //months
            {
                if($lang->local  == 'ar')
                {
                $obj = 'شهر';
                }
                else{
                $obj = 'months';
                }
            $time = $since.' '.round($diff / (60 *60 *24*30)) . ' '. $obj;
                
            }
            elseif($diff >= (60*60*24*30*12) ) //years
            {
                if($lang->local  == 'ar')
                {
                $obj = 'سنة';
                }
                else{
                $obj = 'years';
                }
            $time = $since.' '.round($diff / (60*60*24*30*12 )) . ' '. $obj;
                
            }
            
        }
        $arr[$key]['time'] = $time;
        }
        return response()->json($arr,  200); 
        
        
        
    }
    
    
    public function following(Request $request){
        $device = Device::where('ip',$request->header('ip'))->first();
        $lang = Language::where('local',$request->header('language'))->first();
        $lang_id = $lang->id;
        
        if(!$device){
           $device = Device::create(['ip'=>$request->header('ip')]);
        }
        $device_id = $device->id;
        Follow::where('device_id',$device_id)->delete();
        if(isset($request->categories)){
        if(count($request->categories)){
        foreach($request->categories as $row){
            Follow::create([
                'type'=>'1',
                'target_id'=>$row,
                'device_id'=>$device_id
                ]);
        }
        }
        }
        if(isset($request->models)){
        if(count($request->models)){
        foreach($request->models as $row){
             Follow::create([
                'type'=>'2',
                'target_id'=>$row,
                'device_id'=>$device_id
                ]);
        }
        }
        }
        
        $categories = Category::cursor();
       foreach($categories as $keyCat=>$category){
         $arr[$keyCat]['id'] = $category['id'];
         $arr[$keyCat]['banner'] = is_null($category->banner)? null :asset('').$category['banner']; 
        
         $categpryname = CategoryDescription::where('category_id',$category['id'])->where('language_id',$lang_id)->first();
         $arr[$keyCat]['name'] = $categpryname['name']; 
         $arr[$keyCat]['follow'] = devicecat($category['id'],$device_id); 
         
         foreach(modelscategory($category['id']) as $keyModel=>$model){
             $arr[$keyCat]['models'][$keyModel]['id'] = $model['id']; 
             $modelname = ModelDescription::where('model_id',$model['id'])->where('language_id',$lang_id)->first();
             $arr[$keyCat]['models'][$keyModel]['image'] =  asset('').$model['image']; 
             $arr[$keyCat]['models'][$keyModel]['name'] = $modelname['name']; 
             $arr[$keyCat]['models'][$keyModel]['banner'] = is_null($model->banner)? null :asset('').$model['banner']; 
             $arr[$keyCat]['models'][$keyModel]['follow'] = devicemodel($model['id'],$device_id); 
         }
     }
         
        
        return response()->json($arr,  200); 
        
       /* if($request->header('language') == 'ar')
        {
            $message = 'تم بنجاح';
        }
        else
        {
            $message = 'Saved Successfully';
        }
         return response()->json($message,  200); 
        */
        
    }
    public function contacting(ContactingRequest $request){
        $contact = Contact::create($request->all());
        if($request->header('language') == 'ar')
        {
            $message = 'تم بنجاح';
        }
        else
        {
            $message = 'Saved Successfully';
        }
         return response()->json($message,  200); 
        
         //return response()->json($contact,  200); 
    }
    public function search(Request $request){
         $lang = Language::where('local',$request->header('language'))->first();
        $lang_id = $lang->id;
         $arr = array();
         $ids = ProductDescription::where('name','LIKE', '%'.$request->search.'%')->pluck('product_id');
         $max = ceil(count($ids)/10)-1;
         $products = Product::whereNotIn('id',fake_ids())->where('show','yes')->whereIn('id',$ids)->skip($request->paginate*10)->take(10)->get();
         
        foreach($products as $key=>$product)
         {
         $productname = ProductDescription::where('product_id',$product['id'])->where('language_id',$lang_id)->first();
         $arr[$key]['id'] = $product['id']; 
         $arr[$key]['name'] = $productname['name']; 
         if(!is_null($product->model_id)){
         $modelname = ModelDescription::where('model_id',$product['model_id'])->where('language_id',$lang_id)->first();
         $arr[$key]['model'] = $modelname['name']; 
         
         }else{
         $categoryname = CategoryDescription::where('category_id',$product['category_id'])->where('language_id',$lang_id)->first();
             $arr[$key]['model'] =$categoryname['name']; 
             
             
         }
         $arr[$key]['color'] = $product->category->color; 
         $arr[$key]['image'] = is_null($product['image'])? null : asset('').$product['image']; 
         
        $now = time(); 
        $your_date = strtotime($product['created_at']);
        $diff = $now - $your_date;
         
        if($lang->local  == 'ar'){
                $since = 'منذ';
            }
            else{
                $since = 'since';
            }
        if($diff < 60) //seconds
        {
            if($lang->local  == 'ar'){
                $obj = 'ثانية';
            }
            else{
                $obj = 'seconds';
            }
            $time = $since.' '.round($diff ) . ' '. $obj;
        }
        else{
            if($diff < (60*60) && $diff >= 60) //minutes
            {
                if($lang->local  == 'ar')
                {
                $obj = 'دقائق';
                }
                else{
                $obj = 'minutes';
                }
            $time = $since.' '.round($diff / (60  )) . ' '. $obj;
            }
            elseif($diff < (60*60*24) && $diff >= (60*60)) //hours
            {
                if($lang->local  == 'ar')
                {
                $obj = 'ساعة';
                }
                else{
                $obj = 'hours';
                }
            $time = $since.' '.round($diff / (60 *60 )) . ' '. $obj;
                
            }
            elseif($diff < (60*60*24*30) && $diff >= (60*60*24)) //days
            {
                if($lang->local  == 'ar')
                {
                $obj = 'يوم';
                }
                else{
                $obj = 'days';
                }
            $time = $since.' '.round($diff / (60 *60 *24)) . ' '. $obj;
                
            }
            elseif($diff < (60*60*24*30*12) && $diff >= (60*60*24*30)) //months
            {
                if($lang->local  == 'ar')
                {
                $obj = 'شهر';
                }
                else{
                $obj = 'months';
                }
            $time = $since.' '.round($diff / (60 *60 *24*30)) . ' '. $obj;
                
            }
            elseif($diff >= (60*60*24*30*12) ) //years
            {
                if($lang->local  == 'ar')
                {
                $obj = 'سنة';
                }
                else{
                $obj = 'years';
                }
            $time = $since.' '.round($diff / (60*60*24*30*12 )) . ' '. $obj;
                
            }
            
        }
        $arr[$key]['time'] = $time;
        
        }
         return response()->json(['max'=>$max,'data'=>$arr],  200); 
    }
    
    
    
    
     public function newsingle(Request $request,$id){
         $product = Product::find($id);
         $photos = ProductPhoto::where('product_id',$id)->get();
         $product->update(['visit_counts'=>$product->visit_counts +1]);
         $device = Device::where('ip',$request->header('ip'))->first();
        $lang = Language::where('local',$request->header('language'))->first();
        $lang_id = $lang->id;
         $arr = array();
        if(!$device){
            $device = Device::create(['ip'=>$request->header('ip')]);
            
        }
        $gallery = Gallery::find(1);
        
        $device_id = $device->id;
        $comments = Comment::where('product_id',$id)->get();
        $arr['id'] = $product->id;
        $arr['comment']= count($comments);
        $arr['favorite']= favorite($id,$device_id);
        if($gallery->show == 'yes')
        {
            $arr['banners']= asset('').$gallery['image']; 
            $arr['banners']= $gallery['link']; 
        }
        else{
            $arr['banners']= null;
            $arr['banners']= null;
        }
            $arr['link']= url('').'/'.$id;
        
        $productname = ProductDescription::where('product_id',$product['id'])->where('language_id',$lang_id)->first();
        $arr['name'] = $productname['name'];
        $arr['description'] = $productname['description'];
        
        //$writer = WriterDescription::where('writer_id',$product->writer_id)->where('language_id',$lang_id)->first();
        $arr['writer'] = $product['writer'];
        $arr['image'] = is_null($product['image'])? null : asset('').$product['image']; 
        $arrPhoto = array();
        foreach ($photos as $key => $row) {
            $arr['photos'][$key]['image'] = asset('').$row['image'];
            $arr['photos'][$key]['link'] = $row['link'];
        }
        //$arr['video'] = is_null($product['video'])? null : asset('').$product['video']; 
        if(date("m",strtotime($product['created_at'])) == '1'){ if($lang->local == 'ar'){$month='يناير';}else{$month='January';}}
        elseif(date("m",strtotime($product['created_at'])) == '2'){ if($lang->local == 'ar'){$month='فبراير';}else{$month='February';}}
        elseif(date("m",strtotime($product['created_at'])) == '3'){ if($lang->local == 'ar'){$month='مارس';}else{$month='March';}}
        elseif(date("m",strtotime($product['created_at'])) == '4'){ if($lang->local == 'ar'){$month='ابريل';}else{$month='April';}}
        elseif(date("m",strtotime($product['created_at'])) == '5'){ if($lang->local == 'ar'){$month='مايو';}else{$month='May';}}
        elseif(date("m",strtotime($product['created_at'])) == '6'){ if($lang->local == 'ar'){$month='يونيو';}else{$month='June';}}
        elseif(date("m",strtotime($product['created_at'])) == '7'){ if($lang->local == 'ar'){$month='يوليو';}else{$month='July';}}
        elseif(date("m",strtotime($product['created_at'])) == '8'){ if($lang->local == 'ar'){$month='اغسطس';}else{$month='Auguest';}}
        elseif(date("m",strtotime($product['created_at'])) == '9'){ if($lang->local == 'ar'){$month='سبتمبر';}else{$month='September';}}
        elseif(date("m",strtotime($product['created_at'])) == '10'){ if($lang->local == 'ar'){$month='اكتوبر';}else{$month='October';}}
        elseif(date("m",strtotime($product['created_at'])) == '11'){ if($lang->local == 'ar'){$month='نوفمبر';}else{$month='November';}}
        elseif(date("m",strtotime($product['created_at'])) == '12'){ if($lang->local == 'ar'){$month='ديسمبر';}else{$month='December';}}
        
        
        if(date("D",strtotime($product['created_at'])) == 'Sat'){ if($lang->local == 'ar'){$week='السبت';}else{$week='Saterday';}}
        elseif(date("D",strtotime($product['created_at'])) == 'Sun'){ if($lang->local == 'ar'){$week='الاحد';}else{$week='Sunday';}}
        elseif(date("D",strtotime($product['created_at'])) == 'Mon'){ if($lang->local == 'ar'){$week='الاثنين';}else{$week='Monday';}}
        elseif(date("D",strtotime($product['created_at'])) == 'Tue'){ if($lang->local == 'ar'){$week='الثلاثاء';}else{$week='Tuesday';}}
        elseif(date("D",strtotime($product['created_at'])) == 'Wed'){ if($lang->local == 'ar'){$week='الاربعاء';}else{$week='Wednesday';}}
        elseif(date("D",strtotime($product['created_at'])) == 'Thu'){ if($lang->local == 'ar'){$week='الخميس';}else{$week='Thursday';}}
        elseif(date("D",strtotime($product['created_at'])) == 'Fri'){ if($lang->local == 'ar'){$week='الجمعة';}else{$week='Friday';}}
        
        
        if(date("a",strtotime($product['created_at'])) == 'am'){ if($lang->local == 'ar'){$type='ص';}else{$type='am';}}
        else{ if($lang->local == 'ar'){$type='م';}else{$type='pm';}}
        if($lang->local == 'ar'){
             $arr['date'] = $week. date(" j",strtotime($product['created_at'])).' '.$month.date(" Y",strtotime($product['created_at'])).date(" g:i",strtotime($product['created_at'])).' '.$type;
        }
        else{
        $arr['date'] = $week. ' ' . $month. date(" j,  g:i ",strtotime($product['created_at'])).$type.' '.date(" Y",strtotime($product['created_at']));
        }
         return response()->json($arr,  200); 
     }
    
    
    
    public function favorites(Request $request){
        $device = Device::where('ip',$request->header('ip'))->first();
        $lang = Language::where('local',$request->header('language'))->first();
        $lang_id = $lang->id;
        
        $arr = array();
        if(!$device){
            $device = Device::create(['ip'=>$request->header('ip')]);
        }
        $device_id = $device->id;
        $fav = Favorite::where('device_id',$device_id)->pluck('product_id');
        $products = Product::whereNotIn('id',fake_ids())->where('show','yes')->whereIn('id',$fav)->get();
        foreach($products as $key=>$product){
              $productname = ProductDescription::where('product_id',$product['id'])->where('language_id',$lang_id)->first();
         $arr[$key]['id'] = $product['id']; 
         $arr[$key]['name'] = $productname['name']; 
         if(!is_null($product->model_id)){
         $modelname = ModelDescription::where('model_id',$product['model_id'])->where('language_id',$lang_id)->first();
         $arr[$key]['model'] = $modelname['name']; 
         
         }else{
         $categoryname = CategoryDescription::where('category_id',$product['category_id'])->where('language_id',$lang_id)->first();
             $arr[$key]['model'] =$categoryname['name']; 
             
             
         }
         $arr[$key]['color'] = $product->category->color; 
         $arr[$key]['image'] = is_null($product['image'])? null : asset('').$product['image']; 
         
        $now = time(); 
        $your_date = strtotime($product['created_at']);
        $diff = $now - $your_date;
         
        if($lang->local  == 'ar'){
                $since = 'منذ';
            }
            else{
                $since = 'since';
            }
        if($diff < 60) //seconds
        {
            if($lang->local  == 'ar'){
                $obj = 'ثانية';
            }
            else{
                $obj = 'seconds';
            }
            $time = $since.' '.round($diff ) . ' '. $obj;
        }
        else{
            if($diff < (60*60) && $diff >= 60) //minutes
            {
                if($lang->local  == 'ar')
                {
                $obj = 'دقائق';
                }
                else{
                $obj = 'minutes';
                }
            $time = $since.' '.round($diff / (60  )) . ' '. $obj;
            }
            elseif($diff < (60*60*24) && $diff >= (60*60)) //hours
            {
                if($lang->local  == 'ar')
                {
                $obj = 'ساعة';
                }
                else{
                $obj = 'hours';
                }
            $time = $since.' '.round($diff / (60 *60 )) . ' '. $obj;
                
            }
            elseif($diff < (60*60*24*30) && $diff >= (60*60*24)) //days
            {
                if($lang->local  == 'ar')
                {
                $obj = 'يوم';
                }
                else{
                $obj = 'days';
                }
            $time = $since.' '.round($diff / (60 *60 *24)) . ' '. $obj;
                
            }
            elseif($diff < (60*60*24*30*12) && $diff >= (60*60*24*30)) //months
            {
                if($lang->local  == 'ar')
                {
                $obj = 'شهر';
                }
                else{
                $obj = 'months';
                }
            $time = $since.' '.round($diff / (60 *60 *24*30)) . ' '. $obj;
                
            }
            elseif($diff >= (60*60*24*30*12) ) //years
            {
                if($lang->local  == 'ar')
                {
                $obj = 'سنة';
                }
                else{
                $obj = 'years';
                }
            $time = $since.' '.round($diff / (60*60*24*30*12 )) . ' '. $obj;
                
            }
            
        }
        $arr[$key]['time'] = $time;
        }
        return response()->json($arr,  200); 
        
    }
    public function followcategories(Request $request){
        $categories = Category::cursor();
     $arr = array();
      $lang = Language::where('local',$request->header('language'))->first();
        $lang_id = $lang->id;
        $device = Device::where('ip',$request->header('ip'))->first();
        if(!$device){
         $device = Device::create(['ip'=>$request->header('ip')]);
        }
     
        $device_id = $device->id;
     foreach($categories as $keyCat=>$category){
         $arr[$keyCat]['id'] = $category['id'];
         $arr[$keyCat]['banner'] = is_null($category->banner)? null :asset('').$category['banner']; 
        
         $categpryname = CategoryDescription::where('category_id',$category['id'])->where('language_id',$lang_id)->first();
         $arr[$keyCat]['name'] = $categpryname['name']; 
         $arr[$keyCat]['follow'] = devicecat($category['id'],$device_id); 
         
         foreach(modelscategory($category['id']) as $keyModel=>$model){
             $arr[$keyCat]['models'][$keyModel]['id'] = $model['id']; 
             $modelname = ModelDescription::where('model_id',$model['id'])->where('language_id',$lang_id)->first();
             $arr[$keyCat]['models'][$keyModel]['image'] =  asset('').$model['image']; 
             $arr[$keyCat]['models'][$keyModel]['banner'] = is_null($model->banner)? null :asset('').$model['banner']; 
             $arr[$keyCat]['models'][$keyModel]['name'] = $modelname['name']; 
             $arr[$keyCat]['models'][$keyModel]['follow'] = devicemodel($model['id'],$device_id); 
         }
     }
      return response()->json($arr,  200); 
    }
    
     public function modelNews(Request $request,$id){
         $news = Product::whereNotIn('id',fake_ids())->where('show','yes')->orderBy('id','DESC')->where('model_id',$id)->skip($request->paginate *10)->take(10)->get();
         $counter = Product::whereNotIn('id',fake_ids())->where('show','yes')->orderBy('id','DESC')->where('model_id',$id)->get();
         $model = Models::find($id);
         $cat = Category::find($model->category_id);
         $cat->update(['counter'=>$cat->counter+1]);
         
         $max = ceil(count($counter)/10)-1;
         $arr = array();
         $lang = Language::where('local',$request->header('language'))->first();
         $lang_id = $lang->id;
         foreach($news as $key=>$product)
         {
         $productname = ProductDescription::where('product_id',$product['id'])->where('language_id',$lang_id)->first();
         $arr[$key]['id'] = $product['id']; 
         $arr[$key]['name'] = $productname['name']; 
         $modelname = ModelDescription::where('model_id',$product['model_id'])->where('language_id',$lang_id)->first();
         $arr[$key]['model'] = $modelname['name']; 
         
         $arr[$key]['image'] = is_null($product['image'])? null : asset('').$product['image']; 
         $arr[$key]['color'] = $product->category->color; 
         
          $now = time(); 
        $your_date = strtotime($product['created_at']);
        $diff = $now - $your_date;
         
        if($lang->local  == 'ar'){
                $since = 'منذ';
            }
            else{
                $since = 'since';
            }
        if($diff < 60) //seconds
        {
            if($lang->local  == 'ar'){
                $obj = 'ثانية';
            }
            else{
                $obj = 'seconds';
            }
            $time = $since.' '.round($diff ) . ' '. $obj;
        }
        else{
            if($diff < (60*60) && $diff >= 60) //minutes
            {
                if($lang->local  == 'ar')
                {
                $obj = 'دقائق';
                }
                else{
                $obj = 'minutes';
                }
            $time = $since.' '.round($diff / (60  )) . ' '. $obj;
            }
            elseif($diff < (60*60*24) && $diff >= (60*60)) //hours
            {
                if($lang->local  == 'ar')
                {
                $obj = 'ساعة';
                }
                else{
                $obj = 'hours';
                }
            $time = $since.' '.round($diff / (60 *60 )) . ' '. $obj;
                
            }
            elseif($diff < (60*60*24*30) && $diff >= (60*60*24)) //days
            {
                if($lang->local  == 'ar')
                {
                $obj = 'يوم';
                }
                else{
                $obj = 'days';
                }
            $time = $since.' '.round($diff / (60 *60 *24)) . ' '. $obj;
                
            }
            elseif($diff < (60*60*24*30*12) && $diff >= (60*60*24*30)) //months
            {
                if($lang->local  == 'ar')
                {
                $obj = 'شهر';
                }
                else{
                $obj = 'months';
                }
            $time = $since.' '.round($diff / (60 *60 *24*30)) . ' '. $obj;
                
            }
            elseif($diff >= (60*60*24*30*12) ) //years
            {
                if($lang->local  == 'ar')
                {
                $obj = 'سنة';
                }
                else{
                $obj = 'years';
                }
            $time = $since.' '.round($diff / (60*60*24*30*12 )) . ' '. $obj;
                
            }
            
        }
        $arr[$key]['time'] = $time;
        
        }
         return response()->json(['max'=>$max,'data'=>$arr],  200); 
     }
     public function categoryNews(Request $request,$id){
         $news = Product::whereNotIn('id',fake_ids())->orderBy('id','DESC')->where('category_id',$id)->skip($request->paginate *10)->take(10)->get();
         $counter = Product::whereNotIn('id',fake_ids())->orderBy('id','DESC')->where('category_id',$id)->get();
         $max = ceil(count($counter)/10)-1;
         $cat = Category::find($id);
         $cat->update(['counter'=>$cat->counter+1]);
         $arr = array();
         $lang = Language::where('local',$request->header('language'))->first();
         $lang_id = $lang->id;
         foreach($news as $key=>$product)
         {
            $productname = ProductDescription::where('product_id',$product['id'])->where('language_id',$lang_id)->first();
         $arr[$key]['id'] = $product['id']; 
         $arr[$key]['name'] = $productname['name']; 
          if(!is_null($product->model_id)){
         $modelname = ModelDescription::where('model_id',$product['model_id'])->where('language_id',$lang_id)->first();
         $arr[$key]['model'] = $modelname['name']; 
         
         }else{
         $categoryname = CategoryDescription::where('category_id',$product['category_id'])->where('language_id',$lang_id)->first();
             $arr[$key]['model'] =$categoryname['name']; 
             
             
         }
         $arr[$key]['color'] = $product->category->color; 
         $arr[$key]['image'] = is_null($product['image'])? null : asset('').$product['image']; 
         
          $now = time(); 
        $your_date = strtotime($product['created_at']);
        $diff = $now - $your_date;
         
        if($lang->local  == 'ar'){
                $since = 'منذ';
            }
            else{
                $since = 'since';
            }
        if($diff < 60) //seconds
        {
            if($lang->local  == 'ar'){
                $obj = 'ثانية';
            }
            else{
                $obj = 'seconds';
            }
            $time = $since.' '.round($diff ) . ' '. $obj;
        }
        else{
            if($diff < (60*60) && $diff >= 60) //minutes
            {
                if($lang->local  == 'ar')
                {
                $obj = 'دقائق';
                }
                else{
                $obj = 'minutes';
                }
            $time = $since.' '.round($diff / (60  )) . ' '. $obj;
            }
            elseif($diff < (60*60*24) && $diff >= (60*60)) //hours
            {
                if($lang->local  == 'ar')
                {
                $obj = 'ساعة';
                }
                else{
                $obj = 'hours';
                }
            $time = $since.' '.round($diff / (60 *60 )) . ' '. $obj;
                
            }
            elseif($diff < (60*60*24*30) && $diff >= (60*60*24)) //days
            {
                if($lang->local  == 'ar')
                {
                $obj = 'يوم';
                }
                else{
                $obj = 'days';
                }
            $time = $since.' '.round($diff / (60 *60 *24)) . ' '. $obj;
                
            }
            elseif($diff < (60*60*24*30*12) && $diff >= (60*60*24*30)) //months
            {
                if($lang->local  == 'ar')
                {
                $obj = 'شهر';
                }
                else{
                $obj = 'months';
                }
            $time = $since.' '.round($diff / (60 *60 *24*30)) . ' '. $obj;
                
            }
            elseif($diff >= (60*60*24*30*12) ) //years
            {
                if($lang->local  == 'ar')
                {
                $obj = 'سنة';
                }
                else{
                $obj = 'years';
                }
            $time = $since.' '.round($diff / (60*60*24*30*12 )) . ' '. $obj;
                
            }
            
        }
        $arr[$key]['time'] = $time;
        
        }
         return response()->json(['max'=>$max,'data'=>$arr],  200); 
     }
     public function categories(Request $request){
     $categories = Category::orderBy('num','ASC')->get();
     $arr = array();
      $lang = Language::where('local',$request->header('language'))->first();
        $lang_id = $lang->id;
     
     foreach($categories as $keyCat=>$category){
         $arr[$keyCat]['id'] = $category['id'];
        
         $categpryname = CategoryDescription::where('category_id',$category['id'])->where('language_id',$lang_id)->first();
         $arr[$keyCat]['name'] = $categpryname['name']; 

         if ($category->show == 'yes') {
             
         $arr[$keyCat]['banner']['link'] = $category['link']; 
         $arr[$keyCat]['banner']['image']  = is_null($category->banner)? null :asset('').$category['banner']; 
         }
         else{

         $arr[$keyCat]['banner']['link'] = null; 
         $arr[$keyCat]['banner']['image'] = null; 
         }


         $arr[$keyCat]['image'] = asset('').$category['image']; 
         foreach(modelscategory($category['id']) as $keyModel=>$model){
             $arr[$keyCat]['models'][$keyModel]['id'] = $model['id']; 
             $modelname = ModelDescription::where('model_id',$model['id'])->where('language_id',$lang_id)->first();
             $arr[$keyCat]['models'][$keyModel]['image'] =  asset('').$model['image']; 

            if ($model->show == 'yes') {
             
                    $arr[$keyCat]['models'][$keyModel]['banner']['link'] = $model['link']; 
                    $arr[$keyCat]['models'][$keyModel]['banner']['image'] = is_null($model['banner'])?null:asset('').$model['banner']; 
                }
            else{

                    $arr[$keyCat]['models'][$keyModel]['banner']['link'] = null; 
                    $arr[$keyCat]['models'][$keyModel]['banner']['image'] = null; 
            }
             


             $arr[$keyCat]['models'][$keyModel]['name'] = $modelname['name']; 
         }
     }
      return response()->json($arr,  200); 
 }
  
 public function category(Request $request ,$id){
     
 }
    public function comments(Request $request ,$id){
        $lang = $request->header('language');
        $comments = Comment::where('product_id',$id)->get();
        $arr = array();
        $now = time();
        foreach($comments as $key=>$row){
             ;
            $arr[$key]['id'] = $row['id'];
            $arr[$key]['subject'] = $row['subject'];
            $arr[$key]['comment'] = $row['comment'];
            
            
          $now = time(); 
        $your_date = strtotime($row['created_at']);
        $diff = $now - $your_date;
         
        if($lang == 'ar'){
                $since = 'منذ';
            }
            else{
                $since = 'since';
            }
        if($diff < 60) //seconds
        {
            if($lang == 'ar'){
                $obj = 'ثانية';
            }
            else{
                $obj = 'seconds';
            }
            $time = $since.' '.round($diff ) . ' '. $obj;
        }
        else{
            if($diff < (60*60) && $diff >= 60) //minutes
            {
                if($lang == 'ar')
                {
                $obj = 'دقائق';
                }
                else{
                $obj = 'minutes';
                }
            $time = $since.' '.round($diff / (60  )) . ' '. $obj;
            }
            elseif($diff < (60*60*24) && $diff >= (60*60)) //hours
            {
                if($lang == 'ar')
                {
                $obj = 'ساعة';
                }
                else{
                $obj = 'hours';
                }
            $time = $since.' '.round($diff / (60 *60 )) . ' '. $obj;
                
            }
            elseif($diff < (60*60*24*30) && $diff >= (60*60*24)) //days
            {
                if($lang == 'ar')
                {
                $obj = 'يوم';
                }
                else{
                $obj = 'days';
                }
            $time = $since.' '.round($diff / (60 *60 *24)) . ' '. $obj;
                
            }
            elseif($diff < (60*60*24*30*12) && $diff >= (60*60*24*30)) //months
            {
                if($lang == 'ar')
                {
                $obj = 'شهر';
                }
                else{
                $obj = 'months';
                }
            $time = $since.' '.round($diff / (60 *60 *24*30)) . ' '. $obj;
                
            }
            elseif($diff >= (60*60*24*30*12) ) //years
            {
                if($lang == 'ar')
                {
                $obj = 'سنة';
                }
                else{
                $obj = 'years';
                }
            $time = $since.' '.round($diff / (60*60*24*30*12 )) . ' '. $obj;
                
            }
            
        }
        $arr[$key]['time'] = $time;
            
            
        }
        return response()->json($arr,  200);
    }
    public function createComment(CommentRequest $request){
        
        $lang = $request->header('language');
        $msg = Comment::create($request->all());
        
         $comments = Comment::where('product_id',$request->product_id)->get();
        $arr = array();
        $now = time();
        foreach($comments as $key=>$row){
             ;
            $arr[$key]['id'] = $row['id'];
            $arr[$key]['subject'] = $row['subject'];
            $arr[$key]['comment'] = $row['comment'];
            
            $now = time(); 
        $your_date = strtotime($row['created_at']);
        $diff = $now - $your_date;
         
        if($lang == 'ar'){
                $since = 'منذ';
            }
            else{
                $since = 'since';
            }
        if($diff < 60) //seconds
        {
            if($lang == 'ar'){
                $obj = 'ثانية';
            }
            else{
                $obj = 'seconds';
            }
            $time = $since.' '.round($diff ) . ' '. $obj;
        }
        else{
            if($diff < (60*60) && $diff >= 60) //minutes
            {
                if($lang == 'ar')
                {
                $obj = 'دقائق';
                }
                else{
                $obj = 'minutes';
                }
            $time = $since.' '.round($diff / (60  )) . ' '. $obj;
            }
            elseif($diff < (60*60*24) && $diff >= (60*60)) //hours
            {
                if($lang == 'ar')
                {
                $obj = 'ساعة';
                }
                else{
                $obj = 'hours';
                }
            $time = $since.' '.round($diff / (60 *60 )) . ' '. $obj;
                
            }
            elseif($diff < (60*60*24*30) && $diff >= (60*60*24)) //days
            {
                if($lang == 'ar')
                {
                $obj = 'يوم';
                }
                else{
                $obj = 'days';
                }
            $time = $since.' '.round($diff / (60 *60 *24)) . ' '. $obj;
                
            }
            elseif($diff < (60*60*24*30*12) && $diff >= (60*60*24*30)) //months
            {
                if($lang == 'ar')
                {
                $obj = 'شهر';
                }
                else{
                $obj = 'months';
                }
            $time = $since.' '.round($diff / (60 *60 *24*30)) . ' '. $obj;
                
            }
            elseif($diff >= (60*60*24*30*12) ) //years
            {
                if($lang == 'ar')
                {
                $obj = 'سنة';
                }
                else{
                $obj = 'years';
                }
            $time = $since.' '.round($diff / (60*60*24*30*12 )) . ' '. $obj;
                
            }
            
        }
        $arr[$key]['time'] = $time;
            
            
            
        }
        return response()->json($arr,  200);
    }

    public function urgent(Request $request){
            $lang = Language::where('local',$request->header('language'))->first();
        $lang_id = $lang->id;
            $news = News::orderBy('id','DESC')->get();
        $arr = array();
        foreach ($news as $key => $row) {
            $arr[$key]['id'] = $row['id'];
            $newname = NewDescription::where('new_id',$row['id'])->where('language_id',$lang_id)->first();
            $arr[$key]['name'] = $newname['name'];
        }
        
        return response()->json($arr, 200); 
    }
    public function pdfs(Request $request){
            
            $pdfs = Day::where('date','<=',date('Y-m-d'))->orderBy('date','DESC')->take('30')->get();
        $arr = array();
        foreach ($pdfs as $key => $row) {
            $arr[$key]['id'] = $row['id'];
                $arr[$key]['date'] = $row['date'];
                $hijri = Meladi2Hijri(date('d',strtotime($row['date'])),date('m',strtotime($row['date'])),date('Y',strtotime($row['date'])));
                if($request->header('language') == 'ar'){
                	if ($hijri['month'] == '1') {
                		$month = 'محرم';
                	}
                	elseif ($hijri['month'] == '2') {
                		$month = 'صفر';
                	}
                	elseif ($hijri['month'] == '3') {
                		$month = 'ربيع الأول';
                	}
                	elseif ($hijri['month'] == '4') {
                		$month = 'ربيع الآخر';
                	}
                	elseif ($hijri['month'] == '5') {
                		$month = 'جمادى الأولى';
                	}
                	elseif ($hijri['month'] == '6') {
                		$month = 'جمادى الآخرة';
                	}
                	elseif ($hijri['month'] == '7') {
                		$month = 'رجب';
                	}
                	elseif ($hijri['month'] == '8') {
                		$month = 'شعبان';
                	}
                	elseif ($hijri['month'] == '9') {
                		$month = 'رمضان';
                	}
                	elseif ($hijri['month'] == '10') {
                		$month = 'شوال';
                	}
                	elseif ($hijri['month'] == '11') {
                		$month = 'ذو القعدة';
                	}
                	elseif ($hijri['month'] == '12') {
                		$month = 'ذو الحجة';
                	}
                }
                else{
					if ($hijri['month'] == '1') {
                		$month = 'Muharram';
                	}
                	elseif ($hijri['month'] == '2') {
                		$month = 'Sufer';
                	}
                	elseif ($hijri['month'] == '3') {
                		$month = 'Rabi al-Awwal';
                	}
                	elseif ($hijri['month'] == '4') {
                		$month = 'Rabee Al-Akhir';
                	}
                	elseif ($hijri['month'] == '5') {
                		$month = 'Jumada al-Ula';
                	}
                	elseif ($hijri['month'] == '6') {
                		$month = 'Jumada Al-Akhirah';
                	}
                	elseif ($hijri['month'] == '7') {
                		$month = 'Rajab';
                	}
                	elseif ($hijri['month'] == '8') {
                		$month = 'Shaaban';
                	}
                	elseif ($hijri['month'] == '9') {
                		$month = 'Ramadan';
                	}
                	elseif ($hijri['month'] == '10') {
                		$month = 'Shawwal';
                	}
                	elseif ($hijri['month'] == '11') {
                		$month = 'Dhul Qiadah';
                	}
                	elseif ($hijri['month'] == '12') {
                		$month = 'Dhul-Hijjah';
                	}
                }
                $arr[$key]['hijri'] = $hijri['day'].' '.$month.' '.$hijri['year'];
                $arr[$key]['file'] = asset('').$row['file'];
        }
        
        return response()->json($arr, 200); 
    }
}