<?php

namespace App\Http\Controllers;

use Auth;
use DB;
use App\Models\Journal;
use App\Models\Ebook;
use App\Models\Volume;
use App\Models\Ebookvolume;
use App\Models\Issue;
use App\Models\Article;
use App\Models\Chapter;
use App\Models\ContentAccess;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;

class IssueAccessController extends BenthamController
{

    public function index(Request $request)
    {   
    	$journal_search=array();
    	$issue_list=array();
    	$content_access_exist=array();
    	$selected_journal=array();

        $param['journal_id'] = $journal_id = $request->get('journal_id');
        $param['volume_id'] = $volume_id = $request->get('volume_id');
		
		$param['access_type'] = $access_type = $request->get('access_type');
		$param['from_date'] = $from_date = $request->get('from_date');
		$param['to_date'] = $to_date = $request->get('to_date');

        $ajx_search = $request->get('ajx_search');
                   
               
            $journal_search = '';
            $volume_search = '';

                $journal_search = Journal::GetJournalTitle();

                if (!empty($journal_id))
                {
                    $p['journal_id'] = $journal_id;
                    $volume_search = Volume::GetVolumeTitleById($p);
                    
                    if (!empty($volume_id))
                    {
						$p['volume_id'] = $volume_id;
                       
						// get issue list
						$p['access_type'] = $request->get('access_type');
						
						if (!empty($from_date))
						{
							$p['from_date'] = date("Y-m-d", strtotime($from_date));	
						}
						if (!empty($to_date))
						{
							$p['to_date'] = date("Y-m-d", strtotime($to_date));	
						}
						$p['ASC'] =true;
						$issue_list = ContentAccess::GetContentAccessIssueList($p);	
                    }
                    
                }

                if ($ajx_search == true)
                {
                    return response()->json(array(
                        'journal_search'=> $journal_search,
                        'volume_search'=> $volume_search
                        ), 200);                    
                }     

        $compact_variables = with(['journal_search' => $journal_search,                                     
                                     'volume_search' => $volume_search,
                                     'issue_list' => $issue_list,
                                     'param' => $param,
									 'content_access_exist' => $content_access_exist,
                                     'selected_journal' => $selected_journal]);

        return View::make('admin.content_access.issue',$compact_variables);

    }
	
	public function create(Request $request)
    {   
    	$issue_list=array();
    	$content_access_exist="";
    	$selected_journal=array();

        $param['journal_id'] = $journal_id = $request->get('journal_id');
        $param['volume_id'] = $volume_id = $request->get('volume_id');
        $ajx_search = $request->get('ajx_search');
                   
            $journal_search = '';
            $volume_search = '';

                $journal_search = Journal::GetJournalTitle();

                if (!empty($journal_id))
                {
                    $p['journal_id'] = $journal_id;
                    $volume_search = Volume::GetVolumeTitleById($p);
                    
                    if (!empty($volume_id))
                    {
                        $p['volume_id'] = $volume_id;
						$volume_content_access = ContentAccess::CheckVolumeContentAccess($p['volume_id']);
						
						if (!empty($volume_content_access[0]))
						{
							// if content access is already given on issue donot disply issues						
							$content_access_exist = 'content access on volume '.$volume_content_access[0]->volume_name.' is already given';
						}
						else
						{
							// get issue list
							$p['ASC'] = true;
							$issue_list = ContentAccess::GetContentAccessIssueList($p);	
						}
                        
                    }
                    
                }

                if ($ajx_search == true)
                {
                    return response()->json(array(
                        'journal_search'=> $journal_search,
                        'volume_search'=> $volume_search
                        ), 200);                    
                }     

        $compact_variables = with(['journal_search' => $journal_search,                                     
                                     'volume_search' => $volume_search,
                                     'issue_list' => $issue_list,
                                     'param' => $param,
									 'content_access_exist' => $content_access_exist,
                                     'selected_journal' => $selected_journal]);

        return View::make('admin.content_access.add_issue',$compact_variables);

    }
	
	public function store(Request $request)
    {
		
     	$content_type_id = $request->access_check;
		$perpetual = $request->perpetual_check;
		$from_date = $request->from_date;
		$to_date = $request->to_date;
		$content_type_access_id = $request->access_op;
		$user = Auth::user();
		
		foreach($content_type_id as $val)
		{
			$add_issue['content_type'] 		= 'i';
			$add_issue['content_type_id'] 	= $val;
			$add_issue['content_type_access_id'] 	= isset($content_type_access_id[$val]) ? $content_type_access_id[$val] : '';
			$add_issue['perpetual'] 		= isset($perpetual[$val]) ? $perpetual[$val] : '';
			$add_issue['from_date'] 		= isset( $from_date[$val] ) ? date("Y-m-d", strtotime($from_date[$val])) : null;
			$add_issue['to_date'] 			= isset( $to_date[$val] ) ? date("Y-m-d", strtotime($to_date[$val])) : null;
			$add_issue['system_time'] 		= time();
			$add_issue['system_user_name']	= $user->id;
			
			$add_issue_data[] = $add_issue;
			empty($add_issue);
		}
        try {// try transaction start here for data integrity

			DB::beginTransaction();
		// (1).  Delete All Issue Contetnt Access in case if exist
		ContentAccess::whereIn('content_type_id', $content_type_id)->where('content_type', 'i')->delete();
		
		// (2).  Insert All Issues Contetnt Access
		ContentAccess::insert($add_issue_data);
                DB::commit();
        } catch (Exception $e) {

            DB::rollback();
            Session::flash('error', 'Somthing wrong with this page. Please try again!');
            return Redirect::to('admin/content_access_issue');
        }
		return Redirect::to('admin/content_access_issue/create');

    }
	
	public function destroy(Request $request)
	{
	 
		$access_check = $request->get('access_check');
		
		// get all content access issues rows
		$issue_rows = ContentAccess::whereIn('content_type_id', $access_check)->where('content_type', 'i')->get();
		
		foreach($issue_rows as $row)
		{
			
			$log['id'] = $row->id;
			$log['content_type'] = $row->content_type;
			$log['content_type_id'] = $row->content_type_id;
			$log['content_type_access_id'] = $row->content_type_access_id;
			$log['perpetual'] = $row->perpetual;
			$log['from_date'] = $row->from_date;
			$log['to_date'] = $row->to_date;
			$log['remarks'] = $row->remarks;
			$log['system_time'] = $row->system_time;
			$log['system_user_name'] = $row->system_user_name;
			$issue_log[] = $log;
			
		}
        try {// try transaction start here for data integrity

			DB::beginTransaction();
		// insert content access issue in log 
		DB::table('journal_access_log')->insert($issue_log);
	
		// delte content access issue
		ContentAccess::whereIn('content_type_id', $access_check)->where('content_type', 'i')->delete();	
		
                DB::commit();
        } catch (Exception $e) {

            DB::rollback();
            Session::flash('error', 'Somthing wrong with this page. Please try again!');
            return Redirect::to('admin/content_access_issue');
        }
		return Redirect::to('admin/content_access_issue');
	}

}

