<!DOCTYPE html>
<html lang="en">
<head>
	<!-- Main navbar -->
		<?php
        helper('form');
		$session = session();
		$validation = \Config\Services::validation();
		echo view('admin/common_header'); ?>
	<!-- /main navbar -->
	
 <style>
    .help-block
    {
        color:red;
    }
    </style>

	<!-- file upload JS files -->
	<script type="text/javascript" src="<?php echo base_url('assets/js/plugins/uploaders/fileinput.min.js');?>"></script>

	<!-- 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>

	
	<!-- Bootsrap form validation JS files -->
	<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 src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script>   -->
        <!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min.js"> </script>   -->
	

	<!-- Theme JS files -->
	<script type="text/javascript" src="<?php echo base_url('assets/js/core/app.js');?>"></script>
	<!-- /theme JS files -->

	<!-- date picker JS files -->
	<script type="text/javascript" src="<?php echo base_url('assets/js/pages/jqueryui_forms.js');?>"></script>

	<!-- file upload JS files -->
	<script type="text/javascript" src="<?php echo base_url('assets/boots/js/fileinput.min.js');?>"></script>
	
	<!-- bootsrap switch checkbox JS files -->
    <script src="<?php echo base_url('assets/boots/js/bootstrap-switch.js');?>"></script>

    <!-- 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/ckeditor/ckeditor.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>
	<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>

	 <!-- select box JS files -->
	<script type="text/javascript" src="<?php echo base_url('assets/js/pages/form_bootstrap_select.js');?>"></script>
	<!-- /select box JS files -->




   
    
    

</style>
</head>

