<?php
require_once('config.php');

if (isset($_POST['add_story'])) {
  $post_form_data = array(
    "story_title"      => htmlentities($_POST['story_title'], ENT_QUOTES, 'utf-8'),
    "story_items"      => json_encode($_POST['story_items']),
    "story_cat"      => $_POST['story_cat'],
    "story_type"       => "photostories",
    "story_url"        => randomString(),
    "story_date"       => $the_time,
    "story_status"     => htmlentities($_POST['story_status'], ENT_QUOTES, 'utf-8'),
    "story_admin"      => getSingleValue('admin_user', 'admin_security_key', $_COOKIE['Google_SLKA'] , 'admin_id'),
  );

   // Insert Database
   $insert_now = InsertData('mahamudfutureit_photostories', $post_form_data);
      if ($insert_now) {
          header("Location:photo-stories-edit.php?story_id=".$insert_now);
          exit;
       }else {
         $tt_alert = ttAlert('danger', 'Something went wrong to add Story !');
       }
    }

require_once('inc/header.php');
?>
<main id="main" class="main">
  <div class="row">
    <div class="col-6 align-items-center">
      <div class="pagetitle mb-2">
        <h1><?php echo $page_title ?></h1>
      </div>
    </div>
    <div class="col-6 text-end">
        <a href="photo-stories.php" class="btn btn-primary btn-sm"><i class="bi bi-grid"></i> All Stories </a>
    </div>
  </div>

  <?php
  echo $tt_alert ?? '';
  ?>
  <section class="section mt-4">
    <form class="row" action="" method="post" enctype="multipart/form-data">
      <div class="col-md-8">
        <div class="bg-white rounded-2 p-3 mb-3">

            <!-- ***********************
            Story Title
            *************************-->
            <div class="form-group mb-2">
              <label>Story Title <span class="text-danger">*</span> </label>
              <input name="story_title" type="text" class="form-control" placeholder="Story Title" required>
            </div>

            <div class="row g-2" id="storyItems">

              <?php
                for ($i = 1; $i <= 9; $i++) {
                    echo "<div class='col-md-6 col-lg-4 story-single'>
                            <div class='border rounded-2 p-2'>
                              <div class='position-relative'>
                                <textarea class='story-caption' name='story_items[$i][storycaption]' placeholder='Story Caption'></textarea>
                                <img class='img-fluid w-100 bg-white' id='psItem{$i}_Preview'
                                     src='assets/img/placeholder/story-placeholder.jpg'
                                     style='aspect-ratio: 920 / 1560; object-fit: cover; width: 100%; height: auto; max-height: 1560px;'>
                              </div>
                              <div class='m-0 input-group'>
                                <input id='psItem{$i}' name='story_items[$i][storyimage]' type='url' class='form-control rounded-0 border-success' value='' placeholder='Select file'>
                                <a href='#' class='addMediaButton input-group-text border-success bg-success text-white rounded-0' data-media_type='' data-bs-toggle='modal' data-bs-target='#SelectMedia' data-target='#psItem{$i}'>
                                  <i class='bi bi-upload'></i>
                                </a>
                              </div>
                            </div>
                          </div>";
                }
              ?>
            </div>
        </div>
      </div>
      <div class="col-md-4">
        <div class="bg-white rounded-2 p-3">
          <div class="form-group mb-2">
            <label>Story Category <span class="text-danger">*</span> </label>
            <select class="form-control tt-select-single" name="story_cat" required>
              <option value="">Select Category</option>
              <?php
              $data_p_cat = showAllttdata("mahamudfutureit_category" , "WHERE CategorySataus ='Published' and CategoryParent IS NULL OR CategoryParent = '' ORDER BY  CategoryID  DESC ");
              if (!empty($data_p_cat)) {
                foreach ($data_p_cat as $parent_category) {
                  ?>
                  <option value="<?php echo $parent_category['CategoryID'] ?>">
                    <?php echo $parent_category['CategoryName'] ?>
                  </option>
                  <?php
                  $parent = $parent_category['CategoryID'];
                  $data_p_cat = showAllttdata("mahamudfutureit_category", "WHERE CategorySataus ='Published' and  CategoryParent = '$parent' ORDER BY  CategoryID  DESC ");
                  if (!empty($data_p_cat)) {
                    foreach ($data_p_cat as $tt_sub_category) {
                      ?>
                      <option value="<?php echo $tt_sub_category['CategoryID'] ?>">
                        --<?php echo $tt_sub_category['CategoryName'] ?>
                      </option>
                      <?php
                    }
                  }
                }
              }
              ?>
            </select>
          </div>
          <div class="form-group">
            <label class="form-label">Published Date <span class="text-danger">*</span></label>
            <input type="datetime-local" step="1"  class="form-control" value="<?php echo date('Y-m-d\TH:i') ?>" name="story_date" required>
          </div>
          <div class="form-group mt-3" >
            <label class="form-label">Publish Status</label>
            <select name="story_status" class="form-select form-control">
              <option value="Published">Published</option>
              <option value="Draft">Draft</option>
            </select>
          </div>
          <button type="submit" class="mt-3 btn btn-success w-100" name="add_story" ><i class="bi bi-floppy pe-2"></i> Add New Story</button>
        </div>
      </div>
    </form>
  </section>
</main>
<script>
$(document).ready(function() {
  $("#imageInput").change(function() {
    const preview = $("#previewImage")[0];
    const fileInput = $(this)[0];

    if (fileInput.files && fileInput.files[0]) {
      const reader = new FileReader();
      reader.onload = function(e) {
        preview.src = e.target.result;
      }
      reader.readAsDataURL(fileInput.files[0]);
    } else {
      preview.src = 'assets/img/featured_image_placeholder.webp';
    }
  });
});
</script>
<?php require_once('inc/footer.php'); ?>
