<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
?>

<?php
  $typeLabels = array(
    'activity_action' => 'Posts',
    'video' => 'Videos',
    'music_playlist_song' => 'Songs',
    'album' => 'Albums',
    'user' => 'Profiles',
    'blog' => 'Blogs',
    'event' => 'Events',
    'classified' => 'Classifieds',
    'sespage_page' => 'Pages',
    'sesgroup_group' => 'Groups',
		'sesmusic_album' => 'Music Album',
		'sesmusic_albumsong' => 'Song',
		'sesmusic_artists' => 'Artist',
		'sesmusic_artist' => 'Artist',
		'sesmusic_playlist' => 'Playlist',
  );
  $baseUrl = $this->url(array('module' => 'core', 'controller' => 'savecollection', 'action' => 'index'), 'default', true);
?>

<div class="layout_middle">
  <div class="generic_layout_container layout_core_content">
    <h2 class="mb-0"><?php echo $this->translate("Saved Items"); ?></h2>
      
    <div class="section_tabs d-flex justify-content-between align-items-center">
      <ul>
        <li><a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'savecollection', 'action' => 'index'), 'default', true); ?>" class="active"><?php echo $this->translate("Saved Items"); ?></a></li>

        <li><a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'savecollection', 'action' => 'collections'), 'default', true); ?>"><?php echo $this->translate("Collections"); ?></a></li>
      </ul>
      <div class="savecollection_sort">
        <select id="savecollection_sort_select">
          <option value="date_newest" <?php if ($this->sort == 'date_newest') echo 'selected'; ?>><?php echo $this->translate("Newest First"); ?></option>
          <option value="date_oldest" <?php if ($this->sort == 'date_oldest') echo 'selected'; ?>><?php echo $this->translate("Oldest First"); ?></option>
        </select>
      </div>
    </div>

    <div class="savecollection_type_filters d-none">
      <a href="<?php echo $baseUrl; ?>?sort=<?php echo urlencode($this->sort); ?>&search=<?php echo urlencode($this->search); ?>" class="btn <?php echo empty($this->contentType) ? 'btn-primary' : 'btn-alt'; ?>"><?php echo $this->translate("All"); ?></a>
      <?php foreach ($this->supportedTypes as $type): ?>
        <?php $label = isset($typeLabels[$type]) ? $typeLabels[$type] : ucfirst(str_replace('_', ' ', $type)); ?>
        <a href="<?php echo $baseUrl; ?>?content_type=<?php echo urlencode($type); ?>&sort=<?php echo urlencode($this->sort); ?>&search=<?php echo urlencode($this->search); ?>" class="btn <?php echo ($this->contentType == $type) ? 'btn-primary' : 'btn-alt'; ?>"><?php echo $this->translate($label); ?></a>
      <?php endforeach; ?>
    </div>

    <?php if ($this->paginator->getTotalItemCount() > 0): ?>
      <ul class="savecollection_grid grid_listing">
        <?php foreach ($this->paginator as $item): ?>
          <?php
            $contentItem = null;
            try { $contentItem = Engine_Api::_()->getItem($item->content_type, $item->content_id); } catch (Exception $e) {}
            $displayInfo = Engine_Api::_()->getApi('savecollection', 'core')->getContentDisplayInfo($contentItem);
            $title = $displayInfo['title'] ?: $this->translate('Unknown Item');
            $photo = $displayInfo['photo'];
            $href = $displayInfo['href'];
            $typeLabel = isset($typeLabels[$item->content_type]) ? $typeLabels[$item->content_type] : ucfirst(str_replace('_', ' ', $item->content_type));
          ?>
          <li class="grid_listing_item grid_listing_item" data-id="<?php echo $item->getIdentity(); ?>">
            <article>
              <div class="grid_listing_item_thumb savecollection_card_thumb">
                <a href="<?php echo $href; ?>">
                  <?php if ($photo): ?>
                    <img src="<?php echo $photo; ?>" alt="<?php echo $this->escape($title); ?>" loading="lazy" />
                  <?php else: ?>
                    <i class="fa-regular fa-bookmark savecollection_card_thumb_icon"></i>
                  <?php endif; ?>
                </a>
                <div class="savecollection_card_icon_overlay savecollection_icon savecollection_icon_saved" data-id="<?php echo $item->getIdentity(); ?>" data-content-type="<?php echo $this->escape($item->content_type); ?>" data-content-id="<?php echo $item->content_id; ?>" data-saved="1" title="<?php echo $this->translate('Remove from Saved'); ?>" aria-label="<?php echo $this->translate('Remove from Saved'); ?>" role="button">
                  <i class="fa-regular fa-bookmark"></i>
                </div>
              </div>

              <div class="grid_listing_item_info">
                <div class="grid_listing_item_title">
                  <a href="<?php echo $href; ?>"><?php echo $this->escape($title); ?></a>
                </div>
                <div class="grid_listing_item_owner">
                  <span><?php echo $this->translate($typeLabel); ?></span>
                  <span><?php echo $this->timestamp(strtotime($item->created_at)); ?></span>
                </div>
              </div>
            </article>
          </li>
        <?php endforeach; ?>
      </ul>
      <?php if ($this->paginator->count() > 1): ?>
        <div class="savecollection_pagination">
          <?php echo $this->paginationControl($this->paginator, null, null, array(
            'pageAsQuery' => true,
            'query' => array('sort' => $this->sort, 'content_type' => $this->contentType),
          )); ?>
        </div>
      <?php endif; ?>
    <?php else: ?>
      <div class="no_result_tip">
        <i class="noicon center_item"><svg viewBox="0 0 24 24" fill="currentColor"><path d="M2.849,23.55a2.954,2.954,0,0,0,3.266-.644L12,17.053l5.885,5.853a2.956,2.956,0,0,0,2.1.881,3.05,3.05,0,0,0,1.17-.237A2.953,2.953,0,0,0,23,20.779V5a5.006,5.006,0,0,0-5-5H6A5.006,5.006,0,0,0,1,5V20.779A2.953,2.953,0,0,0,2.849,23.55Z"/></svg></i>
        <p><?php echo $this->translate("You haven't saved any items yet."); ?></p>
        <p><?php echo $this->translate("Tap the bookmark icon on any content to save it here."); ?></p>
      </div>
    <?php endif; ?>
  </div>
</div>

<script type="text/javascript">
en4.core.runonce.add(function() {
  var baseUrl = <?php echo json_encode($baseUrl); ?>;
  var currentType = <?php echo json_encode($this->contentType); ?>;
  var sortSelect = document.getElementById('savecollection_sort_select');
  if (sortSelect) {
    sortSelect.addEventListener('change', function() { reloadPage(); });
  }
  function reloadPage() {
    var sort = document.getElementById('savecollection_sort_select').value;
    var params = [];
    if (sort && sort !== 'date_newest') params.push('sort=' + encodeURIComponent(sort));
    if (currentType) params.push('content_type=' + encodeURIComponent(currentType));
    window.proxyLocation.href = baseUrl + (params.length ? '?' + params.join('&') : '');
  }
});
</script>
