<?php echo $header; ?>
<div id="content">
<div class="box">
<div class="heading">

      <h1><img src="view/image/report.png" alt="" /> <?php echo $heading_title; ?></h1>
      <div class="buttons">
       <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
		<input type="file" name="upload" />
		<a onclick="$('#form').submit();" class="button"><span><?php echo $button_import; ?></span></a>
		<a onclick="exportExcel();" class="button"><?php echo $button_csv; ?></a></div>
	  </form>
 </div>


 <div class="content">
  <?php if ($error_warning) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>
  <?php if ($success) { ?>
  <div class="success"><?php echo $success; ?></div>
  <?php } ?>
  
    <table class="form">
	<thead>
        <tr>
      
          <td><?php echo $entry_category; ?>
            <select name="filter_category">
			
			  <?php if (0 == $filter_category) { ?>
			  <option value="0" selected="selected"></option>
			  <?php } else { ?>
			  <option value="0"></option>
			  <?php } ?>
              <?php foreach ($categories as $category) { ?>
              <?php if ($category['category_id'] == $filter_category) { ?>
              <option value="<?php echo $category['category_id']; ?>" selected="selected"><?php echo $category['name']; ?></option>
              <?php } else { ?>
              <option value="<?php echo $category['category_id']; ?>"><?php echo $category['name']; ?></option>
              <?php } ?>
			  
              <?php } ?>
		
            </select></td>
			
		   <td><?php echo $entry_store; ?>
            <select name="filter_store">
			
			  <?php if (0 == $filter_store) { ?>
			  <option value="0" selected="selected"></option>
			  <?php } else { ?>
			  <option value="0"></option>
			  <?php } ?>
              <?php foreach ($stores as $store) { ?>
              <?php if ($store['store_id'] == $filter_store) { ?>
              <option value="<?php echo $store['store_id']; ?>" selected="selected"><?php echo $store['name']; ?></option>
              <?php } else { ?>
              <option value="<?php echo $store['store_id']; ?>"><?php echo $store['name']; ?></option>
              <?php } ?>
			  
              <?php } ?>
		
            </select></td>	
		 <td style="text-align: left;"><?php echo $entry_calc; ?>
            <select name="filter_calc">
			
			  <?php if (0 == $filter_calc) { ?>
			  <option value="0" selected="selected"></option>
			  <option value="1"><?php echo $entry_calc1 ?></option>
			  <?php } else { ?>
			  <option value="0"></option>
			  <option value="1" selected="selected"><?php echo $entry_calc1 ?></option>
			  <?php } ?>
              
		
            </select></td>
         
          <td style="text-align: right;"><a onclick="filter();" class="button"><?php echo $button_filter; ?></a></td>
        </tr>
		</thead>
      </table> 
  