<body class="navbar-top-md-md">
	<!-- <div class="pre_loader"></div> -->
	<!-- 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 -->
	<div class="page-header">
		<div class="page-header-content">
			<div class="page-title">
				<h4><i id="icon-bck" class="icon-arrow-left52 position-left"></i> <span class="text-semibold">Trade Strategy Settings</span></h4>

				<ul class="breadcrumb breadcrumb-caret position-right">
					<?php foreach ($breadcrumbs as $breadcrumb) { if(isset($breadcrumb['href'])){?>
					    <li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
					<?php } else { ?> <li><?php echo $breadcrumb['text']; ?></li><?php } } ?>
				</ul>
			</div>
			<?php echo view('admin/'.$module_name.'_header'); ?>
			
		</div>
	</div>
	<!-- /page header -->


	<!-- Page container -->
	<div class="page-container">

		<!-- Page content -->
		<div class="page-content">

			<!-- Main content -->
			<div class="content-wrapper">

				<!-- Horizontal form -->
				<div class="panel panel-flat">
					<div class="panel-heading">
						<h5 class="panel-title">
					 Trade Strategy
						</h5>
						<div class="heading-elements">
							<ul class="icons-list">
		                		<li><a data-action="collapse"></a></li>
		                		<li><a data-action="close"></a></li>
		                	</ul>
	                	</div>
                	</div>
                	
					<div class="panel-body">
						<form class="form-horizontal tradepair" id="tradepair" action="<?=site_url($action);?>" method="post" enctype="multipart/form-data">
							<?php 
								if($validation->listErrors()) { 
									$form_err_data = str_replace('<p>', '', $validation->listErrors());
									$form_err_data = str_replace('</p>', '', $form_err_data);
									$form_err_data = str_replace('.', '', $form_err_data);
							?>
									
									<script type="text/javascript">
									
									(function( $ ) {
										$.jGrowl.defaults.closer = false;
								        var from_daa = '<?php $form_err_data;?>';
										$.jGrowl(from_daa, {
								            header: '<?php echo "All Fields are required and only accept a numbers";?>',
								            theme: 'bg-danger'
								        });
								    
									})(jQuery);


									</script>
							<?php
								}
							?>
							<div class="form-group  ">
								<label class="control-label col-lg-2 text-semibold">First Buy Amount <span class="text-danger">*</span></label>
								<div class="col-lg-5">
									<div style="width:100%" class="input-group">
									<input name="first_buy_amount" class="form-control elastic" cols="4" rows="3" placeholder="Enter first buy amount" value="<?php echo set_value('first_buy_amount',isset($strategy->first_buy_amount) ? ucfirst($strategy->first_buy_amount) : ''); ?>" required>
									</div>
                                    <span class="help-block"> <?php echo $validation->getError('first_buy_amount');?></span>
								
								</div>
							</div>

                            <div class="form-group">
                                <label class="control-label col-lg-2 text-semibold"> Double Open Position <span class="text-danger">*</span></label>
                                <div class="col-lg-5">
                                    <div style="width:100%;" class="input-group">
                                            <select class="form-control" name="open_position_doubled" >
                                                <option value='' >None Selected</option>
                                                <option  value="On" <?php if($strategy->open_position_doubled=='On'){ echo "selected" ;}?> >On</option>
                                                <option value="Off" 
                                                <?php if($strategy->open_position_doubled=='Off'){ echo "selected" ;}?> >Off</option> 
                                           
                                            </select>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group  <?php if($validation->getError('margin_column_limit')) echo "has-error";?>">
								<label class="control-label col-lg-2 text-semibold">Margin Cloumn Limit <span class="text-danger">*</span></label>
								<div class="col-lg-5">
									<div style="width:100%" class="input-group">
									<input name="margin_column_limit" id="margin_column_limit" class="form-control elastic" cols="4" rows="3" placeholder="Enter magin column limit" value="<?php echo set_value('margin_column_limit',isset($strategy->margin_column_limit) ? ucfirst($strategy->margin_column_limit) : ''); ?>" required>
									</div>
                                    <span class="help-block"> <?php echo $validation->getError('margin_column_limit');?></span>
								
								</div>
							</div>
                          

                                <div class="row ">
                                    <div class="col-lg-5">
                                        <div class="modal-head">
                                            <h6 class="mont" style="color:green">Margin Configuration</h6>
                                        </div>
                                        <div class="row">
                                            <div class="col-lg-6">
                                                <div class="modal-head">
                                                <p class="mont">Margin Call drop</p>
                                                </div>
                                                <span id="ex_demo1"></span>

                                                <span class="help-block"> <?php echo $validation->getError('call_time.*');?></span>
                                                
                                            </div>
                                            <div class="col-lg-6">
                                                <div class="modal-head">
                                                <p class=" mont">Multiply Buy in ratio</p>
                                                </div>
                                                <span id="ex_demo2"></span>
                                                <span class="help-block"> <?php echo $validation->getError('order_time.*');?></span>
                                     
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-lg-5">
                                        <div class="modal-head">
                                            <h6 class="mont" style="color:green">Distributed and take profit Allocation</h6>
                                        </div>
                                        <div class="row">
                                            <div class="col-lg-6">
                                                <div class="modal-head">
                                                <p class="mont">Percentage of spilt-to-profit</p>
                                                </div>
                                                <span id="ex_demo"></span>
                                                <span class="help-block"> <?php echo $validation->getError('rep_time.*');?></span>
                                               
                                            </div>
                                        </div>
                                    </div>
                                </div>

                            <div class="form-group  <?php if($validation->getError('whole_profit_ratio')) echo "has-error";?>" style="margin-top:20px">
								<label class="control-label col-lg-2 text-semibold">Whole Profit Ratio <span class="text-danger">*</span></label>
								<div class="col-lg-5">
									<div style="width:100%" class="input-group">
									<input name="whole_profit_ratio" class="form-control elastic" cols="4" rows="3" placeholder="Enter while profit ratio" value="<?php echo set_value('whole_profit_ratio',isset($strategy->whole_profit_ratio) ? ucfirst($strategy->whole_profit_ratio) : ''); ?>" required>
									</div>
                                    <span class="help-block"> <?php echo $validation->getError('whole_profit_ratio');?></span>
								
								</div>
							</div>

                            <div class="form-group  <?php if($validation->getError('whole_profit_callback')) echo "has-error";?>">
								<label class="control-label col-lg-2 text-semibold">Whole Profit Callback <span class="text-danger">*</span></label>
								<div class="col-lg-5">
									<div style="width:100%" class="input-group">
									<input name="whole_profit_callback" class="form-control elastic" cols="4" rows="3" placeholder="Enter whole profit callback" value="<?php echo set_value('whole_profit_callback',isset($strategy->whole_profit_callback) ? ucfirst($strategy->whole_profit_callback) : ''); ?>" required>
									</div>
                                    <span class="help-block"> <?php echo $validation->getError('whole_profit_callback');?></span>
									
								</div>
							</div>

                            <div class="form-group  <?php if($validation->getError('buyin_callback')) echo "has-error";?>">
								<label class="control-label col-lg-2 text-semibold">Buy in call back <span class="text-danger">*</span></label>
								<div class="col-lg-5">
									<div style="width:100%" class="input-group">
									<input name="buyin_callback" class="form-control elastic" cols="4" rows="3" placeholder="Enter Buy in callback" value="<?php echo set_value('buyin_callback',isset($strategy->buyin_callback) ? ucfirst($strategy->buyin_callback) : ''); ?>" required>
									</div>
                                    <span class="help-block"> <?php echo $validation->getError('buyin_callback');?></span>
							
								</div>
							</div>

                            <div class="form-group  <?php if($validation->getError('sub_position_callback')) echo "has-error";?>">
								<label class="control-label col-lg-2 text-semibold">Sub Position Callback <span class="text-danger">*</span></label>
								<div class="col-lg-5">
									<div style="width:100%" class="input-group">
									<input name="sub_position_callback" class="form-control elastic" cols="4" rows="3" placeholder="Enter first buy amount" value="<?php echo set_value('sub_position_callback',isset($strategy->sub_position_callback) ? ucfirst($strategy->sub_position_callback) : ''); ?>" required>
									</div>
                                    <span class="help-block"> <?php echo $validation->getError('sub_position_callback');?></span>
									
								</div>
							</div>

                            <div class="text-right col-lg-4">
								<button type="submit" class="btn btn-primary"><?php  echo lang('language_lang.button_save');  ?> <i class="icon-arrow-right14 position-right"></i></button>
							</div>
						</form>
					</div>
				</div>
				<!-- /horizotal form -->

			</div>
			<!-- /main content -->

		</div>
		<!-- /page content -->
		

		<!-- Footer -->
		<div class="footer text-muted">
			<?php echo site_footer();?>
		</div>
		<!-- /footer -->

	</div>
	<!-- /page container -->
