<html lang="en">
<?php 
$validation =  \Config\Services::validation();
$this->db = db_connect();

$this->session = session();
$uusersid = $this->session->get('uusersid');
$referral1=$this->db->query("SELECT * FROM bind_process WHERE uusersid='".$uusersid."'")->getResult();
$ref=$referral1[0];
$key = $ref->api_key;
$secret = $ref->secret_key;
if(empty($ref)){
  $key = "wcYWabBn1sHWzvVmYjwQrF8JMbh8ngCUW3oA1IbEJrghBgqmB1U9BMMQUGmU42hv";
$secret = "1cSiCLlNnfKhcaX0wyIhXB1QLFtckdzKxR7Lqq2TlB0CQB31xdNoeGbwx39NjDra";
$api = new Binance\API($key,$secret,true);
}else{
   $api = new Binance\API($key,$secret);
   $account = $api->accountStatus();
   if($account['sapi']['code'] == "-2008" || $account['sapi']['code'] == "-1022" || $account['sapi']['code'] == "-2015")
    {
        $key = "tJuR6dN9AZp9KiAn41avxq64KsyDxa8FIaRjxTf0k1qplZIv1MRnHrS3zQIqHuzZ";
        $secret = "GlRLQS2VGYfa1sOUaS3NIgGmszJOAuxu7Xk0anY1r4ifRmGYVYxoG25LOBnESE67";
      $api = new Binance\API($key,$secret,true);
    }
 }
echo view('user/common_style1');?>
</head>
<body>
  <?php echo view('user/common_header');?>
  <link rel="stylesheet" href="<?php echo base_url('styles/css/table.css');?>">
  <style type="text/css">
    .table-responsive {
      overflow-x: visible !important;
    }
    table tr th {
      background: #203c9d none repeat scroll 0 0;
      color: #fff;
      font-size: 13px;
      font-weight: 300;
      padding: 16px 20px;
      text-align: left;
      text-transform: capitalize;
      white-space: nowrap;
    }
    @media only screen and (max-width:768px){
    #table_example_wrapper{
       overflow: auto !important;
       overflow-y: hidden !important;
    }
   }
    input[type="text"], input[type="email"], input[type="password"], input[type="search"] {
      background-color: #fff;
      border: 1px solid #e1e1e1;
      color: #000;
      font-size: 14px;
      height: 42px;
      margin: 5px 0;
      padding: 10px 7px 10px 10px;
      width: 100%;
    }
    #table_example_filter
    {
      background: #f3f4f6;
    }
  </style>

  <section class="trading-bg">
    <div class="container-fluid">
      <div class="row">
        <div class="col-lg-12">
          <div class="inner-title pt-4 pb-4">
            <h6 class="color-2"><i class="far fa-compass"></i> Trading Records</h6>
          </div>
          <div class="row">
            <div class="col-lg-2">
              <div class="trading-tab">
                <ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
                  <li class="nav-item" role="presentation">
                    <button class="nav-link active" id="pills-home-tab" data-bs-toggle="pill" data-bs-target="#pills-home" type="button" role="tab" aria-controls="pills-home" aria-selected="true">Binance</button>
                  </li>
                </ul>
              </div></div>

              <div class="col-lg-3">
               <div class="trading-tab">
                <ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
                 <li class="nav-item" role="presentation">
                    <button class="nav-link active" id="pills-profile-tab" data-bs-toggle="pill" data-bs-target="#pills-home" type="button" role="tab" aria-controls="pills-profile" aria-selected="false">All</button>
                  </li>
                  <li class="nav-item" role="presentation">
                    <button class="nav-link" id="pills-sell-tab" data-bs-toggle="pill" data-bs-target="#pills-sell" type="button" role="tab" aria-controls="pills-sell" aria-selected="false">Sell</button>
                  </li>
                  <li class="nav-item" role="presentation">
                    <button class="nav-link" id="pills-buy-tab" data-bs-toggle="pill" data-bs-target="#pills-buy" type="button" role="tab" aria-controls="pills-buy" aria-selected="false">Buy</button>
                  </li>
                </ul>
              </div>
            </div>
            <div class="col-lg-7">
              <div class="row">
                <div class="col-lg-4">

                </div>

                <div class="col-lg-8">

     <!--   <div class="trading-date">
       <input type="text" id="id_search" name="datetimes" />
     </div> -->


     <div class="select-trans">
      <select id="target" class="form-select mb-3" aria-label="Default select example"  onchange="if (this.value) window.location.href= '<?php echo base_url() ?>/orders/' + this.value">
        <option value="">Select Pair</option>
        <?php

        $marketpair = $api->prevDay();


        $i=0;
