<?php
if (!defined('ABSPATH'))
   exit;
?>
<div class="imh-6310">
   <h1>Plugin Settings</h1>
   <?php

   wp_enqueue_media();


   $font_awesome = imh_6310_get_option('imh_6310_font_awesome_status');
   $closeIcon = imh_6310_get_option('imh_6310_close_icon');
   $desktopSize = imh_6310_get_option('imh_6310_desktop_size');
   $mobileSize = imh_6310_get_option('imh_6310_mobile_size');

   $closeIcon = $closeIcon ? $closeIcon : imh_6310_plugin_dir_url . 'assets/images/close.png';
   $desktopSize = $desktopSize ? $desktopSize : 30;
   $mobileSize = $mobileSize ? $mobileSize : 20;

   if (!empty($_POST['update']) && $_POST['update'] == 'Update') {
      $nonce = $_REQUEST['_wpnonce'];
      if (!wp_verify_nonce($nonce, 'imh-6310-nonce-update')) {
         die('You do not have sufficient permissions to access this page.');
      } else {
         //fontawesome Font Start
         if ($font_awesome != '') {
            $wpdb->query("UPDATE {$wpdb->prefix}options set 
        option_value='" . $_POST['font_awesome'] . "' 
        where option_name = 'imh_6310_font_awesome_status'");
         } else {
            $wpdb->query("DELETE FROM {$wpdb->prefix}options where option_name='imh_6310_font_awesome_status'");
            $wpdb->query("INSERT INTO {$wpdb->prefix}options(option_name, option_value) VALUES ('imh_6310_font_awesome_status', '" . $_POST['font_awesome'] . "')");
         }
         $font_awesome = $_POST['font_awesome'];

         //Next image start
         $imh_6310_close_icon = imh_6310_get_option('imh_6310_close_icon');
         if (!$imh_6310_close_icon) {
            $wpdb->query("DELETE FROM {$wpdb->prefix}options where option_name='imh_6310_close_icon'");
            $wpdb->query("INSERT INTO {$wpdb->prefix}options(option_name, option_value) VALUES ('imh_6310_close_icon', '" . $_POST['imh_6310_close_icon'] . "')");
         } else {
            $wpdb->query("UPDATE {$wpdb->prefix}options set 
                      option_value='" . $_POST['imh_6310_close_icon'] . "' 
                      where option_name = 'imh_6310_close_icon'");
         }
         $closeIcon =  $_POST['imh_6310_close_icon'];


         //Desktop Size
         $imh_6310_desktop_size = imh_6310_get_option('imh_6310_desktop_size');
         if (!$imh_6310_desktop_size) {
            $wpdb->query("DELETE FROM {$wpdb->prefix}options where option_name='imh_6310_desktop_size'");
            $wpdb->query("INSERT INTO {$wpdb->prefix}options(option_name, option_value) VALUES ('imh_6310_desktop_size', '" . $_POST['imh_6310_desktop_size'] . "')");
         } else {
            $wpdb->query("UPDATE {$wpdb->prefix}options set 
                      option_value='" . $_POST['imh_6310_desktop_size'] . "' 
                      where option_name = 'imh_6310_desktop_size'");
         }
         $desktopSize =  $_POST['imh_6310_desktop_size'];

         //Mobile size
         $imh_6310_mobile_size = imh_6310_get_option('imh_6310_mobile_size');
         if (!$imh_6310_mobile_size) {
            $wpdb->query("DELETE FROM {$wpdb->prefix}options where option_name='imh_6310_mobile_size'");
            $wpdb->query("INSERT INTO {$wpdb->prefix}options(option_name, option_value) VALUES ('imh_6310_mobile_size', '" . $_POST['imh_6310_mobile_size'] . "')");
         } else {
            $wpdb->query("UPDATE {$wpdb->prefix}options set 
                      option_value='" . $_POST['imh_6310_mobile_size'] . "' 
                      where option_name = 'imh_6310_mobile_size'");
         }
         $mobileSize =  $_POST['imh_6310_mobile_size'];
      }
   }
   ?>
   <form action="" method="post">
      <?php wp_nonce_field("imh-6310-nonce-update") ?>
      <div class="imh-6310-modal-body-form">
         <table width="100%" cellpadding="10" cellspacing="0">
            <tr>
               <td width="200px">
                  <b>Font Awesome Activation:</b><br />
               </td>
               <td width="500px" colspan="2">
                  <input type="radio" name="font_awesome" value="2" checked> Active &nbsp;&nbsp;&nbsp;
                  <input type="radio" name="font_awesome" value="1" <?php echo ($font_awesome == 1) ? ' checked' : '' ?>> Inactive
               </td>
            </tr>
            <tr>
               <td width="200px"><b>Change Close Icon</b></td>
               <td width="500px">
                  <input type="text" required name="imh_6310_close_icon" id="close-icon-src" value="<?php echo $closeIcon ?>" class="imh-form-input lg">
                  <input type="button" id="close-icon" value="Change Image" class="imh-6310-btn-success">
               </td>
               <td>
                  <img src="<?php echo $closeIcon ?>" width="40" />
               </td>
            </tr>
            <tr>
               <td width="200px"><b>Close Icon Size in Desktop</b></td>
               <td width="500px">
                  <input type="number" required min="10" name="imh_6310_desktop_size" id="close-icon-src" value="<?php echo $desktopSize ?>" class="imh-form-input lg">
               </td>
            </tr>
            <tr>
               <td width="200px"><b>Close Icon Size in Mobile</b></td>
               <td width="500px">
                  <input type="number" required min="10" name="imh_6310_mobile_size" id="close-icon-src" value="<?php echo $mobileSize ?>" class="imh-form-input lg">
               </td>
            </tr>
            <tr>
               <td colspan="3">
                  <input type="submit" name="update" class="imh-6310-btn-primary imh-margin-right-10" value="Update" />
               </td>
            </tr>
         </table>
      </div>
      <br class="imh-6310-clear" />
   </form>

   <script type="text/javascript">
      jQuery(document).ready(function() {
         jQuery("body").on("click", "#close-icon", function(e) {
            e.preventDefault();
            var image = wp
               .media({
                  title: "Upload Image",
                  multiple: false,
               })
               .open()
               .on("select", function(e) {
                  var uploaded_image = image.state().get("selection").first();
                  var image_url = uploaded_image.toJSON().url;
                  jQuery("#close-icon-src").val(image_url);
               });

            jQuery("#imh_6310_add_new_media").css({
               "overflow-x": "hidden",
               "overflow-y": "auto",
            });
         });
      })
   </script>