<?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__)."/query-functions.php");
require_once(dirname(__FILE__)."/table-functions.php");

class payment_module {

	public static $plugin_name = "payment_module";
	public static $plugin_root = 'contents\plugins\payment_module';
	public static $plugin_ref = "Payment Module";

	public function __construct () {
		
	}

	public static function install() {
		$common_access = ['pm_payment_method', 'finance', 'bank_deposit', 'payment_history', 'wallet_statement', "my_invoices", "invoice"];
		add_common_access($common_access);
		$super_admin = ['pm_payment_method', 'finance', 'confirm_payments', 'ledger', 'payment_history', 'all_payment_history', 'credit_user', 'debit_user', "approve_expense", "approve_direct_credit", 'direct_credits', 'request_payout', 'payouts', 'approve_payout', 'expenses', 'approve_expense', 'post_new_expense', 'wallet_statement', "view_others_wallet_statement", "approve_direct_debit", "my_invoices", "invoice", "reject_payments", "invoices", "access_others_wallet_statement", "create_invoice", "view_others_invoice", "pm_add_payment", "pm_top_up", "pm_account", "pm_manage_bank_accounts", "pm_bank_statement", "pm_add_discount"]; //content owner
		$super_admin_tech = ['pm_payment_method', 'finance', 'confirm_payments', 'ledger', 'payment_history', 'all_payment_history', 'credit_user', 'debit_user', "approve_expense", "approve_direct_credit", 'direct_credits', 'request_payout', 'payouts', 'approve_payout', 'expenses', 'approve_expense', 'post_new_expense', 'wallet_statement', "view_others_wallet_statement", "approve_direct_debit", "my_invoices", "invoice", "reject_payments", "invoices", "access_others_wallet_statement", "create_invoice", "view_others_invoice", "pm_add_payment", "pm_top_up", "pm_account", "pm_manage_bank_accounts", "pm_bank_statement", "pm_add_discount"]; //technical owner
		$admin = ['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", "pm_add_payment", "pm_top_up", "pm_account"]; //content owner
		$admin_tech = ['pm_payment_method', 'bank_deposit', '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", "pm_add_payment", "pm_top_up", "pm_account"]; //technical owner
		$member = ['pm_payment_method', 'finance', 'bank_deposit', 'payment_history', 'request_payout', 'payouts', 'wallet_statement', "my_invoices", "invoice", "pm_top_up", "pm_account"];
		$se = ['pm_edit_invoice', 'pm_cancel_invoice'];
		$pending = ['pm_payment_method', 'finance', 'bank_deposit', 'payment_history', 'wallet_statement', "my_invoices", "invoice", "pm_top_up"];
		$unverified = [];
		update_role('software_engineer', array_unique(array_merge($super_admin, $admin, $super_admin_tech, $admin_tech, $member, $se, $pending)));
		update_role('super_admin', $super_admin);
		update_role('admin', $admin);
		update_role('super_admin_tech', $super_admin_tech);
		update_role('admin_tech', $admin_tech);
		update_role('member', $member);
		update_role('pending', $pending);
		update_role('unverified', $unverified);

		register_admin_page(array(
			'title' => 'Select Payment Method',
			'callback' => 'pm_payment_method_cb',
			'access' => 'pm_payment_method',
			'permalink' => 'pm-payment-method'
		));

		register_admin_page(array(
			'title' => 'Bank Deposit/Transfer Method',
			'callback' => 'bank_deposit_cb',
			'access' => 'bank_deposit',
			'permalink' => 'bank-deposit'
		));

		register_admin_page(array(
			'title' => 'Confirm Payments',
			'callback' => 'confirm_payments_cb',
			'access' => 'confirm_payments',
			'permalink' => 'confirm-payments'
		));

		register_admin_page(array(
			'title' => 'Direct Credits',
			'callback' => 'direct_credits_cb',
			'access' => 'direct_credits',
			'permalink' => 'direct-credits'
		));

		register_admin_page(array(
			'title' => 'Request Payout/CashOut',
			'callback' => 'request_payout_cb',
			'access' => 'request_payout',
			'permalink' => 'request-payout'
		));

		register_admin_page(array(
			'title' => 'Expenses',
			'callback' => 'expenses_cb',
			'access' => 'expenses',
			'permalink' => 'expenses'
		));

		register_admin_page(array(
			'title' => 'Reject Payment',
			'callback' => 'reject_payment_cb',
			'access' => 'reject_payments',
			'permalink' => 'reject-payment'
		));

		register_admin_page(array(
			'title' => 'New Expense',
			'callback' => 'post_new_expense_cb',
			'access' => 'post_new_expense',
			'permalink' => 'post-new-expense'
		));

		register_admin_page(array(
			'title' => 'Wallet Statement',
			'callback' => 'wallet_statement_cb',
			'access' => 'wallet_statement',
			'permalink' => 'wallet-statement'
		));

		register_admin_page(array(
			'title' => 'Ledger',
			'callback' => 'ledger_cb',
			'access' => 'ledger',
			'permalink' => 'ledger'
		));

		register_admin_page(array(
			'title' => 'Payouts',
			'callback' => 'payouts_cb',
			'access' => 'payouts',
			'permalink' => 'payouts'
		));

		register_admin_page(array(
			'title' => 'Payment History',
			'callback' => 'payment_history_cb',
			'access' => 'payment_history',
			'permalink' => 'payment-history'
		));

		register_admin_page(array(
			'title' => 'Add Discount',
			'callback' => 'pm_add_discount_cb',
			'access' => 'pm_add_discount',
			'permalink' => 'pm-add-discount'
		));

		register_admin_page(array(
			'title' => 'Credit User',
			'callback' => 'credit_user_cb',
			'access' => 'credit_user',
			'permalink' => 'credit-user'
		));

		register_admin_page(array(
			'title' => 'Debit User',
			'callback' => 'debit_user_cb',
			'access' => 'debit_user',
			'permalink' => 'debit-user'
		));

		register_admin_page(array(
			'title' => 'My Invoices',
			'callback' => 'my_invoices_cb',
			'access' => 'my_invoices',
			'permalink' => 'my-invoices'
		));

		register_admin_page(array(
			'title' => 'Invoice',
			'callback' => 'invoice_cb',
			'access' => 'invoice',
			'permalink' => 'invoice'
		));

		register_admin_page(array(
			'title' => 'Invoices',
			'callback' => 'invoices_cb',
			'access' => 'invoices',
			'permalink' => 'invoices'
		));

		register_admin_page(array(
			'title' => 'Create Invoice',
			'callback' => 'create_invoice_cb',
			'access' => 'create_invoice',
			'permalink' => 'create-invoice'
		));

		register_admin_page(array(
			'title' => 'Edit Invoice',
			'callback' => 'edit_invoice_cb',
			'access' => 'pm_edit_invoice',
			'permalink' => 'edit-invoice'
		));

		register_admin_page(array(
			'title' => 'Add Payment',
			'callback' => 'pm_add_payment_cb',
			'access' => 'pm_add_payment',
			'permalink' => 'pm-add-payment'
		));

		register_admin_page(array(
			'title' => 'e-Wallet Top Up',
			'callback' => 'pm_top_up_cb',
			'access' => 'pm_top_up',
			'permalink' => 'pm-top-up'
		));

		register_admin_page(array(
			'title' => 'Account',
			'callback' => 'pm_account_cb',
			'access' => 'pm_account',
			'permalink' => 'pm-account'
		));

		register_admin_page(array(
			'title' => 'Approve Payout',
			'callback' => 'pm_approve_payout_cb',
			'access' => 'approve_payout',
			'permalink' => 'pm-approve-payout'
		));

		register_admin_page(array(
			'title' => 'Manage Bank Accounts',
			'callback' => 'pm_manage_bank_accounts_cb',
			'access' => 'pm_manage_bank_accounts',
			'permalink' => 'pm-manage-bank-accounts'
		));

		register_admin_page(array(
			'title' => 'Edit Bank Account',
			'callback' => 'pm_edit_bank_account_cb',
			'access' => 'pm_manage_bank_accounts',
			'permalink' => 'pm-edit-bank-account'
		));

		register_admin_page(array(
			'title' => 'Bank Statement',
			'callback' => 'pm_bank_statement_cb',
			'access' => 'pm_bank_statement',
			'permalink' => 'pm-bank-statement'
		));

		register_admin_menu(array(
			'id' => 'finance',
			'title' => 'Finance',
			'icon' => 'fas fa-money-check-alt',
			'page' => '',
			'access' => 'finance',
			'order' => 2
		));

		register_admin_submenu(array(
			'mm_id' => 'finance',
			'id' => 'Confirm_payments',
			'title' => 'Confirm Payments',
			'icon' => '',
			'page' => 'confirm-payments',
			'access' => 'confirm_payments'
		));

		register_admin_submenu(array(
			'mm_id' => 'finance',
			'id' => 'pm_manage_bank_accounts',
			'title' => 'Manage Bank Accounts',
			'icon' => '',
			'page' => 'pm-manage-bank-accounts',
			'access' => 'pm_manage_bank_accounts'
		));

		unregister_admin_submenu('payment_history');

		register_admin_submenu(array(
			'mm_id' => 'finance',
			'id' => 'ledger',
			'title' => 'Ledger',
			'icon' => '',
			'page' => 'ledger',
			'access' => 'ledger'
		));

		unregister_admin_submenu('credit_user');
		// register_admin_submenu(array(
		// 	'mm_id' => 'finance',
		// 	'id' => 'credit_user',
		// 	'title' => 'Credit User',
		// 	'icon' => '',
		// 	'page' => 'credit-user',
		// 	'access' => 'credit_user'
		// ));

		register_admin_submenu(array(
			'mm_id' => 'finance',
			'id' => 'create_invoice',
			'title' => 'Create Invoice',
			'icon' => '',
			'page' => 'create-invoice',
			'access' => 'create_invoice'
		));

		unregister_admin_submenu('debit_user');

		register_admin_submenu(array(
			'mm_id' => 'finance',
			'id' => 'direct_credits',
			'title' => 'Direct Credits',
			'icon' => '',
			'page' => 'direct-credits',
			'access' => 'direct_credits'
		));

		unregister_admin_submenu('request_payout');

		unregister_admin_submenu('payouts');

		unregister_admin_submenu('expenses');

		unregister_admin_submenu('post_new_expense');

		register_admin_submenu(array(
			'mm_id' => 'finance',
			'id' => 'wallet_statement',
			'title' => 'Wallet Statement',
			'icon' => '',
			'page' => 'wallet-statement',
			'access' => 'wallet_statement'
		));

		register_admin_submenu(array(
			'mm_id' => 'finance',
			'id' => 'invoices',
			'title' => 'Invoices',
			'icon' => '',
			'page' => 'invoices',
			'access' => 'invoices'
		));

		register_admin_submenu(array(
			'mm_id' => 'finance',
			'id' => 'my_invoices',
			'title' => 'My Invoices',
			'icon' => '',
			'page' => 'my-invoices',
			'access' => 'my_invoices'
		));

		register_admin_submenu(array(
			'mm_id' => 'settings',
			'id' => 'pm_account',
			'title' => 'Account',
			'icon' => '',
			'page' => 'pm-account',
			'access' => 'pm_account'
		));

		add_action("_pm_make_payment_button", "_pm_make_payment_button");
		add_action("payment_method_display", "_pm_bank_deposit_payment_method");
		add_action('more_settings_field_right', "_pm_settings");
		add_action('save_more_settings_field', "_save_pm_settings");
		remove_action('display_ledger_search_result', 'display_ledger_search_result');
		add_action('display_wallet_statement_result', 'display_wallet_statement_result');
		add_action('_pm_cashout_btn', '_pm_cashout_btn');
		add_action("pm_query_filters", "pm_query_filters");
		add_action("more_edit_profile_field", "pm_more_edit_profile_field");
		add_action('load_other_dependencies', 'pm_load_dependencies');
		remove_action('search_query_filter_ledger', 'search_query_date_range');
		remove_action('search_query_filter_ledger', 'pm_ledger_filter');
		add_action('search_query_qf_filter_ledger', 'search_query_date_range');
		add_action('search_query_qf_filter_ledger', 'pm_ledger_filter');
		add_action('search_query_filter_pm_payments', 'pm_payments_filter');
		add_action('search_query_filter_pm_payments', 'search_query_date_range');
		add_action('search_query_filter_pm_invoices', 'search_query_date_range');
		add_action('search_query_filter_pm_invoices', 'pm_invoices_filter');
		add_action('search_query_filter_pm_payment_history', 'pm_payments_filter');
		add_action('search_query_filter_pm_payment_history', 'search_query_date_range');
		add_action('search_query_filter_pm_wallet_statement', 'search_query_date_range');
		add_action('search_query_filter_pm_wallet_statement', 'pm_wallet_statement_filter');
		add_action('search_query_qf_filter_pm_qf_invoices', 'search_query_date_range');
		add_action('search_query_qf_filter_pm_qf_invoices', 'pm_invoices_filter');
		add_action('search_query_filter_pm_my_invoices', 'search_query_date_range');
		add_action('search_query_filter_pm_my_invoices', 'pm_my_invoices_filter');
		add_action('search_query_qf_filter_pm_qf_my_invoices', 'pm_my_invoices_filter');
		add_action('dashboard_row', 'pm_dashboard_row');
		add_action("pm_quick_menu", "pm_quick_menu");
		add_action("pm_list_payment_method", "bank_payment_pm_list_payment_method");

		add_filter("filter_page_meta_tags", "pm_filter_page_meta_tags");
		if(class_exists("pm_invoice")) {
			$invoice = new pm_invoice();
			$invoice->init();
		}
		$args = [
			'gateway_name' => 'Bank Deposit/Transfer',
			'gateway_description' => 'You can make payment using our bank account details and upload your teller or any other evidence of payment online for verification.',
			'gateway_icon' => 'contents/plugins/payment_module/img/bank-72x72.png',
			'gateway_page' => 'bank-deposit',
			'gateway_id' => 'bank-deposit'
		];
		update_option("gtw_meta-bank-deposit", $args);

		if(!get_option("pm_email_template_registered")) {
			$template = new template;
			$template->register_template("contents/plugins/payment_module/templates/payment-confirmation/index.html", "payment_confirmation", "email");
			update_option("pm_email_template_registered", 1);
		}
		if(!get_option("pm_default_invoice_templates")) {
			$template = new template;
			$template->register_template("contents/plugins/payment_module/templates/invoice-templates/thead.html", "default-invoice-thead", "invoice-thead");
			$template->register_template("contents/plugins/payment_module/templates/invoice-templates/tbody.html", "default-invoice-tbody", "invoice-tbody");
			$template->register_template("contents/plugins/payment_module/templates/invoice-templates/tfoot.html", "default-invoice-tfoot", "invoice-tfoot");
			$template->register_template("contents/plugins/payment_module/templates/invoice-templates/invoice.html", "default-invoice", "invoice");
			$template->register_template("contents/plugins/payment_module/templates/invoice-templates/invoice-items.html", "default-invoice-items", "invoice-items");
			$template->register_template("contents/plugins/payment_module/templates/invoice-templates/payments.html", "default-payments", "payments");
			$template->register_template("contents/plugins/payment_module/templates/invoice-templates/payment-methods.html", "default-payment-methods", "payment-methods");
			$template->register_template("contents/plugins/payment_module/templates/invoice-templates/accounts.html", "default-accounts", "accounts");
			update_option("pm_default_invoice_templates", 1);
		}
	}

	public static function uninstall() {
		unregister_admin_page([
			'pm-payment-method', 'bank-deposit', 'confirm-payments', 'direct-credits', 'request-payout', 'expenses', 'post-new-expense', 'wallet-statement', 'ledger', 'payouts', 'payment-history', 'credit-user'
		]);

		unregister_admin_menu([
			'finance'
		]);

		remove_action("_pm_make_payment_button", "_pm_make_payment_button");
		remove_action("payment_method_display", "_pm_bank_deposit_payment_method");
		remove_action('more_settings_field_right', "_pm_settings");
		remove_action('save_more_settings_field', "_save_pm_settings");
		remove_action('display_ledger_search_result', 'display_ledger_search_result');
		remove_action('display_wallet_statement_result', 'display_wallet_statement_result');
		remove_action('_pm_cashout_btn', '_pm_cashout_btn');
		remove_action("pm_query_filters", "pm_query_filters");
		remove_action("more_edit_profile_field", "pm_more_edit_profile_field");
		remove_action('search_query_filter_ledger', 'search_query_date_range');
		remove_action('search_query_filter_pm_payments', 'pm_payments_filter');
		remove_action('search_query_filter_pm_payments', 'search_query_date_range');
		remove_action('search_query_filter_pm_invoices', 'search_query_date_range');
		remove_action('search_query_filter_pm_invoices', 'pm_invoices_filter');
		// remove_action('search_query_qf_filter_pm_qf_my_invoices', 'search_query_date_range');
	}

}