<?php namespace App\Modules\Backend\Dashboard\Controllers;

use Auth, Input, Redirect, View;

class DashboardController extends \BaseController {

	public function getIndex()
	{
		return View::make('dashboard::inicio');
	}

	public function getAttachments()
	{
		$files = \File::files('filesattach');
		$files_array = [];
		foreach ($files as $file)
		{
			$info = pathinfo($file);
			$files_array[] = [
							'url' 	=> asset($file),
							'text' 	=> str_limit($info['filename'], $limit = 20, $end = '...') . '.' . $info['extension']
					];
		}
		$files = $files_array;

		return View::make('dashboard::attachments', compact('files'));
	}

	public function uploadAttachments()
	{
		$data 	= Input::all();
		$rules 	= array(
            'file'  => 'required|mimes:jpeg,gif,png,doc,docx,xls,xlsx,pdf'
        );
		$validator = \Validator::make($data, $rules);
        if(!$validator->passes())
        {
        	\Alert::message("Archivo invalido", 'danger');
        	return Redirect::back()
        			->withInput();
        }

        if(Input::hasFile('file') and !Input::file('file')->isValid())
		{
			\Alert::message('El documento ' . Input::file($file)->getClientOriginalName() . ' no es valido.', 'danger');
			return Redirect::back()
    			->withInput();
		}
		else if(Input::hasFile('file'))
		{
			$file 		= Input::file('file');
			$file_name  = $file->getClientOriginalName();
			$file->move('filesattach',	$file_name);
		}
		\Alert::message("Archivo agregado", 'success');
		return Redirect::route('attachments.index');
	}

	public function destroyAttachments()
	{
		$file = Input::get('file');
		$file = explode('/', $file);
		$file = end($file);
		unlink('filesattach/' . $file);
		\Alert::message("Archivo eliminado", 'success');
		return Redirect::route('attachments.index');
	}
}