<?php

class SettingsController extends BaseController {

	public function getIndex()
	{
		$url = url() . '/'. Route::getCurrentRoute()->getPath();
		$dbFields = SettingsModel::where('name', '=', 'reportSettings');

		if($dbFields->count())
		{
			$dbFields = $dbFields->firstOrFail()
			->toArray();
			$dbFields = (array) json_decode($dbFields['value']);
		}

		else
		{
			$dbFields = [];
		}

		$fields = 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'						=> '',
			'advertisingType'									=> '',
			'adDesignedBy'										=> '',
		], $dbFields);

		return View::make('admin.settings', [
			'url'		=> $url,
			'fields'	=> $fields
		]);
	}



	public function postIndex()
	{
		$fields = Input::all();
		$formFields = array();

		$exists = SettingsModel::where('name', '=', 'reportSettings')->count();
		if( $exists < 1 )
		{
			DB::table('settings')->insert([
				'name' => 'reportSettings',
				'value' => json_encode($fields)
			]);
		}
		else
		{
			DB::table('settings')
			->where('name', 'reportSettings')
			->update([
				'value' => json_encode($fields)
			]);
		}

		$formFields = $fields;

		$url = url() . '/'. Route::getCurrentRoute()->getPath();
		return View::make('admin.settings', [
			'url'		=> $url,
			'fields'	=> $formFields
		]);
	}

}
