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

if (isset($_POST['add_story'])) {
    $post_form_data = array(
        "gallery_title"               => htmlentities($_POST['gallery_title'], ENT_QUOTES, 'utf-8'),
        "gallery_items"               => json_encode($_POST['gallery_items'],JSON_UNESCAPED_UNICODE),
        "gallery_author"              => $_POST['Author'],
        "gallery_author_name"         => $_POST['WriterName'],
        "gallery_author_designation"  => $_POST['WriterDesignation'],
        "gallery_url"                 => randomString(),
        "gallery_view"                 => '0',
        "gallery_date"                => $the_time,
        "gallery_status"              => htmlentities($_POST['gallery_status'], ENT_QUOTES, 'utf-8'),
        "gallery_admin"               => getSingleValue('admin_user', 'admin_security_key', $_COOKIE['Google_SLKA'] , 'admin_id'),
    );

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

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-gallery.php" class="btn btn-primary btn-sm"><i class="bi bi-grid"></i> All Gallery </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 Gallery <span class="text-danger">*</span> </label>
              <input name="gallery_title" type="text" class="form-control" placeholder="Gallery Title" required>
            </div>

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

              <?php
                for ($i = 1; $i <= 10; $i++) {
                    echo "<div class='col-md-12 col-lg-6 story-single'>
                            <div class='border rounded-2 p-2'>
                              <div class='position-relative'>
                                <img class='img-fluid w-100 bg-white border rounded-2' id='psItem{$i}_Preview'
                                     src='assets/img/placeholder/photo-placeholder.png'
                                     style='aspect-ratio: 1280 / 720; object-fit: cover; width: 100%; height: auto; max-height: 720px;'>
                              </div>
                              <div class='m-0 input-group my-2'>
                                <input id='psItem{$i}' name='gallery_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>
                            <textarea class='form-control ' name='gallery_items[$i][storycaption]' placeholder='Story Caption'></textarea>

                            </div>
                          </div>";
                }
              ?>
            </div>
        </div>
      </div>
      <div class="col-md-4">
        <div class="bg-white rounded-2 p-3">
          <!-- ***********************
          Guest Writer Name
          *************************-->
          <div class="form-group mt-3">
            <label for="WriterName" class="form-label">Guest Writer Name</label>
            <input type="text" class="form-control" name="WriterName" id="WriterName" placeholder="Writer Name">
          </div>

          <!-- ***********************
          Guest Writer Designation
          *************************-->
          <div class="form-group mt-3">
            <label for="WriterDesignation" class="form-label">Guest Writer Designation</label>
            <input type="text" class="form-control" name="WriterDesignation" id="WriterDesignation" placeholder="Writer Designation">
          </div>

          <!-- ***********************
          Select Writer
          *************************-->
          <div class="form-group mt-3">
            <label>Article Writer <span class="text-danger">*</span> </label>
            <select class="form-control tt-select-single" name="Author">
              <option value="" disabled selected>Select Writer</option>
              <?php
              $tt_writers = showAllttdata("tt_accounts", "WHERE AccountStatus = 'active' and PostAbility = 'yes' ORDER BY  UserID  ASC");
              foreach ($tt_writers as $the_writer) {
                ?>
                <option value="<?php echo $the_writer['UserID'] ?>"><?php echo $the_writer['UserName']; echo ($the_writer['UserDesignation'] !='')?  ' - '.$the_writer['UserDesignation'] : ''; ?> </option>
                <?php
              }
              ?>
            </select>
          </div>
          <hr>
          <div class="form-group mt-3">
            <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="gallery_date" required>
          </div>
          <div class="form-group mt-3" >
            <label class="form-label">Publish Status</label>
            <select name="gallery_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 Gallery</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'); ?>
