<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
?>
<?php if($this->viewMore): ?>
  <script type="text/javascript">
    function viewMore<?php echo $this->identity ?>() {
      if (document.getElementById('view_more_<?php echo $this->identity ?>'))
        document.getElementById('view_more_<?php echo $this->identity ?>').style.display = "<?php echo ( $this->results->count() == $this->results->getCurrentPageNumber() || $this->count == 0 ? 'none' : '' ) ?>";

      if(document.getElementById('view_more_<?php echo $this->identity ?>'))
        document.getElementById('view_more_<?php echo $this->identity ?>').style.display = 'none';
      
      if(document.getElementById('loading_image_<?php echo $this->identity ?>'))
      document.getElementById('loading_image_<?php echo $this->identity ?>').style.display = '';

      en4.core.request.send(scriptJquery.ajax({
        method: 'post',
        dataType: 'html',
        url: en4.core.baseUrl + 'widget/index/mod/poke/name/back-button/',
        data: {
          format: 'html',
          page: "<?php echo sprintf('%d', $this->results->getCurrentPageNumber() + 1) ?>",
          viewmore: 1,
          params: '<?php echo json_encode($this->all_params); ?>',
        },
        success: function(responseHTML) {
          scriptJquery('#pokeback_results_<?php echo $this->identity ?>').append(responseHTML);

          if(document.getElementById('view_more_<?php echo $this->identity ?>'))
            scriptJquery('#view_more_<?php echo $this->identity ?>').remove();
          
          if(document.getElementById('loading_image_<?php echo $this->identity ?>'))
          scriptJquery('#loading_image_<?php echo $this->identity ?>').remove();
        }
      }));
      return false;
    }
  </script>
<?php endif; ?>

<?php if($this->showType): ?>
  <?php if (empty($this->viewmore)): ?>
    <ul class="poke_list" id= "pokeback_results_<?php echo $this->identity ?>">
  <?php endif; ?>
      <?php foreach($this->results as $item): 
        $manageActions = Engine_Api::_()->getItem('poke_manageaction', $item['manageaction_id']); 
        $user = Engine_Api::_()->getItem('user', $item['poster_id']);
        $name = ucfirst($manageActions->name);
        $icon = $manageActions['icon'];
      ?>
      <li id="cancel_request_<?php echo $item['poke_id'] ?>" class="clearfix">
        <article class="d-flex flex-wrap">
          <div class="poke_list_thumb">
            <?php echo $this->htmlLink($user->getHref(), $this->itemPhoto($user, 'thumb.profile', $user->getTitle())) ?>
          </div>
          <div class='poke_list_info'>
            <div class='poke_list_info_title'>
              <a title="<?php echo $this->translate('Remove'); ?>" href="javascript:void(0)" onclick="cancelRequest(<?php echo $item['poke_id'] ?>)" class="poke_close_btn fa fa-times"></a>
              <?php echo $this->htmlLink($user->getHref(), $user->getTitle()) ?>
            </div>
            <div class="poke_list_info_stat">
              <?php echo $this->translate("Sent to you %s!", $name); ?>
            </div>
            <div class="poke_list_info_stat text_light">
              <?php echo $this->timestamp($item->creation_date); ?>
            </div>
            <div class="poke_list_info_btn">
              <a href="javascript:void(0);" onclick="backRequest(<?php echo $item['poke_id'] ?>)" class="poke_button">
                <?php if($icon) { ?>
                  <?php $icon = Engine_Api::_()->core()->getFileUrl($icon); ?>
                  <i style="background-image:url(<?php echo $icon ?>);"></i>
                <?php } else { ?>
									<?php $icon = 'application/modules/Poke/externals/images/action_icons/'.$item->slug.'.png'; ?>
                  <i style="background-image:url(<?php echo $icon; ?>);"></i>
                <?php } ?>
                <?php echo $this->translate("Send Back"); ?>
              </a>
            </div>
          </div>
        </article>
      </li>
      <?php endforeach; ?>
      <?php if ($this->viewMore && !empty($this->results) && $this->results->count() > 1): ?>
        <?php if ($this->results->getCurrentPageNumber() < $this->results->count()): ?>
          <div class="clear" id="viewmore_scroll"></div>
          <div class="poke_load_btn" id="view_more_<?php echo $this->identity ?>" onclick="viewMore<?php echo $this->identity ?>();" style="display: block;">
            <a href="javascript:void(0);" class="poke_link_btn" id="feed_viewmore_link_<?php echo $this->identity ?>"><i class="fa fa-sync"></i><span><?php $this->translate('View More'); ?></span></a>
          </div>
          <div class="poke_load_btn" id="loading_image_<?php echo $this->identity ?>" style="display: none;">
            <span class="poke_link_btn"><i class="fa fa-spinner fa-spin"></i></span>
          </div>
        <?php endif; ?>
      <?php endif; ?>
      <?php if (empty($this->viewmore)): ?>
    </ul>
  <?php endif; ?>
