
<!-- BuyModal model pages  -->
<div class="modal fade"  id="BuyModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">BUY</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="row">

<div class="col-lg-12">
  <input type="hidden" id="pair" value="<?php echo $id;?>">

<div class="row">
<div class="col-lg-6">
 <h6> Position amount </h6>
</div>
<div class="col-lg-6" >
 <span id="trade_position_amount1"><?php echo $tradeupdate['total_position_amount'] ? $tradeupdate['trade_position_amount'] : 0 ;?></span> USDT
</div>
</div>

<div class="row mt-2">
<div class="col-lg-6">
<h6>Average position price</h6>
</div>
<div class="col-lg-6">
  <span id="trade_avg_position1"><?php echo $tradeupdate['trade_avg_position'] ? $tradeupdate['trade_avg_position'] : 0 ;?></span> USDT
</div>
</div>

<div class="row mt-2">
<div class="col-lg-6">
<h6> Position quantity</h6>
</div>
<div class="col-lg-6">
  <span id="trade_position_quantity"><?php echo $tradeupdate['trade_position_quantity'] ? $tradeupdate['trade_position_quantity'] : 0 ;?></span>&nbsp;&nbsp;<?php echo substr($id, 0,-4);?>
</div>
</div>

<div class="row mt-2">
<div class="col-lg-6">
<h6>Current price</h6>
</div>
<div class="col-lg-6 all_list_price">
   <?php 
  if($output['price'])
   echo $output['price'];
   else
    echo '0.00';?>
</div>
</div>

<div class="row mt-2">
<div class="col-lg-6">
<h6>Rate of return</h6>
</div>
<div class="col-lg-6">
 <span id="trade_rate_percentage2"><?php echo $tradeupdate['trade_rate_percentage'] ? $tradeupdate['trade_rate_percentage'] : "0%" ;?></span>
</div>
</div>

<hr>

<div class="row mt-2">
<div class="col-lg-6">
<h6>Estimated average position price</h6>
</div>
<div class="col-lg-6 all_list_price1">
 -
</div> 
</div>

<div class="row mt-2">
<div class="col-lg-6">
<h6>Estimated holding profit and loss</h6>
</div>
<div class="col-lg-6" id="trade_estimate_price2">
 -
</div>
</div>


<div class="col-lg-6">
<h6>Trigger Price</h6>
</div>

<div class="row mt-2">
<div class="col-lg-7">
<div class="input-group mb-4">
  <input type="text" class="form-control" id='limit_clk' placeholder="Enter Trigger's price" aria-label="Recipient's username" aria-describedby="basic-addon2">
  <div class="input-group-append">
    <button class="btn btn-primary" id="market_clk" type="button">Market Price</button>
  </div>
</div>
</div>
</div>

<div class="row mt-2">
<div class="col-lg-6">
<h6>Amount of margin call</h6>
</div>
</div>


<div class="row mt-2">
<div class="col-lg-6">
<div class="form-block mb-3 position-relative">
<input name="code1" class="form-control"   type="text" id="code1" required="" placeholder="Please Enter Purchase Amount" value="" />
</div>
</div>
</div>

</div>

</div>
</div>
<div class="modal-footer" style="padding: 0px;">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" id="buy" class="btn btn-primary">Buy</button>
</div>
</div>
</div>
</div>



<div class="modal fade" id="SellModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">SELL</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="row">

<div class="col-lg-12">
  <input type="hidden" id="pair1" value="<?php echo $id;?>">

<div class="row">
<div class="col-lg-6">
 <h6> Position amount </h6>
</div>
<div class="col-lg-6">
  <span class="trade_position_amount"><?php echo $tradeupdate['trade_position_amount'] ? $tradeupdate['trade_position_amount'] : 0;?></span> USDT
</div>
</div>

<div class="row mt-2">
<div class="col-lg-6">
<h6>Average position price</h6>
</div>
<div class="col-lg-6">
  <span class="trade_avg_position"><?php echo $tradeupdate['trade_avg_position'] ? $tradeupdate['trade_avg_position'] : 0;?></span>USDT
</div>
</div>

<div class="row mt-2">
<div class="col-lg-6">
<h6>Position quantity</h6>
</div>
<div class="col-lg-6 ">
 <span class="trade_position_quantity"><?php echo $tradeupdate['trade_position_quantity'] ? $tradeupdate['trade_position_quantity'] : 0;?></span>&nbsp;&nbsp;<?php echo substr($id, 0,-4);?>
