<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use Carbon\Carbon;
use App\Resource;
use App\Role;
use Excel;
use Gate;
use DB;
use Illuminate\Database\Eloquent\Model;

class ExcelController extends Controller {

	public function spend() {
		if (Gate::denies('regular')) {
			abort(403, 'You cannot export reports.');
		}
		$export = true;
		$now = Carbon::now()->format('Y-m-d');
		$data = Resource::all();
		Excel::create("TPS As Of $now", function($excel) use ($data, $export) {
			$excel->sheet('Total Project Spending', function($sheet) use ($data, $export) {
				$sheet->loadView('reports.spend')->with('data', $data)->with('export', $export);
			});
		})->download('xls');
    }

	public function spendWeek() {
		if (Gate::denies('regular')) {
			abort(403, 'You cannot export reports.');
		}

		$export = true;
		$startWeek = Carbon::now()->timezone('America/Denver')->startOfWeek();
		$endWeek = Carbon::now()->timezone('America/Denver')->endOfWeek()->subDays(2);
		$weekOf = $startWeek->format('Y-m-d');
		$data = Resource::all();
		Excel::create("TPS Week Of $weekOf", function($excel) use ($data, $export, $startWeek, $endWeek) {
			$excel->sheet('Weekly Spending', function($sheet) use ($data, $export, $startWeek, $endWeek) {
				$sheet->loadView('reports.spendweek')->with('data', $data)
					->with('export', $export)
					->with('startWeek', $startWeek)
					->with('endWeek');
			});
		})->download('xls');
	}

	public function roles() {
		if (Gate::denies('regular')) {
			abort(403, 'You cannot export reports.');
		}
		$export = true;
		$now = Carbon::now()->format('Y-m-d');
		$roleIdArray = Role::get()->lists('id')->toArray();
		Excel::create("Role Report $now", function($excel) use ($roleIdArray, $export) {
			$excel->sheet('Total Roles', function($sheet) use ($roleIdArray, $export) {
				$sheet->loadView('reports.roles')->with('roleIdArray', $roleIdArray)->with('export', $export);
			});
		})->download('xls');
	}

	public function unavailable() {
		if (Gate::denies('regular')) {
			abort(403, 'You cannot export reports.');
		}
		$export = true;
		$now = Carbon::now()->format('Y-m-d');
		$data = DB::table('resources')
		          ->select('name','project_assigned')
		          ->where('project_assigned' ,'=', '0')
		          ->get();

		Excel::create("Unassigned Report $now", function($excel) use ($data, $export) {
			$excel->sheet('Available People', function($sheet) use ($data, $export) {
				$sheet->loadView('reports.unavailable')->with('data', $data)->with('export', $export);
			});
		})->download('xls');
	}

//	public function upload() {
//		Model::unguard();
//		Excel::load(storage_path('excel') .'/converted.xls', function($reader) {
//			foreach ($reader->toArray() as $row) {
////			$first = $reader->first()->toArray();
//				foreach($row as $entry){
//					if(@isset($entry['email']) && @isset($entry['name']))
//					try {
//						Resource::create($entry);
//					} catch(\Exception $e) {}
//				}
////				Resource::create( $row );
//			}
//		});
//		Model::reguard();
//	}
}