<?php

namespace App\Http\Controllers\Agencies;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use DB;
use App\BankModel;
use App\AgencyBankModel;
use Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Hash;
use Image;
class AdminAccountController extends Controller
{
    
    public function index()
    {
		$user_log = Auth::user()->id;
		
		$bank = BankModel::select(
			DB::raw('CONCAT(id," ",name) AS name_mixed'),'id')
            ->pluck('name_mixed','id')
            ->prepend('Please Select', '');
		
		$find_bank = DB::select("SELECT nso007_bank.name  FROM `nso007_agency_bank` 
								left JOIN  nso007_bank ON nso007_bank.id = nso007_agency_bank.bank_id
								WHERE agency_id = '$user_log'");
		$bank_name = empty($find_bank[0]->name)? '' : $find_bank[0]->name;
		
		$find_agency = AgencyBankModel::where('agency_id',$user_log)->get();
		$find_obj = empty($find_agency[0]->agency_id)? '' : $find_agency[0]->agency_id;
		if($find_obj > 0){
			$account_number = $find_agency[0]->account_number;
			$account_name = $find_agency[0]->account_name;
			$id = $find_agency[0]->id;
		}else{
			$account_number = '';
			$account_name = '';
			$id = '';
		}
		
		$user = User::find($user_log);
        
       
        return view('agencies.profile.admin-profile',compact('user','bank','account_number','account_name','id','bank_name'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
	
	public function change_profile_images(Request $request)

    {
		$id = $request->user_id;
        $user = User::find($id);
        $this->validate($request, [
            'user_firstname'    => 'required',
            'user_lastname'     => 'required',
            'user_gender'   => 'required',
            'user_dob'  => 'required',
            'user_cardid'   => 'required',
            'user_phone'    => 'required',
            'user_reference'    => 'required',
            'email'     => 'required|unique:users,email,'.$id,
			
        ]);
        $oldImage = $request->hidden_image_name;
        if($request->file('image') == null){
            $file_name = $oldImage;
        }else{
            $newfile = $request->file('image');
            $file_name = $newfile->getClientOriginalName();
            if(file_exists(public_path().'/assets/images/resize'.$oldImage)){
                unlink(public_path().'/assets/images/resize'.$oldImage);
            }
            $file_name     = date('his'). $file_name ;
            $destinationPath = 'assets/images/resize';
            $get_img = Image::make($newfile->getRealPath())->resize(150, 150);
            $get_img->save($destinationPath.'/'.$file_name);
        }
		
        $user->user_firstname   = $request->user_firstname;
        $user->user_lastname    = $request->user_lastname;
        $user->user_gender  = $request->user_gender;
        $user->user_dob     = $request->user_dob;
        $user->user_cardid  = $request->user_cardid;
        $user->user_phone   = $request->user_phone;
        $user->user_reference = $request->user_reference;
        $user->address   = $request->address;
        $user->email    = $request->email;
        $user->role     = 'Agencies';
        $user->user_status     = 2;
        $user->user_activity     = $request->user_activity;
        $user->avatar  = $file_name;
		
		
        $user->update();
        return back()->with('success','Image Uploaded successfully.');

    }
	public function change_password(Request $request){
		$validator = Validator::make($request->all(), [
            'password' => 'required_with:new_password|password|max:8',
            'new_password' => 'required|max:8',
        ]);
		$user = User::find(Auth::user()->id);
		if (Hash::check($request->password, $user->password)) { 
			 $user->password = Hash::make($request->get('new_password'));
			if( $user->save()){
			return redirect()->route('account.admin')
				->with('success','Password updated successfully');
			}
		}
		else{
			return redirect()->route('account.admin')
				->with('danger','Old password not match with password');
		}
        
	}
	
	public function angecy_bank_store(Request $request){
		 
		
		
		$id = $request->id;
		// Update Bank Info
		if($id>0){
			$this->validate($request, [
				'agency_id' 	=> 'required|unique:nso007_agency_bank,agency_id,'.$id,
				'bank_id' => 'required',
				'account_number' => 'required',
				'account_name' => 'required',
			]);

			$update_agency_bank = AgencyBankModel::find($id);
			$update_agency_bank->bank_id = $request->bank_id;
			$update_agency_bank->agency_id = $request->agency_id;
			$update_agency_bank->account_number = $request->account_number;
			$update_agency_bank->account_name = $request->account_name;
			$update_agency_bank->update();
			return redirect()->route('account.admin')
					->with('success','Update created successfully');
		}
		// Add Bank Info
		else{
			$this->validate($request, [
				'agency_id' 	=> 'required|unique:nso007_agency_bank,agency_id',
				'bank_id' => 'required',
				'account_number' => 'required',
				'account_name' => 'required',
			]);

			$agency_bank = new AgencyBankModel();
			$agency_bank->bank_id = $request->bank_id;
			$agency_bank->agency_id = $request->agency_id;
			$agency_bank->account_number = $request->account_number;
			$agency_bank->account_name = $request->account_name;
			$agency_bank->save();
			return redirect()->route('account.admin')
					->with('success','Accont created successfully');
		}
		
		
	}

}
