<?php 
// version 1.0
require_once(dirname(__FILE__)."/functions.php");
require_once(dirname(__FILE__)."/actions.php");
require_once(dirname(__FILE__)."/do-action.php");
require_once(dirname(__FILE__)."/filters.php");
require_once(dirname(__FILE__)."/table-functions.php");

class school_fees_manager {

	public static $plugin_name = "school_fees_manager";
	public static $plugin_ref = "School Fees Manager";

	public function __construct () {
		
	}

	public static function install() {
		
		$super_admin = ['school_fees_set_up', 'manage_fee_description', 'sfm_edit_fee_description', 'sfm_send_fee_invoice', 'sfm_pay_school_fees', 'sfm_download_fees'];
		$admin = [];
		$bursar = ['school_fees_set_up', 'manage_fee_description', 'sfm_edit_fee_description', 'sfm_send_fee_invoice', 'pm_payment_method', 'bank_deposit', 'finance', 'ledger', 'payment_history', 'all_payment_history', 'request_payout', 'payouts', 'expenses', 'post_new_expense', 'wallet_statement', "view_others_wallet_statement", "my_invoices", "invoice", "invoices", "access_others_wallet_statement", "create_invoice", "view_others_invoice", "users", "see_from_admin", 'edit_others_profile', 'edit_own_profile', 'uploads', 'view_upload_library', 'add_user', "deactivate_user", "activate_user", "standalone_upload", "delete_post", "deactivate_post", "activate_post", "pm_add_payment", "sfm_change_debt_status", 'sfm_pay_school_fees', 'manage_students_in_class', 'sfm_download_fees'];
		$teacher = [];
		$student = ['sfm_pay_school_fees'];
		$debtor = [];
		$se = [];
		update_role('software_engineer', array_unique(array_merge($super_admin, $admin, $teacher, $student, $debtor, $se, $bursar)));
		update_role('super_admin', $super_admin);
		update_role('admin', $admin);
		update_role('teacher', $teacher);
		update_role('student', $student);
		update_role('debtor', $debtor);
		update_role('bursar', $bursar);

		$post = new post;
		register_admin_page(array(
			'title' => 'School Fees Set-up',
			'callback' => 'school_fees_set_up_cb',
			'access' => 'school_fees_set_up',
			'permalink' => 'school-fees-set-up'
		));

		register_admin_page(array(
			'title' => 'Manage Fee Description',
			'callback' => 'manage_fee_description_cb',
			'access' => 'manage_fee_description',
			'permalink' => 'manage-fee-description'
		));

		register_admin_page(array(
			'title' => 'Edit Fee Description',
			'callback' => 'sfm_edit_fee_description_cb',
			'access' => 'sfm_edit_fee_description',
			'permalink' => 'sfm-edit-fee-description'
		));

		register_admin_page(array(
			'title' => 'Pay School Fees',
			'callback' => 'sfm_pay_school_fees_cb',
			'access' => 'sfm_pay_school_fees',
			'permalink' => 'sfm-pay-school-fees'
		));

		register_admin_page(array(
			'title' => 'Download Fees',
			'callback' => 'sfm_download_fees_cb',
			'access' => 'sfm_download_fees',
			'permalink' => 'sfm-download-fees'
		));

		register_admin_submenu(array(
			'mm_id' => 'finance',
			'id' => 'school_fees_set_up',
			'title' => 'School Fees Set-up',
			'icon' => '',
			'page' => 'school-fees-set-up',
			'access' => 'school_fees_set_up'
		));

		// register_admin_submenu(array(
		// 	'mm_id' => 'finance',
		// 	'id' => 'manage_fee_description',
		// 	'title' => 'Manage Fee Description',
		// 	'icon' => '',
		// 	'page' => 'manage-fee-description',
		// 	'access' => 'manage_fee_description'
		// ));

		unregister_admin_submenu('manage_fee_description');
		register_admin_submenu(array(
			'mm_id' => 'finance',
			'id' => 'sfm_pay_school_fees',
			'title' => 'Pay School Fees',
			'icon' => '',
			'page' => 'sfm-pay-school-fees',
			'access' => 'student_only+sfm_pay_school_fees'
		));

		register_admin_submenu(array(
			'mm_id' => 'finance',
			'id' => 'sfm_download_fees',
			'title' => 'Download Fees',
			'icon' => '',
			'page' => 'sfm-download-fees',
			'access' => 'sfm_download_fees'
		));

		if(!get_option("sfm_school_fees")) {
			$existing = db_post_unique(['type' => 'sfm_description', 'description' => 'school fees']);
			if($existing) {
				update_option("sfm_school_fees", $existing['id']);
			} else {
				$args = [
					'type' => 'sfm_description',
					'description' => 'School Fees',
					'status' => 'active'
				];
				$id = $post->add_post($args);
				update_option("sfm_school_fees", $id);
			}
		}

		add_action('_after_student_profile_more_info_link', 'sfm_student_profile_invoice_link');
		add_action('_after_student_profile_more_info', 'sfm_student_profile_invoice');
		add_action("pm_payment_registration_success", "sfm_payment_registration_success");
		add_action("more_pm_settings", "sfm_settings");
		add_action("save_more_pm_settings", "save_sfm_settings");
		add_action("users_filter_after", "sfm_users_filter_after");
		add_action("eedu_student_profile_more", "sfm_student_profile_more");
		add_action("search_query_filter_stud_in_class", "sfm_fee_description", 3);
		add_action("after_dz_upload_sfm_upload_fees", "sfm_upload_fees");

		add_filter("pm_invoice_tagline", "sfm_invoice_tagline");
		add_filter("filter_user_args", "sfm_filter_user_args");
		add_filter("eedu_stud_in_class_table", "sfm_stud_in_class_table");
		add_filter("eedu_stud_in_class_columns", "sfm_stud_in_class_columns");
		add_filter("eedu_stud_in_class_dynamic_columns", "sfm_stud_in_class_dynamic_columns");
		add_filter("before_users_table_display", "sfm_before_users_table_display");
	}

	public static function uninstall() {
		unregister_admin_page([
			
		]);

		unregister_admin_menu([
			
		]);

		unregister_admin_submenu([
			
		]);
	}

}