<?php

/**
 * Settings class for Gogglesheet Role settings
 * @since 1.0.0
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
   exit;
}

/**
 * GSHFJF_JetForm_Role_Settings Class
 * @since 1.0.0
 */

class GSHFJF_JetForm_Role_Settings
{

   /**
    * @var string group name
    */

   protected $gsc_group_name = 'gshfjf-jetform-settings';

   /**
    * @var string roles that can access Google Sheet page
    *@since 1.0.0
    */

   protected $gshfjf_jetform_page_roles_setting_option_name = 'gshfjf_jetform_tab_roles_setting';

   /**
    * Set things up.
    * @since 1.0.0
    */

   public function __construct()
   {
      add_action('admin_init', array($this, 'init_settings'));
   }

   /**
    * Register settings for JetForm Google Sheets access roles with validation.
    */

   public function init_settings()
   {
      register_setting('gshfjf-jetform-settings', $this->gshfjf_jetform_page_roles_setting_option_name, array($this, 'gshfjf_validate_jetform_access_roles'));
   }

   /**
    * do validate and sanitize selected participants
    * @param array $selected_roles
    * @return array $roles
    * @since 1.0.0
    */

   public function gshfjf_validate_jetform_access_roles($selected_roles)
   {
      $roles = array();
      $system_roles = GSHFJF_JetForm_Connector_Utility::instance()->get_system_roles();


      if ($selected_roles && count($selected_roles) > 0) {

         foreach ($system_roles as $role => $display_name) {
            if (is_array($selected_roles) && in_array(esc_attr($role), $selected_roles)) {
               // preselect specified role
               $roles[$role] = $display_name;
            }
         }
      }
      return $roles;
   }

   /**
    * Display the Roles Settings page for managing which user roles
    * have access to the Google Sheet integration page.
    * Only accessible by administrators.
    */

