<?php

namespace App\Http\Controllers\DS;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use LynX39\LaraPdfMerger\Facades\PdfMerger;

use Carbon\Carbon;
use Validator;
use PDF;

use App\Staff;
use App\Model\StaffDetail;
use App\Model\WorkOrder;
use File;

class ProfileControllerA extends Controller
{

	public function encryptPassword($action, $string) {
		$output = false;
		$encrypt_method = "AES-256-CBC";
		$secret_key = '28472B4B6250655368566D5970337336';
		$secret_iv =  md5(md5($string)); //'This is my secret iv';
		
		// hash
		$key = hash('sha256', $secret_key);
		
		$iv = substr(hash('sha256', $secret_iv), 0, 16);
		if ( $action == 'encrypt' ) {
			$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
			$output = base64_encode($output);
		} else if( $action == 'decrypt' ) {
			$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
		}
		return $output;
	}


	public function viewProfile(){

		$id = Auth::guard('agent')->id();
		$user = Staff::with('StaffDetail')->find($id);

    	return view('sales.view_profile',compact('user'));
	}

	public function updateProfilePicture(Request $request){
		$this->validate($request, [
			'prof_img' => 'mimes:jpeg,jpg,png|required|dimensions:width=128,height=128'
		]);


		$pathIcon = '';
		if($request->hasfile('prof_img')){
				// Move / Upload Icon into server
			$destinationPath = 'assets/avatar';
				// create folder/dir if not exist
			if(!File::exists(public_path().$destinationPath)){
				File::makeDirectory(public_path().$destinationPath,0777,true);
			}

			$icon = $request->file('prof_img');
			$icon->move($destinationPath,$request->id.'.'.$icon->getClientOriginalExtension());
			$pathIcon = $destinationPath.'/'.$request->id.'.'.$icon->getClientOriginalExtension();
		}

		$user = Staff::with('StaffDetail')->where('_id',$request->id)->first();
		$user->StaffDetail->user_pic = $pathIcon;
		$user->StaffDetail->save();

		if($pathIcon != ''){
			return redirect()->back()->with('success_msg', 'Success update your image profile');
		}else{
			return redirect()->back()->with('error_msg', 'Cant update your image profile');
		}
	}

	public function updateFullName(Request $request){
		$user = Staff::with('StaffDetail')->where('_id',$request->id)->first();
		$user->StaffDetail->name = $request->name;
		$user->StaffDetail->save();

		return redirect()->back()->with('success_msg', 'Success update your full name');
	}

	public function updateIdentity(Request $request){
		$user = Staff::with('StaffDetail')->where('_id',$request->id)->first();
		$user->StaffDetail->ic = $request->ic;
		$user->StaffDetail->save();

		return redirect()->back()->with('success_msg', 'Success update your indentity/NRIC');
	}

	public function updatePhone(Request $request){
		$user = Staff::with('StaffDetail')->where('_id',$request->id)->first();
		$user->StaffDetail->phone = $request->phone;
		$user->StaffDetail->save();

		return redirect()->back()->with('success_msg', 'Success update your phone');
	}

	public function updatePassword(Request $request){
		$user = Staff::with('StaffDetail')->where('_id',$request->id)->first();

		$enc_pass = $this->encryptPassword('encrypt',$request->password);
		$user->password = $enc_pass;
		$user->save();
		$user->StaffDetail->password = $enc_pass;
		$user->StaffDetail->save();

		return redirect()->back()->with('success_msg', 'Success update your password');
	}
}