<?php
      $errors = $this->session->userdata('errors');
      if (isset($errors)) {
          ?>
   <script>
      swal ( "Oops" ,  "<?php echo $errors ?>" ,  "error" )
   </script>
   <?php echo $errors ?>
   <?php
      $this->session->unset_userdata('errors');
      }
      $error = $this->session->userdata('error');
      if (isset($error)) {
      ?>
   <script>
      swal ( "Oops" ,  "<?php echo $error ?>" ,  "error" )
   </script>
   <?php
      $this->session->unset_userdata('error');
      }
      $success = $this->session->userdata('success');
      if (isset($success)) {
      ?>
   <script>
      swal({
      title: "Done!",
      text: "<?php echo $success ?>",
      icon: "success",
      });
   </script>
   <?php
      $this->session->unset_userdata('success');
      }
      ?>
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>E-Book</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="<?php echo admin_url() ?>admin">Home</a></li>
              <li class="breadcrumb-item active">E-Book</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <!-- SELECT2 EXAMPLE -->
        <div class="card card-default">
          <div class="card-header">
            <h3 class="card-title">Add New Ebook</h3>
            <div class="card-tools">
              <a href="<?php echo admin_url() ?>ebook/view_all_ebook"><button type="button" class="btn btn-success btn-sm">View All Event</button></a>
            </div>
          </div>
          <!-- /.card-header -->
          <div class="card-body">
            <div class="row">
             <div class="col-md-12 py-3">
               <?php
                  // Add page properties
                  if (isset($_POST['submit'])) 
                  {
                    if (!empty($_FILES['image']['name'])) 
                    {
                        $errors = array();
                        $file_name = $_FILES['image']['name'];
                        $file_size = $_FILES['image']['size'];
                        $file_tmp = $_FILES['image']['tmp_name'];
                        $file_type = $_FILES['image']['type'];
                        $file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
                        // $file_ext = strtolower(end(explode('.', $_FILES['image']['name'])));

                        $extensions = array(
                            "jpeg",
                            "jpg",
                            "png",
                            "gif"
                        );

                        if (in_array($file_ext, $extensions)) {
                            if (file_exists("uploads/editor/" . $file_name)) {
                                $errors[] = $file_name . " is already exists.";
                            } else {
                                move_uploaded_file($file_tmp, "uploads/editor/" . $file_name);
                                echo '<div class="alert alert-success" role="alert">';
                                echo "Your file was uploaded successfully.";
                                echo '</div>';
                            }
                        } else {
                            $errors[] = "Extension not allowed, please choose a JPEG, JPG, PNG or GIF file. <br/>Or you have not selected a file";
                        }

                        if ($file_size > 2097152) {
                            $errors[] = 'File size must be excately 2 MB';
                        }

                        if (empty($errors) === true) {
                            echo '<div class="alert alert-success" role="alert">';
                            echo "Success";
                            echo '</div>';
                        } else {
                            foreach ($errors as $key => $item) {
                                echo '<div class="alert alert-danger" role="alert">';
                                echo "$item <br>";
                                echo '</div>';
                            }
                        }
                    } 
                    else 
                    {
                        echo '<div class="alert alert-danger" role="alert">';
                        echo "It is necessary to add an image that relates the page";
                        echo '</div>';
                    }

                    $title = $_POST['title']; // Page name
                    $link = strtolower(str_replace(" ", "-", $_POST['link'])); // Page link
                    $keyword = $_POST['keyword'];
                    $description = $_POST['description'];
                    $author   = $_POST['author'];
                    $event_mode = $_POST['event_mode'];
                    $main_price = $_POST['main_price'];
                    $discount_price = $_POST['discount_price'];
                    $short_description = $_POST['short_description'];
                    $create_mode = $_POST['create_mode'];
                    $schema = $_POST['schema'];
                    // Insert info in table PAGE 
                    $data = array(
                      'title' => $title,
                      'link' => $link, 
                      'keyword' => $keyword, 
                      'description' => $description, 
                      'image' => $file_name,
                      'author' => $author, 
                      'event_mode' => $event_mode, 
                      'main_price' => $main_price, 
                      'discount_price' => $discount_price, 
                      'short_description' => $short_description,
                      'create_mode' => $create_mode,
                      'schema' => $schema
                    );
                    $insert = $this->db->insert('ebook', $data);
                    $last_id = $this->db->insert_id();
                    if ($insert === TRUE) 
                    {
                      $data1 = array(
                        'page_id' => $last_id,
                        'title_page' => $title, 
                        'link_page' => $link, 
                      );
                      $insert1 = $this->db->insert('menu_ebook', $data1);
                        if ($insert1 === TRUE) 
                        {
                           echo '
                           <div class="alert alert-success" role="alert">';
                              echo "Page " . $title . " : Created ";
                              echo '
                           </div>
                           ';
                        } 
                        else 
                        {
                           echo '
                           <div class="alert alert-danger" role="alert">';
                              echo "Failed: The page was not added to the menu";
                              echo '
                           </div>
                           ';
                        }
                    } 
                    else 
                    {
                      echo '
                      <div class="alert alert-danger" role="alert">';
                        echo "Failed: The page has not been created";
                        echo '
                      </div>';
                    }
                    if($create_mode == 1)
                    {
                        echo '
                        <meta http-equiv="refresh" content="3; url='.base_url().'admin/editor/view_editor_for_ebook?id=' . $last_id . '" />';
                    }
                    else
                    {
                        echo '
                        <meta http-equiv="refresh" content="3; url='.base_url().'admin/ebook/build_new_page/' . $last_id . '" />';
                    }
                  }
               echo '
               <form method="post" enctype="multipart/form-data">
                  ' . "\n";
                  echo '
                  <div class="row">
                     <div class="col-md-6">
                        ' . "\n";
                        echo '
                        <div class="form-group">
                           <label for="title">Title <span class="required">*</span></label>
                           <input type="text" class="form-control" id="title" name="title" placeholder="Enter E-Book Title" reuqired>
                        </div>
                        ' . "\n";
                        echo '
                     </div>
                     <div class="col-md-6">
                        ' . "\n";
                        echo '
                        <div class="form-group">
                           <label for="link">Slug <span class="required">*</span></label>
                           <input type="text" class="form-control" id="link" name="link" placeholder="Enter E-Book Slug" required>
                        </div>
                        ' . "\n";
                        echo '
                     </div>
                  </div>
                  <div class="form-group">
                     <label for="keyword">Keyword</label>
                     <textarea type="text" class="form-control" id="keyword" name="keyword" placeholder="Enter E-Book Keywords"></textarea>
                  </div>
                  ' . "\n";
                 
                  echo '
                  <div class="form-group">
                     <label for="description">E-Book Meta Description</label>
                     <textarea class="form-control" id="description" name="description" placeholder="Enter E-Book Meta Description"></textarea>
                  </div>
                  ' . "\n";
                  echo '
                  <div class="form-group">
                     <label for="description">E-Book Short Description <span class="required">*</span></label>
                     <textarea class="form-control" name="short_description" placeholder="Enter E-Book Short Description" required></textarea>
                  </div>
                  <div class="form-group">
                     <label for="description">E-Book Schema</label>
                     <textarea class="form-control" name="schema" placeholder="Enter E-Book Schema" required></textarea>
                  </div>
                  ' . "\n";
                   echo '
                  <div class="row">
                     <div class="col-md-6">
                        ' . "\n";
                        echo '
                        <div class="form-group">
                           <label for="title">Front Image <span class="required">*</span></label>
                           <input type="file" class="form-control" name="image" reuqired>
                        </div>
                        ' . "\n";
                        echo '
                     </div>
                     <div class="col-md-6">
                        ' . "\n";
                        echo '
                        <div class="form-group">
                           <label for="link">E-Book Author<span class="required">*</span></label>
                           <input type="text" class="form-control" name="author"  required placeholder="Enter E-Book Author">
                        </div>
                        ' . "\n";
                        echo '
                     </div>
                  </div>' . "\n";
                  echo '
                  <div class="form-group">
                     <label for="description">E-Book Mode</label>
                     <select class="form-control" id="event_mode" name="event_mode" reuqired>
                       <option>Select Mode</option>
                       <option value="1">Free E-Book</option>
                       <option value="2">Paid E-Book</option>
                     </select>
                  </div>
                  ' . "\n";
                   echo '

                  <div class="row" >
                     <div class="col-md-4 event_price">
                        ' . "\n";
                        echo '
                        <div class="form-group">
                           <label for="title">Enter Main Price<span class="required">*</span></label>
                           <input type="number" class="form-control" name="main_price" id="main_price" placeholder="Enter E-Book Main Price">
                        </div>
                        ' . "\n";
                        echo '
                     </div>
                     <div class="col-md-4 event_price">
                        ' . "\n";
                        echo '
                        <div class="form-group">
                           <label for="title">Enter Discount Price <span class="required">*</span></label>
                           <input type="number" class="form-control" name="discount_price" id="discount_price" placeholder="Enter E-Book Discount Price" reuqired value="0">
                        </div>
                        ' . "\n";
                        echo '
                     </div>
                     <div class="col-md-4">
                        ' . "\n";
                        echo '
                        <div class="form-group">
                           <label for="title">Page Create Mode <span class="required">*</span></label>
                           <select class="form-control" name="create_mode"  placeholder="Enter E-Book Discount Price" reuqired>
                                <option value="1">Page Builder</option>
                                <option value="2">Landing page</option>
                           </select>
                        </div>
                        ' . "\n";
                        echo '
                     </div>
                  </div>' . "\n";
                  echo '<input type="submit" name="submit" class="btn btn-primary" value="Save">' . "\n";
                  echo '
               </form>
               ' . "\n";
               ?>
            </div>
          </div>
        </div>
      </div>
    </section>
    <!-- /.content -->
  </div>
  <style type="text/css">
  .event_price
  {
    display: none;
  }
  </style>
  <script>
      $(document).ready(function(){
      $('#event_mode').on('change', function() {
        if ( this.value == '2')
        //.....................^.......
        {
          $(".event_price").show();
          document.getElementById("main_price").required = true;
          document.getElementById("discount_price").required = true;
        }
        else
        {
          $(".event_price").hide();
          document.getElementById("main_price").required = false;
          document.getElementById("discount_price").required = false;
        }
      });
  });
  $(function () {
     $("#title").keyup(function () {
          var Text = $(this).val();
          Text = Text.toLowerCase();
          Text = Text.replace(/[^a-zA-Z0-9]+/g,'-');
          $("#link").val(Text);
     }).keyup();
  });
</script>