<?php
namespace App\Http\Controllers\App;
use App\Http\Controllers\Controller;
use App\Models\Advertisment;
use App\Models\Category;
use App\Models\City;
use App\Models\Form;
use App\Models\OrderDetail;
use App\Models\Plan;
use App\Models\Setting;
use App\Models\State;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
class HomeController extends Controller
{
public function index()
{
$response = [];
$response['app_name'] = optional(Setting::firstWhere('key', 'app_name'))->value;
$response['app_logo'] = optional(Setting::firstWhere('key', 'app_logo'))->value;
$response['app_issue'] = optional(Setting::firstWhere('key', 'app_issue'))->value;
$response['is_date_show'] = optional(Setting::firstWhere('key', 'is_date_show'))->value;
$response['meta_tag'] = optional(Setting::firstWhere('unique', 'keyword'))->value;
$response['meta_description'] = optional(Setting::firstWhere('unique', 'description'))->value;
$response['expire_day'] = optional(Plan::firstWhere('key', 'extension'))->expire;
if ($response['expire_day'] === null)
$response['expire_day'] = 30;
// SELECT CITY --> WITH COOKIE OR FIRST CITY
if (Cookie::get('city') != '') {
$response['state'] = State::where('parent', Cookie::get('city'))->get();
} else {
$parent = City::orderBy('name', 'ASC')->first();
$response['state'] = State::where('parent', $parent->id)->get();
}
// CHECK LOAD FIRST OR BACK FROM SHOW ADVERTISMENT
if (Cookie::get('category') !== '') {
$response['back_category'] = Category::firstWhere('id', Cookie::get('category'));
$name = 'category';
Cookie::queue($name, '', 1);
}
// CHECK LOAD FIRST OR BACK FROM SHOW ADVERTISMENT DESKTOP
if (Cookie::get('state_desktop') !== '') {
$response['back_state_desktop'] = Cookie::get('state_desktop');
$name = 'state_desktop';
Cookie::queue($name, '', 1);
}
// CHECK LOAD FIRST OR BACK FROM SHOW ADVERTISMENT MOBILE
if (Cookie::get('state_mobile') !== '') {
$response['back_state_mobile'] = Cookie::get('state_mobile');
$name = 'state_mobile';
Cookie::queue($name, '', 1);
}
$name = 'categorybacktemp';
Cookie::queue($name, '', 1);
$advertisment = Advertisment::query();
if (\request()->has('category')) {
$category = Category::where('slug', \request()->get('category'))->first();
if ($category) {
$response['meta_tag'] = $category->meta_tag;
$response['meta_description'] = $category->meta_description;
if (count($category->Child) != 0) {
// GET CHILD OF THIS CATEGORY
$list = Category::whereIn('parent', $category->Child->pluck('id'))->get()->pluck('id');
/* check childs of childs of category */
if (count($list) != 0)
$advertisment->whereIn('category', $list);
else
$advertisment->whereIn('category', $category->Child->pluck('id'));
} else {
$advertisment->where('category', \request()->get('category'));
}
if (\request()->has('parent')) {
$response['categories'] = Category::where('parent', \request()->get('parent'))->orderBy('id', 'ASC')->get();
} else {
$response['categories'] = Category::where('parent', $category->id)->orderBy('id', 'ASC')->get();
}
} else {
$response['categories'] = Category::where('parent', '1')->orderBy('id', 'ASC')->get();
}
} else {
$response['categories'] = Category::where('parent', '1')->orderBy('id', 'ASC')->get();
}
$response['categories'] = Category::where('parent', '1')->orderBy('id', 'ASC')->get();
if (\request()->has('urgent')) {
$urgents = OrderDetail::where([['plan', 'urgent'], ['pay', 'paid'], ['created_at', '>', Carbon::now()->subDays(optional(\App\Models\Plan::firstWhere('key', 'urgent'))->expire)]])->get()->pluck('advertisment');
$advertisment->whereIn('id', $urgents);
}
// SEARCH BOX
if (\request()->has('search')) {
if (\request()->get('search') != '') {
$advertisment->where('name', 'LIKE', '%' . \request()->get('search') . '%');
$advertisment->orWhere('id', \request()->get('search'));
}
}
// SELECT THE LAST ADVERTISMENT
$response['advertisments'] = $advertisment->where([['show', 'success'], ['created_at', '>', Carbon::now()->subDays($response['expire_day'])]])
->whereIn('state', $response['state']->pluck('id'))->orderBy('updated_at', 'DESC')->limit(10)
->with(['Meta' => function ($list) {
$list->whereIn('model_id', Form::where('show_thumbnail', 1)->get()->pluck('id'))->get();
}])
->get();
// if (\request()->has('category')) {
// $response['advertisments'] = $response['advertisments']->where('category', \request()->category);
// }
// CATEGORY
return view('front.page.main', compact('response'));
}
}