<table class="list">
  <thead>
    <tr>
	 <td></td>
      <td style="text-decoration: underline"><a onclick="sort('name');"><?php echo $column_name; ?></a></td>	  
      <td style="text-decoration: underline"><a onclick="sort('model');"><?php echo $column_model; ?></a></td>
	  <td style="text-decoration: underline"><a onclick="sort('sku');"><?php echo $column_sku; ?></a></td>
	  <td style="text-decoration: underline"><a onclick="sort('subtract');"><?php echo $column_subtract; ?></a></td>
      <td style="text-decoration: underline"><a onclick="sort('stock');"><?php echo $column_stock; ?></a></td>
      <td style="text-decoration: underline"><a onclick="sort('pricesell');"><?php echo $column_pricesell; ?></a></td>
      <td style="text-decoration: underline"><a onclick="sort('pricebuy');"><?php echo $column_pricebuy; ?></a></td>
      <td style="text-decoration: underline"><a onclick="sort('stockval');"><?php echo $column_stockval; ?></a></td>
      <td style="text-decoration: underline"><a onclick="sort('margin');"><?php echo $column_margin; ?></a></td>  
	  <input type="hidden" name="direction" value="<?php echo $direction ?>" />
    </tr>
  </thead>
  <tbody>
    <?php if ($products) { ?>
    <?php $class = 'odd'; ?>
    <?php foreach ($products as $product) { ?>
    <?php $class = ($class == 'even' ? 'odd' : 'even');
    
    ?>
    <tr class="<?php echo $class; ?>">
	
	<?php
	    $this->load->model('tool/image');
		
		
		
			if ($product['image'] && file_exists(DIR_IMAGE . $product['image'])) {
				$image = $this->model_tool_image->resize($product['image'], 40, 40);
			} else {
				$image = $this->model_tool_image->resize('no_image.jpg', 40, 40);
			}
		
	?>
	  <td><?php echo '<a href="' . $url_product . $product['id']. '&token=' . $this->session->data['token'] . '"><img src="' . $image . '"></a>' ; ?></td>
      <td class="left"><?php echo '<a href="' . $url_product . $product['id']. '&token=' . $this->session->data['token'] . '">' . $product['name'] . '</a>' ; ?></td>
      <td class="left"><?php echo $product['model']; ?></td>
	  <td class="left"><?php echo $product['sku']; ?></td>
	  <td class="left"><?php echo $product['subtract']; ?></td>
	  <?php if ($product['option_id'] == '0') { ?>
	  <td class="right"><?php echo $product['totalstock']; ?></td>
	  <?php } else { ?>
      <td class="right"><?php echo $product['stock']; ?></td>
	  <?php } ?>
      <td class="right"><?php echo number_format($product['pricesell'],2); ?></td>
      <td class="right"><?php echo number_format($product['pricebuy'],2); ?></td>
      <td class="right"><?php echo number_format($product['stockval'],2); ?></td>
      <td class="right"><?php echo number_format($product['margin'],2); ?></td>
    </tr>
    <?php } ?>
    <?php } else { ?>
    <tr class="even">
      <td class="center" colspan="8"><?php echo $text_no_results; ?></td>
    </tr>
    <?php } ?>
  </tbody>
</table>
<div class="pagination"><?php echo $pagination; ?></div>
</div>
</div>
</div>
<script type="text/javascript"><!--
function filter() {
	url = 'index.php?route=report/stock&token=<?php echo $token; ?>';
			
	var filter_category = $('select[name=\'filter_category\']').attr('value');
	
	if (filter_category) {
		url += '&filter_category=' + encodeURIComponent(filter_category);
	}
	
	var filter_calc = $('select[name=\'filter_calc\']').attr('value');
	
	if (filter_calc) {
		url += '&filter_calc=' + encodeURIComponent(filter_calc);
	}
	
	var filter_store = $('select[name=\'filter_store\']').attr('value');
	
	if (filter_store) {
		url += '&filter_store=' + encodeURIComponent(filter_store);
	}
	
	location = url;
}

function sort(column) {
	url = 'index.php?route=report/stock&token=<?php echo $token; ?>';
			
	var filter_category = $('select[name=\'filter_category\']').attr('value');
	
	var direction = $('input[name=\'direction\']').attr('value');
	
	if (filter_category) {
		url += '&filter_category=' + encodeURIComponent(filter_category) + '&sort=' + encodeURIComponent(column) + encodeURIComponent(direction);
	}
	
	location = url;
}


function exportExcel() {
	url = 'index.php?route=report/stock&token=<?php echo $token; ?>';
			
	
	var filter_store = $('select[name=\'filter_store\']').attr('value');
	if (filter_store) {
		url += '&filter_store=' + encodeURIComponent(filter_store);
	}
	
	var filter_category = $('select[name=\'filter_category\']').attr('value');
	if (filter_category) {
		url += '&filter_category=' + encodeURIComponent(filter_category);
	}
	url += '&option2=csv';
	
	location = url;
}
//--></script> 
<?php echo $footer; ?>