<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Repository\AdminRepository;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Bidding;
use App\Models\Property;
use Auth;
use Image;
use File;

class AdminDashboardController extends Controller {

    

    function index(AdminRepository $AdminRepository) {
    	$table = new Property;
        $table1 = new User;
        $table3 = new Bidding;

    	$where = array('status'=>1, 'is_deleted'=>0);
    	$liveBid = $AdminRepository->getAllByLimitInPaginate(5, $table, $where);

        $liveBid1 = $AdminRepository->getAllByWhere($table, $where);

        $where1 = array('parent_catid'=>1, 'status'=>1, 'is_deleted'=>0);
        $property = $AdminRepository->getAllByLimit(3, $where1, $table);

        $where2 = array('parent_catid'=>2, 'status'=>1, 'is_deleted'=>0);
        $vehicle = $AdminRepository->getAllByLimit(3, $where2, $table);
        
        $where1 = array('role_id'=>2);
        $user = $AdminRepository->getAllByLimitWithoutWhere(5,  $table1, $where1);

        $where3 = array('status'=>2, 'is_deleted'=>0);
        $expiredBid = $AdminRepository->getAllByWhere($table, $where3);

        $latestBid = $AdminRepository->getAllPaginate(4, $table3);

        $where4 = array('status'=>1, 'is_deleted'=>0, 'endDate'=> Date('Y-m-d'));
        $todayExpired = $AdminRepository->getAllByLimit(4, $where4, $table);
        
    	
        return view('admin.pages.dashboard.index', compact('liveBid', 'property', 'user', 'vehicle', 'liveBid1', 'expiredBid', 'latestBid', 'todayExpired'));
    }

    

    

    function profile(AdminRepository $AdminRepository) {
    	$table = new User;
    	$where = array('role_id'=>1);
    	$data = $AdminRepository->getByWhere($table, $where);
        return view('admin.pages.profile', compact('data'));
    }


    function profileUpdate(AdminRepository $AdminRepository, Request $request, $id) {

    	$table = new User;
    	$updId = base64_decode($id);
    	if($request->input('pword')=='')
    	{
    		$data = array('name'=> $request->input('name'));
    	}
    	else
    	{
    		$data = array(
    			'name'     => $request->input('name'),
    			'password' => Hash::make($request->input('pword'))
    			);
    	}
    	$update = $AdminRepository->update($data, $updId, false, $table);
    	
    	if($update)
    	{
    		session()->flash('success', 'Admin Profile Updated Successfully');
            return redirect()->route('admin.profile');
    	}
        else
        {
        	session()->flash('errors', 'Admin Profile Not Successfully Update');
            return redirect()->route('admin.profile');
        }
    }


    public function profileImageUpdate(AdminRepository $AdminRepository, Request $request, $id)
    {
    	$table = new User;
    	$imgId = base64_decode($id);

    	$lastImg = $table::find($imgId);

    	$request->validate([
            'profilepic'		  => 'required'
        ]);

        //Main Image Insert 
        if(!empty($request->profilepic))
        {
         //Delete Old Images
            
            if(File::exists('upload/profile/'.$lastImg->image))
            {
                File::delete('upload/profile/'.$lastImg->image);
            }

         //Insert New Image
         $image = $request->file('profilepic');
         //Create Image Name
         $img = 'profile.'.$image->getClientOriginalExtension(); 
         $location = public_path('upload/profile/'.$img);
         Image::make($image)->save($location);
        }

        $data = array('image' => $img);
        $return = $AdminRepository->update($data, $imgId, false, $table);

        if($return)
        {
            session()->flash('success', ' Profile Image Update Successfully');
            return redirect()->route('admin.profile');
        }
        else
        {
            session()->flash('errors', 'Profile Image Not Update');
            return redirect()->route('admin.profile');
        }
    }

}
