<?php

/**
* The admin-specific functionality of the plugin.
*
* @link       http://www.exapartners.com/
* @since      1.0.0
*
* @package    Esi_Management
* @subpackage Esi_Management/admin
*/

/**
* The admin-specific functionality of the plugin.
*
* Defines the plugin name, version, and two examples hooks for how to
* enqueue the admin-specific stylesheet and JavaScript.
*
* @package    Esi_Management
* @subpackage Esi_Management/admin
* @author     Kremer Nicolas <nicolas.kremer@exapartners.com>
*/
class Esi_Management_Admin {

	/**
	* The ID of this plugin.
	*
	* @since    1.0.0
	* @access   private
	* @var      string    $plugin_name    The ID of this plugin.
	*/
	private $plugin_name;

	/**
	* The version of this plugin.
	*
	* @since    1.0.0
	* @access   private
	* @var      string    $version    The current version of this plugin.
	*/
	private $version;

	/**
	* Initialize the class and set its properties.
	*
	* @since    1.0.0
	* @param      string    $plugin_name       The name of this plugin.
	* @param      string    $version    The version of this plugin.
	*/
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version = $version;

	}



	/**
	* Creates news custom post type
	*
	* @since 	1.0.0
	* @access 	public
	* @uses 	register_post_type()
	*/
	public function create_news_type() {

		$cap_type 	= 'post';
		$plural 	= 'News';
		$single 	= 'News';
		$new_name 	= 'news';

		$opts['can_export']								= TRUE;
		$opts['capability_type']						= $cap_type;
		$opts['description']							= '';
		$opts['exclude_from_search']					= FALSE;
		$opts['has_archive']							= FALSE;
		$opts['hierarchical']							= FALSE;
		$opts['map_meta_cap']							= TRUE;
		$opts['menu_icon']								= 'dashicons-media-document';
		$opts['menu_position']							= 5;
		$opts['public']									= TRUE;
		$opts['publicly_querable']						= TRUE;
		$opts['query_var']								= TRUE;
		$opts['register_meta_box_cb']					= '';
		$opts['rewrite']								= FALSE;
		$opts['show_in_admin_bar']						= TRUE;
		$opts['show_in_menu']							= TRUE;
		$opts['show_in_nav_menu']						= TRUE;
		$opts['show_ui']								= TRUE;
		$opts['supports']								= array( 'title', 'editor', 'thumbnail' );
		$opts['taxonomies']								= array();

		$opts['capabilities']['delete_others_posts']	= "delete_others_{$cap_type}s";
		$opts['capabilities']['delete_post']			= "delete_{$cap_type}";
		$opts['capabilities']['delete_posts']			= "delete_{$cap_type}s";
		$opts['capabilities']['delete_private_posts']	= "delete_private_{$cap_type}s";
		$opts['capabilities']['delete_published_posts']	= "delete_published_{$cap_type}s";
		$opts['capabilities']['edit_others_posts']		= "edit_others_{$cap_type}s";
		$opts['capabilities']['edit_post']				= "edit_{$cap_type}";
		$opts['capabilities']['edit_posts']				= "edit_{$cap_type}s";
		$opts['capabilities']['edit_private_posts']		= "edit_private_{$cap_type}s";
		$opts['capabilities']['edit_published_posts']	= "edit_published_{$cap_type}s";
		$opts['capabilities']['publish_posts']			= "publish_{$cap_type}s";
		$opts['capabilities']['read_post']				= "read_{$cap_type}";
		$opts['capabilities']['read_private_posts']		= "read_private_{$cap_type}s";

		$opts['labels']['add_new']						= esc_html__( "Add New {$single}", 'now-hiring' );
		$opts['labels']['add_new_item']					= esc_html__( "Add New {$single}", 'now-hiring' );
		$opts['labels']['all_items']					= esc_html__( $plural, 'now-hiring' );
		$opts['labels']['edit_item']					= esc_html__( "Edit {$single}" , 'now-hiring' );
		$opts['labels']['menu_name']					= esc_html__( $plural, 'now-hiring' );
		$opts['labels']['name']							= esc_html__( $plural, 'now-hiring' );
		$opts['labels']['name_admin_bar']				= esc_html__( $single, 'now-hiring' );
		$opts['labels']['new_item']						= esc_html__( "New {$single}", 'now-hiring' );
		$opts['labels']['not_found']					= esc_html__( "No {$plural} Found", 'now-hiring' );
		$opts['labels']['not_found_in_trash']			= esc_html__( "No {$plural} Found in Trash", 'now-hiring' );
		$opts['labels']['parent_item_colon']			= esc_html__( "Parent {$plural} :", 'now-hiring' );
		$opts['labels']['search_items']					= esc_html__( "Search {$plural}", 'now-hiring' );
		$opts['labels']['singular_name']				= esc_html__( $single, 'now-hiring' );
		$opts['labels']['view_item']					= esc_html__( "View {$single}", 'now-hiring' );

		$opts['rewrite']['ep_mask']						= EP_PERMALINK;
		$opts['rewrite']['feeds']						= FALSE;
		$opts['rewrite']['pages']						= TRUE;
		$opts['rewrite']['slug']						= esc_html__( strtolower( $plural ), 'now-hiring' );
		$opts['rewrite']['with_front']					= FALSE;

		//$opts = apply_filters( 'now-hiring-cpt-options', $opts );

		register_post_type( strtolower( $new_name ), $opts );

	} // new_cpt_job()

	/**
	* Register the stylesheets for the admin area.
	*
	* @since    1.0.0
	*/
	public function enqueue_styles() {

		/**
		* This function is provided for demonstration purposes only.
		*
		* An instance of this class should be passed to the run() function
		* defined in Esi_Management_Loader as all of the hooks are defined
		* in that particular class.
		*
		* The Esi_Management_Loader will then create the relationship

		* between the defined hooks and the functions defined in this
		* class.
		*/

		wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/esi-management-admin.css', array(), $this->version, 'all' );
		wp_enqueue_style( 'jquery.tree-multiselect.css', plugin_dir_url( __FILE__ ) . 'css/jquery.tree-multiselect.css', array(), $this->version, 'all' );
		wp_enqueue_style( 'multi-select.css', plugin_dir_url( __FILE__ ) . 'css/multi-select.css', array(), $this->version, 'all' );
		wp_enqueue_style( 'chosen.min.css', plugin_dir_url( __FILE__ ) . 'css/chosen.min.css', array(), $this->version, 'all' );

	}

	/**
	* Register the JavaScript for the admin area.
	*
	* @since    1.0.0
	*/
	public function enqueue_scripts() {

		/**
		* This function is provided for demonstration purposes only.
		*
		* An instance of this class should be passed to the run() function
		* defined in Esi_Management_Loader as all of the hooks are defined
		* in that particular class.
		*
		* The Esi_Management_Loader will then create the relationship
		* between the defined hooks and the functions defined in this
		* class.
		*/
		wp_enqueue_script( 'chosen.jquery', plugin_dir_url( __FILE__ ) . 'js/chosen.jquery.min.js', array( 'jquery' ), $this->version, false );
		wp_enqueue_script( 'jquery.tree-multiselect', plugin_dir_url( __FILE__ ) . 'js/jquery.tree-multiselect.js', array( 'jquery' ), $this->version, false );
		wp_enqueue_script( 'jquery.multi-select', plugin_dir_url( __FILE__ ) . 'js/jquery.multi-select.js', array( 'jquery' ), $this->version, false );
		wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/esi-management-admin.js', array( 'jquery' ), $this->version, false );
	}

	/**
	* Register the administration menu for this plugin into the WordPress Dashboard menu.
	*
	* @since    1.0.0
	*/
	public function add_plugin_admin_menu()
	{

		/*
		* Add a settings page for this plugin to the Settings menu.
		*
		* NOTE:  Alternative menu locations are available via WordPress administration menu functions.
		*
		*        Administration Menus: http://codex.wordpress.org/Administration_Menus
		*
		*/
		/** Top Menu **/
		add_menu_page( __( 'ESI Management', 'esi' ), __( 'ESI Management', 'esi' ), 'manage_options', $this->plugin_name, array( $this, 'plugin_radar' ), 'dashicons-groups', null );

		$plugin_admin_radar = new Esi_Management_Radar( $this->plugin_name, $this->version );
		add_submenu_page( $this->plugin_name, __( 'Radar', 'esi' ), __( 'Radar', 'esi' ), 'manage_options', 'esi-management-radar', array( $plugin_admin_radar, 'plugin_page' ) );

		$plugin_admin_news = new Esi_Management_News( $this->plugin_name, $this->version );
		add_submenu_page( $this->plugin_name, __( 'News', 'esi' ), __( 'News', 'esi' ), 'manage_options', 'esi-management-news', array( $plugin_admin_news, 'plugin_page' ) );

		$plugin_admin_type = new Esi_Management_Type( $this->plugin_name, $this->version );
		add_submenu_page( $this->plugin_name, __( 'News Type', 'esi' ), __( 'News Type', 'esi' ), 'manage_options', 'esi-management-type', array( $plugin_admin_type, 'plugin_page' ) );

		$plugin_admin_media = new Esi_Management_Media( $this->plugin_name, $this->version );
		add_submenu_page( $this->plugin_name, __( 'Media', 'esi' ), __( 'Media', 'esi' ), 'manage_options', 'esi-management-media', array( $plugin_admin_media, 'plugin_page' ) );

		$plugin_admin_company = new Esi_Management_Entity( $this->plugin_name, $this->version );
		add_submenu_page( $this->plugin_name, __( 'Entity', 'esi' ), __( 'Entity', 'esi' ), 'manage_options', 'esi-management-entity', array( $plugin_admin_company, 'plugin_page' ) );

		$plugin_admin_investment = new Esi_Management_Investment( $this->plugin_name, $this->version );
		add_submenu_page( $this->plugin_name, __( 'Investment', 'esi' ), __( 'Investment', 'esi' ), 'manage_options', 'esi-management-investment', array( $plugin_admin_investment, 'plugin_page' ) );

		$plugin_admin_investment_type = new Esi_Management_Investment_Type( $this->plugin_name, $this->version );
		add_submenu_page( $this->plugin_name, __( 'Investment Type', 'esi' ), __( 'Investment Type', 'esi' ), 'manage_options', 'esi-management-investment-type', array( $plugin_admin_investment_type, 'plugin_page' ) );

		$plugin_admin_region = new Esi_Management_Region( $this->plugin_name, $this->version );
		add_submenu_page( $this->plugin_name, __( 'Region', 'esi' ), __( 'Region', 'esi' ), 'manage_options', 'esi-management-region', array( $plugin_admin_region, 'plugin_page' ) );

		$plugin_admin_ability = new Esi_Management_Ability( $this->plugin_name, $this->version );
		add_submenu_page( $this->plugin_name, __( 'Ability', 'esi' ), __( 'Ability', 'esi' ), 'manage_options', 'esi-management-ability', array( $plugin_admin_ability, 'plugin_page' ) );

		$plugin_admin_patent = new Esi_Management_Patent( $this->plugin_name, $this->version );
		add_submenu_page( $this->plugin_name, __( 'Patent', 'esi' ), __( 'Patent', 'esi' ), 'manage_options', 'esi-management-patent', array( $plugin_admin_patent, 'plugin_page' ) );

	}

	/**
	* Add settings action link to the plugins page.
	*
	* @since    1.0.0
	*/
	public function add_action_links($links)
	{
		/*
		*  Documentation : https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name)
		*/
		$settings_link = array(
			'<a href="' . admin_url('options-general.php?page=' . $this->plugin_name) . '">' . __('Settings', $this->plugin_name) . '</a>',
		);
		return array_merge($settings_link, $links);

	}

	public function esi_company_ajax_callback(){
		$data = $_POST;
		if(!empty($data))
		{
			$companyForm = new Esi_Management_Entity_Form();
			$response = $companyForm->handle_form(true);

			echo json_encode($response);
			wp_die();
		}
	}

	public function esi_media_ajax_callback(){
		$data = $_POST;
		if(!empty($data))
		{
			$mediaForm = new Esi_Management_Media_Form();
			$response = $mediaForm->handle_form(true);

			echo json_encode($response);
			wp_die();
		}
	}

	public function esi_investment_ajax_callback(){
		$data = $_POST;
		if(!empty($data))
		{
			$investmentForm = new Esi_Management_Investment_Form();
			$response = $investmentForm->handle_form(true);

			echo json_encode($response);
			wp_die();
		}
	}

	public function esi_radar_ajax_callback(){
		$data = $_POST;
		if(!empty($data))
		{
			$response = array_merge(array('industry' => esi_get_radar_industry($data['radar_id'])), array('function' => esi_get_radar_function($data['radar_id'])));

			echo json_encode($response);
			wp_die();
		}
	}

	public function esi_radar_action_callback(){

		$data = $_POST;
		if(!empty($data["table"]))
		{
			global $wpdb;
			$table_name = $wpdb->prefix . $data["table"];
			$wpdb->insert(
			$table_name,
			array(
				'name' => $data["name"],
				'parent' => $data["parent"],
			));

			$data["id"] = $wpdb->insert_id;
			$data["parent"] = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . $data["table"] .' WHERE id = %d', $data["parent"] ) );

			// Response
			echo json_encode($data);
			wp_die();
		}

	}

	public function esi_industries_ajax_callback(){
		$data = $_POST;
		if(!empty($data))
		{
			$response = array('industry' => esi_get_list_industry($data['industry_id']));

			echo json_encode($response);
			wp_die();
		}
	}

	public function esi_technologies_ajax_callback(){
		$data = $_POST;
		if(!empty($data))
		{
			$response = array('returned_data' => esi_get_list_technology($data['id']));

			echo json_encode($response);
			wp_die();
		}
	}

	/**
	 * Show custom user profile fields
	 * @param  obj $user The user object.
	 * @return void
	 */
	public function esi_custom_user_profile_fields($user) {

		$template = dirname( __FILE__ ) . '/modules/radar/partials/esi-management-radar-user.php';

		if ( file_exists( $template ) ) {
			include $template;
		}
	}

	/**
	* Handles the plugin page
	*
	* @return void
	*/
	public function plugin_radar() {

	}

}