<!-- jQuery -->

<script type="text/javascript">
    // Ensure no conflicts with other JavaScript libraries using $
    jQuery.noConflict();

    // Document ready function to ensure all scripts execute after the DOM is fully loaded
    jQuery(document).ready(function($) {
      

        // Fetch margin column limit or set a default value
        var num = "<?php echo !empty($strategy->margin_column_limit) ? $strategy->margin_column_limit : 7; ?>";

        // If margin column limit is empty, set it to 7 as per your logic
        if (num === "") {
            num = 7;
        }

        // Perform AJAX requests using jQuery
        $.ajax({
            type: 'post',
            dataType: 'html',
            url: '<?php echo base_url("/margin_column_limit1"); ?>',
            data: {'number1': num},
            headers: {'X-Requested-With': 'XMLHttpRequest'},
            success: function(msg) {
                if (msg) {
                    $('#ex_demo1').html(msg);
                }
            },
            error: function(xhr, textStatus, errorThrown) {
                console.error('AJAX Error:', textStatus);
            }
        });

        $.ajax({
            type: 'post',
            dataType: 'html',
            url: '<?php echo base_url("/margin_column_limit2"); ?>',
            data: {'number2': num},
            headers: {'X-Requested-With': 'XMLHttpRequest'},
            success: function(msg) {
                if (msg) {
                    $('#ex_demo2').html(msg);
                }
            },
            error: function(xhr, textStatus, errorThrown) {
                console.error('AJAX Error:', textStatus);
            }
        });

        $.ajax({
            type: 'post',
            dataType: 'html',
            url: '<?php echo base_url("/margin_column_limit"); ?>',
            data: {'number': num},
            headers: {'X-Requested-With': 'XMLHttpRequest'},
            success: function(msg) {
                if (msg) {
                    $('#ex_demo').html(msg);
                }
            },
            error: function(xhr, textStatus, errorThrown) {
                console.error('AJAX Error:', textStatus);
            }
        });


        // Initialize jGrowl for displaying flash messages
        $.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 } ?>
    

     
        // Initialize form validation with formValidation plugin
        $('.tradepair').formValidation({
            framework: 'bootstrap',
            excluded: [':disabled'],
            // icon: {
            //     validating: 'glyphicon glyphicon-refresh'
            // },
            fields: {
                first_buy_amount: {
                    message: '<?php echo "First buy amount is invalid";?>',
                    validators: {
                        notEmpty: {
                            message: '<?php echo "First buy amount is required and cannot be empty";?>'
                        },
                        numeric: {
                            message: 'Please enter a valid number'
                        }
                    }
                },
                open_position_doubled: {
                    message: '<?php echo "Double open position is invalid";?>',
                    validators: {
                        notEmpty: {
                            message: '<?php echo "Double open position is required and cannot be empty";?>'
                        }
                    }
                },
                margin_column_limit: {
                    message: '<?php echo "Margin column limit is invalid";?>',
                    validators: {
                        notEmpty: {
                            message: '<?php echo "Margin column limit is required and cannot be empty";?>'
                        },
                        numeric: {
                            message: 'Please enter a valid number'
                        }
                    }
                },
                whole_profit_ratio: {
                    message: '<?php echo "Whole profit ratio is invalid";?>',
                    validators: {
                        notEmpty: {
                            message: '<?php echo "Whole profit ratio is required and cannot be empty";?>'
                        },
                        numeric: {
                            message: 'Please enter a valid number'
                        }
                    }
                },
                whole_profit_callback: {
                    message: '<?php echo "Whole profit callback is invalid";?>',
                    validators: {
                        notEmpty: {
                            message: '<?php echo "Whole profit callback is required and cannot be empty";?>'
                        },
                        numeric: {
                            message: 'Please enter a valid number'
                        }
                    }
                },
                buyin_callback: {
                    message: '<?php echo "Buyin callback is invalid";?>',
                    validators: {
                        notEmpty: {
                            message: '<?php echo "Buyin callback is required and cannot be empty";?>'
                        },
                        numeric: {
                            message: 'Please enter a valid number'
                        }
                    }
                },
                sub_position_callback: {
                    message: '<?php echo "Subposition callback is invalid";?>',
                    validators: {
                        notEmpty: {
                            message: '<?php echo "Subposition callback is required and cannot be empty";?>'
                        },
                        numeric: {
                            message: 'Please enter a valid number'
                        }
                    }
                }
              }
        });

        // AJAX request when key is pressed in #margin_column_limit input
        $('#margin_column_limit').keyup(function() {
            var num = $(this).val();

            if (num >= 5) {
                $.ajax({
                    type: 'post',
                    dataType: 'html',
                    url: '<?php echo base_url("/margin_column_limit"); ?>',
                    data: {'number': num},
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                    success: function(msg) {
                        if (msg) {
                            $('#ex_demo').html(msg);
                            $('#subposition').css('display', 'block');
                           
                        }
                    },
                    error: function(xhr, textStatus, errorThrown) {
                        console.error('AJAX Error:', textStatus);
                    }
                });
            } else {
                $('#ex_demo').html("");
                $('#subposition').css('display', 'none');
            }
        });

        // Additional AJAX requests for other scenarios (assuming different endpoints)
        // Ensure these are properly configured in your application

        $('#margin_column_limit').keyup(function() {
            var num = $(this).val();

            if (num > 0) {
                $.ajax({
                    type: 'post',
                    dataType: 'html',
                    url: '<?php echo base_url("/margin_column_limit1"); ?>',
                    data: {'number1': num},
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                    success: function(msg) {
                        if (msg) {
                            $('#ex_demo1').html(msg);
                           
                        }
                    },
                    error: function(xhr, textStatus, errorThrown) {
                        console.error('AJAX Error:', textStatus);
                    }
                });
            }
        });

        // Another keyup event handler for margin_column_limit, similar to the above
        $('#margin_column_limit').keyup(function() {
            var num = $(this).val();

            if (num > 0) {
                $.ajax({
                    type: 'post',
                    dataType: 'html',
                    url: '<?php echo base_url("/margin_column_limit2"); ?>',
                    data: {'number2': num},
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                    success: function(msg) {
                        if (msg) {
                            $('#ex_demo2').html(msg);
                           
                        }
                    },
                    error: function(xhr, textStatus, errorThrown) {
                        console.error('AJAX Error:', textStatus);
                    }
                });
            }
        });

        // Example of an event handler
        $('#icon-bck').on('click', function() {
            window.history.back();
        });

        // Initialize Bootstrap Switch
        $(".switchery").bootstrapSwitch();

   

    });

  

</script>






</body>
</html>
