<?php

namespace App\Http\Controllers;
use View, DB;
use App\About;
use App\Media;
use App\Categories;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
use Illuminate\Routing\Controller as BaseController;

class CategoriesController extends BaseController {

	public function index(){
		$categories = DB::table('about')->where('id', '=', 1)->first();
		return view('categories.categories', ['categories'=>$categories]);
	}
	public function showcategory(Request $request, $url_slug, $page = 0){
		$category = Categories::where('url_slug', '=', $url_slug)->first();
		/*$categoriesparent = Categories::where('parentID', '=', 0)->where('url_slug', '=', $url_slug)->get();
		foreach ($categoriesparent as $item) {
			$categorychild = Categories::where('parentID', '=', $item->id)->get();
			foreach ($categorychild as $key) {
				$mediachid = Media::where('category_id', '=', $key->id)->orderBy('created_at', 'DESC')->offset($page*12)->take(12)->get();
			}
		}*/
		$media = Media::where('category_id', '=', $category->id)->orderBy('created_at', 'DESC')->offset($page*12)->take(12)->get();
		$total = DB::table('media')->where('category_id', '=', $category->id)->count();
		//exit();
		//echo "<pre>"; var_dump($categoriesparent->toArray()); echo "<br/>"; die('123 end');
		/*if (!empty($categoriesparent->toArray())) {
			exit('123123');
			return view('categories.categories', ['category'=>$category, 'media'=>$media, 'total'=>$total, 'categoriesparent'=>$categoriesparent, 'mediachid'=>$mediachid]);
			
		} else{
			exit('aaa');*/
			return view('categories.categories', ['category'=>$category, 'media'=>$media, 'total'=>$total]);
		//}
		
	}
	public function loadmore($pageNumber, $url_slug){
		$id = Input::get("post_id");
		$url_slug = $url_slug;
		$page = config('view.numPage');
		$pageNumber = $pageNumber;
		$offset = ($pageNumber - 1) * $page;
		return view('partials.loadmore_categories', ['page'=>$page, 'pageNumber'=>$pageNumber, 'offset'=>$offset, 'id'=>$id, 'url_slug'=>$url_slug]);

	}
}