<?php else : ?>
  <?php if (empty($this->viewmore)): ?>
    <ul class="poke_sidebar_list clearfix" id= "pokeback_results_<?php echo $this->identity ?>">
  <?php endif; ?>
      <?php foreach($this->results as $item):
        $manageActions = Engine_Api::_()->getItem('poke_manageaction', $item->manageaction_id); 
        $user = Engine_Api::_()->getItem('user', $item->poster_id);
        $name = ucfirst($manageActions->name);
        $icon = $manageActions->icon;
      ?>
        
        <li id="cancel_request_<?php echo $item->poke_id ?>" class="clearfix">
          <?php echo $this->htmlLink($user->getHref(), $this->itemPhoto($user, 'thumb.icon', $user->getTitle()), array('class' => 'poke_sidebar_list_thumb')) ?>
          <div class='poke_sidebar_list_info'>
            <div class='poke_sidebar_list_title'>
              <a href="javascript:void(0)" onclick="cancelRequest(<?php echo $item->poke_id ?>)" class="poke_close_btn fa fa-times"></a>
              <?php echo $this->htmlLink($user->getHref(), $user->getTitle()) ?>
            </div>
            <div class="poke_list_info_stat">
              <?php echo $this->translate("Sent to you %s!", ucfirst($manageActions->name)); ?>
            </div>
            <div class="poke_list_info_stat text_light">
              <?php echo $this->timestamp($item->creation_date); ?>
            </div>
            <div class="poke_sidebar_list_btn">
              <a href="javascript:void(0);" class="poke_button" onclick="backRequest(<?php echo $item->poke_id ?>)">
                <?php if($icon) { ?>
                  <?php $icon = Engine_Api::_()->core()->getFileUrl($icon); ?>
                  <i style="background-image:url(<?php echo $icon ?>);"></i>
                <?php } ?>
                <?php echo $this->translate("Send Back"); ?>
              </a>
            </div>
          </div>
        </li>
      <?php endforeach; ?>
      <?php if ($this->viewMore && !empty($this->results) && $this->results->count() > 1): ?>
        <?php if ($this->results->getCurrentPageNumber() < $this->results->count()): ?>
          <div class="clear" id="viewmore_scroll"></div>
          <div class="poke_load_btn" id="view_more_<?php echo $this->identity ?>" onclick="viewMore<?php echo $this->identity ?>();" style="display: block;">
            <a href="javascript:void(0);" class="poke_link_btn" id="feed_viewmore_link_<?php echo $this->identity ?>"><i class="fa fa-sync"></i><span><?php $this->translate('View More'); ?></span></a>
          </div>
          <div class="poke_load_btn" id="loading_image_<?php echo $this->identity ?>" style="display: none;">
            <span class="poke_link_btn"><i class="fa fa-spinner fa-spin"></i></span>
          </div>
        <?php endif; ?>
      <?php endif; ?>
      <?php if (empty($this->viewmore)): ?>
    </ul>
  <?php endif; ?>
<?php endif; ?>
<script>
  function backRequest(id) {
    en4.core.request.send(scriptJquery.ajax({
      url: en4.core.baseUrl + 'poke/index/back-request',
      dataType: 'json',
      data: {
        format: 'json',
        'id': id,
      },
      success: function(responseJSON) {
        document.getElementById('cancel_request_'+ id).remove();
        if(scriptJquery('#pokeback_results_<?php echo $this->identity ?>').children().length == 0) {
					scriptJquery('#pokeback_results_<?php echo $this->identity ?>').parent().remove();
        }
      }
    }));
  }
  
  function cancelRequest(id) {
    en4.core.request.send(scriptJquery.ajax({
      url: en4.core.baseUrl + 'poke/index/cancel-request',
      dataType: 'json',
      data: {
        format: 'json',
        'id': id,
      },
      success: function(responseJSON) {
        document.getElementById('cancel_request_'+ id).remove();
        if(scriptJquery('#pokeback_results_<?php echo $this->identity ?>').children().length == 0) {
					scriptJquery('#pokeback_results_<?php echo $this->identity ?>').parent().remove();
        }
      }
    }));
  }
</script>
