<?php
  /**
   * Transactions
   *
   * @package Wojo Framework
   * @author wojoscripts.com
   * @copyright 2017
   * @version $Id: transactions.tpl.php, v1.00 2017-02-05 10:12:05 gewa Exp $
   */
  if (!defined("_WOJO"))
      die('Direct access to this location is not allowed.');
	  
  if (!Auth::checkAcl("owner")) : print Message::msgError(Lang::$word->NOACCESS); return; endif;
?>
<?php switch(Url::segment($this->segments)): case "new": ?>
<!-- Start new -->
<div class="row horizontal-gutters align-middle">
  <div class="columns">
    <h2><?php echo Lang::$word->TRX_NEW;?></h2>
  </div>
  <div class="columns shrink"><a data-action="toggle" data-page="transaction" class="wojo small primary icon label sAction"><i class="icon question sign"></i></a>
  </div>
</div>
<form method="post" id="wojo_form" name="wojo_form">
  <div class="wojo segment form">
    <div class="wojo fields align-middle">
      <div class="field four wide labeled">
        <label class="content-right mobile-content-left"><?php echo Lang::$word->USER;?>
          <i class="icon asterisk"></i></label>
      </div>
      <div class="field">
        <select name="user_id" class="wojo fluid dropdown selection">
          <?php echo Utility::loopOptions($this->users, "id", "name");?>
        </select>
      </div>
    </div>
    <div class="wojo fields align-middle">
      <div class="field four wide labeled">
        <label class="content-right mobile-content-left"><?php echo Lang::$word->CF_SEC_P;?>
          <i class="icon asterisk"></i></label>
      </div>
      <div class="field">
        <select name="product_id" class="wojo fluid dropdown selection">
          <?php echo Utility::loopOptions($this->products, "id", "title");?>
        </select>
      </div>
    </div>
    <div class="wojo fields align-middle">
      <div class="field four wide labeled">
        <label class="content-right mobile-content-left"><?php echo Lang::$word->GW_NAME;?>
          <i class="icon asterisk"></i></label>
      </div>
      <div class="field">
        <select name="pp" class="wojo fluid dropdown selection">
          <?php echo Utility::loopOptions($this->gateways, "name", "displayname");?>
        </select>
      </div>
    </div>
    <div class="wojo fields align-middle">
      <div class="field four wide labeled">
        <label class="content-right mobile-content-left"><?php echo Lang::$word->QUANTITY;?>
          <i class="icon asterisk"></i></label>
      </div>
      <div class="field">
        <input data-range='{"step":1,"from":1, "to":20, "scale":[1,10,15,20]}' type="hidden" name="qty" value="1" class="rangeslider">
      </div>
    </div>
    <div class="wojo fields align-middle">
      <div class="field four wide labeled">
        <label class="content-right mobile-content-left"><?php echo Lang::$word->USR_NOTIFY;?>
          <i class="icon asterisk"></i></label>
      </div>
      <div class="field">
        <div class="wojo toggle checkbox">
          <input name="notify" type="checkbox" value="1">
          <label><?php echo Lang::$word->YES;?></label>
        </div>
      </div>
    </div>
    <div class="wojo fields">
      <div class="field four wide labeled">
        <label class="content-right mobile-content-left"><?php echo Lang::$word->TRX_MEMO;?></label>
      </div>
      <div class="field">
        <textarea name="memo" placeholder="<?php echo Lang::$word->TRX_MEMO;?>"></textarea>
      </div>
    </div>
  </div>
  <div class="content-center">
    <a href="<?php echo Url::url("/admin/transactions");?>" class="wojo small basic button"><?php echo Lang::$word->CANCEL;?></a>
    <button type="button" data-action="processTransaction" name="dosubmit" class="wojo primary button"><?php echo Lang::$word->TRX_CREATE;?></button>
  </div>
</form>
<?php break;?>
<!-- Start default -->
<?php default: ?>
<div class="row half-gutters align-middle">
  <div class="column">
    <h2><?php echo Lang::$word->TRX_PAY;?></h2>
  </div>
  <div class="columns shrink">
    <a href="<?php echo Url::url(Router::$path, "new/");?>" class="wojo small primary basic rounded button"><i class="icon plus alt"></i><?php echo Lang::$word->TRX_NEW;?></a>
  </div>
</div>
<div class="wojo big space divider"></div>
<div class="wojo card" id="pData">
  <div class="header">
    <div class="row horizontal-gutters align-middle">
      <div class="column">
        <div class="wojo small dropdown top left pointing" id="timeRange"><i class="icon horizontal ellipsis"></i>
          <div class="menu">
            <div class="item" data-value="all"><?php echo Lang::$word->ALL;?></div>
            <div class="item" data-value="day"><?php echo Lang::$word->TODAY;?></div>
            <div class="item" data-value="week"><?php echo Lang::$word->THIS_WEEK;?></div>
            <div class="item" data-value="month"><?php echo Lang::$word->THIS_MONTH;?></div>
            <div class="item" data-value="year"><?php echo Lang::$word->THIS_YEAR;?></div>
          </div>
        </div>
      </div>
      <div class="column shrink">
        <div id="legend" class="wojo small horizontal list">
        </div>
      </div>
    </div>
  </div>
  <div class="content" id="payment_chart" style="height:400px"></div>
