<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::as('site.')->group(function (){
    Route::get('/', function () {
        return view('site.pages.index');
    })->name('index');

    Route::get('/deprem-yardim-formu', function () {
        return view('site.pages.deprem_yardim_formu');
    })->name('need_home');

    Route::get('/evimi-acabilirim', function () {
        return view('site.pages.evimi_acabilirim');
    })->name('has_home');

    Route::get('/basvuru-sorgula', function () {
        return view('site.pages.basvuru_sorgula');
    })->name('check_applicant');

    Route::get('/basvuru-kontrol',[\App\Http\Controllers\HelperController::class,'basvuruSorgula'])->name('search_applicant');

    Route::get('/basvuru-alindi', function () {
        return view('site.pages.basvuru_alindi');
    })->name('applicant_completed');

    Route::resources([
        'need-homes' => \App\Http\Controllers\Site\NeedHomeController::class,
        'has-homes' => \App\Http\Controllers\Site\HasHomeController::class
    ]);
});

Route::post('/getTownsFromCity',[\App\Http\Controllers\HelperController::class,'getTownsFromCity'])->name('getTownsFromCity');
Route::post('/getDistrictsFromTown',[\App\Http\Controllers\HelperController::class,'getDistrictsFromTown'])->name('getDistrictsFromTown');
Route::post('/getStreetsFromDistrict',[\App\Http\Controllers\HelperController::class,'getStreetsFromDistrict'])->name('getStreetsFromDistrict');

Route::get('/admin/giris-yap',function (){
    return view('admin.pages.login');
})->name('admin.login');

Route::post('/loginPost',[\App\Http\Controllers\Admin\UserController::class,'adminLoginPost'])->name('admin.loginPost');

Route::as('admin.')->prefix('admin')->middleware('admin')->group(function (){
   Route::get('/',function (){
       if (auth()->user()->has_home_management){
           $hasHomes = \App\Models\Site\HasHome::all();
           return view('admin.pages.evimi_acabilirim',['hasHomes'=>$hasHomes]);
       }elseif (auth()->user()->need_home_management){
           $needHomes = \App\Models\Site\NeedHome::all();
           return view('admin.pages.need_home',['needHomes'=>$needHomes]);
       }elseif (auth()->user()->user_management){
           $users = \App\Models\User::all();
           return view('admin.pages.users_index',['users'=>$users]);
       }else{
           return redirect('/');
       }
   })->name('index');

   Route::get('/evimi-acabilirim',function (){
       $hasHomes = \App\Models\Site\HasHome::all();
       return view('admin.pages.evimi_acabilirim',['hasHomes'=>$hasHomes]);
   })->name('has_home');

    Route::get('/ev-ihtiyacim-var',function (){
        $needHomes = \App\Models\Site\NeedHome::all();
        return view('admin.pages.need_home',['needHomes'=>$needHomes]);
    })->name('need_home');

    Route::get('/kullanici-yonetimi',function (){
        if (isset($_GET['search']) && $_GET['search'] != '')
            $users = \App\Models\User::where('name','like',"%{$_GET['search']}%")->get();
        else
            $users = \App\Models\User::all();
        return view('admin.pages.users_index',['users'=>$users]);
    })->name('user_management');
});



//$user->email = 'admin@kardesyuvan.com';
//$user->password = bcrypt('kardesyuva.');

