<!doctype html>
<html lang="en" class="w-[640px]">
   <head>
      <?php echo view('user/layout/head');?>
   </head>
   <style>
      .notify {
      position: fixed;
      top: 10px;
      left: 10px;
      -moz-box-sizing: border-box;
      box-sizing: border-box;
      width: 350px;
      font-size: 14px;
      text-align: center;
      }
      /* Positions
      ========================================================================== */
      .notify-top-right,
      .notify-bottom-right {
      left: auto;
      right: 10px;
      }
      .notify-top-center,
      .notify-bottom-center {
      left: 50%;
      margin-left: -175px;
      }
      .notify-bottom-left,
      .notify-bottom-right,
      .notify-bottom-center {
      top: auto;
      bottom: 10px;
      }
      .notify button {
      -webkit-appearance: none;
      padding: 0;
      cursor: pointer;
      background: 0 0;
      border: 0;
      display: none;
      }
      /* Responsiveness
      ========================================================================== */
      /* Phones portrait and smaller */
      @media (max-width: 479px) {
      /*
      * Fit in small screen
      */
      .notify {
      left: 10px;
      right: 10px;
      width: auto;
      margin: 0;
      }
      }
      /* Sub-object: `notify-message`
      ========================================================================== */
      .notify-message {
      position: relative;
      cursor: pointer;
      }
      .notify .alert-default {
      background: #fff;
      border: 1px solid #e5e5e5;
      }
      .form-icos
      {
         top:26px  !important;
      }
       .rounded-circle
      {
         margin-top:29px;
       }
      
       @media only screen and (max-width:432px) {
     .referral{
         height:72vh !important;
     } 
         .rounded-circle
      {
         margin-top:8px !important;
       }
    }
   </style>
   <body class="section-overflow">
      <!--banner start -->
      <?php 
         $this->db = db_connect();
         $this->session = session();
         $uusersid = $this->session->get('uusersid');
         function formatTradingPair($pair) 
         {
            $base = substr($pair, 0, -4);  // Extract the base currency (all but the last 4 characters)
            $quote = substr($pair, -4);    // Extract the quote currency (last 4 characters)
            return strtoupper($base) . '/' . strtoupper($quote); // Combine and convert to lowercase
          }
         
         
          $qury = $this->db->query("SELECT * FROM trade_process WHERE uusersid='".$uusersid."' AND  pair = '".$symbol."' and trade_status != 2 order by id desc limit 1")->getResult();
          
          
          if(!empty($qury))
          {
            
            //$qry=$qury[0];
            //$qury = $this->db->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;
            $strategy_type = $qry->strategy_type;
            $risk_type=$qry->risk_type;
          }
          else
          {
              
            $qry='';
            $qury = $this->db->query("SELECT * FROM user_strategy WHERE uusersid='".$uusersid."' And pair='".$symbol."'")->getResult();
            if(!empty($qury[0]))
            {
               
                //echo '1';
                $qury = $this->db->query("SELECT * FROM user_strategy WHERE uusersid='".$uusersid."' And pair='".$symbol."'")->getResult();
                $qry=$qury[0];
            }
            else
            {
               
                $qury = $this->db->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;
            $strategy_type = $qry->strategy_type;
            $risk_type=$qry->risk_type;
          
          
          }
         ?>
      <section class="banner-bg overall-bg ">
         <div class="container">
            <?php echo view('user/layout/header');?>
         </div>
         <section class="dash-wrap referral">
            <div class="container">
               <div class="row">
                  <div class="col-12">
                     <form class="" id="exampleModal1" method="post" action="<?php echo base_url().'/tradedetails/'.$symbol;?>" enctype="multipart/form-data">
                        <div class=" team-profit-content" style="text-align:start !important">
                           <div class="row">
                              <div class="col-sm-12">

                                 <!--<div class=" form-block position-relative mb-3 mt-3">
                                    <div class="row mb-3" >
                                       <div class="col-sm-4">
                                          <label for="" class="form-label text-white">Risk Type</label>
                                       </div>
                                       <div class="col-sm-8">
                                          <select class="form-control ps-2 form-arrow" name="risk_type" id="risk_type">
                                             <option value="1" <?php if($risk_type==='1'){ echo "selected==selected";} ?>>Steady</option>
                                             <option value="2" <?php  if($risk_type==='2') { echo "selected==selected";}?>>Conservative</option>
                                             <option value="3" <?php if($risk_type==='3'){ echo "selected==selected";}?>>Radical</option>
                                          </select>
                                       </div>
                                    </div>
                                 </div>-->
                                 
                                  <div class="form-btn my-4 text-center col-lg-6 offset-lg-3">
                                           <button type="submit" class="btn btn-primary " name="strategy" value="strategy">Get a Suggested Strategy</button>
                                 </div>

                                 <div class=" form-block position-relative mb-3 mt-3">
                                    <div class="row mb-3" >
                                       <div class="col-sm-4">
                                          <label for="" class="form-label text-white">Strategy Mode</label>
                                       </div>
                                       <div class="col-sm-8">
                                          <select class="form-control ps-2 form-arrow" name="strategy_mode" id="strategy_mode">
                                          <option value="1" <?php if($strategy_mode==='1'){ echo "selected==selected";} ?>>Whole warehouse mode</option>
                                          <!--<option value="2" <?php  if($strategy_mode==='2') { echo "selected==selected";}?>>Sub-bin mode</option>-->
                                          <option value="3" <?php if($strategy_mode==='3'){ echo "selected==selected";}?>>Sub-bin mode Realtime settlement</option>
                                          </select>
                                       </div>
                                    </div>
                                 </div>

                                 <div class="mb-2 form-block position-relative">
                                    <div class="row mb-3" >
                                       <div class="col-sm-4">
                                          <label for="" class="form-label  text-white">First order amount</label>
                                       </div>
                                       <div class="col-sm-8">
                                          <input type="number" class="form-control required" name="first_buy_amount" id="first_buy_amount" min='10' value="<?php echo $first_buy_amount;?>"  onkeypress="return isNumber(event)">
                                       </div>
                                    </div>
                                 </div>
                                 <div id="old_error1" class="help-block"></div>

                                 <div class=" form-block position-relative mb-3 mt-3">
                                    <div class="row mb-3" >
                                       <div class="col-sm-4">
                                          <label for="" class="form-label text-white">Double open position</label>
                                       </div>
                                       <div class="col-sm-8">
                                          <select class="form-control ps-2 form-arrow"name="open_position_doubled">
                                          <option value="off" <?php if($open_position_doubled=='off') { echo "selected==selected";}?>>No</option>
                                          <option value="on" <?php if($open_position_doubled=='on') { echo "selected==selected";}?>>Yes</option>
                                          </select>
                                       </div>
                                    </div>
                                 </div>

                                 <div class="mb-2 form-block position-relative">
                                    <div class="row mb-3" >
                                       <div class="col-sm-4">
                                          <label for="" class="form-label  text-white">Replenishment times</label>
                                       </div>
                                       <div class="col-sm-8">
                                          <input type="number" class="form-control required" id="replenishment_time" name="replenishment_time" placeholder=""  value="<?php echo $margin_column_limit;?>"  onkeypress="return isNumber(event)" oninput="validateNumberRange(this)">
                                             <small id="replenishment_error" class="text-danger d-none">
                Please enter a number between 1 and 12.
            </small>                                  
                                       </div>
                                    </div>
                                 </div>

                                 <div class="mb-2 form-block position-relative">
                                    <div class="row mb-3" >
                                       <div class="col-sm-4">
                                          <label for="" class="form-label  text-white">Whole position take profit ratio (%)</label>
                                       </div>
                                       <div class="col-sm-8">
                                          <input type="number" class="form-control required" name="whole_profit_ratio" value="<?php echo $whole_profit_ratio;?>"  onkeypress="return isNumber(event)">
                                       </div>
                                    </div>
                                 </div>

                                 <div class="mb-2 form-block position-relative">
                                    <div class="row mb-3" >
                                       <div class="col-sm-4">
                                          <label for="" class="form-label  text-white">Whole position take profit Callback (%)</label>
                                       </div>
                                       <div class="col-sm-8">
                                          <input type="number" class="form-control required" name="whole_profit_callback" value="<?php echo $whole_profit_callback;?>"  onkeypress="return isNumber(event)">
                                       </div>
                                    </div>
                                 </div>

                                 <div class="mb-2 form-block position-relative">
                                    <div class="row mb-3" >
                                       <div class="col-sm-4">
                                          <label for="" class="form-label  text-white">Sub-Position take-profit callback (%)</label>
                                       </div>
                                       <div class="col-sm-8">
                                          <input type="number" class="form-control required" name="sub_position_callback"  value="<?php echo $sub_position_callback;?>" onkeypress="return isNumber(event)">
                                       </div>
                                    </div>
                                 </div>

                                 <div class="mb-2 form-block position-relative">
                                    <div class="row mb-3" >
                                       <div class="col-sm-4">
                                          <label for="" class="form-label  text-white">Buy in callback (%)</label>
                                       </div>
                                       <div class="col-sm-8">
                                          <input type="number" class="form-control required"  name="buyin_callback" value="<?php echo $buyin_callback;?>" onkeypress="return isNumber(event)">
                                       </div>
                                    </div>
                                 </div>

                                 <div class=" form-block position-relative mb-3 mt-3">
                                    <div class="row mb-3" >
                                       <div class="col-sm-4">
                                          <label for="" class="form-label text-white">Strategy Type</label>
                                       </div>
                                       <div class="col-sm-8">
                                          <select class="form-control ps-2 form-arrow" name="strategy_type">
                                             <option value="Cycle Strategy" <?php if($strategy_type=='Cycle Strategy') { echo "selected==selected";}?>>Cycle</option>
                                             <option value="Single strategy" <?php if($strategy_type=='One-shot strategy') { echo "selected==selected";}?>>Single</option>
                                          </select>
                                       </div>
                                    </div>
                                 </div>

                                 <div class="row mt-4">
                                    <div class="col-sm-4">
                                          <div class="form-block position-relative login-ico">
                                             <label class="text-white">Margin Call drop</label>
                                             <hr class="text-white">
                                             <label id="ex_demo1"></label>
                                          </div>
                                    </div>

                                    <div class="col-sm-4">
                                          <div class="form-block position-relative login-ico">
                                             <label class="text-white">Multiply Buy in ratio</label>
                                             <hr class="text-white">
                                             <label id="ex_demo2"></label>
                                          </div>
                                    </div>

                                    <div class="col-sm-4">
                                          <div class="form-block position-relative login-ico">
                                             <label class="text-white">Distributed and take profit Allocation</label>
                                             <hr class="text-white">
                                                <label id="ex_demo"></label>
                                          </div>
                                    </div>
                </div>

                                 

                                 <div class="form-btn my-4 text-center col-lg-6 offset-lg-3"> <button class="btn btn-primary" type="button" id="submit_button"  name="sub" value="sub">
                                 Submit
                                    </button>
                                 </div>
                              </div>
                           </div>
                        </div>
                     </form>
                  </div>
               </div>
            </div>
         </section>
         <!--footer start -->
         <?php echo view('user/layout/footer');?>
         <!--footer end -->
      </section>
      <?php echo view('user/layout/footer-script');?>
      <script>
         AOS.init({
         });
      </script>
      <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
         <script src="<?php echo base_url();?>/styles/new_style/js/notify.js"></script>
      <script src="<?php echo base_url();?>/styles/web_style/js/jquery.dataTables.min.js"></script>
      <script type="text/javascript">
      
       function validateNumberRange(input) {
        const errorElement = document.getElementById('replenishment_error');
        const value = parseInt(input.value, 10);

        if (value < 1 || value > 12 || isNaN(value)) {
            errorElement.classList.remove('d-none'); // Show the error message
        } else {
            errorElement.classList.add('d-none'); // Hide the error message
        }
    }
      
      function sug_stra()
      {
         // alert('1');
           $.ajax({
           type: 'post',
           dataType:'html',
           url : '<?php echo base_url("/sug_strategy");?>',
           //data : {'number1':num,'pair':'<?php echo $symbol;?>'},
           //headers: {'X-Requested-With': 'XMLHttpRequest'},
           success : function(msg)
           {
             console.log('1');
           }
           }); 
      }
        
        function isNumber(evt) {
            evt = (evt) ? evt : window.event;
            var charCode = (evt.which) ? evt.which : evt.keyCode;
           
            if (charCode > 31 && (charCode < 48 || charCode > 57)) 
            {
                if(charCode!='46')
                return false;
                else
                return true;
            }
            return true;
        }

         function margin_pass()
         {
          
           var num = $('#replenishment_time').val();
           window.location.href = "<?php echo base_url();?>/margin-configuration/"+"<?php echo $symbol;?>/"+num;

         }
         
         
         $(window).on('load', function () 
         {
           var num= "<?php if(!empty($trade_process->margin_column_limit)) { echo $trade_process->margin_column_limit; } else echo "";?>";
           if(num =="")
           num = 7;
         
           $.ajax({
           type: 'post',
           dataType:'html',
           url : '<?php echo base_url("/replenishment_time1");?>',
           data : {'number1':num,'pair':'<?php echo $symbol;?>'},
           headers: {'X-Requested-With': 'XMLHttpRequest'},
           success : function(msg)
           {
           if(msg)
           {
           $('#ex_demo1').html(msg);
           }
           }
           });
         
           $.ajax({
           type: 'post',
           dataType:'html',
           url : '<?php echo base_url("/replenishment_time2");?>',
           data : {'number2':num,'pair':'<?php echo $symbol;?>'},
           headers: {'X-Requested-With': 'XMLHttpRequest'},
           success : function(msg)
           {
           if(msg)
           {
           $('#ex_demo2').html(msg);
           }
           }
           });
         
           $.ajax({
           type: 'post',
           dataType:'html',
           url : '<?php echo base_url("/replenishment_time");?>',
           data : {'number':num,'pair':'<?php echo $symbol;?>'},
           headers: {'X-Requested-With': 'XMLHttpRequest'},
           success : function(msg)
           {
           if(msg)
           {
           $('#ex_demo').html(msg);
           }
           }
           });
         
         });
         
         
         $('#replenishment_time').keyup(function(){
         
         var num = $('#replenishment_time').val();
         
         if(num>=5 && num <= 12)
         {
                   $.ajax({
                   type: 'post',
                   dataType:'html',
                   url : '<?php echo base_url("/replenishment_time");?>',
                   data : {'number':num,'pair':'<?php echo $symbol;?>'},
                   headers: {'X-Requested-With': 'XMLHttpRequest'},
                   success : function(msg)
                   {
                       if(msg)
                       {
                       $('#ex_demo').html(msg);
                       }
                   }
                   });
         
                   $('#subposition').css('display', 'block');
                   $('.sublabel').css('display', 'block');
         
         }
         else
         {
           $('#ex_demo').html("");
           $('#subposition').css('display', 'block');
           $('.sublabel').css('display', 'block');
         }
         });
         
         
         $('#replenishment_time').keyup(function(){
         
         var num = $('#replenishment_time').val();
         
         if(num>=1 && num <= 12)
         {
                   $.ajax({
                   type: 'post',
                   dataType:'html',
                   url : '<?php echo base_url("/replenishment_time1");?>',
                   data : {'number1':num,'pair':'<?php echo $symbol;?>'},
                   headers: {'X-Requested-With': 'XMLHttpRequest'},
                   success : function(msg)
                   {
                       if(msg)
                       {
                       $('#ex_demo1').html(msg);
                       }
                   }
                   });
         
         }
         });
         
         $('#replenishment_time').keyup(function(){
         var num = $('#replenishment_time').val();
         if(num>0 && num <= 12)
         {
                   $.ajax({
                   type: 'post',
                   dataType:'html',
                   url : '<?php echo base_url("/replenishment_time2");?>',
                   data : {'number2':num,'pair':'<?php echo $symbol;?>'},
                   headers: {'X-Requested-With': 'XMLHttpRequest'},
                   success : function(msg)
                   {
                       if(msg)
                       {
                       $('#ex_demo2').html(msg);
                       }
                   }
                   });
         
         }
         });
         
         $('#submit_button').click(function(){

            var symbol = '<?php echo $symbol;?>';
            // alert(symbol);
            <?php 
           $this->session = session();
            $margin_call_session = $this->session->get($symbol) ?? '0'; 
         
         ?>
         var msession = '<?php echo $margin_call_session;?>';

         //alert(msession);
         var first_buy_amount =  $("[name=first_buy_amount]").val();
         var whole_profit_ratio =  $("[name=whole_profit_ratio]").val();
         var whole_profit_callback =  $("[name=whole_profit_callback]").val();
         var sub_position_callback = $("[name=sub_position_callback]").val();
         var limit = $("[name=replenishment_time]").val();
         var buyin_callback =  $("[name=buyin_callback]").val();
         var flag = 1;

         // if(msession != '1')
         // {
         //    $('#mcall').css('display','block');
         //    $.notify(' <i class="fas fa-exclamation-triangle"></i> Please set the margin calls', 'danger');
         //     flag = 0;
         // }
         if (limit < 1 || limit > 12) 
         {
            //alert('kkk');
            $.notify(' <i class="fas fa-exclamation-triangle"></i>   Please enter a number between 1 and 12 for replenisment time', 'danger');
             flag = 0;
         }

         if(!(whole_profit_ratio > 0)){
             $.notify(' <i class="fas fa-exclamation-triangle"></i> Please enter a value greater than 1', 'danger');
             flag = 0;
         }
           if(limit >= 5){
           var j=0;
           var i;
              for(i=5; limit >= i; i++){
               var var_name = "rep_time"+j;
               if($("[name="+var_name+"]").val() < sub_position_callback && flag == 1){
               $.notify(' <i class="fas fa-exclamation-triangle"></i> Subbin Take profit ratio will be greater than subbin take profit callback', 'danger');
               flag = 0;
               }
               j++;
              }
           }
         
         
         if(whole_profit_ratio < whole_profit_callback && flag == 1){
             $.notify(' <i class="fas fa-exclamation-triangle"></i> Take profit ratio will be greater than take profit callback', 'danger');
             //flag = 0;
           }
           if(!(buyin_callback >= 0) && flag == 1){
             $.notify(' <i class="fas fa-exclamation-triangle"></i> Please enter a positive value', 'danger');
             flag = 0;
           }
           /*if(!(first_buy_amount < 10 && flag == 1)){
             $.notify(' <i class="fas fa-exclamation-triangle"></i> Minimum first buy amount is 10 USDT', 'danger');
             flag = 1;
         }*/
         
         if(parseFloat(first_buy_amount) < 10 && flag==1)
         {
             $.notify(' <i class="fas fa-exclamation-triangle"></i> Minimum first buy amount is 10 USDT', 'danger');
             flag = 0;
             
         }
         
         var empty = true;
         
         $('#exampleModal1 input[type="text"]').each(function() {
          var fieldValue = $(this).val().trim();

          //alert(fieldValue);

          if ($(this).hasClass('required') && fieldValue === '') {
          
         $flag = 0;
         empty = false;
         return false;
         }
        
         });
         
         if(empty == false){
             $.notify(' <i class="fas fa-exclamation-triangle"></i> Some fields are empty', 'danger');
             $("html, body").animate({ scrollTop: 0 }, "slow");
         }
         else
         {
             console.log(flag);
              if(flag!='0')
              {
               $('#exampleModal1').submit();
               $.notify('<i class="fas fa-check"></i>  Set Successfully', 'success');
              }
         }
         
           if(flag == 1 && empty == true){
             $('#exampleModal1').submit();
             $.notify('<i class="fas fa-check"></i>  Set Successfully', 'success');
           }
             
         });  
         
         $('#subposition').css('display', 'block');
         $('.sublabel').css('display', 'block');
         
          var num = $('#replenishment_time').val();
          if (num >= 5) {
         
        $('#subposition').css('display', 'block');
       
        $('.sublabel').css('display', 'block');
    
    } else {
        $('#subposition').css('display', 'block');
        $('.sublabel').css('display', 'block');
    }
         
      </script>
   </body>
</html>