$required_pairs = ['1INCH','ADA','AAVE','ATOM','ALGO','AION','AVAX','AKRO','ANT','APE','BAT','BTC','BCH','BNB','BTCST','BAKE','CRV','COMP','CAKE','COS','DOGE','DASH','DOT','ETH','EOS','ETC','FTM','FTT','FIL','GALA','GRT','GMT','HBAR','HOT','IOTA','ICP','IOST','JST','KAVA','KSM','LTC','LINK','LIT','LUNA','MANA','MATIC','MDX','NEO','OMG','ONT','QTUM','REEF','RSR','SUN','SHIB','SLP','SOL','SUSHI','SXP','TRX','THETA','TFUEL','UNI','VET','WRX','XRP','XMR','XEM','XLM','XTZ','YFI','YFII','ZEC','ZEN'];
        foreach ($marketpair as $key => $value) {
          if(substr($value['symbol'], -4) == "USDT"){
            if(in_array(substr_replace($value['symbol'] ,"",-4), $required_pairs)){
              $output[$i]['symbol'] = $value['symbol'];
              ?>
              <option <?php if($filter == $output[$i]['symbol']){ echo "selected";} ?> value="<?php echo $output[$i]['symbol']; ?>"><?php echo   $output[$i]['symbol']; ?></option>
              <?php

            }
            $i++;
          }
        }

        ?>

            <!--   <option selected="">ADAUSDT</option>
              <option value="1">ATOMUSDT</option>
              <option value="2">BATUSDT</option>
              <option value="3">BTCUSDT</option>
              <option value="4">BNBUSDT</option> -->
            </select>
          </div>
        </div>
        
      </div>
    </div>


    <div class="tab-content trading-mar" id="pills-tabContent">
      <div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">
        <div class="trading-table bg-white">
          <div class="table-responsive main-table">
            <table class="table-striped" id="table_example">
              <thead>
                <tr>
                  <th> S.No </th>
                  <th> Trading pair </th>
                  <th> Buy in/Sell </th>
                  <th> Time </th>
                  <th> Order Number </th>
                  <!-- <th> Type </th> -->
                  <th> Quantity </th>
                            <th> Average buyin price (USDT)</th>
                            <th> profit(USDT) </th>
                            <th> profit(%) </th>
                </tr>
              </thead>
              <tbody>
                <?php
                $i=0;

                if ($sell_records) {

                        foreach ($sell_records as $key => $value) { $i++;
                          if (!empty($value)) {
                              if($value->subsell_status == 1){
                                 $buy_sell_status = 'SUB-SELL';
                                        $avg_trans_price = $value->live_price - $value->avg_price;
                              }

                               else{ 

                                        if($value->description == "manual_buy"){
                                           $buy_sell_status = "Manual-Buy";
                                        }else if($value->description == "manual_sell" ){
                                          $buy_sell_status = "Manual-Sell";
                                        }else{
                                          if($value->type == 1)
                                              $buy_sell_status = "Buy";
                                          else
                                              $buy_sell_status = "Sell";
                                      }
                                        // if($sell1['type'] == "TAKE_PROFIT_LIMIT" || $sell1['type'] == "STOP_LOSS_LIMIT")
                                        // $avg_trans_price = $value->avg_price;
                                        // else
                                          $avg_trans_price = $value->live_price;
                                    }

                                   
                        
                                  
                             

                        if($value->type == 1)
                        $class = "success";
                        else
                        $class = "fail";

                            // if($sell1['type'] == "LIMIT")
                            //   $type = "Limit Price";
                            // else
                            //   $type = "Market Price";
                            //print_r($sell);

                  
                      ?>
                     <tr>
                      <td class="text-center"><?php echo $i; ?></td>
                              <td><img height="20" width="20" src="<?php echo base_url('styles/images/tradingpair_images/'.$value->pair.'.png');?>" ><?php echo " ".$value->pair;?></td>
                              <td><span class="<?php echo $class;?>"> <?php echo $buy_sell_status;?> </span></td>
                              <td><?php echo date('Y-m-d h:i:s',substr($value->time,0,-3));?></td>
                               <td><button type="button" class="btn btn-outline-success" onclick="orderModal(<?php echo $value->order_id;?>,'<?php echo $value->pair;?>');" data-toggle="modal" data-target="#exampleModal5" id="orderModal" style="padding: 10px 17px 7px 13px !important;"><span><?php echo $value->order_id;?></span></button></td>
                              <!-- <td><?php echo $type;?></td> -->
                              <td><?php echo $value->quantity;?></td>
                              <td><?php if($avg_trans_price > 0){ echo $avg_trans_price;}else{ echo "---";}?></td>
                              <td><?php if($value->type == 2){ echo $value->avg_price; }else{ echo '---';} ?></td>
                              <td> <?php if($value->type == 2){ if($value->subsell_status == '1'){ echo $value->profit_percentage;}elseif($value->subsell_status == '' || $value->subsell_status == '0'){ echo $value->profit_percentage; }else{ echo "---"; }}else{ echo "---";} ?> </td>
                            </tr>
                    <?php } }  } ?>

                  </tbody>
                </table>
              </div>
            </div>
          </div>
          <div class="tab-pane fade" id="pills-sell" role="tabpanel" aria-labelledby="pills-profile-tab">
            <div class="trading-table bg-white">
              <div class="table-responsive main-table">
                <table class="table-striped" id="table_example">
                  <thead>
                    <tr>
                      <th> S.No </th>
                      <th> Trading pair </th>
                      <th> Buy in/Sell </th>
                      <th> Time </th>
                      <th> Order Number </th>
                      <!-- <th> Type </th> -->
                       <!-- <th> Order Number </th> -->
                  <!-- <th> Type </th> -->
                  <th> Quantity </th>
                            <th> Average buyin price (USDT)</th>
                            <th> profit(USDT) </th>
                            <th> profit(%) </th>
                    </tr>
                    <tbody>
                      <?php 
                      $i=0;
                      if ($sell) {

                        foreach ($sell as $key => $value) { $i++;
                          if (!empty($value)) {
                                          if($value->subsell_status == 1){
                                 $buy_sell_status = 'SUB-SELL';
                                        $avg_trans_price = $value->live_price - $value->avg_price;
                              }

                               else{ 

                                        if($value->description == "manual_buy"){
                                           $buy_sell_status = "Manual-Buy";
                                        }else if($value->description == "manual_sell" ){
                                          $buy_sell_status = "Manual-Sell";
                                        }else{
                                         if($value->type == 1)
                                              $buy_sell_status = "Buy";
                                          else
                                              $buy_sell_status = "Sell";
                                      }
                                        // if($sell['type'] == "TAKE_PROFIT_LIMIT" || $sell['type'] == "STOP_LOSS_LIMIT")
                                        // $avg_trans_price = $value->avg_price;
                                        // else
                                          $avg_trans_price = $value->live_price;
                                    }

                        if($value->type == 1)
                        $class = "success";
                        else
                        $class = "fail";


                            // if($sell['type'] == "LIMIT")
                            //   $type = "Limit Price";
                            // else
                            //   $type = "Market Price";
                            //print_r($sell);

                            ?>
                            <tr>
                               <td class="text-center"><?php echo $i; ?></td>
                              <td><img height="20" width="20" src="<?php echo base_url('styles/images/tradingpair_images/'.$value->pair.'.png');?>" ><?php echo " ".$value->pair;?></td>
                              <td><span class="<?php echo $class;?>"> <?php echo $buy_sell_status;?> </span></td>
                              <td><?php echo date('Y-m-d h:i:s',substr($value->time,0,-3));?></td>
                               <td><button type="button" class="btn btn-outline-success" onclick="orderModal(<?php echo $value->order_id;?>,'<?php echo $value->pair;?>');" data-toggle="modal" data-target="#exampleModal5" id="orderModal" style="padding: 10px 17px 7px 13px !important;"><span><?php echo $value->order_id;?></span></button></td>
                              <!-- <td><?php echo $type;?></td> -->
                              <td><?php echo $value->quantity;?></td>
                              <td><?php if($avg_trans_price > 0){ echo $avg_trans_price;}else{ echo "---";}?></td>
                              <td><?php if($value->type == 2){ echo $value->avg_price; }else{ echo '---';} ?></td>
                              <td> <?php if($value->type == 2){ if($value->subsell_status == '1'){ echo $value->profit_percentage;}elseif($value->subsell_status == '' || $value->subsell_status == '0'){ echo $value->profit_percentage; }else{ echo "---"; }}else{ echo "---";} ?> </td>
                            </tr>
                            <?php
                          }
                        }

                      }

                      ?>



                    </tbody>
                  </table>
                </div>
              </div>
            </div>
            <div class="tab-pane fade" id="pills-buy" role="tabpanel" aria-labelledby="pills-profile-tab">
              <div class="trading-table bg-white">
                <div class="table-responsive  main-table">
                  <table class="table-striped" id="table_example">
                    <thead>
                      <tr>
                        <th>S.No</th>
                        <th> Trading pair </th>
                        <th> Buy in/Sell </th>
                        <th> Time </th>
                        <th> Order Number </th>
                        <!-- <th> Type </th> -->
                       <th> Quantity </th>
                            <th> Average buyin price (USDT)</th>
                            <th> profit(USDT) </th>
                            <th> profit(%) </th>
                      </tr>
                      <tbody>
                        <?php 
                      if ($buy) {
                         $i=0;
                        foreach ($buy as $key => $value) {  $i++;
                          
                          if ($value->order_id) {
                            
                          // print_r($value->order_id);
                          // exit();
                          
                          // print_r($buy);
                          if (!empty($value)) {

                            if($value->subsell_status == 1){
                                 $buy_sell_status = 'SUB-SELL';
                                        $avg_trans_price = $value->live_price - $value->avg_price;
                              }

                               else{ 

                                        if($value->description == "manual_buy"){
                                           $buy_sell_status = "Manual-Buy";
                                        }else if($value->description == "manual_sell" ){
                                          $buy_sell_status = "Manual-Sell";
                                        }else{
                                        if($value->type == 1)
                                              $buy_sell_status = "Buy";
                                        else
                                              $buy_sell_status = "Sell";
                                      }
                                        // if($buy['type'] == "TAKE_PROFIT_LIMIT" || $buy['type'] == "STOP_LOSS_LIMIT")
                                        // $avg_trans_price = $value->avg_price;
                                        // else
                                          $avg_trans_price = $value->live_price;
                                    }

                            //       if($buy['type'] == "LIMIT")
                            //   $type = "Limit Price";
                            // else
                            //   $type = "Market Price";
                            //print_r($sell);
                           

                            // if($buy['type'] == "LIMIT")
                            //   $type = "Limit Price";
                            // else
                            //   $type = "Market Price";
                            //print_r($sell);

                            ?>
                            <tr>
                              <td class="text-center"><?php echo $i;?></td>
                              <td><img height="20" width="20" src="<?php echo base_url('styles/images/tradingpair_images/'.$value->pair.'.png');?>" ><?php echo " ".$value->pair;?></td>
                              <td><span class="<?php echo 'success';?>"> <?php echo $buy_sell_status;?> </span></td>
                              <td><?php echo date('Y-m-d h:i:s',substr($value->time,0,-3));?></td>
                    <td><button type="button" class="btn btn-outline-success" onclick="orderModal(<?php echo $value->order_id;?>,'<?php echo $value->pair;?>');" data-toggle="modal" data-target="#exampleModal5" id="orderModal" style="padding: 10px 17px 7px 13px !important;"><span><?php echo $value->order_id;?></span></button></td>
                              <!-- <td><?php echo $type;?></td> -->
                              <td><?php echo $value->quantity;?></td>
                              <td><?php if($avg_trans_price > 0){ echo $avg_trans_price;}else{ echo "---";}?></td>
                              <td><?php if($value->type == 2){ echo $value->avg_price; }else{ echo '---';} ?></td>
                              <td> <?php if($value->type == 2){ if($value->subsell_status == '1'){ echo $value->profit_percentage;}elseif($value->subsell_status == '' || $value->subsell_status == '0'){ echo $value->profit_percentage; }else{ echo "---"; }}else{ echo "---";} ?> </td>
                            </tr>
                            <?php
                          }
                        }

                      }
                    }
                      ?>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>

