<?php

class ReportsController extends BaseController {

	public function getReportsIndex()
	{
		$url = url() . '/'. Route::getCurrentRoute()->getPath();
		$settings = $this->getSettings(true);

		return View::make('admin.reports',[
			'url'		=> $url,
			'from_date'	=> date('m/d/Y G:i A', strtotime('yesterday midnight')),
			'to_date'	=> date('m/d/Y G:i A', strtotime('midnight')),
			'settings'	=> $settings
		]);
	}

	public function postReportsIndex()
	{
		$url = url() . '/'. Route::getCurrentRoute()->getPath();

		$fromDate = Input::get('from-date');
		$toDate = Input::get('to-date');

		$start = new DateTime($fromDate);
		$end = new DateTime($toDate);
		// dd($start, $end);

		$statuses = StatusesModel::whereBetween('state',[2,3])
			->whereBetween('published_at', [$start, $end])
			->orderBy('order','asc')
			->get();

		$settings = $this->getSettings();

		return View::make('admin.reports',[
			'url'		=> $url,
			'from_date'		=> $fromDate,
			'to_date'		=> $toDate,
			'statuses'	=> $statuses,
			'settings'	=> $settings
		]);
	}


	public function downloadReport()
	{
		if (!Input::has(['from_date', 'to_date', 'submition_date', 'submition_place']))
		{
			dd(Input::all());
			return Redirect::route('reports');
		}

		$fromDate = Input::get('from_date');
		$toDate = Input::get('to_date');

		$start = new DateTime($fromDate);
		$start->modify('today');
		$end = new DateTime($toDate);
		$end->modify('today');

		$statuses = DB::table('statuses')
			->whereIn('state',[2, 3])
			->whereBetween('published_at', [$start, $end])
			->orderBy('order','asc')
			->get();

		$settings = $this->getSettings();

		$submitionDate = Input::get('submition_date');
		$submitionPlace = Input::get('submition_place');

		$pdf = PDF::loadView('pdf.reports',[
			'statuses'			=> $statuses,
			'settings'			=> $settings,
			'submitionPlace'	=> $submitionPlace,
			'submitionDate'		=> $submitionDate
		]);

		$name = 'report-'.time().'.pdf';

		return $pdf->download($name);
	}



	public function getSettings($required = false)
	{
		$dbFields = SettingsModel::where('name', '=', 'reportSettings');

		if($dbFields->count())
		{
			$dbFields = $dbFields
				->firstOrFail()
				->toArray();

			$dbFields = (array) json_decode($dbFields['value']);
		}
		else
		{
			if($required)
			{
				return null;
			}
			$dbFields = [];
		}

		return array_replace([
			'companyName'								=> '',
			'companyAddress'							=> '',
			'companyActivityCode'						=> '',
			'companyActivity'							=> '',
			'companyEIN'								=> '',
			'companyAuthorizedPersonName'				=> '',
			'companyAuthorizedPersonAddress'			=> '',
			'companyAuthorizedPersonPIN'				=> '',
			'companyAuthorizedPersonID'					=> '',
			'companyAuthorizedPersonPlaceOfIssuance'	=> '',

			'advertiserCompanyName'						=> '',
			'advertiserCompanyAddress'					=> '',
			'advertiserCompanyActivityCode'				=> '',
			'advertiserCompanyActivity'					=> '',
			'advertiserCompanyLicenseNumber'			=> '',
			'advertiserCompanyIDNum'					=> '',
			'advertiserCompanyEIN'						=> '',
			'advertiserCompanyPhone'					=> '',
			'advertiserCompanyAuthorizedPersonName'		=> '',
			'advertiserCompanyAuthorizedPersonAddress'	=> '',
			'advertiserCompanyAuthorizedPersonPIN'		=> '',
			'advertiserCompanyAuthorizedPersonID'		=> '',
			'advertiserCompanyAuthorizedPersonPlaceOfIssuance'	=> '',
			'advertisingDimensionsDuration'				=> ''

		], $dbFields);
	}

}
