<?php
require_once('../config.php');
if (isset($_POST['data_type'])) {
  if ($_POST['data_type'] == 'all_media') {
    $itemsPerNextLoad = 12;
    $pageNumber = $_POST['page'];
    $item_offset = ($pageNumber - 1) * $itemsPerNextLoad;
    $media_library = showAllttdata('tt_media', 'ORDER BY  MediaId  DESC LIMIT '.$itemsPerNextLoad.' OFFSET ' .$item_offset);
    if (!empty($media_library)){
      foreach ($media_library as $the_media) {
        ?>
        <div class="col-4 col-sm-4 col-md-3 col-lg-2  mb-2">
          <div class="attachment-preview " data-bs-toggle="modal" data-bs-target="#mediaInformation<?php echo $the_media['MediaId'] ?>">
            <div class="thumbnail">
              <?php
              $tt_file_type = isImage($the_media['MediaName']);
              if ($tt_file_type == 'image') {
                ?>
                <div class="centered">
                  <img src="<?php echo $the_media['MediaURL'] ?>" alt="<?php echo $the_media['MediaName'] ?>">
                </div>
                <?php
              }else {
                ?>
                <div class="text-center">
                  <span class="p-1 mb-2 bg-primary text-white d-block"><i class="fs-3 ri-file-warning-line"></i></span>
                  <span class="fs-6"><?php echo $the_media['MediaName'] ?></span>
                </div>
                <?php
              }
              ?>
            </div>
          </div>
        </div>
        <div class="modal fade" id="mediaInformation<?php echo $the_media['MediaId'] ?>" tabindex="-1" aria-hidden="true">
          <div class="modal-dialog modal-dialog-scrollable modal-xl">
            <div class="modal-content border-0 rounded-0">
              <div class="modal-header p-2">
                <h5 class="modal-title">Media details</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
              </div>
              <div class="bg-white" style="overflow-x:hidden;">
                <div class="row">
                  <div class="col-lg-8">
                    <div class="p-3">
                      <?php
                      if ($tt_file_type == 'image') {
                        ?>
                        <div class=" thumbnail-image">
                          <img class="details-image" src="<?php echo $the_media['MediaURL'] ?>" draggable="false"  alt="<?php echo $the_media['MediaName'] ?>">
                        </div>
                        <?php
                      }else {
                        ?>
                        <div class="text-center bg-light rounded-3 p-4">
                          <span style="text-transform:uppercase" class="p-2 fs-3 bg-primary text-white rounded-3"><?php echo $tt_file_type ?></span>
                          <br>
                          <span class="mt-3 fs-6 d-block"><?php echo $the_media['MediaName'] ?></span>
                        </div>
                        <?php
                      }
                       ?>
                    </div>
                  </div>
                  <div class="col-lg-4 bg-light">
                    <div class="p-2">
                      <p class="mb-2 rounded-2">
                        <strong>Uploaded on:</strong> <?php echo date('F j, Y', strtotime($the_media['MediaUploadedTime'])); ?>
                      </p>
                      <p class="mb-2 rounded-2">
                        <strong>Uploaded by:</strong> <?php echo getSingleValue('admin_user', 'admin_id', $the_media['MediaUploader'], 'admin_name') ?>
                      </p>
                      <p class="mb-2 rounded-2">
                        <strong>File name:</strong> <?php echo $the_media['MediaName'] ?>
                      </p>
                      <p class="mb-2 rounded-2">
                        <strong>File type:</strong>
                        <?php
                        if ($tt_file_type == 'image') {
                          echo "Image file";
                        }else {
                          echo $tt_file_type. ' file';
                        }
                        ?>
                      </p>
                      <p class="mb-2 rounded-2">
                        <strong>File size:</strong>
                        <?php
                        $fileSizeBytes = filesize('../../uploads/'.$the_media['MediaName']);
                        if ($fileSizeBytes < 1024) {
                            echo  $fileSizeBytes . ' bytes';
                        } elseif ($fileSizeBytes < (1024 * 1024)) {
                            echo  round($fileSizeBytes / 1024, 2) . ' KB';
                        } else {
                            echo  round($fileSizeBytes / (1024 * 1024), 2) . ' MB';
                        }
                         ?>
                      </p>
                      <?php
                      if ($tt_file_type == 'image') {
                        ?>
                        <p class="mb-2 rounded-2">
                          <strong>Dimensions:</strong>
                          <?php
                          $imageSize = getimagesize('../../uploads/'.$the_media['MediaName']);
                          echo   $imageSize[0].' by '.$imageSize[1].'pixels';
                          ?>
                        </p>
                        <?php
                      }
                       ?>
                       <div class="input-group my-3">
                           <input  value="<?php echo $the_media['MediaURL'] ?>" type="url" class="form-control form-control-sm rounded-0 border-success">
                           <a href="javascript:void(0);" class="btn-sm input-group-text border-success bg-success text-white rounded-0 copy_link" onclick="navigator.clipboard.writeText('<?php echo $the_media['MediaURL']; ?>')">
                               <i class="ri-links-line pe-2"></i> Copy Link
                           </a>
                       </div>
                      <div class="row mb-3">
                        <div class="col-6">
                          <a  onclick="confirmDelete(event)" href="media-library.php?delete=<?php echo $the_media['MediaId'] ?>" class="btn btn-danger rounded-0 btn-sm w-100 text-center"><i class="ri-delete-bin-2-fill"></i> Delete Media</a>
                        </div>
                        <div class="col-6">
                          <a  href="<?php echo $the_media['MediaURL'] ?>"  target="_blank" class="delete-action btn btn-primary rounded-0 btn-sm w-100 text-center"> <i class=" ri-arrow-right-line"></i> View Media</a>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <?php
      }
      ?>
      </div>
      <?php
    }else {
      ?>
      <script type="text/javascript">
        $('#loadMoreBtn').hide();
      </script>
      <div class="col-12">
          <p class="m-0 text-muted  rounded-3 p-3 mb-2 text-center bg-white">No More Media !</p>
      </div>
      <?php
    }
  }elseif ($_POST['data_type'] == 'select_media') {

    $tt_media_type = $_POST['ttmore_mediaType'];
    if ($tt_media_type == 'all' || $tt_media_type == '') {
      $custom_condition = "";
    }else {
      $custom_condition = "WHERE MediaType = '$tt_media_type'";
    }

    $itemsPerNextLoad = 12;
    $pageNumber = $_POST['page'];
    $item_offset = ($pageNumber - 1) * $itemsPerNextLoad;
    $media_library = showAllttdata('tt_media', $custom_condition .'ORDER BY  MediaId  DESC LIMIT '.$itemsPerNextLoad.' OFFSET ' .$item_offset);
    if (!empty($media_library)){
      foreach ($media_library as $the_media) {
        ?>
        <div class="col-4 col-sm-4 col-md-3 col-lg-2  mb-2">
          <div class="attachment-preview clickableText"  data-bs-dismiss="modal" data-target="<?php echo $_POST['ttmore_target'] ?>" data-url="<?php echo $the_media['MediaURL'] ?>">
            <div class="thumbnail">
              <?php
              $tt_file_type = isImage($the_media['MediaName']);
              if ($tt_file_type == 'image') {
                ?>
                <div class="centered">
                  <img src="<?php echo $the_media['MediaURL'] ?>" alt="<?php echo $the_media['MediaName'] ?>">
                </div>
                <?php
              }else {
                ?>
                <div class="text-center">
                  <span class="p-1 mb-2 bg-primary text-white d-block"><i class="fs-6 ri-file-warning-line"></i></span>
                  <span class="fs-6"><?php echo $the_media['MediaName'] ?></span>
                </div>
                <?php
              }
              ?>
            </div>
          </div>
        </div>
         <?php
      }
      ?>
      </div>
      <script type="text/javascript">
      $(document).ready(function() {
          $('.clickableText').on('click', function() {
              var urlValue = $(this).data('url');
              var targetInputId = $(this).data('target');
              $(targetInputId).val(urlValue);
              var Preview_Image_Div = targetInputId+'_Preview';
              $(Preview_Image_Div).attr('src', urlValue)
          });
      });
      </script>
      <?php
    }else {
      ?>
      <script type="text/javascript">
        $('#loadMoreBtnTTAdvanceNews').hide();
      </script>
      <div class="col-12">
          <p class="m-0 text-muted  rounded-3 p-3 mb-2 text-center bg-white">No More Media !</p>
      </div>
      <?php
    }
  }
}
?>
