<?php

namespace App\Http\Controllers;

use App\Modules;
use App\Role;
use App\Models\Country;
use App\Models\UserAccess\UserAccessRequest;
use App\Models\UserAccess\UserAccessOrganization;
use App\Models\UserAccess\UserAccessIpRange;
use App\Models\UserAccess\UserAccess;
use App\User;
use DB;
use Auth;
use File;
use Hash;
use ZipArchive;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Crypt;
use stdClass;
use View;

class UserAccessRequestController extends BenthamController
{
	

    public function __construct()
    {
        //this->some_service = new UserAccessRequestFormController();
    }	
	public function index(Request $request)
	{
		
		$editPermission   = false;
		$deletePermission = false;
		$section=$request->section;
		if (checkPermission('user_access', 'edit')) {
			$editPermission = true;
		}if (checkPermission('user_access', 'delete')) {
			$deletePermission = true;
		}
		$ajax_search = $request->get('ajax_search');
		if ($ajax_search == true)
		{
			if($request->section == "access_organization_list")
			{
				$param['access_organization_id'] = $request->get('org_id');
				$request_id = $request->get('request_id');
				$param['page_limit'] = 10;
				$org_user_list = UserAccess::GetUserAccessInfoList($param);
				return View::make('admin.user_access.includes.model_window_user_list', with(['org_user_list' => $org_user_list,
					'request_id' => $request_id]));
			}

		}
		else
		{
			switch ($request->section) 
			{	
				case "access_request_list":

				$search  = $request->get('search');
	            include 'UserAccess/UserAccessRequestFormController.php';
	            $user_access_request = new UserAccessRequestFormController();
	            $access_list_new = $user_access_request->AccessRequestList($search,'N', 10, $user_id = "");
	            $access_list_process = $user_access_request->AccessRequestDetailList($search,'P', 10, $user_id = "");
	            $access_list_complete = $user_access_request->AccessRequestDetailList($search,'C', 10, $user_id = "");
				return View::make('admin.user_access.access_request_list', with(['access_list_new' => $access_list_new,
					'access_list_process' => $access_list_process,
					'access_list_complete' => $access_list_complete,
					 'editPermission' => $editPermission,
					  'deletePermission' => $deletePermission,
					  'search' => $search]));
				break;	
				
				case "access_organization_list":
				$search  = $request->get('search');
				$request_id=$request->request_id;
	            include 'UserAccess/UserAccessOrganizationController.php';
	            $user_access_organization = new UserAccessOrganizationController();
				$organization_list = $user_access_organization->AccessOrganizationList($search);
				return View::make('admin.user_access.access_organization_list', with(['organization_list' => $organization_list,
					'request_id' => $request_id,
					 'editPermission' => $editPermission,
					  'deletePermission' => $deletePermission,
					  'search' => $search]));
				break;	
			

				case "access_list":
				include 'UserAccess/UserAccessContentController.php';
	            $volume_access = new UserAccessContentController();
				return $volume_access->access_list($request);
				break;	


			}	
		}
	}
	
	public function show($id)
	{	
		$files = DB::table('user_access_pd_file')->where('request_id', '=', decrypt($id))->where('access_file', '=', 1)->orderby('pd_file_id', 'desc')->get();
		if (count($files) == 0) {
			$files = UserAccessRequest::GetAccessRequestPDFiles(decrypt($id));
		}

		$this->DownloadZipFile(decrypt($id),$files);
	}

  public function DownloadZipFile($id,$files)
    {	
            $path=config('constants.admin.root_path_uploads').$id;

            $File = $path.'/'.$id.'-'. time().'.zip';
            
            $zip = new \ZipArchive();

            $res = $zip->open($File, \ZipArchive::CREATE);

			if ($res === TRUE) {

                foreach($files as $file) {
                    $zip->addFile($path."/".$file->file_name,$file->file_name);
                }        

			    $zip->close();
			}
            
            header("Content-Type: application/zip");
            header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
            header("Content-Length: " . filesize($File));
            header("Pragma: no-cache");
            header("Expires: 0");
            readfile($File);
            if(file_exists($File))
            {
            	unlink($File);
            }

            header("Connection: close");

    }

	public function create(Request $request, $id = null)
    {
		switch ($request->section) 
		{
			case "access_request_form":
			return View::make('admin.user_access.access_request_form');
			break;	

			case "access_organization_form":
			$country         = Country::GetCountryList();
			$request_id         = $request->request_id;
			return View::make('admin.user_access.access_organization_form', with(['country' => $country,
				'request_id' => $request_id]));
			break;	
			
			break;
		}
	}
	