<div class="modal fade" id="exampleModal5" 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">Order Details</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
       <div id="data_store"> </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

<script type="text/javascript">
  function orderModal(id,symbol){
    jQuery.ajax({
                    method: 'POST',
                    data: {'id':id,'uusersid':"<?php echo $uusersid;?>","pair":symbol},
                    url: '<?php echo base_url("/orderDetails");?>',
                    success:function(resp) {
                      console.log(resp);
                         jQuery('#data_store').html(resp);
                         jQuery('#exampleModal5').modal('show');
                    }
                });
  }
</script>

<script src="<?php echo base_url()?>/styles/new_style/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script> 



    <?php
    echo view('user/footer1');
    ?> 

    <script type="text/javascript" src="<?php echo base_url('/styles/js/datatables.min.js');?>"></script>

    <script type="text/javascript">

      $(document).ready(function(){
        $('.main-table table').dataTable({
          "language": { search: "" },
          "Info": false
        });
      });


    </script>

    <script>
      $(document).ready(function(){
        $('input#id_search').quicksearch('table#table_example tbody tr');
      });

      $(function() {
        $('input[name="datetimes"]').daterangepicker({
          timePicker: true,
          startDate: moment().startOf('hour'),
          endDate: moment().startOf('hour').add(32, 'hour'),
          locale: {
            format: 'M/DD hh:mm A'
          }
        });
      });
    </script>


    <script>
      wow = new WOW(
      {
        animateClass: 'animated',
        offset:       100,
        callback:     function(box) {
          console.log("WOW: animating <" + box.tagName.toLowerCase() + ">")
        }
      }
      );
      wow.init();
      document.getElementById('moar').onclick = function() {
        var section = document.createElement('section');
        section.className = 'section--purple wow fadeInDown';
        this.parentNode.insertBefore(section, this);
      };
    </script>
    <script type="text/javascript">
      $( "#target" ).change(function() {


          var url = <?php echo base_url();?>; // get selected value
            
          var value = $('#target').val();

         

            window.location.href =  url + value; 
      });
    </script>


    </html>