<?php

namespace App\Controllers;
//require_once APPPATH.'ThirdParty/formula/src/Formula.php';
//use Formula;
//use APPPATH.ThirdParty/formula/src/Formula.php';
//use App\Libraries\formula\Formula;
use App\Libraries\formula\Formula;
use CodeIgniter\Files\File;

class MasterUpl extends BaseController
{

	protected $helpers = ['form'];

	public function index()
	{
		return view('upload_form', ['errors' => []]);
	}

	public function upload()
	{
		$validationRule = [
			'userfile' => [
				'label' => 'Image File',
				'rules' => 'uploaded[userfile]'
					. '|is_image[userfile]'
					. '|mime_in[userfile,image/jpg,image/jpeg,image/gif,image/png,image/webp]'
					. '|max_size[userfile,100]'
					. '|max_dims[userfile,1024,768]',
			],
		];
		if (!$this->validate($validationRule)) {
			$data = ['errors' => $this->validator->getErrors()];

			return view('upload_form', $data);
		}

		$img = $this->request->getFile('userfile');

		if (!$img->hasMoved()) {
			$filepath = WRITEPATH . 'uploads/' . $img->store();

			$data = ['uploaded_flleinfo' => new File($filepath)];

			return view('upload_success', $data);
		} else {
			$data = ['errors' => 'The file has already been moved.'];

			return view('upload_form', $data);
		}
	}
}