	public function edit(Request $request, $id)
    { 
		switch ($request->section) 
		{
			case "access_request_form":
				$access = UserAccessRequest::GetAccessRequestByField('request_id',decrypt($id));
				$pd_files = UserAccessRequest::GetAccessRequestPDFiles(decrypt($id))->toArray();
				return view('admin.user_access.access_request_form', with(['access' => $access,'pd_files' => $pd_files]));
			break;

			case "access_organization_form":
				$country         = Country::GetCountryList();
				$request_id         = $request->request_id;
				$access = UserAccessOrganization::GetAccessOrganizationByField('access_organization_id',$id);
				return view('admin.user_access.access_organization_form', with(['access' => $access,
					'country' => $country,
					'request_id' => $request_id]));
			break;
			
		}
	}

	public function update(Request $request, $id)
	{	
		switch ($request->section) 
		{	
			case "access_request_form":
	            include 'UserAccess/UserAccessRequestFormController.php';
	            $user_access_request = new UserAccessRequestFormController();
				$return = $user_access_request->UpdateAccessRequest($request, decrypt($id));
	            if ($return[0] == True)
				{
					return Redirect::route('user_access.index', ['section'=>'access_request_list'])->with('success', 'Access Request Update Successfully.');
				} 
				else 
				{
					return back()->withInput()->withErrors($return[1]);
				}
			break;

			case "access_organization_form":
	            include 'UserAccess/UserAccessOrganizationController.php';
	            $user_access_organization = new UserAccessOrganizationController();
				$return = $user_access_organization->UpdateAccessOrganization($request, $id);

	            if ($return[0] == True)
				{
					return Redirect::route('user_access.index', ['section'=>'access_organization_list','request_id' => $request->request_id]);
				} 
				else 
				{
					return back()->withInput()->withErrors($return[1]);
				}
			break;
		}
	}

	public function store(Request $request)
    {
		switch ($request->section) 
		{
			case "access_request_form":
	            include 'UserAccess/UserAccessRequestFormController.php';
	            $user_access_request = new UserAccessRequestFormController();
				$return = $user_access_request->InsertAccessRequest($request);
	            if ($return[0] == True)
				{
					return Redirect::route('user_access.index', ['section'=>'access_request_list']);
				} 
				else 
				{
					return back()->withInput()->withErrors($return[1]);
				}
			break;

			case "access_organization_form":
	            include 'UserAccess/UserAccessOrganizationController.php';
	            $user_access_organization = new UserAccessOrganizationController();
				$return = $user_access_organization->InsertAccessOrganization($request);
	            if ($return[0] == True)
				{
					return Redirect::route('user_access.index', ['section'=>'access_organization_list','request_id' => $request->request_id]);
				} 
				else 
				{
					return back()->withInput()->withErrors($return[1]);
				}
			break;

		}
		
	}

	public function destroy($id, Request $request)
	{
		$editPermission   = false;
		$deletePermission = false;

		if (checkPermission('user_access', 'edit')) {
			$editPermission = true;
		}if (checkPermission('user_access', 'delete')) {
			$deletePermission = true;
		}

		switch ($request->section) 
		{
			case "access_request_form":
	            
	            include 'UserAccess/UserAccessRequestFormController.php';
	            $user_access_request = new UserAccessRequestFormController();

					if(isset($request->action) && $request->action == "revert")
					{
						$return = $user_access_request->DeleteAccessRequest($request, decrypt($id));

						return Redirect::route('user_access.index', ['section'=>'access_request_list'])->with('success', 'Access Request Revert Successfully.');
					}
					else if(isset($request->action) && $request->action == "complete")
					{
						$return = $user_access_request->DeleteAccessRequest($request, decrypt($id));
						return Redirect::route('user_access.index', ['section'=>'access_request_list'])->with('success', 'Access Request Mark Completed Successfully.');
					}
					else if(isset($request->action) && $request->action == "delete")
					{
						UserAccessRequest::DeleteUserAccessRequest(decrypt($id));

						return Redirect::route('user_access.index', ['section'=>'access_request_list'])->with('warning', 'Access Request Delete Successfully.');
					}
			break;

			case "access_organization_form":
	            include 'UserAccess/UserAccessOrganizationController.php';
	            $user_organization_request = new UserAccessOrganizationController();
				$return = $user_organization_request->DeleteOrganizationRequest($request, $id);
	            if ($return[0] == True)
				{
					return Redirect::route('user_access.index', ['section'=>'access_organization_list','request_id' => $request->request_id])->with('success', 'Access Organization Delete Successfully.');
				} 
				else 
				{
					return back()->withInput()->withErrors($return[1]);
				}
			break;
			
		}
	}
	
	
}
