<?php
namespace DOZENT_LMS_CERTIFICATE;

defined( 'ABSPATH' ) || exit;

/**
 * Class WP Dashboard
 *
 * @package Dozent
 *
 */

class WPDashboard {

	public function __construct() {
		add_action( 'dozent_admin_menu_group_three', array( $this, 'certificate_menu' ) );
		add_action('admin_head', [ $this, 'dlc_current_menu' ], 50);
		add_action( 'wp_ajax_set_default_certificate', [ $this, 'default_certificate' ] );
	}

	public function certificate_menu() {
		add_submenu_page( 'dozent', __( 'Certificates', 'dozent-lms-certificate' ), __( 'Certificates', 'dozent-lms-certificate' ), 'manage_options',
			'dozent_certificate', [ $this, 'certificate_lists' ] );
	}

	public function certificate_lists(){
		include_once DLC_ABSPATH . 'core/views/certificates-list.php';
	}

	function dlc_current_menu(){
		$screen = get_current_screen();
		if ( $screen->id == 'dozent_certificate' || $screen->id == 'edit-dozent_certificate' ) {
			?>
			<script type="text/javascript">
				jQuery(document).ready(function($) {
					$('#toplevel_page_dozent').addClass('wp-has-current-submenu wp-menu-open menu-top menu-top-first').removeClass('wp-not-current-submenu');
					$('#toplevel_page_dozent > a').addClass('wp-has-current-submenu').removeClass('wp-not-current-submenu');
				});
			</script>
			<?php
		}

		if ( $screen->id == 'dozent_certificate' ) {
			?>
			<script type="text/javascript">
				jQuery(document).ready(function($) {
					$('a[href="admin.php?page=dozent_certificate"]').parent().addClass('current');
					$('a[href="admin.php?page=dozent_certificate"]').addClass('current');
				});
			</script>
			<?php
		}

		if ( $screen->id == 'edit-dozent_certificate' ) {
			?>
			<script type="text/javascript">
				jQuery(document).ready(function($) {
					$('a[href$="nerfherder_plugin_show_posts"]').parent().addClass('current');
					$('a[href$="nerfherder_plugin_show_posts"]').addClass('current');
				});
			</script>
			<?php
		}
	}

	public function default_certificate(){
	    $certificate_id = (int) sanitize_text_field( $_POST[ 'certificate_id' ] );
	    update_option( 'default_certificate_id', $certificate_id );
	}

}