<?php

namespace AhmadFatoni\ApiGenerator\Controllers\API;

use Cms\Classes\Controller;
use BackendMenu;

use Illuminate\Http\Request;
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
use Illuminate\Support\Facades\Validator;
use tps\Airport\Models\Content;
use tps\Airport\Models\Menu;

use Illuminate\Http\Resources\Json\JsonResource;
use RainLab\Translate\Behaviors\TranslatableModel;
use Config;
use RainLab\Blog\Models\Post;
use DB;

class postController extends Controller
{
    protected $Content;

    protected $helpers;

    public function __construct(Content $Content, Helpers $helpers)
    {
        parent::__construct();
        $this->Content    = $Content;
        $this->helpers          = $helpers;
    }

    protected function validateForm($data, $rules)
    {
        return Validator::make($data, $rules);
    }

    public function index(Request $request)
    {    
         $dataVal = $request->all();
        
         
         $data = Post::with(['categories:id,name', 'featured_images'])
                       ->where('published', 1)
                       ->select('id', 'title', 'published_at', 'slug', 'excerpt')
                       ->orderBy('published_at', 'desc')
                       ->with('views')
                       ->paginate($dataVal["per_page"] ?? 6);
        
  
        //$data->translateContext($dataVal["locale"]);
        if($data){
            $data->each(function ($item, $key) use($dataVal) {
                $item->translateContext($dataVal["locale"] ?? 'ru');
            });
        }
        
        if (count($data) != 0) {
           // return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
            return response()->json($data, 200);
        } else {
            return $this->helpers->apiArrayResponseBuilder(404, 'not found data', []);
        }
    }
    
    
    public function getPost(Request $request, $id)
    {
         $dataVal = $request->all();
        
         $data = Post::with(['categories:id,name', 'featured_images'])
                         ->select('id', 'title', 'published_at', 'slug', 'excerpt', 'content_html')
                         ->with('views')
                         ->find($id);
    
    
        if($data){         
        $obj = Db::table('vdomah_blogviews_views')
                ->where('post_id', $id);

            if ($obj->count() > 0) {
                $row = $obj->first();

                // $views = $row->views + rand(1,10);;
              
               $views = $row->views + 1;;

                $obj->update(['views' => $views]);
               // $data['views'] = $row->views;
            }
            else {

                Db::table('vdomah_blogviews_views')->insert([
                    'post_id' => $data->getKey(),
                    'views'   => 1
                ]);
                
                //$data['views'] = 0;
            }
            
        
        
        $data->translateContext($dataVal["locale"] ?? 'ru');
        
        return response()->json($data, 200);  
        //return $this->helpers->apiArrayResponseBuilder(200, 'success', [$data]);
        }else{
            
            return $this->helpers->apiArrayResponseBuilder(404, 'not found data', []);     
        }
   
    }
    
    
  
}