   public function add_role_setting_page()
   {
      if (!current_user_can('administrator')) {
?>
         <span class="per_not_allo"><?php echo esc_html__("Permission Not Allowed", 'gsheetconnector-for-jetform'); ?></span>
      <?php
         return;
      }
      $gshfjf_jetform_page_roles = get_option($this->gshfjf_jetform_page_roles_setting_option_name);

      ?>
      <form id="gsc_jetform_settings_form" method="post" action="options.php">
         <?php
         // adds nonce and option_page fields for the settings page
         settings_fields('gshfjf-jetform-settings');
         settings_errors();
         ?>

         <!--Start Pro Setting(Roll Permissions)-->
         <div class="gsc-pro-promo ml-15 mr-pro-15">

            <div class="gsc-pro-header">
               <div class="gsc-pro-icon">
                  <svg width="40" height="40" viewBox="0 0 24 24" fill="none" stroke="#28a745" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                     <path d="M5 19c-1 1-2 1-3 1 0-1 0-2 1-3l4-4"></path>
                     <path d="M14 3l7 7"></path>
                     <path d="M9 18l-4 4"></path>
                     <path d="M15 3c2 0 6 4 6 6-2 2-6 6-8 8l-6-6c2-2 6-8 8-8z"></path>
                     <circle cx="15" cy="9" r="1.5"></circle>
                  </svg>

               </div>

               <div>
                  <div class="unlock-header"><?php echo esc_html(__('Unlock Role-Based Access Control', 'gsheetconnector-for-jetform')); ?></div>
                  <span class="gsc-pro-badge"><?php echo esc_html(__('FREE users get special upgrade pricing', 'gsheetconnector-for-jetform')); ?></span>
               </div>
            </div>

            <!-- Feature Tabs -->
            <div class="gsc-pro-tabs pt-20 pb-20 pl-20 pr-20">
               <div>
                  <div class="mb-20 fw-600 text-dark pro-roll-sub-header"><?php echo esc_html(__('Role Permissions', 'gsheetconnector-for-jetform')); ?></div>
                  <div class="gsc-pro-grid">
                     <ul>
                        <li><?php esc_html_e('Allow specific WordPress roles', 'gsheetconnector-for-jetform'); ?></li>
                        <li><?php esc_html_e('Enable/disable integration access', 'gsheetconnector-for-jetform'); ?></li>
                        <li><?php esc_html_e('Control form feed visibility', 'gsheetconnector-for-jetform'); ?></li>
                        <li><?php esc_html_e('Restrict settings management', 'gsheetconnector-for-jetform'); ?></li>
                     </ul>
                  </div>
               </div>

               <div>
                  <div class="mb-20 fw-600 text-dark pro-roll-sub-header"><?php echo esc_html(__('Security Control', 'gsheetconnector-for-jetform')); ?></div>
                  <div class="gsc-pro-grid">
                     <ul>
                        <li><?php esc_html_e('Prevent unauthorized changes', 'gsheetconnector-for-jetform'); ?></li>
                        <li><?php esc_html_e('Secure Google Sheet credentials', 'gsheetconnector-for-jetform'); ?></li>
                        <li><?php esc_html_e('Role-based configuration control', 'gsheetconnector-for-jetform'); ?></li>
                        <li><?php esc_html_e('Protect integration settings', 'gsheetconnector-for-jetform'); ?></li>
                     </ul>
                  </div>
               </div>

               <div>
                  <div class="mb-20 fw-600 text-dark pro-roll-sub-header"><?php echo esc_html(__('Management Benefits', 'gsheetconnector-for-jetform')); ?></div>
                  <div class="gsc-pro-grid">
                     <ul>
                        <li><?php esc_html_e('Grant access to trusted editors', 'gsheetconnector-for-jetform'); ?></li>
                        <li><?php esc_html_e('Hide settings from subscribers', 'gsheetconnector-for-jetform'); ?></li>
                        <li><?php esc_html_e('Team-based permission structure', 'gsheetconnector-for-jetform'); ?></li>
                        <li><?php esc_html_e('Improved dashboard security', 'gsheetconnector-for-jetform'); ?></li>
                     </ul>
                  </div>
               </div>

               <div>
                  <div class="mb-20 fw-600 text-dark pro-roll-sub-header"><?php echo esc_html(__('Audit & Monitoring', 'gsheetconnector-for-jetform')); ?></div>
                  <div class="gsc-pro-grid">
                     <ul>
                        <li><?php esc_html_e('Track role-based changes', 'gsheetconnector-for-jetform'); ?></li>
                        <li><?php esc_html_e('Monitor integration access', 'gsheetconnector-for-jetform'); ?></li>
                        <li><?php esc_html_e('Review permission updates', 'gsheetconnector-for-jetform'); ?></li>
                        <li><?php esc_html_e('Maintain admin accountability', 'gsheetconnector-for-jetform'); ?></li>
                     </ul>
                  </div>
               </div>
            </div>

            <!-- CTA -->
            <div class="gsc-pro-footer text-center">
               <a href="#"
                  target="_blank"
                  class="btn btn-primary text-decoration-none link-hover-white">
                  <?php echo esc_html(__('Upgrade to Unlock', 'gsheetconnector-for-jetform')); ?>
               </a>
            </div>

         </div>
         <!--End Pro Setting(Roll Permissions)-->

         <div class="gshfjf-role-settings" id="gsc-googlesheet">
            <div class="wrap w-100 m-0">
               <div class="inner-wrap w-100 bg-white p-40 blur-pro-feature">
                  <div class="heading mt-0"><?php echo esc_html__('Access Control', 'gsheetconnector-for-jetform'); ?></div>
                  <p><?php echo esc_html__('Control which user roles are allowed to access and manage the Google Sheets integration for your forms.', 'gsheetconnector-for-jetform'); ?></p>
                  <div class="gshfjf-jetform-card">
                     <?php
                     GSHFJF_JetForm_Connector_Utility::instance()->gshfjf_jetform_checkbox_roles_multi(
                        $this->gshfjf_jetform_page_roles_setting_option_name,
                        $gshfjf_jetform_page_roles
                     );
                     ?>
                  </div>
                  <div class="gsc-access-info">
                     <div class="para-heading fw-600 mb-20"><?php echo esc_html__('Permission Guidelines', 'gsheetconnector-for-jetform'); ?></div>
                     <ul>
                        <li><?php echo esc_html__('Grant access only to users you trust', 'gsheetconnector-for-jetform'); ?>Grant access only to users you trust</li>
                        <li><?php echo esc_html__('Editors can manage and modify integrations', 'gsheetconnector-for-jetform'); ?></li>
                        <li><?php echo esc_html__('Review role permissions on a regular basis', 'gsheetconnector-for-jetform'); ?></li>
                        <li><?php echo esc_html__('Do not allow access to subscribers', 'gsheetconnector-for-jetform'); ?></li>
                     </ul>
                  </div>
               </div>
               <div class="">
                  <div class="select-info text-right mt-30">
                     <input type="submit" class="btn btn-primary button-large" name="gshfjf_jetform_settings"
                        value="<?php echo esc_html__("Save Settings", 'gsheetconnector-for-jetform'); ?>" />
                  </div>
               </div>
            </div>
         </div>
      </form>
      </div>
      </div>
<?php
   }
}

$GSHFJF_JetForm_Role_Settings = new GSHFJF_JetForm_Role_Settings();