</div>
</div>

<div class="row mt-2">
<div class="col-lg-6">
<h6>Current price</h6>
</div>
<div class="col-lg-6 all_list_price">
 <?php 
  if($output['price'])
   echo $output['price'];
   else
    echo '0.00';?>
</div>
</div>

<!-- <div class="row mt-2">
<div class="col-lg-6">
<h6>Rate of return</h6>
</div>
<div class="col-lg-6">
<span id="trade_rate_percentage1">0%</span>
</div>
</div>
 -->

 <div class="row mt-2">
<div class="col-lg-6">
<h6>Position profit and loss</h6>
</div>
<div class="col-lg-6" id="trade_estimate_price">
 <?php echo $tradeupdate['trade_estimate_price'] ? $tradeupdate['trade_estimate_price'] : " 0%";?>
</div>
</div>


<hr>



<div class="row mt-2">
<div class="col-lg-6">
<h6>Estimated selling profit</h6>
</div>
<div class="col-lg-6 estimate_sell">
</div>
</div>




<div class="col-lg-6">
<h6>Trigger Price (Sell at market price after reaching the trigger price)</h6>
</div>


<div class="row mt-2">
<div class="col-lg-7">
<div class="input-group mb-4">
  <input type="text" class="form-control" id='limit_clk1' placeholder="Enter Trigger's Price" aria-label="Recipient's username" aria-describedby="basic-addon2">
  <div class="input-group-append">
    <button class="btn btn-primary" id="market_clk1" type="button">Market Price</button>
  </div>
</div>
</div>
</div>

<div class="row mt-2">
<div class="col-lg-6">
<h6>Clearance Quantity</h6>
</div>
</div>


<div class="row mt-2">
<div class="col-lg-6">
<div class="form-block mb-3 position-relative">
<input name="code1" class="form-control clearance"   type="text" id="clearance" required="" placeholder="Please Enter Clearance Quantity in  <?php echo substr($id,0,-4);?>" value="" />
</div>
</div>
</div>

<div class="row mt-2">
<div class="col-lg-9">
<button class="btn btn-danger" id="quarter" type="button">25%</button>
<button class="btn btn-danger" id="half" type="button">50%</button>
<button class="btn btn-danger" id="onebuythree" type="button">75%</button>
<button class="btn btn-danger" id="full" type="button">100%</button>
</div>
</div>
<br/>
<div class="row mt-2">
<div class="col-lg-6">
<h6>Remaining Position Amount</h6>
</div>
<div class="col-lg-6">
 <span id="remaining_amount"><?php echo $tradeupdate['total_position_amount'] ? $tradeupdate['trade_position_amount'] : 0;?></span> USDT
</div>
<span id="err" style="color: red;"></span>
</div>

</div>

</div>
</div>
<div class="modal-footer" style="padding: 0px;">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" id="sell"  class="btn btn-primary">Sell</button>
</div>
</div>
</div>
</div>

