<!-- Search box -->
<input type="text" id="searchClient" placeholder="Search by Client Name" class="form-control" style="width:250px; margin-bottom:10px;">
<b id="total" class="text-danger"></b>
<table class="table table-bordered table-striped">
  <thead>
    <tr>
      <th>S.No.</th>
      <th>Client</th>
      <th>Pair</th>
      <th>Buy</th>
      <th>Sell</th>
      <th>LotSize</th>
      <th><?php echo ($allt->trade_status == 'EXIT') ? 'Profit/Loss' : 'At Price'; ?></th>
      <th>TGT/SL</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    <?php $i = 1; foreach($trades as $allt){ ?>
      <tr>
        <td><?php echo $i; ?></td>
        <td class="client-name">#<?php echo $allt->trade_id.'   '.$allt->first_name; ?><br/>
      <span class="badge badge-dark"><?php echo $allt->trade_exit_time;?></span></td>
        <td><?php echo $allt->cpair; ?></td>
        <td><?php echo $allt->buy_price; ?></td>
        <td><?php echo $allt->sell_price; ?></td>
        <td><?php echo $allt->trade_type.' '.$allt->lotsize.' Lot @'.$allt->trade_price; ?></td>
        <td><?php echo ($allt->trade_status == 'EXIT') ? $allt->profit_loss : $allt->trade_price; ?></td>
        <td><?php echo 'TGT: '.$allt->target.'<br/>SL: '.$allt->stoploss; ?></td>
        <td>
          <button id="<?php echo $allt->trade_id; ?>" type="button" class="btn btn-sm btn-danger deletetr">DELETE</button>
        </td>
      </tr>
    <?php $i++; } ?>
  </tbody>
</table>

<script>
// DELETE ROW + AJAX CALL
$(".deletetr").click(function(){
  var id = $(this).attr("id");
  if (!confirm("Sure you want to remove this trade?")) {
    return false;
  }
  $(this).closest("tr").remove();
  $.ajax({
    type: "POST",
    url: "<?php echo base_url(); ?>home/ajax_delete_trade",
    data: { id: id },
    success: function(res) {
      console.log("Deleted:", res);
    }
  });
});

// CLIENT NAME SEARCH
$("#searchClient").on("keyup", function() {
  var value = $(this).val().toLowerCase();
  $("table tbody tr").filter(function() {
    $(this).toggle($(this).find(".client-name").text().toLowerCase().indexOf(value) > -1);
  });
  var totalRecordsFound = $("table tbody tr:visible").length;
  $("#total").text(totalRecordsFound+' Orders');
});
</script>
