<?php 
require_once("../../../config.php");
require ABSPATH.'app/lib/phpspreadsheet/vendor/autoload.php';
ini_set('max_execution_time', 300);
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Protection;
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;


require_once(ABSPATH."app/functions.php");
// require_once(ABSPATH."app/includes/phpmailer/class.phpmailer.php");
spl_autoload_register(function($class_name) {
	$file = ABSPATH."app/classes/class-".$class_name.".php";
	if (file_exists($file)) {
		include $file;
	}
});

require_once("../plugins.php");
$plugins = new plugins();
$plugins->load();

$session = Session::getInstance();

if ($_SERVER["REQUEST_METHOD"] == "POST") :

	if(isset($_POST['action'])){
		$response = array();
		$args = sanitize($_POST);
		unset($args['action']);
		$user = new user;
		$post = new post;
		$support = new support(['software_engineer', 'super_admin']);

		switch ($_POST['action']) {
			case 'upload_result_sheet':
				$path = ABSPATH.UPLOADFOLDER.'/Result Templates/'.str_replace('/', '-', get_session_name($args['session'])).'/'.get_term_name($args['term']).'/';
				$res = UploadFile($_FILES, 'upload-file', $path, ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'], 500000);
				if($res[0] != 0) {
					$response['msg'] = $res[1];
					$response['error'] = 1;
				} else {
					$args['file'] = $res[1];
					$response = update_result_from_template($args);
					$response['error'] = (isset($response['error'])) ? $response['error'] : 0;
				}
				echo json_encode($response);
				exit();
				break;

			// case 'upload_bulk_students_sheet':
			// 	$path = ABSPATH.UPLOADFOLDER.'/Other Templates/';
			// 	$res = UploadFile($_FILES, 'upload-file', $path, ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'], 500000);
			// 	$response['file'] = $_FILES;
			// 	if($res[0] != 0) {
			// 		$response['msg'] = $res[1];
			// 		$response['error'] = 1;
			// 	} else {
			// 		$args['file'] = $res[1];
			// 		$response = update_students_from_template($args);
			// 		$response['error'] = (isset($response['error'])) ? $response['error'] : 0;
			// 	}
			// 	echo json_encode($response);
			// 	exit();
			// 	break;

			// case 'upload_bulk_staff_sheet':
			// 	$path = ABSPATH.UPLOADFOLDER.'/Other Templates/';
			// 	$res = UploadFile($_FILES, 'upload-file', $path, ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'], 500000);
			// 	if($res[0] != 0) {
			// 		$response['msg'] = $res[1];
			// 		$response['error'] = 1;
			// 	} else {
			// 		$args['file'] = $res[1];
			// 		$response = update_staff_from_template($args);
			// 		$response['error'] = (isset($response['error'])) ? $response['error'] : 0;
			// 	}
			// 	echo json_encode($response);
			// 	exit();
			// 	break;

			case 'upload_ft_comments_sheet':
				$path = ABSPATH.UPLOADFOLDER.'/Other Templates/';
				$res = UploadFile($_FILES, 'upload-file', $path, ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'], 500000);
				if($res[0] != 0) {
					$response['msg'] = $res[1];
					$response['error'] = 1;
				} else {
					$args['file'] = $res[1];
					$response = update_ft_comments_offline($args);
					$response['error'] = (isset($response['error'])) ? $response['error'] : 0;
				}
				echo json_encode($response);
				exit();
				break;

			case 'upload_mock_result_sheet':
				$path = ABSPATH.UPLOADFOLDER.'/Mock Result Templates/'.str_replace('/', '-', get_session_name($args['session'])).'/'.get_term_name($args['term']).'/';
				$res = UploadFile($_FILES, 'upload-file', $path, ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'], 500000);
				if($res[0] != 0) {
					$response['msg'] = $res[1];
					$response['error'] = 1;
				} else {
					$args['file'] = $res[1];
					$response = _update_mock_result_from_template($args);
					$response['error'] = (isset($response['error'])) ? $response['error'] : 0;
				}
				echo json_encode($response);
				exit();
				break;

			case 'update_student_mock_result':
				$student_meta = get_user_meta($args['C']);
				$mock = [
					'type' => 'mock_exam',
					'student_id' => $student_meta['id'],
					'score' => $args['D'],
					'exam' => $args['exam'],
					'course' => $args['course'],
					'term' => $args['term'],
					'session' => $args['acsession'],
					'status' => 'active',
					'class' => $args['class'],
					'category' => $args['category']
				];
				$existing = $post->get_post_by_meta_data(['type' => 'mock_exam', 'student_id' => $student_meta['id'], 'exam' => $args['exam'], 'course' => $args['course'], 'session' => $args['acsession'], 'term' => $args['term']]);
				if(_count($existing)) {
					$existing = $existing[0];
					$post->update_post_meta($existing['id'], $mock);
				} else {
					$post->add_post($mock);
				}
				$response['error'] = 0;
				$response['msg'] = "Updated ".$args['B'];
				echo json_encode($response);
				exit();
				break;

			case "generate_result_template_zip":
				$class = eedu_portal_get_class_selection($args['class_category']);
				$settings = [
					'term' => $args['term'],
					'class' => $class['class'],
					'category' => $class['category']
				];
				$response['file'] = create_gradebook_for_all_class($settings);
				$response['time'] = time();
				echo json_encode($response);
				exit();
				break;

			case "generate_mock_result_template_zip":
				$class = eedu_portal_get_class_selection($args['class_category']);
				$settings = [
					'term' => $args['term'],
					'class' => $class['class'],
					'category' => $class['category'],
					'ext_exam' => $args['ext_exam']
				];
				$response['file'] = create_mock_gradebook_for_all_class($settings);
				$response['time'] = time();
				echo json_encode($response);
				exit();
				break;

			case "generate_students_zip":
				$response['file'] = _create_bulk_student_template();
				$response['time'] = time();
				echo json_encode($response);
				exit();
				break;

			default:
				# code...
				break;
		}
		// json_encode($response);
	}

endif;