<!DOCTYPE html>
<html lang="en">
   <head>
      <!-- Main navbar -->
      <?php 
        use App\Libraries\MyEncrypt;
        use App\Helpers\site_helper;
        use App\Helpers\form_helper;
        $this->db = db_connect();      
            
         $this->form_validation =  \Config\Services::validation();
         $this->finance_model = model('finance_model');
         $this->finance_model = model('CommonModel');
         //use App\Libraries\MyEncrypt;
         $session = session();
        $this->uri = service('uri');
        echo view('admin/common_header'); 
         
        $admin_login=$session->get('userid');
		$get_admin=$this->db->query("select * from admin where id='".$admin_login."'")->getRow();
		$user_group = $get_admin->user_group;
						

         
         ?>
      <!-- /main navbar -->
    
      <!-- date picker JS files -->
      <script type="text/javascript" src="<?php echo base_url('assets/js/core/libraries/jquery_ui/interactions.min.js');?>"></script>
      <script type="text/javascript" src="<?php echo base_url('assets/js/core/libraries/jquery_ui/widgets.min.js');?>"></script>
      <script type="text/javascript" src="<?php echo base_url('assets/js/core/libraries/jquery_ui/effects.min.js');?>"></script>
      <script type="text/javascript" src="<?php echo base_url('assets/js/plugins/extensions/mousewheel.min.js');?>"></script>
      <script type="text/javascript" src="<?php echo base_url('assets/js/core/libraries/jquery_ui/globalize/globalize.js');?>"></script>
      <!-- select box theme JS files -->
      <script type="text/javascript" src="<?php echo base_url('assets/js/plugins/forms/selects/bootstrap_select.min.js');?>"></script>
      <!-- Theme JS files -->
      <script type="text/javascript" src="<?php echo base_url('assets/js/plugins/tables/datatables/datatables.min.js');?>"></script>
    
      <script type="text/javascript" src="<?php echo base_url('assets/js/plugins/forms/selects/select2.min.js');?>"></script>
      <script type="text/javascript" src="<?php echo base_url('assets/js/core/app.js');?>"></script>
      <script type="text/javascript" src="<?php echo base_url('assets/js/pages/datatables_responsive.js');?>"></script>
      <!-- /theme JS files -->
      <!-- notify JS files -->
      <script type="text/javascript" src="<?php echo base_url('assets/js/plugins/notifications/jgrowl.min.js');?>"></script>
      <!-- Theme JS files -->
      <script type="text/javascript" src="<?php echo base_url('assets/js/plugins/notifications/bootbox.min.js');?>"></script>
      <script type="text/javascript" src="<?php echo base_url('assets/js/plugins/notifications/sweet_alert.min.js');?>"></script>

	  <script type="text/javascript" src="<?php echo base_url('assets/js/formValidation.js');?>"></script>
	  <script type="text/javascript" src="<?php echo base_url('assets/js/bootstrap.js');?>"></script>
     <script type="text/javascript" src="<?php echo base_url();?>/assets/js/plugins/notifications/jgrowl.min.js"></script>
      <style type="text/css">
         .icons-list a[data-action="reload1"]:after {
         content: '\e9fb';
         }
         .btns {
         padding: 5px;
         }

         .dataTables_filter { display:none;}

         .green { color:green;}
    .blue { color:blue;}
    .orange { color:orange;}
    .red { color:red;}
     .ui-menu {
      min-height:230px;
      overflow:scroll;
    }
      </style>
   </head>
   <body class="navbar-top-md-md pace-done">
      <!-- Fixed navbars wrapper -->
      <div class="navbar-fixed-top">
         <!-- Main navbar -->
         <?php echo view('admin/mainnav'); ?>
         <!-- /main navbar -->
         <!-- Second navbar -->
         <?php echo view('admin/secondnav'); ?>
         <!-- /second navbar -->
      </div>
      <!-- /fixed navbars wrapper -->
      <!-- Page header -->
      <?php echo view('admin/'.$module_name.'_header'); ?>
      <!-- /page header -->
      <!-- Page container -->
      <div class="page-container">
         <!-- Page content -->
         <div class="page-content">
            <!-- Main content -->
            <div class="content-wrapper">
               <!-- Basic responsive configuration -->
               <div class="panel panel-flat">
                  <div class="panel-heading">
                     <h5 class="panel-title"><?php echo $page_title; ?></h5>
                     <div class="heading-elements">
                        <ul class="icons-list">
                           <!-- <li><a href="<?php echo base_url();?>admin/users/add" class="glyphicon-plus"></a></li> -->
                           <li><a data-action="collapse"></a></li>
                           <li><a data-action="reload"></a></li>
                           <li><a data-action="close"></a></li>
                        </ul>
                     </div>
                  </div>
                  <?php $currency_info = defaultcurrency();?>
                  <div class="panel-body">

                     <form class="form-horizontal export validateDontSubmit" name="frmexport" id="frmexport" action="<?php echo base_url('admin/bot/activate-list');?>" method="post">
                       
                     
                     <div class="form-group">
                                <label class="control-label col-lg-2 text-semibold">Select username</label>
                                <div class="col-lg-4">
                                    <div style="width:100%;" class="input-group">
                                            <select class="form-control" name="user_group" id="user_group">
                                                <option value='' >All</option>
                                                <?php 
                                                   if($user_group=='0')
                                                   {	 
                                                      ?>
                                                      <option value="0" <?php if($user_type=='0') { echo "selected==selected";}?>>Group A</option>
                                                      <option value="1" <?php if($user_type=='1') { echo "selected==selected";}?>>Group B</option>
                                                   <?php
                                                   }
                                                   ?>
                                                    <?php 
                                                         if($user_group=='1')
                                                         {	 
                                                      ?>
                                                      <option value="0" <?php if($user_type=='0') { echo "selected==selected";}?>>Group A</option>
                                                      <?php
                                                      }
                                                      ?>
                                                      <?php 
                                                               if($user_group=='2')
                                                               {	 
                                                            ?>
                                                      <option value="1" <?php if($user_type=='1') { echo "selected==selected";}?>>Group B</option>
                                                      <?php
                                                      }
                                                      ?>
                                          
                                            </select>
                                    </div>
                                </div>
                        </div>

                     <div class="form-group ">
                          
                           <div class="col-lg-3">
                              <label class="control-label text-semibold"><?php echo lang('language_lang.lbl_date_from'); ?> </label>
                              <div class="input-group">
                                 <span class="input-group-addon"><i class="icon-calendar22"></i></span>
                                 <input type="text" class="form-control start_date" id="start_date" placeholder="<?php echo lang('language_lang.place_start_date'); ?>" name="start_date" value="<?php echo $fromdate;?>">
                              </div>
                           </div>
                           <div class="col-lg-3">
                              <label class="control-label text-semibold"><?php echo lang('language_lang.lbl_date_to'); ?> </label>
                              <div class="input-group">
                                 <span class="input-group-addon"><i class="icon-calendar22"></i></span>
                                 <input type="text" class="form-control end_date" id="end_date" placeholder="<?php echo lang('language_lang.place_end_date'); ?>" name="end_date" value="<?php echo $todate;?>">
                              </div>
                           </div>

                        </div>


                       
                        <div class="form-group">
                                <label class="control-label col-lg-2 text-semibold">Username</label>
                                <div class="col-lg-4">
                                    <div style="width:100%;" class="input-group">
                                          <input type="text" placeholder="Type to Filter..." name="username" id="username" class="form-control" value="<?php echo $user;?>">
                                           <input type="hidden" name="code" id="code" >
                                    </div>
                                </div>
                        </div>

                        <div class="form-group">
                                <label class="control-label col-lg-2 text-semibold">Filter by Pairs</label>
                                <div class="col-lg-4">
                                    <div style="width:100%;" class="input-group">
                                            <select class="form-control" name="symbols">
                                            <option value=''>None Selected</option>

                                                <?php 
                                                if($tradepair)
                                                {
                                                    foreach($tradepair as $row1)
                                                    {
                                                ?>
                                                <option value="<?php echo $row1->pair_name.'USDT';?>" <?php if($pairs==$row1->pair_name.'USDT') { echo "selected";}?>><?php echo $row1->pair_name;?></option>
                                                
                                                <?php } }?>
                                            </select>
                                    </div>
                                </div>
                        </div>

                        <div class="form-group">
                                <label class="control-label col-lg-2 text-semibold">Filter by Status<span class="text-danger">*</span></label>
                                <div class="col-lg-4">
                                    <div style="width:100%;" class="input-group">
                                            <select class="form-control" name="status">
                                                <option value=''>None Selected</option>
                                                <option  value="1" <?php if($status=='1') { echo "selected";}?>>Active</option>
                                                <option value="0" <?php if($status=='0') { echo "selected";}?>>Pause</option>  
                                            </select>
                                    </div>
                                </div>
                        </div>
                        <center>
                        <input type="submit" value="Filter Trades" class="btn btn-primary">
                     </center>
                       <?php
                  
                        function get_tradeinfo($symbol,$live_price,$userid)
                        {
                           $price_down='0';
                           $CI = db_connect();
                           $CI->session = session();

                           if(!empty($CI->session->get('exchange')))
                           $exchange=$CI->session->get('exchange');
                        else
                           $exchange='1';


                     
                     
                  // $CI =db_connect();
                     $uusersid = $userid;
                  
                     $trade_process =$CI->query("SELECT * FROM trade_process WHERE pair='".$symbol."' and uusersid='".$uusersid."' and exchange ='".$exchange."'  and trade_status != 2")->getRow();
                     
                     if(!$trade_process){
                        $trade_process =$CI->query("SELECT * FROM user_strategy WHERE uusersid='".$uusersid."'")->getRow();
                        
                     }
                     $pair =$CI->query("SELECT * FROM trade_process WHERE uusersid='".$uusersid."' and pair ='".$symbol."' and trade_status != 2 and exchange ='".$exchange."' order by id desc")->getRow();
                     if(!empty($pair))
                     {
                        $sync_strategy_status = $pair->sync_strategy_status ? $pair->sync_strategy_status : 0;
                        $manual_buy = $CI->table('api_manual_trade')->where('trade_process_id',$pair->id)->where('type',1)->where('exchange',$exchange)->where('status','1')->get()->getRow();
                        $manual_sell = $CI->table('api_manual_trade')->where('trade_process_id',$pair->id)->where('type',2)->where('exchange',$exchange)->where('status','1')->get()->getRow();
                        $pair_trade_status=$pair->trade_status;
                     }
                     else
                     {
                        $sync_strategy_status='0';
                        $pair_trade_status='';
                     }
                     
                     $qury = $CI->query("SELECT * FROM trade_process WHERE uusersid='".$uusersid."' AND  pair = '".$symbol."' and trade_status != 2 and exchange='".$exchange."' ")->getResult();
                     if(!empty($qury))
                     {
                     $qry=$qury[0];
                     $qury = $CI->query("SELECT * FROM user_strategy WHERE uusersid='".$uusersid."'")->getResult();
                     $qry=$qury[0];
                     $first_buy_amount=$qry->first_buy_amount;
                     $open_position_doubled=$qry->open_position_doubled;
                     $margin_column_limit=$qry->margin_column_limit;
                     $whole_profit_ratio=$qry->whole_profit_ratio;
                     $whole_profit_callback=$qry->whole_profit_callback;
                     $buyin_callback=$qry->buyin_callback;
                     $sub_position_callback=$qry->sub_position_callback;
                     $strategy_mode=$qry->strategy_mode;
                     
                     
                     }
                     else
                     {
                     
                     $qry='';
                     $qury = $CI->query("SELECT * FROM user_strategy WHERE uusersid='".$uusersid."'")->getResult();
                     $qry=$qury[0];
                     
                     $first_buy_amount=$qry->first_buy_amount;
                     $open_position_doubled=$qry->open_position_doubled;
                     $margin_column_limit=$qry->margin_column_limit;
                     $whole_profit_ratio=$qry->whole_profit_ratio;
                     $whole_profit_callback=$qry->whole_profit_callback;
                     $buyin_callback=$qry->buyin_callback;
                     $sub_position_callback=$qry->sub_position_callback;
                     $strategy_mode=$qry->strategy_mode;
                     }
                     if(!empty($pair)) 
                     { 
                     
                     
                           $api_trade_data = array(
                           'trade_process_id' => $pair->id,
                           'uuserid' => $uusersid,
                           'type' => '1',
                           'trade_close' => 0,
                           'exchange' => $exchange
                           );
                     
                           $last_call_drop_data = array(
                              'trade_process_id' => $pair->id,
                              'uuserid' => $uusersid,
                              'exchange' => $exchange
                           );
                     
                           $api_trade_data1 = array(
                              'trade_process_id' => $pair->id,
                              'uuserid' => $uusersid,
                              'type' => '2',
                              'description' => 'manual_sell',
                              'exchange' => $exchange,
                              'status' => 0
                           );
                     
                           $api_trade_data2 = array(
                              'trade_process_id' => $pair->id,
                              'uuserid' => $uusersid,
                              'type' => '1',
                              'status' => 0,
                              'exchange' => $exchange,
                              'margin_call_drops = ' => 0
                           );
                     
                     
                           $profit_where = array(
                              'uuserid' => $uusersid,
                              'type' => '3',
                              'trade_process_id' => $pair->id,
                              'exchange' => $exchange,
                              'status' => 0
                           );
                           
                          
                     
                     
                           if($pair->strategy_mode == 1)
                           {
                                 $total_contract = $CI->table('api_trade_history')->selectSum('received_amount')->where($api_trade_data)->get()->getRow();
                                 $total_usdt  = $CI->table('api_trade_history')->selectSum('quantity')->where($api_trade_data)->get()->getRow();
                                 if($total_contract->received_amount && $total_usdt->quantity)
                                    $avg_position_amount = $total_usdt->quantity / $total_contract->received_amount;
                                 else
                                    $avg_position_amount = 0;
                           }
                           else
                           {
                                 if($pair->strategy_mode == 3) 
                                 {
                        
                                 $total_contract = $CI->table('api_trade_history')->selectSum('received_amount')->where($api_trade_data)->where('margin_call_drops <=',4)->get()->getRow();
                                 $total_usdt = $CI->table('api_trade_history')->selectSum('quantity')->where($api_trade_data)->where('margin_call_drops <=',4)->get()->getRow();
                                 if($total_contract->received_amount && $total_usdt->quantity)
                                    $avg_position_amount = $total_usdt->quantity / $total_contract->received_amount;
                                 else
                                    $avg_position_amount = 0;
                              }else{
                                 $total_contract = $CI->table('api_trade_history')->selectSum('received_amount')->where($api_trade_data)->where('margin_call_drops <=',4)->get()->getRow();
                                 $total_usdt = $CI->table('api_trade_history')->selectSum('quantity')->where($api_trade_data)->where('margin_call_drops <=',4)->get()->getRow();
                                 $total_contract2 = $CI->table('api_trade_history')->selectSum('received_amount')->where($profit_where)->get()->getRow();
                                 $total_contract = $total_contract->received_amount + $total_contract2->received_amount;
                                 $total_usdt2 =$CI->table('api_trade_history')->selectSum('quantity')->where($profit_where)->get()->getRow();
                                 $total_usdt = $total_usdt->quantity + $total_usdt2->quantity;
                                 if($total_contract && $total_usdt)
                                    $avg_position_amount = $total_usdt / $total_contract;
                                 else
                                    $avg_position_amount = 0;
                              }
                        }
                     
                        $last_call_drop = $CI->table('api_trade_history')->where($last_call_drop_data)->orderBy('id','DESC')->get()->getRow();
                        $last_average = $avg_position_amount;
                        $avg_price = round($last_average,8) * $pair->whole_profit_ratio / 100;
                        $max_increase_price = $last_average + $avg_price ;
                        $max_increase_price = round($max_increase_price,8);
                        $max_down_percentage = $max_increase_price * $pair->whole_profit_callback / 100;
                        $max_down_price = $max_increase_price - $max_down_percentage;
                        $max_down_price = round($max_down_price,8);
                        if(intval($max_increase_price) > 999)
                           $max_increase_price = number_format($max_increase_price,2);
                        else
                           $max_increase_price = number_format($max_increase_price,8);
                                 
                        if(!empty($last_call_drop))
                        {
                           if($last_call_drop->type == 2)
                                 $max_increase_price = $last_call_drop->live_price + $price_down;
                        }

                           $tradeupdate['take_profit_trigger'] = $max_increase_price ? "> ".$max_increase_price : "";
                           $trade_where_new = array(
                           'uuserid' => $uusersid,
                           'type' => '1',
                           'status' => 0,
                           'trade_process_id' => $pair->id,
                           );
                           
                           $last_buy_recheck = $CI->table('api_trade_history')->where($trade_where_new)->orderBy('margin_call_drops','DESC')->get()->getRow();
                           if(!empty($last_buy_recheck))
                              $last_margin_call_drop = $last_buy_recheck->margin_call_drops ? $last_buy_recheck->margin_call_drops : 0;
                           else
                              $last_margin_call_drop='0';

                           
                           $call_perc = explode(',', $pair->margincall_drop);
                           if(!empty($call_perc[$last_margin_call_drop]))
                              $call_perc=$call_perc[$last_margin_call_drop];
                           else
                              $call_perc='0';

                           $tradeupdate['margin_call_drops'] = $call_perc;
                           
                           $price_down='0';
                           if(!empty($last_call_drop))
                           {
                              if($last_call_drop->type == 1)
                                 $price_down = $last_call_drop->live_price * $call_perc / 100;
                              else
                                 $price_down = $last_call_drop->live_price * $pair->buyin_callback / 100;
                           }
                           if(!empty($last_call_drop->live_price))
                           $down_price = $last_call_drop->live_price - $price_down;
                           else
                              $down_price='';
                           
                           $tradeupdate['down_price']  = "< ".$down_price;
                           
                           $total_position_amount = $CI->table('api_trade_history')->selectSum('quantity')->where($api_trade_data)->get()->getRow();
                           
                           $total_sell_amount = $CI->table('api_trade_history')->selectSum('quantity')->where($api_trade_data1)->get()->getRow();
                           $total_position_amount->quantity = $total_position_amount->quantity - $total_sell_amount->quantity;
                           if(!empty($last_call_drop))
                              $tradeupdate['current_position_amount'] = $last_call_drop->quantity;
                           else
                              $tradeupdate['current_position_amount']='0';
                           
                           $total_position_quantity = $CI->table('api_trade_history')->selectSum('received_amount')->where($api_trade_data)->get()->getRow();
                           $total_sell_quantity = $CI->table('api_trade_history')->selectSum('received_amount')->where($api_trade_data1)->get()->getRow();
                           $total_position_quantity->received_amount = $total_position_quantity->received_amount - $total_sell_quantity->received_amount;
                           
                           if($total_position_amount->quantity > 0)
                           {
                              $tradeupdate['trade_position_amount'] = number_format($total_position_amount->quantity,5);  
                           } 
                           else 
                           {
                              $tradeupdate['trade_position_amount'] = 0.00;
                           }
                           
                           if($total_position_quantity->received_amount > 0) 
                           {
                                 $tradeupdate['trade_position_quantity'] = number_format($total_position_quantity->received_amount,5);
                           }
                           else
                           {
                                 $tradeupdate['trade_position_quantity'] = 0.00;
                           }
                                    
                           if ($avg_position_amount > 0) {
                           $tradeupdate['trade_avg_position'] = $avg_position_amount;
                           }else{
                           $tradeupdate['trade_avg_position'] = 0.00;
                           }
                           
                           $call_drop = $CI->table('api_trade_history')->where($api_trade_data)->where('margin_call_drops !=',0)->orderBy('id','DESC')->get()->getRow();
                           
                           if(!empty($call_drop)){
                              if($call_drop->margin_call_drops == $trade_process->margin_column_limit)
                              {
                                 $tradeupdate['down_price'] = "Limit Reached";
                                 $tradeupdate['margin_call_drops'] = "0";
                              }
                        }
                           
                           
                                    
                                 if(!empty($call_drop)){
                                    if ($call_drop->margin_call_drops != '' || $call_drop->margin_call_drops != '0') {
                                       $tradeupdate['trade_number_of_call'] = $call_drop->margin_call_drops;
                                    }else{
                                       $tradeupdate['trade_number_of_call'] = '0';
                                    }
                                 }
                                 else
                                 {
                                    $tradeupdate['trade_number_of_call'] = '0';
                                 }
                           
                                       $api_trade_data = array(
                                       'trade_process_id' => $pair->id,
                                       'uuserid' => $uusersid,
                                       'exchange' => $exchange,
                                       'type' => '1',
                                       );
                           
                                       $tradeupdate['live_price'] = $live_price;
                                    
                                       $check_percentage=0;
                                       if($avg_position_amount == 0 &&  $tradeupdate['trade_position_amount'] == 0){
                                       $check_percentage = '0';
                                       }
                           
                                       else if ($avg_position_amount > $live_price) 
                                       {
                                       
                                       $check_percentage = ($avg_position_amount - $live_price);
                                       $check_percentage = $check_percentage  / $avg_position_amount;
                                       $check_percentage = $check_percentage  * 100 ;
                                       $check_percentage = '-'.number_format($check_percentage,2).'%';
                                       }elseif ($avg_position_amount == $live_price) {
                                       
                                       $check_percentage = '0';
                                       }else{
                                       
                                       $check_percentage = $live_price - $avg_position_amount;
                                       $check_percentage = $check_percentage  / $live_price;
                                       $check_percentage = $check_percentage  * 100 ;
                                       }
                           
                           
                                       $tradeupdate['trade_rate_percentage'] = $check_percentage;
                                    if ($avg_position_amount > $live_price) 
                                       {
                                          $check_percentage1 = ($avg_position_amount - $live_price) / $avg_position_amount;
                                          $check_percentage1 = $check_percentage1  * 100 ;
                                          $check_percentage1 = '-'.number_format($check_percentage1,2).'%';
                                       }
                                       elseif($avg_position_amount == $live_price) 
                                       {
                                          $check_percentage1 = '0.00';
                                       }
                                       else
                                       {
                                          $check_percentage1 = ($live_price - $avg_position_amount) / $live_price;
                                          $check_percentage1 = $check_percentage1  * 100 ;
                                       }
                           
                           
                                 $tradeupdate['trade_estimate_price'] = $check_percentage1;
                                       
                           
                           $total_sell_amount = $CI->table('api_trade_history')->selectSum('quantity')->where($api_trade_data1)->get()->getRowArray();
                           
                           $total_position_amount1 = $CI->table('api_trade_history')->selectSum('quantity')->where($api_trade_data)->get()->getRowArray();
                           
                           $total_position_amount = $total_position_amount1['quantity'] - $total_sell_amount['quantity'];
                           $total_position_amount = number_format($total_position_amount,4);
                           
                           $tradeupdate['trade_remain_amount'] = $tradeupdate['trade_position_amount'];
                  
                        return $tradeupdate;
                  
                     }
                  }
                  ?>
						

                     </form>
  
                     <table class="table datatable-responsive table-hover" style="width:130%;">
						
							<thead>
								<tr>
                     
									<th class="" width="100px">Date</th>
									<th class="" width="100px">Username</th>
									<th class="" width="100px">Symbol</th>
									<th class="" width="100px">Currency</th>
				               <th class="" width="100px">First Order Amount </th>
                           <th class="" width="100px">Open Position Doubled</th>
                           <th class="" width="100px">Quantity</th>
                           <th class="" width="140px">Coverage Price</th>
                           <th class="" width="140px">Liquidation Price</th>
                           <th class="" width="100px">live Price</th>
                           <th class="" width="100px">Avg Price</th>
                           <th class="" width="100px">No.of call</th>
                           <th class="" width="100px">Multiple investment Limit</th>
                           <th class="" width="100px">Open Position</th>
                           <th class="" width="100px">Mode</th>
                           <th class="" width="100px">Take Profit Ratio</th>
                           <th class="" width="100px">Earning call back</th>
									<th class="" width="100px">Status</th>
									<th class="" width="100px">Action</th>
                           
								</tr>
							</thead>
							<tbody>
                     <?php 
                        $data_binance = json_decode(file_get_contents('https://revotechbot.com/trading_pairs.json'), true);
                        if($trades)
                        {
                           foreach($trades as $trade)
                           {
                              $get_user=$this->db->query("select * from uusers where uusersid='".$trade->uusersid."'")->getRow();
                              $order_time=date("Y-m-d H:i:s",$trade->created_time);
                              $get_quantity=$this->db->query("select sum(quantity) as qty from api_trade_history where trade_process_id='".$trade->id."' and trade_close='0'")->getRow();
                              $tot_quantity=0;
                              if($get_quantity)
                              {
                                 $tot_quantity=$get_quantity->qty;
                              }

                              $mode=str_replace('Strategy','',$trade->strategy_type);

                              
                              if($data_binance)
                              {
                                 foreach ($data_binance as $marketpair) 
                                 {
                                     if ($marketpair['symbol'] === $trade->pair) 
                                     {
                                        $live_price = $marketpair['lastPrice'];
                                        break;
                                     }
                                 }
                              }

                              $tradeupdate=get_tradeinfo($trade->pair,$live_price,$trade->uusersid);

                              if(!empty($tradeupdate))
                              {
                                  
                                $exchange=$trade->exchange;

                                 if($trade->trade_status=='0')
                                 {
                                    $display='<lable class="btn btn-default" style="background:orange;color:#fff;border-radius:10px;"><i class="fa fa-pause white"></i> <span class="text-white"> Paused </span></lable>';
                                    $display1='<button class="btn btn-success" onclick="resume_trade('."'".$trade->pair."'".','."'".$trade->uusersid."'".','."'".$exchange."'".');"> <i class="fa fa-times-circle"> </i> Resume </button>';
                                 }
                                 else
                                 {


                                    if($trade->trade_log=='1')
                                    {
                                       $display='<lable class="btn btn-default" style="background:green;color:#fff;border-radius:10px;"><i class="fa fa-check-circle white"></i> <span class="text-white"> Trade </span></lable>';
                                       $display1='<button class="btn btn-success" OnClick="pause_trade('."'".$trade->pair."'".','."'".$trade->uusersid."'".','."'".$exchange."'".');"> <i class="fa fa-pause "> </i> Pause </button>';
                                    }
                                    else if($trade->trade_log=='2')
                                    {
                                       $display='<lable class="btn btn-default" style="background:orange;color:#fff;border-radius:10px;"><i class="fa fa-warning white"></i> <span class="text-white"> Insufficent Gas Fuel </span></lable>';
                                       $display1='<button class="btn btn-success" OnClick="resume_trade('."'".$trade->pair."'".','."'".$trade->uusersid."'".','."'".$exchange."'".');"> <i class="fa fa-check-circle "> </i> Active </button>';
                                    }
                                    else if($trade->trade_log=='3')
                                    {
                                       $display='<lable class="btn btn-default" style="background:orange;color:#fff;border-radius:10px;"><i class="fa fa-warning white"></i> <span class="text-white"> Insufficent USDT</span></lable>';
                                       $display1='<button class="btn btn-success"  OnClick="resume_trade('."'".$trade->pair."'".','."'".$trade->uusersid."'".','."'".$exchange."'".');"> <i class="fa fa-check-circle "> </i> Active </button>';
                                 }

                               }


                               $flag=0;

                               if($user_group=='0')
                               {
                                  if($user_type=='0')
                                  {
 
                                     if($get_user->new_entry=='0')
                                        $flag=1;
                                  }
                                  else if($user_group=='1')
                                  {
                                     if($get_user->new_entry=='1')
                                        $flag=1;
 
                                  }
                                  else
                                  {
                                     $flag=1;
                                  }
 
                               }
                               else
                               {
                                  if($user_group=='1')
                                  {
 
                                     if($get_user->new_entry=='0')
                                        $flag=1;
                                  }
                                  else if($user_group=='2')
                                  {
                                     if($get_user->new_entry=='1')
                                        $flag=1;
 
                                  }
                                 
 
                               }

                               
								?>

								<tr>
                         	<td class=""><?php echo $order_time;?></td>
									<td class="text-center"><?php echo $get_user->username;?></td>
									<td class=""><?php echo substr($trade->pair, 0, -4);?></td>
									<td class=""><?php echo 'USDT';?></td>
                           <td class=""><?php echo $trade->first_buy_amount;?></td>
                           <td class=""><?php echo $trade->open_position_doubled;?></td>
							<td class=""><?php if(!empty($tradeupdate['trade_position_amount'])) { echo $tradeupdate['trade_position_amount']; } else echo 0;?></td>
                           <td class=""><?php if(!empty($tradeupdate['down_price'])) { echo $tradeupdate['down_price'] ;} else { echo "< 0.0";}?></td>
                           <td class=""><?php if(!empty($tradeupdate['take_profit_trigger'])) { echo $tradeupdate['take_profit_trigger'] ;} else { echo "> 0.0";}?></td>
                           <td class=""><?php echo number_format($live_price,8);?></td>
									<td class=""><?php echo number_format($tradeupdate['trade_avg_position'],8);?></td>
                           <td class=""><?php if(!empty($tradeupdate['trade_number_of_call'])) { echo $tradeupdate['trade_number_of_call']; } else { echo 0; }?></td>
                           <td class=""><?php echo $trade->margin_column_limit;?></td>
                           <td class=""><?php echo $trade->buyin_ratio_times;?></td>
                           <td class=""><?php echo $mode;?></td>
                           <td class=""><?php echo $trade->whole_profit_ratio;?></td>
                           <td class=""><?php echo $trade->whole_profit_callback;?></td>
                           <td class=""><?php echo $display;?></td>
                           <td class=""><?php echo $display1;?> <br><BR><button class="btn btn-danger" onclick="close_trade('<?php echo $trade->pair;?>','<?php echo $trade->uusersid;?>','<?php echo $trade->exchange;?>');"> <i class="fa fa-times-circle"> </i> Close</button></td>
                      
					
								</tr>
                        <?php    
                              }
                           }
                        }
                        ?>
					
							</tbody>
						</table>


                  </div>
                  <!-- </div> -->
               </div>
               <!-- /basic responsive configuration -->
            </div>
            <!-- /main content -->
         </div>
         <!-- /page content -->
         <!-- Footer -->
         <div class="footer text-muted">
            <?php echo site_footer();?>
         </div>
         <!-- /footer -->
      </div>
      <!-- /page container -->

      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@10.6.0/dist/sweetalert2.min.css">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10.6.0/dist/sweetalert2.all.min.js"></script>
      <script type="text/javascript">
         jQuery.noConflict();
         (function( $ ) {
         $(function() {
         
         
         
           
         
         	$(document).ready(function() {
         
         		$('.bootstrap-select').selectpicker();
         
         		$.jGrowl.defaults.closer = false;

     		<?php if($session->getFlashData('error_message')) { ?>
				$.jGrowl("<?php echo $session->getFlashData('error_message');?>", {
		            header: '<?php echo lang("language_lang.error");?>',
		            theme: 'bg-danger'
		        });

            <?php } ?>
                    
            <?php if($session->getFlashData('success_message')) { ?>
                
                $.jGrowl("<?php echo $session->getFlashData('success_message');?>", {
		            header: '<?php echo lang("language_lang.success");?>',
		            theme: 'bg-success'
		        });

            <?php } ?>
         
      $("#username").autocomplete({
                    source: function(request, response) {

                     var user_type="<?php echo $user_group;?>";
                     var user_group =$("#user_group").val();
                        $.ajax({ 
                            url: "<?php echo base_url('admin/users/autocomplete'); ?>",
                            data: { bahasa: request.term,usergroup:user_group,user_type:user_type},
                            dataType: "json",
                            type: "POST",
                            success: function(data){
                                response(data);
                            }    
                        });
                    },
                    select: function (event, ui) {
                        $('#username').val(ui.item.label);
                        $('#code').val(ui.item.value);
                        return false;
                    }
                });
         	
         
                    $(".end_date").datepicker({
                    	format: 'mm-dd-yyyy',
                    	maxDate: 0
                    });
         
                    $(".start_date").datepicker({
                    	format: 'mm-dd-yyyy',
                    	maxDate: 0
                    });
         
                
         
                    
         
         	});
             // $('[name="page_content"]').ckeditor();
          	$('#icon-bck').on('click',function() {
         		window.history.back();
         	});

			 });
         })(jQuery);

         function resume_trade(pair,userid,exchange)
         {
            Swal.fire({
                    title: 'Trade Request change',
                    text: "Are You sure you want to change it Active Mode",
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes, confirm it!'
                        }).then((result) => {
                            // Check if user confirmed
                            if (result.isConfirmed) {
                              
                              jQuery.ajax({
                            type: 'post',
                            dataType:'html',
                            url : '<?php echo base_url("/admin/bot/activate-trade");?>',
                            data : {'pair':pair,'userid':userid,'exchange':exchange},
                            headers: {'X-Requested-With': 'XMLHttpRequest'},
                            success : function(msg)
                            {
                            if(msg)
                            {
                              window.location.href="<?php echo base_url("/admin/bot/activate-list");?>";
                            }
                            }
                            });
                    }
                }); 
         }

         function pause_trade(pair,userid,exchange)
         {
            Swal.fire({
                    title: 'Trade Request chage',
                    text: "Are You sure you want to change it Pause Mode",
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes, confirm it!'
                        }).then((result) => {
                            // Check if user confirmed
                            if (result.isConfirmed) {
                              
                              jQuery.ajax({
                            type: 'post',
                            dataType:'html',
                            url : '<?php echo base_url("/admin/bot/pause-trade");?>',
                            data : {'pair':pair,'userid':userid,'exchange':exchange},
                            headers: {'X-Requested-With': 'XMLHttpRequest'},
                            success : function(msg)
                            {
                            if(msg)
                            {
                              window.location.href="<?php echo base_url("/admin/bot/activate-list");?>";
                            }
                            }
                            });
                    }
                }); 
         }

         function close_trade(pair,userid,exchange)
         {
            Swal.fire({
                    title: 'Trade Request chage',
                    text: "Are You sure you want to Close current trade!. Please once verify with live price higher or not for negivative profit deduction purpose",
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes, confirm it!'
                        }).then((result) => {
                            // Check if user confirmed
                            if (result.isConfirmed) {
                              
                              jQuery.ajax({
                            type: 'post',
                            dataType:'html',
                            url : '<?php echo base_url("/admin/bot/close-trade");?>',
                            data : {'pair':pair,'userid':userid,'exchange':exchange},
                            headers: {'X-Requested-With': 'XMLHttpRequest'},
                            success : function(msg)
                            {
                            if(msg)
                            {
                               //window.location.href = "<?php echo base_url('admin/bot/activate-list/');?>"
                            }
                            }
                            });
                    }
                }); 
         }

     
         
      </script>
   </body>
</html>