<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://profile.fraxzon.com
 * @since      1.0.0
 *
 * @package    Ffl_Dealers
 * @subpackage Ffl_Dealers/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    Ffl_Dealers
 * @subpackage Ffl_Dealers/admin
 * @author     shafiq <shafiq6171@gmail.com>
 */
class Ffl_Dealers_Admin {
	use Form_Validation;
	/**
	 * 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;

	}

	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles($screen) {
		
		$review_done = get_option('ffl_dealer_review_ok');
        if ($screen == 'ffl-dealers_page_ffl-settings'  && $review_done != 1){
            wp_enqueue_style('ffl-dealers-admin-notice', FFL_DEALERS_DIR_URL . 'admin/css/ffl-dealers-admin-notice.css', array(), $this->version, 'all');
        }
		if ($screen == 'ffl-dealers_page_ffl-settings' || $screen == 'toplevel_page_ffl-dealers') {
			wp_enqueue_style('select2', FFL_DEALERS_DIR_URL . 'admin/css/select2.min.css', array(), $this->version, 'all');
			wp_enqueue_style('spectrum', FFL_DEALERS_DIR_URL . 'admin/css/spectrum.min.css', array(), $this->version, 'all');
			wp_enqueue_style($this->plugin_name, FFL_DEALERS_DIR_URL . 'admin/css/ffl-dealers-admin.css', array(), $this->version, 'all');
		}
	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts($screen) {
		 if ($screen == 'ffl-dealers_page_ffl-settings' || $screen == 'toplevel_page_ffl-dealers') {
			 
			$map_key = esc_attr(get_option('google_map_api_key'));
			
			wp_enqueue_script('select2', plugin_dir_url(__FILE__) . 'js/select2.min.js', array('jquery'), $this->version, true);
			wp_enqueue_media();
			wp_enqueue_script('spectrum', plugin_dir_url(__FILE__) . 'js/spectrum.min.js', array('jquery'), $this->version, true);
			wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/ffl-dealers-admin.js', array('jquery','select2'), $this->version, true);
			wp_enqueue_script('ffl-map-admin', '//maps.googleapis.com/maps/api/js?key=' . $map_key . '&callback=initFFLMap&libraries=places,drawing&language=' . get_google_maps_ffl_locale_country_code(), array($this->plugin_name), false, true);
			$params = array(
				'nonce'     		=> wp_create_nonce( 'form' ),
				'modal_enable' 		=> (1== esc_attr(get_option('locator_modal_enable')))?1:0,
				'shortcode_enable' 	=> (1== esc_attr(get_option('locator_shortcode_enable')))?1:0,
				'google_map_enable' => (1== esc_attr(get_option('ffl_dealer_google_map_enable')))?1:0,
				'ajaxurl' 			=> admin_url( 'admin-ajax.php' ),
			);
			wp_localize_script($this->plugin_name, 'ffld', $params);
		
	  }
	}
	public function check_ffl_dealers_installation_time(){
		$plugin_installed = esc_attr(get_option('ffl_dealer_activation_time'));
        if ( install_date_duration($plugin_installed ) >60) {
            add_action( 'admin_notices', [$this,'ffl_dealer_plugin_review_notice_notice']);
        }
		if( isset( $_GET['ffld'] ) && !empty( $_GET['ffld'] ) ){
			$ffld = $_GET['ffld'];
			if( $ffld == 1 ){
				add_option( 'ffl_dealer_plugin_review_ok' , TRUE );
			}
		}
	}
	public function ffl_dealer_plugin_review_notice_notice(){
		 // wordpress global variable 
		global $pagenow;
		$review_done 	= get_option('ffl_dealer_plugin_review_ok');
		if((isset($_GET['page']) && $_GET['page'] =='ffl-dealers' && $review_done !=1)){
			$dont_disturb = esc_url( get_admin_url() . '?ffld=1' );    
			$plugin_info = get_plugin_data( __FILE__ , true, true );  			
			$reviewurl = esc_url( 'https://wordpress.org/support/plugin/ff-dealers/reviews/' );
			printf(__('<div class="digital-lottery-review wrap">You have been using <b> %s </b> for a while. We hope you liked it ! Please give us a quick rating, it works as a boost for us to keep working on the plugin !<div class="digital-lottery-review-btn"><a href="%s" class="button digital-lottery-notice-button-rate" target="_blank">Rate Now!</a><a href="%s" class="button digital-lottery-notice-button-done"> Already Done !</a></div></div>', $plugin_info['TextDomain']), $plugin_info['Name'], $reviewurl, $dont_disturb );
		}
	}
	public function ffl_dealers_admin_menu(){
		$parent_slug = 'ffl-dealers';
		$capability = 'manage_options';
		$parent_callback = 'ffl_dealers_menu_calback';
		add_menu_page(__('FFL Dealers', 'ffl-dealers'), __('FFL Dealers', 'ffl-dealers'), $capability, $parent_slug, [$this, $parent_callback], FFL_DEALERS_DIR_URL . 'admin/imgs/fire-gun-26.png', 30);
		add_submenu_page($parent_slug,__('Settings', 'ffl-dealers'),__('Settings', 'ffl-dealers'), $capability,'ffl-settings', [$this, 'ffl_dealers_settings_callback'], 2);
	}
	public function ffl_dealers_menu_calback(){
		$active_tab = isset($_GET['action']) ? sanitize_key($_GET['action']) : 'list';
		$page  = (isset($_GET['page']) && $_GET['page']=='ffl-dealers')?'ffl-dealers':"";
		if($page){
			switch($active_tab){
				case 'edit':
				$id = isset($_GET['id'])?sanitize_text_field($_GET['id']):"";
				$dealer = get_single_dealer_by_id($id);
				include_once FFL_DEALERS_PATH . 'admin/partials/ffl-dealer-edit.php';
				break;
				case 'new':
				include_once FFL_DEALERS_PATH . 'admin/partials/ffl-dealer-new.php';
				break;
				default:
					include_once FFL_DEALERS_PATH . 'admin/partials/ffl-dealers.php';
				break;
			}
		}
	}
	public function ffl_dealers_settings_callback(){
        include_once FFL_DEALERS_PATH . 'admin/partials/ffl-dealers-settings.php';
	}
	public function ffl_admin_menu_settings_tabs(){
		 $tabs = array(
            'general' 			=> __('General', 'ffl-dealers'),
            'blacklist_dealers' => __('Blacklist', 'ffl-dealers'),
            'default_dealers' 	=> __('Default', 'ffl-dealers'),
            'map' 				=> __('Map', 'ffl-dealers'),
            'style' 			=> __('Style', 'ffl-dealers'),
           // 'pro_plugin' 		=> __('Pro', 'ffl-dealers'),
            'support' 			=> __('Support', 'ffl-dealers'),
        );
        return $tabs;
	}
	public function ffl_dealers_add_new(){
		if(isset($_POST['ffl_deaer_fields']) && wp_verify_nonce($_POST['ffl_deaer_fields'], 'ffl_deaer_nonce')) {
			$id 			= isset($_POST['dealer_id'])?sanitize_text_field($_POST['dealer_id']):"";
			$license_number = isset($_POST['license_number'])?sanitize_text_field($_POST['license_number']):"";
			$short_lic 		= isset($_POST['short_lic'])?sanitize_text_field($_POST['short_lic']):"";
			$license_name 	= isset($_POST['license_name'])?sanitize_text_field($_POST['license_name']):"";
			$business_name 	= isset($_POST['business_name'])?sanitize_text_field($_POST['business_name']):"";
			$premise_street = isset($_POST['premise_street'])?sanitize_text_field($_POST['premise_street']):"";
			$premise_city 	= isset($_POST['premise_city'])?sanitize_text_field($_POST['premise_city']):"";
			$premise_state 	= isset($_POST['premise_state'])?sanitize_text_field($_POST['premise_state']):"";
			$zipcode 		= isset($_POST['premise_zip_code'])?sanitize_text_field($_POST['premise_zip_code']):"";
			$lat 			= isset($_POST['lat'])?sanitize_text_field($_POST['lat']):"";
			$lng 			= isset($_POST['lng'])?sanitize_text_field($_POST['lng']):"";
			$voice_phone 	= isset($_POST['voice_phone'])?sanitize_text_field($_POST['voice_phone']):"";
			$check_status 	= isset($_POST['check_status'])?1:0;
			$default_dealer = isset($_POST['default_dealer'])?1:0;
			$dealer_data = array(
				'license_number'	=>	$license_number,
				'short_lic'			=>	$short_lic,
				'license_name'		=>	$license_name,
				'business_name'		=>	$business_name,
				'premise_street'	=>	$premise_street,
				'premise_city'		=>	$premise_city,
				'premise_state'		=>	$premise_state,
				'premise_zip_code'	=>	$zipcode,
				'lat'				=>	$lat,
				'lng'				=>	$lng,
				'voice_phone'		=>	$voice_phone,
				'check_status'		=>	$check_status,
				'default_dealer'	=>	$default_dealer
			);

			if(esc_attr(get_option('google_map_api_key'))){
				if(empty($lat) || empty($lng)){
					$longitude_latitude = longitude_latitude_by_street_address($premise_street.', '.$premise_city.', '.$premise_state);
					$dealer_data['lat']	= isset($longitude_latitude['latitude'])?$longitude_latitude['latitude']:0;
					$dealer_data['lng'] = isset($longitude_latitude['longitude'])?$longitude_latitude['longitude']:0;
				}
			}
			
			if ( empty( $license_number ) ) {
				$this->errors['license_number'] = __( 'Please provide a license number', 'wedevs-academy' );
			}

			if ( empty( $short_lic ) ) {
				$this->errors['short_lic'] = __( 'Please provide a short license.', 'wedevs-academy' );
			}
			
			if ( empty( $premise_street ) ) {
				$this->errors['premise_street'] = __( 'Please provide a premise street.', 'wedevs-academy' );
			}
			if ( empty( $premise_city ) ) {
				$this->errors['premise_city'] = __( 'Please provide a premise city.', 'wedevs-academy' );
			}
			if ( empty( $premise_state ) ) {
				$this->errors['premise_state'] = __( 'Please provide a premise state.', 'wedevs-academy' );
			}
			if ( empty( $zipcode ) ) {
				$this->errors['premise_zip_code'] = __( 'Please provide a premise zip code.', 'wedevs-academy' );
			}
			if ( empty( $voice_phone ) ) {
				$this->errors['voice_phone'] = __( 'Please provide a voice phone.', 'wedevs-academy' );
			}
			
			if ( ! empty( $this->errors ) ) {
				return;
			}
		
			if($id){
				$dealer_data['id'] = $id;
			}
		
			$insert_id = wp_insert_dealer_data($dealer_data);
			
			if ( is_wp_error( $insert_id ) ) {
				wp_die( $insert_id->get_error_message() );
			}

			if ( $id ) {
				$redirected_to = admin_url( 'admin.php?page=ffl-dealers&action=edit&updated=true&id=' . $id );
			} else {
				$redirected_to = admin_url( 'admin.php?page=ffl-dealers&action=edit&inserted=true&id=' . $insert_id );
			}

			wp_redirect( $redirected_to );
		}
	}
	public function ffl_admin_menu_settings_fields(){
		 $settings_fields = array(
            'general' => array(
                array(
                    'label' => __('General settings', 'ffl-dealers'),
                    'type' 	=> 'title',
                    'id' 	=> 'section-heading',
                    'class' => 'section-heading'
                ),
               
                array(
                    'label'	=> __('Category', 'ffl-dealers'),
                    'type'	=> 'select',
                    'multiple' => true,
                    'id'	=> 'map_display_by_product_category',
                    'value'	=> get_option('map_display_by_product_category'),
                    'options'=> get_product_all_category(),
                    'desc'	=> __('Firearm dealer  list will be displayed based on the category','ffl-dealers'),
					'class'	=> 'regular-text'
                ),
				 array(
                    'label'	=> __('Search header title ', 'ffl-dealers'),
                    'type'	=> 'text',
                    'id'	=> 'search_heading_text',
                    'value'	=> esc_attr(get_option('search_heading_text')),
                    'class'	=> 'regular-text'
                ),
				array(
                    'label'	=> __('Firearm Warning Message:', 'ffl-dealers'),
                    'type'	=> 'textarea',
                    'id'	=> 'ffl_dealers_warning_message',
                    'value'	=> esc_textarea(get_option('ffl_dealers_warning_message', '')),
                    'class'	=> 'regular-text',
                    'row'	=> 5,
                    'desc'	=> __('You can edit this message that appears on the checkout page', 'ffl-dealers'),
                ),
				array(
                    'label' => __('Search Button text ', 'ffl-dealers'),
                    'type' 	=> 'text',
                    'id' 	=> 'search_button_text',
                    'value' => esc_attr(get_option('search_button_text', ''))
                ),
				array(
                    'label' => __('Show modal', 'ffl-dealers'),
                    'type' 	=> 'checkbox',
                    'id' 	=> 'locator_modal_enable',
                    'class' => 'regular-text',
                    'value' => 1,
                    'checked' => (1== esc_attr(get_option('locator_modal_enable')))?1:0,
                    'desc' 	=> __('to show locator map in modal', 'ffl-dealers'),
                ),
				array(
                    'label' => __('', 'ffl-dealers'),
                    'type' 	=> 'checkbox',
                    'id' 	=> 'locator_modal_hide',
                    'class' => 'regular-text',
                    'value' => 1,
                    'checked' => (1== esc_attr(get_option('locator_modal_hide')))?1:0,
                    'desc' 	=> __('Modal hide after selecting dealer ', 'ffl-dealers'),
					'wrapper_class' => 'enable-modal hidden-el',
                ),
				array(
                    'label' => __('Reopen Modal button text ', 'ffl-dealers'),
                    'type'	=> 'text',
                    'id'	=> 'reopen_modal_button_text',
                    'value'	=> esc_attr(get_option('reopen_modal_button_text', '')),
                    'wrapper_class' => 'enable-modal hidden-el',
                ),
				array(
                    'label' => __('Location in checkount', 'ffl-dealers'),
                    'type'	=> 'select',
                    'id'	=> 'ffl_search_map_location',
                    'value'	=> esc_attr(get_option('ffl_search_map_location', '')),
                    'class'	=> 'ffl_search_map_location',
                    'options' => array(
                        '' => __('--postion--', 'ffl-dealers'),
                        'woocommerce_checkout_order_review' => __('Order Review', 'ffl-dealers'),
                        'woocommerce_before_checkout_billing_form' => __('Above Billing Form', 'ffl-dealers'),
                        'woocommerce_after_checkout_billing_form' => __('Below Billing Form', 'ffl-dealers'),
                        'woocommerce_before_checkout_shipping_form' => __('Above Shipping Form', 'ffl-dealers'),
                        'woocommerce_after_checkout_shipping_form' => __('Below Shipping Form', 'ffl-dealers')
                    ),
                    'wrapper_class' => 'checkout-hooks hidden-el',
                    'desc' => 'Position to show dealer list in checkout',
                ),
				array(
                    'label' => __('Use shortcode', 'ffl-dealers'),
                    'type' 	=> 'checkbox',
                    'id' 	=> 'locator_shortcode_enable',
                    'class' => 'regular-text',
                    'value' => 1,
                    'checked' => (1== esc_attr(get_option('locator_shortcode_enable')))?1:0,
                    'desc' 	=> __('Use shortcode insted of above field "Location in checkount" ', 'ffl-dealers'),
                ),
				array(
                    'label' => __('Shortcode', 'ffl-dealers'),
                    'type' 	=> 'shortcode',
					'id'	=> 'shortcode', 
					'value'	=> '[dealers-locator]', 
                    'wrapper_class' => 'shortcodes hidden-el',
                    'desc'	=> 'you can use the shortcode to show dealer list in checkout',
                ),
                array(
                    'type'	=> 'button',
                    'id'	=> 'general-settings',
                    'button_text' => __('Save Settings', 'ffl-dealers'),
                    'class'	=> 'primary',
                )
            ),
			'blacklist_dealers'=>array(
				array(
                    'label' => __('Blacklist Dealer', 'ffl-dealers'),
                    'type' 	=> 'textarea',
					'value'	=> esc_attr(get_option('ffl_blacklist_dealers')),
					'id'	=> 'ffl_blacklist_dealers',
                    'desc'	=> 'Type dealer phone number  only. separate phone by (,). These dealers will be hidden from filtering deallers',
                ),
				array(
                    'label' => __('Blacklist Zipcodes', 'ffl-dealers'),
                    'type' 	=> 'textarea',
					'value'	=> esc_attr(get_option('ffl_blacklist_zipcodes')),
					'id'	=> 'ffl_blacklist_zipcodes',
                    'desc'	=> 'Zipcodes. separate phone by (,). These zipcodes will be hidden from filtering deallers',
                ),
				array(
                    'type'	=> 'button',
                    'id'	=> 'general-settings',
                    'button_text' => __('Save Settings', 'ffl-dealers'),
                    'class'	=> 'primary',
                )
            ),
			'default_dealers'=>array(
				array(
                    'label' => __('Default Dealers', 'ffl-dealers'),
                    'type' 	=> 'textarea',
					'value'	=> esc_attr(get_option('ffl_default_dealers')),
					'id'	=> 'ffl_default_dealers',
                    'desc'	=> 'Type dealer phone number  only. separate phone by (,). These dealers will be displayed on checkout at begining',
                ),
				/*array(
                    'label' => __('', 'ffl-dealers'),
                    'type' 	=> 'checkbox',
                    'id' 	=> 'table_data_use_as_default',
                    'class' => 'regular-text',
                    'value' => 1,
                    'checked' => (1== esc_attr(get_option('table_data_use_as_default')))?1:0,
                    'desc' 	=> __('Use your inserted data as a default data', 'ffl-dealers'),
                ),*/
				array(
                    'type'	=> 'button',
                    'id'	=> 'general-settings',
                    'button_text' => __('Save Settings', 'ffl-dealers'),
                    'class'	=> 'primary',
                )
            ),
            'map' => array(
                array(
                    'label'	=> __('Map Options', 'ffl-dealers'),
                    'type'	=> 'title',
                    'id'	=> 'section-heading',
                    'class'	=> 'section-heading'
                ),
				array(
                    'label'	=> __('Enable map', 'ffl-dealers'),
                    'type'	=> 'checkbox',
                    'id'	=> 'ffl_dealer_google_map_enable',
                    'value'	=> 1,
                    'checked'=> (1== esc_attr(get_option('ffl_dealer_google_map_enable')))?1:0,
					'class'	=> '',
					'desc'	=> __('Dealer list address will be displayed in google map', 'ffl-dealers'),					
				),
				array(
                    'label'	=> __('Google Maps API Key:', 'ffl-dealers'),
                    'type'	=> 'password',
                    'id'	=> 'google_map_api_key',
                    'value'	=> esc_attr(get_option('google_map_api_key', '')),
                    'class'	=> 'regular-text',
					'wrapper_class' => 'google-map-enable hidden-el',
                ),
                array(
                    'label' => __('Map Starting Location', 'ffl-dealers'),
                    'type'	=> 'text',
                    'id'	=> 'ffl_map_starting_location',
                    'value'	=> esc_attr(get_option('ffl_map_starting_location')),
                    'desc'	=> __('The location the map is initialy centered on when the page loads.', 'ffl-dealers'),
                    'class'	=> 'regular-text google-autocomplete-field',
					'wrapper_class' => 'google-map-enable hidden-el'
                ),
                array(
                    'label'	=> __('Map zoom', 'ffl-dealers'),
                    'type'	=> 'select',
                    'id'	=> 'ffl_map_zoom',
                    'value'	=> esc_attr(get_option('ffl_map_zoom')),
                    'options'=> array(
                        '1'=>1,
                        '2'=>2,
                        '3'=>3,
                        '4'=>4,
                        '5'=>5,
                        '6'=>6,
                        '7'=>7,
                        '8'=>8,
                        '9'=>9,
                        '10'=>10,
                        '11'=>11,
                        '12'=>12,
                        '13'=>13,
                        '14'=>14,
                        '15'=>15,
                        '16'=>16,
                        '17'=>17,
                        '18'=>18,
                        '19'=>19
                    ),
					'wrapper_class' => 'google-map-enable hidden-el'
                ),
                array(
                    'label'	=> __('Map Locator icon', 'ffl-dealers'),
                    'type'	=> 'media',
                    'id'	=> 'ffl_dealers_locator_icon',
                    'value'	=> esc_url(get_option('ffl_dealers_locator_icon', '')),
                    'class'	=> 'regular-text',
                    'desc'	=> __('it\'ll show in locator icon', 'ffl-dealers'),
                    'button_id' => 'upload_locatior_icon',
                    'button_text' => 'Upload',
					'wrapper_class' => 'google-map-enable hidden-el'
                ),
                array(
                    'type'	=> 'button',
                    'id'	=> 'general-settings',
                    'button_text' => __('Save Settings', 'ffl-dealers'),
                    'class'	=> 'primary',
                ),
            ),
			'style'=> array(
				array(
					'label'	=> __('Search header title color ', 'ffl-dealers'),
					'type' 	=> 'color',
					'id'	=> 'search_heading_text_color',
					'value'	=> esc_attr(get_option('search_heading_text_color')),
				),
				   
				array(
					'label'	=> __('Message color ', 'ffl-dealers'),
					'type'	=> 'color',
					'id'	=> 'message_text_color',
					'value'	=> esc_attr(get_option('message_text_color')),
				),
				array(
					'label'	=> __('Message background ', 'ffl-dealers'),
					'type'	=> 'color',
					'id'	=> 'message_background_color',
					'value' => esc_attr(get_option('message_background_color')),
				),
				array(
					'label'	=> __('Button text color ', 'ffl-dealers'),
					'type'	=> 'color',
					'id'	=> 'search_button_text_color',
					'value'	=> esc_attr(get_option('search_button_text_color')),
				),
				array(
					'label'	=> __('Button background ', 'ffl-dealers'),
					'type'	=> 'color',
					'id'	=> 'search_button_background',
					'value'	=> esc_attr(get_option('search_button_background')),
				),
				array(
					'label'	=> __('Select dealer button color ', 'ffl-dealers'),
					'type'	=> 'color',
					'id'	=> 'select_dealer_button_color',
					'value'	=> esc_attr(get_option('select_dealer_button_color')),
				),
				array(
					'label'	=> __('Select dealer button bg ', 'ffl-dealers'),
					'type'	=> 'color',
					'id'	=> 'select_dealer_button_bg',
					'value'	=> esc_attr(get_option('select_dealer_button_bg')),
				),
				
				array(
					'label'	=> __('Modal text color ', 'ffl-dealers'),
					'type'	=> 'color',
					'id'	=> 'modal_text_color',
					'value'	=> esc_attr(get_option('modal_text_color')),
					'wrapper_class' => 'enable-modal hidden-el',
				),
				array(
					'label'	=> __('Modal background color ', 'ffl-dealers'),
					'type'	=> 'color',
					'id'	=> 'modal_background_color',
					'value'	=> esc_attr(get_option('modal_background_color')),
					'wrapper_class' => 'enable-modal hidden-el',
				),
				array(
					'label'	=> __('Modal border color ', 'ffl-dealers'),
					'type'	=> 'color',
					'id'	=> 'modal_border_color',
					'value'	=> esc_attr(get_option('modal_border_color')),
					'wrapper_class' => 'enable-modal hidden-el',
				),
				
				array(
					'label'	=> __('Reopen Modal button color ', 'ffl-dealers'),
					'type'	=> 'color',
					'id'	=> 'reopen_modal_button_color',
					'value'	=> esc_attr(get_option('reopen_modal_button_color')),
					'wrapper_class' => 'enable-modal hidden-el',
				),
				array(
					'label'	=> __('Reopen Modal bg color ', 'ffl-dealers'),
					'type'	=> 'color',
					'id'	=> 'reopen_modal_button_bg',
					'value'	=> esc_attr(get_option('reopen_modal_button_bg')),
					'wrapper_class' => 'enable-modal hidden-el ',
				),
				array(
					'type'	=> 'button',
					'id'	=> 'general-settings',
					'button_text' => __('Save Settings', 'ffl-dealers'),
					'class'	=> 'primary',
				)
			),
			'pro_plugin'=> array(
				array(
                    'label' => __('Pro settings', 'ffl-dealers'),
                    'type' 	=> 'title',
                    'id' 	=> 'section-heading',
                    'class' => 'section-heading',
					'desc'	=> __('Upgrade to the premium version of the FLL dealer plugin to unlock a seamless integration for updated dealer database. With the premium version, clients gain access to the latest FLL dealer information  accroding to ATF in every month, ensuring accurate and up-to-date data at your fingertips. Enjoy all the features and functionalities of the free version but to get updated dealer database, purchase your licence key!. Learn more <a href="https://fraxzon.com/product/ffl-dealers-pro/" target="_blank">Visit</a>','')
                ),
				array(
                    'label'		=> __('Purchase Key', 'ffl-dealers'),
                    'type'		=> 'password',
                    'id'		=> 'ffl_dealer_pro_licence_key',
					'value'		=> esc_attr(get_option('ffl_dealer_pro_licence_key')),
                    'desc'		=> __('Insert your  purchased liecence key if you\'ve ','ffl-dealers'),
					'class'		=> 'regular-text'
                ),
				array(
					'type'	=> 'button',
					'id'	=> 'general-settings',
					'button_text' => __('Save Settings', 'ffl-dealers'),
					'class'	=> 'primary',
				)
			),
			'support'=> array(
				array(
                    'label' => __('Support', 'ffl-dealers'),
                    'type' 	=> 'support',
                    'id' 	=> 'section-heading',
                    'class' => 'section-heading',
                ),
			)
			
            
        );
        return $settings_fields;
	}
	public function save_ffl_admin_menu_settings_fields(){
		global $ffl_dealers_notices;
		$active_tab = isset($_GET['tab']) ? sanitize_key($_GET['tab']) : 'general';
        if (isset($_POST['setting_submit_fields']) && wp_verify_nonce($_POST['setting_submit_fields'], 'setting_submit_nonce')) {
            $settings_fields = apply_filters('ffl_menu_settings_fields', array());
            foreach ($settings_fields as $key => $fields) {
                if ($key === $active_tab) {
                    foreach ($fields as $field) {
                        $field_name = $field['id'];
                        if (isset($_POST[$field_name])) {
                            if (is_array($_POST[$field_name])) {
                                $posted_value = map_deep($_POST[$field_name], 'sanitize_text_field');
                                update_option($field_name, $posted_value);
                            } else {
                                if($field['type'] =='wysiwyg'){
									$posted_value = wp_unslash($_POST[$field_name]);
									update_option($field_name, $posted_value);
								}else{
									$posted_value = sanitize_text_field(wp_unslash($_POST[$field_name]));
									update_option($field_name, $posted_value);
								}
							}
							
							$ffl_dealers_notices = true;
                        } else {
                            delete_option($field_name);
                        }
                    }
                }
            }
			
            do_action('save_settings_ffl_fields', $_POST);
        }
	}
	public function plugin_using_policy_notice() {
		$message = '';
		if($message){
			printf(__('<div class="plugin-perfection-message">%s </div>', 'ffl-dealers'), $message);
		}
    }
	public function ffl_dealers_installation_required(){
		 if (isset($_POST['activation_ffl_field']) && wp_verify_nonce($_POST['activation_ffl_field'], 'activation_ffl_nonce')) {
			if(isset($_POST['name']) && isset($_POST['email'])){
				$name = sanitize_text_field(wp_unslash( $_POST['name']));
				$email = sanitize_email(wp_unslash( $_POST['email']));
				$args = array('name'=>$name,'email'=>$email);
				remote_resource_callback($args);
			} 
		 }
	}
	public function add_map_starting_location($post){
		if(isset($post['ffl_map_starting_location']) && !empty($post['ffl_map_starting_location'])){
			$starting_address = sanitize_text_field($post['ffl_map_starting_location']);
			$lat_lng = longitude_latitude_by_street_address($starting_address);
			update_option('location_longitude_latitude',$lat_lng);
		}
	}
	public function enable_shortcode_or_modal($post){
		if((isset($post['locator_modal_enable']) && !empty($post['locator_modal_enable'])) || (isset($post['locator_shortcode_enable']) && !empty($post['locator_shortcode_enable']))){
			delete_option('ffl_search_map_location');
		}
	}
}