</div>
<?php if($this->data):?>
<div class="wojo segment">
  <form method="get" id="wojo_form" action="<?php echo Url::url(Router::$path);?>" name="wojo_form">
    <div class="row align-middle half-vertical-gutters">
      <div class="column screen-30 tablet-40 mobile-100 phone-100">
        <div class="wojo fluid calendar left icon input" id="fromdate">
          <input name="fromdate" type="text" placeholder="<?php echo Lang::$word->FROM;?>" readonly>
          <i class="icon calendar"></i>
        </div>
      </div>
      <div class="column shrink phone-hide mobile-hide">
        <div class="wojo separator"></div>
      </div>
      <div class="column screen-30 tablet-40 mobile-100 phone-100">
        <div class="wojo fluid calendar left icon input" id="enddate">
          <i class="calendar icon"></i>
          <input name="enddate" type="text" placeholder="<?php echo Lang::$word->TO;?>" readonly>
          <button class="wojo small icon button" id="doDates"><i class="find black small circular link icon"></i></button>
        </div>
      </div>
      <div class="column shrink phone-hide mobile-hide">
        <div class="wojo separator"></div>
      </div>
      <div class="column shrink phone-hide">
        <a href="<?php echo Url::url(Router::$path);?>" class="wojo basic icon button"><i class="icon refresh"></i></a>
      </div>
      <div class="column content-right phone-content-center phone-100">
        <a href="<?php echo ADMINVIEW;?>/helper.php?exportAllTransactions=1" class="wojo primary icon button"><i class="icon spreadsheet"></i></a>
      </div>
    </div>
  </form>
  <div class="wojo divider phone-hide mobile-hide"></div>
  <table class="wojo sorting basic table">
    <thead>
      <tr>
        <th class="disabled center aligned"><i class="icon disabled id"></i></th>
        <th data-sort="string"><?php echo Lang::$word->ITEM;?></th>
        <th data-sort="string"><?php echo Lang::$word->USER;?></th>
        <th data-sort="int"><?php echo Lang::$word->TRX_PP;?></th>
        <th data-sort="int"><?php echo Lang::$word->TRX_TOTAMT;?></th>
        <th data-sort="int"><?php echo Lang::$word->CREATED;?></th>
      </tr>
    </thead>
    <?php $total = 0;?>
    <?php foreach ($this->data as $row):?>
    <?php $total += $row->total;?>
    <tr id="item_<?php echo $row->id;?>">
      <td class="collapsing"><span class="wojo mini basic disabled label"><?php echo $row->id;?></span></td>
      <td><a class="inverted" href="<?php echo Url::url("/admin/products/edit", $row->product_id);?>"><?php echo $row->title;?></a></td>
      <td><a class="inverted" href="<?php echo Url::url("/admin/users/edit", $row->user_id);?>"><?php echo $row->name;?></a></td>
      <td><?php echo $row->pp;?></td>
      <td><?php echo $row->total;?></td>
      <td data-sort-value="<?php echo strtotime($row->created);?>"><?php echo Date::doDate("short_date", $row->created);?></td>
    </tr>
    <?php endforeach;?>
  </table>
  <div class="wojo double divider"></div>
  <div class="wojo secondary basic label"><?php echo Lang::$word->TRX_TOTAMT;?>
    <?php echo Utility::formatMoney($total);?></div>
</div>
<div class="row half-gutters-mobile half-gutters-phone align-middle">
  <div class="columns shrink mobile-100 phone-100">
    <div class="wojo small thick text"><?php echo Lang::$word->TOTAL . ': ' . $this->pager->items_total;?> / <?php echo Lang::$word->CURPAGE . ': ' . $this->pager->current_page . ' '. Lang::$word->OF . ' ' . $this->pager->num_pages;?></div>
  </div>
  <div class="columns mobile-100 phone-100 content-right mobile-content-left"><?php echo $this->pager->display_pages('small');?></div>
</div>
<?php endif;?>
<script type="text/javascript" src="<?php echo SITEURL;?>/assets/morris.min.js"></script>
<script type="text/javascript" src="<?php echo SITEURL;?>/assets/raphael.min.js"></script>
<script type="text/javascript"> 
// <![CDATA[
$(document).ready(function() {	
    function getStats(range) {
        $("#pData").addClass('loading');
		$("#payment_chart").empty();
        $.ajax({
            type: 'GET',
            url: "<?php echo ADMINVIEW;?>/helper.php?action=1&page=salesChart&timerange=" + range,
            dataType: 'json'
        }).done(function(json) {
			var legend = '';
            json.legend.map(function(val) {
               legend += val;
            });
			$("#legend").html(legend);
            Morris.Area({
                element: 'payment_chart',
                data: json.data,
                xkey: 'm',
                ykeys: json.label,
                labels: json.label,
                parseTime: false,
                lineWidth: 1,
                pointSize: 5,
                lineColors: json.color,
				gridTextFamily: "wSansDemi",
				gridTextColor: "rgba(0,0,0,0.6)",
				gridTextSize: 14,
                fillOpacity: '.1',
                hideHover: 'auto',
				preUnits: json.preUnits,
				behaveLikeLine:true,
				hoverCallback: function(index, json, content) {
					var text = $(content)[1].textContent;
					return content.replace(text, text.replace(json.preUnits, ""));
				},
                smooth: true,
                resize: true,
            });
            $("#pData").removeClass('loading');
        });
    }
    getStats('all');
	
    $("#timeRange").on('click', '.item', function() {
		$("#payment_chart").html('');
        getStats($(this).data('value'));
    });
});
// ]]>
</script>
<?php break;?>
<?php endswitch;?>