<script type="text/javascript">
      $('#buy').on('click',function()
    {
      if($('#limit_clk').is(':disabled'))
        var val= "";
      else
        var val = $('#limit_clk').val();
      var num=  $('#code1').val();
      var id=$('#pair').val();
      var uusersid=<?php echo $this->session->get('uusersid');?>;
      var trade_process_id = <?php echo $pair->id;?>;
      
      $.ajax({
        'method': 'POST',
        'url': '<?php echo base_url("/tradingbuy");?>',
         data:{'val':val, 'num':num, 'id':id ,'uusersid':uusersid,'trade_process_id':trade_process_id},
         

        success:function(msg)
        {
          // console.log(msg);
          if(msg == 'failure')
            $.notify(' <i class="fas fa-exclamation-triangle"></i> &nbsp;&nbsp; Buy process failed. Please try again... ', 'danger');
          else if(msg == 'fail')
            $.notify(' <i class="fas fa-exclamation-triangle"></i> &nbsp;&nbsp; Trigger buy order already exist... ', 'danger');
          else
           window.location.reload();
        },
        error: function (request, message, error) {
         let  err =  handleException(request, message, error);
            if(err)
              $.notify(' <i class="fas fa-exclamation-triangle"></i>&nbsp;&nbsp; Buy process failed. Please try again... ', 'danger');
        }
      })
    });

       $('#quarter').on('click',function()
    {
       $('.estimate_sell').html("");
      var price = <?php echo $tradeupdate['trade_remain_amount'] ?  $tradeupdate['trade_remain_amount'] : 0;?>;
      var quantity = <?php echo $tradeupdate['trade_position_quantity'] ?  $tradeupdate['trade_position_quantity'] : 0;?>;
      var quantity_quarter = +quantity * 25 / 100;
      var quarter = +price * 25 / 100;
      var remain = +price - +quarter;
      $('#clearance').val(quantity_quarter);
      $('#remaining_amount').html(remain);
      var profit_loss = $('#trade_estimate_price').html();
      var position_perc = profit_loss.substring(0, profit_loss.length - 1);
      var live_price = $('.all_list_price').html();
      var est_sell_pre = quantity_quarter * live_price;
             var estimate_sell1 =  +parseFloat(price) * +parseFloat(profit_loss) / 100;
      var estimate_sell = estimate_sell1 * 1/4;
       $('.estimate_sell').html(estimate_sell.toPrecision(5));
      // var estimate_sell1 = +parseFloat(est_sell_pre) * +parseFloat(position_perc) / 100;
      // var estimate_sell = estimate_sell1 * 80/100;
      // if(position_perc > 0)
      //   $('.estimate_sell').html(estimate_sell.toPrecision(5));
      // else
      //   $('.estimate_sell').html("-"+estimate_sell.toPrecision(5));
    });


    $('#half').on('click',function()
    {
       $('.estimate_sell').html("");
      var price =<?php echo $tradeupdate['trade_remain_amount'] ?  $tradeupdate['trade_remain_amount'] : 0;?>;
      var quantity = <?php echo $tradeupdate['trade_position_quantity'] ?  $tradeupdate['trade_position_quantity'] : 0;?>;
      var quantity_quarter = +quantity * 50 / 100;
      var quarter = +price * 50 / 100;
      var remain = price - quarter;
      $('#clearance').val(quantity_quarter);
      $('#remaining_amount').html(remain);
      var profit_loss = $('#trade_estimate_price').html();
      var position_perc = profit_loss.substring(0, profit_loss.length - 1);
      var live_price = $('.all_list_price').html();
       var estimate_sell1 =  +parseFloat(price) * +parseFloat(profit_loss) / 100;
      var estimate_sell = estimate_sell1 * 1/2;
       $('.estimate_sell').html(estimate_sell.toPrecision(5));
      // var est_sell_pre = quantity_quarter * live_price;
      // var estimate_sell1 = +parseFloat(est_sell_pre) * +parseFloat(position_perc) / 100;
      // var estimate_sell = estimate_sell1 * 80/100;
      // if(position_perc > 0)
      //   $('.estimate_sell').html(estimate_sell.toPrecision(5));
      // else
      //   $('.estimate_sell').html("-"+estimate_sell.toPrecision(5));
    });


    $('#onebuythree').on('click',function()
    {
       $('.estimate_sell').html("");
        var price = <?php echo $tradeupdate['trade_remain_amount'] ?  $tradeupdate['trade_remain_amount'] : 0;?>;
      var quantity = <?php echo $tradeupdate['trade_position_quantity'] ?  $tradeupdate['trade_position_quantity'] : 0;?>;
      var quantity_quarter = +quantity * 75 / 100;
      var quarter = +price * 75 / 100;
      var remain = price - quarter;
      $('#clearance').val(quantity_quarter);
      $('#remaining_amount').html(remain);
      var profit_loss = $('#trade_estimate_price').html();
      var position_perc = profit_loss.substring(0, profit_loss.length - 1);
      var live_price = $('.all_list_price').html();
      var est_sell_pre = quantity_quarter * live_price;
      var estimate_sell1 =  +parseFloat(price) * +parseFloat(profit_loss) / 100;
      var estimate_sell = estimate_sell1 * 3/4;
 $('.estimate_sell').html(estimate_sell.toPrecision(5));
      // var estimate_sell1 = +parseFloat(est_sell_pre) * +parseFloat(position_perc) / 100;
      // var estimate_sell = estimate_sell1 * 80/100;
      // if(position_perc > 0)
      //   $('.estimate_sell').html(estimate_sell.toPrecision(5));
      // else
      //   $('.estimate_sell').html("-"+estimate_sell.toPrecision(5));
    });


    $('#full').on('click',function()
    {
       $('.estimate_sell').html("");
      var price = <?php echo $tradeupdate['trade_remain_amount'] ?  $tradeupdate['trade_remain_amount'] : 0;?>;
      $('#clearance').val(<?php echo $tradeupdate['trade_position_quantity'] ?  $tradeupdate['trade_position_quantity'] : 0;?>);
      $('#remaining_amount').html(0);
      var profit_loss = $('#trade_estimate_price').html();
      var position_perc = profit_loss.substring(0, profit_loss.length - 1);
      var live_price = $('.all_list_price').html();
    var est_sell_pre = <?php echo $tradeupdate['trade_position_quantity'] ?  $tradeupdate['trade_position_quantity'] : 0;?> * live_price;
      var estimate_sell = +parseFloat(price) * +parseFloat(profit_loss) / 100;
      // var estimate_sell = estimate_sell1 * 80/100;
      // if(profit_loss > 0)
        $('.estimate_sell').html(estimate_sell.toPrecision(5));
      // else
      //   $('.estimate_sell').html("-"+estimate_sell.toPrecision(5));
    });

    $('#clearance').on('change',function(){
      var price =  $('#clearance').val();
      var id=$('#pair1').val();
      var old_price =  <?php echo $tradeupdate['trade_remain_amount'] ?  $tradeupdate['trade_remain_amount'] : 0;?>;
      var total_quantz =  <?php echo $tradeupdate['trade_position_quantity'] ?  $tradeupdate['trade_position_quantity'] : 0;?>;
      if(price <= total_quantz){
        $('#err').html();
        var live_price = $('.all_list_price').html();
        var total_price = live_price * price ;
        var msg = old_price - total_price;
        $('#remaining_amount').html(msg);
        var profit_loss = $('#trade_estimate_price').html();
        var position_perc = profit_loss.substring(0, profit_loss.length - 1);
        var live_price = $('.all_list_price').html();
        var est_sell_pre = price * live_price;
        var estimate_sell1 = +parseFloat(est_sell_pre) * +parseFloat(position_perc) / 100;
        var estimate_sell = estimate_sell1 * 80/100;
      if(position_perc > 0)
        $('.estimate_sell').html(estimate_sell.toPrecision(5));
      else
        $('.estimate_sell').html("-"+estimate_sell.toPrecision(5));
        
     
       }else{
       $('#err').html('Your Clearance Amount is not greater than available position quantity');        
       }
    });



 $('#sell').on('click',function()
  {
      if($('#limit_clk1').is(':disabled'))
        var val= "";
      else
        var val = $('#limit_clk1').val();
      var num=  $('#clearance').val();
      var id=$('#pair1').val();
      var uusersid=<?php echo $this->session->get('uusersid');?>;
      var trade_process_id = <?php echo $pair->id;?>;

      $.ajax({
        'method': 'POST',
        'url': '<?php echo base_url("/tradingsell");?>',
         data:{'val':val,'num':num,'id':id,'uusersid':uusersid,'trade_process_id':trade_process_id},
        
        success:function(msg)
        { 
          if(msg == 'failure')
            $.notify(' <i class="fas fa-exclamation-triangle"></i>&nbsp;&nbsp; Sell process failed. Please try again... ', 'danger');
          else if(msg == 'fail')
            $.notify(' <i class="fas fa-exclamation-triangle"></i>&nbsp;&nbsp; Trigger sell order already exist... ', 'danger');
          else
           window.location.reload();
        },
        error: function (request, message, error) {
         let  err =  handleException(request, message, error);
            if(err)
              $.notify(' <i class="fas fa-exclamation-triangle"></i>&nbsp;&nbsp; Sell process failed. Please try again... ', 'danger');
        }
      })
    });

function handleException(request, message, error) {
    var msg = "";
    msg += "Code: " + request.status + "\n";
    msg += "Text: " + request.statusText + "\n";
    if (request.responseJSON != null) {
        msg += "Message: " +
        request.responseJSON.Message + "\n";
    }
    return msg;
}


</script>