<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
?>
<?php include APPLICATION_PATH .  '/application/modules/Poke/views/scripts/_adminHeader.tpl';?>

<script type="text/javascript">
en4.core.runonce.add(function() {
  scriptJquery("#selectall").click(function(){
    if(this.checked){
      scriptJquery('.checkbox').each(function(){
        scriptJquery(".checkbox").prop('checked', true);
      });
    } else {
      scriptJquery('.checkbox').each(function(){
        scriptJquery(".checkbox").prop('checked', false);
      });
    }
  });


  scriptJquery("input[name='enable'],input[name='disable'],input[name='delete']").on('click', function( event ) {
    event.preventDefault();
    var selectedItems = scriptJquery("input[name='selectedItems[]']");
    var name = scriptJquery(this).attr('name');
    if (selectedItems.filter(':checked').length == 0) {
      alert('<?php echo $this->string()->escapeJavascript($this->translate("Please select items for any mass action.")) ?>');
    } else {
      if (confirm('<?php echo $this->string()->escapeJavascript($this->translate("Are you sure you want to perform this action on selected entries?")) ?>')) {
        scriptJquery('#multidelete_form').append("<input type='hidden' value='"+name+"' name='"+name+"'>");
        scriptJquery('#multidelete_form').trigger("submit");
      }
    }
  });
});
</script>

<h3 style="margin-bottom:6px;"><?php echo $this->translate("Manage Poke & Actions"); ?></h3>
<p><?php echo $this->translate('Here, you can manage Poke and all Actions created by you. You can also enable / disable them anytime. <br />You can click on "Add New Action" button below to add a new action. <br /> Note: You can configure "Users Poke & Action Options" from <a href="admin/authorization/level/edit" target="_blank">Member Level Settings</a> to allow users to choose who can poke and send actions to them.'); ?></p>

<div class="mb-3">
  <?php echo $this->htmlLink(array('route' => 'admin_default', 'module' => 'poke', 'controller' => 'manageactions', 'action' => 'add'), $this->translate("Add New Action"), array('class'=>'admin_link_btn icon_plan_add')); ?>
</div>

<div class='admin_search admin_search_nol'>
  <?php echo $this->formFilter->render($this) ?>
</div>

<?php if(is_countable($this->paginator) &&  engine_count($this->paginator)): ?>
  <form id="multidelete_form" action="<?php echo $this->url();?>" method="POST">
    <div class="admin_manage_action d-flex flex-wrap">
      <div class="_count">
        <?php echo $this->translate(array('%s entry found.', '%s entries found.', $this->paginator->getTotalItemCount()), $this->locale()->toNumber($this->paginator->getTotalItemCount())) ?>
      </div>
      <div class="admin_manage_action_option">
        <span><?php echo $this->translate('With Selected:'); ?></span>
        <input type='submit' value="Enable" name="enable" class="btn btn-primary">
        <input type='submit' value="Disable" name="disable" class="btn btn-primary">
        <input type='submit' value="Delete" name="delete" class="btn btn-danger">
      </div>
      <div class="admin_manage_action_right d-flex flex-wrap align-items-center">
        <?php echo $this->paginationControl($this->paginator); ?>
      </div>
    </div>
    <table class='admin_table admin_responsive_table' style="width:100%;">
      <thead>
        <tr>
          <th class='admin_table_short'><input id="selectall" type='checkbox' /></th>
          <th>
            <?php echo $this->translate("ID"); ?>
          </th>
          <th>
            <?php echo $this->translate('Icon'); ?>
          </th>
          <th>
            <?php echo $this->translate("Name"); ?>
          </th>
          <th class="admin_table_centered">
            <?php echo $this->translate("Enabled"); ?>
          </th>
          <th>
            <?php echo $this->translate("Options"); ?>
          </th>
        </tr>
      </thead>
      <tbody id="menu_list">
        <?php foreach ($this->paginator as $item): ?>
          <tr id="order_<?php echo $item->getIdentity(); ?>">
            <td><input type='checkbox' class='checkbox' name='selectedItems[]' value="<?php echo $item->getIdentity() ?>"/></td>
            <td data-label="<?php echo $this->translate("ID"); ?>"><?php echo $item->getIdentity() ?></td>
            <td data-label="<?php echo $this->translate('Icon'); ?>" class="poke_admin_img">
              <?php if(!empty($item->icon)) { ?>
                <?php $icon = Engine_Api::_()->core()->getFileUrl($item->icon, true); ?>
								<img src="<?php echo $icon['image']; ?>" alt="<?php echo $icon['alt_text'] ? $icon['alt_text']: ''; ?>" style="width:16px;" />
              <?php } else { ?>
								<?php $icon = 'application/modules/Poke/externals/images/action_icons/'.$item->slug.'.png'; ?>
								<img src="<?php echo $icon; ?>" style="width:16px;" />
              <?php } ?>
            </td>
            <td data-label="<?php echo $this->translate("Name"); ?>"><?php if( !empty($item->name) ){ echo $item->name; }else { echo '-'; } ?></td>
            <td data-label="<?php echo $this->translate("Enabled"); ?>" class="admin_table_centered"><?php echo ( $item->enabled ? $this->htmlLink(array('route' => 'admin_default', 'module' => 'poke', 'controller' => 'manageactions', 'action' => 'enabled', 'manageaction_id' => $item->manageaction_id, 'param' => 'action', 'page' => $this->page), $this->htmlImage($this->layout()->staticBaseUrl . 'application/modules/Core/externals/images/admin/check.png', '', array('title' => $this->translate('Disable'))), array())  : $this->htmlLink(array('route' => 'admin_default', 'module' => 'poke', 'controller' => 'manageactions', 'action' => 'enabled', 'manageaction_id' => $item->manageaction_id, 'param' => 'action', 'page' => $this->page), $this->htmlImage('application/modules/Core/externals/images/admin/uncheck.png', '', array('title' => $this->translate('Enable')))) ) ?></td >

            <td class="admin_table_options">
              <?php	echo $this->htmlLink(array('route' => 'admin_default', 'module' => 'poke', 'controller' => 'manageactions', 'action' => 'edit', 'manageaction_id' => $item->manageaction_id), $this->translate("Edit")) ;	?>
              <?php if(empty($item->default)):?>
              | <a href="<?php echo $this->url(array('action' => 'delete', 'manageaction_id' => $item->manageaction_id)) ?>" class="smoothbox"><?php echo $this->translate("Delete") ?>
              </a>
              <?php endif; ?>
            </td>
          </tr>
        <?php  endforeach; ?>
      </tbody>
    </table>
  </form>
<?php else: ?>
  <div class="tip">
    <span>
      <?php echo $this->translate("There are no actions matching your search criteria.") ?>
    </span>
  </div>
<?php endif; ?>
<script type="text/javascript">
  en4.core.runonce.add(function() {
    scriptJquery('#menu_list').addClass('sortable');
    var SortablesInstance = scriptJquery('#menu_list').sortable({
      stop: function( event, ui ) {
        var ids = [];
        scriptJquery('#menu_list > tr').each(function(e) {
          var el = scriptJquery(this);
          ids.push(el.attr('id'));
        });
        // Send request
        var url = '<?php echo $this->url(array('action' => 'order')) ?>';
        scriptJquery.ajax({
            url : url,
            dataType : 'json',
            data : {
                format : 'json',
                order : ids
            }
        });
      }
    });
  });
</script>
