<div class=" pb-5 page-title d-block">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <p class="h5 text-primary fw-bold">
                    Hi, <span class="text-gray"><?php echo $this->session->userdata("name");?></span>
                </p>
            </div>
        </div>
    </div>
</div>
<section class="pb-5">
		<style>
		.btn-default{background:#CCC;}
		</style>
        <div class="container-fluid">
            <div class="row g-4 mb-4">
                <div class="col-12 col-lg-4 col-xl-3">
                    <div class="card border-0">
                        <div class="card-body p-0">
                            
                            <ul class="list-unstyled">
                                <li class="d-flex justify-content-between align-items-center p-3 mb-3 rounded-5" style="background:#8efbb4;">
                                    <div class="d-flex align-items-center">
                                        <i class="cc BTC me-3 fs-3"></i>
                                        <h5 class="h6 fw-semi-bold">Wallet Balance</h5>
                                    </div>
                                    <div class="text-end">
                                        <h5 class="h6 fw-semi-bold">
                                            <?php 	$this->db->order_by("trid","desc");

					$chk=$this->db->get_where("transaction",array("userid"=>$this->session->userdata("user")));

					if($chk->num_rows()>0){

                    $l=$chk->row();

                   echo $wallet=$l->left_amt;}else{echo $wallet=0;}?>
                                        </h5>
                                    </div>
                                </li>
                                <li class="d-flex justify-content-between align-items-center p-3 mb-3 rounded-5" style="background:#ffbb55;">
                                    <div class="d-flex align-items-center">
                                        <i class="cc LTC me-3 fs-3"></i>
                                        <h5 class="h6 fw-semi-bold">
                                            Pending Deposite
                                        </h5>
                                    </div>
                                    <div class="text-end">
                                        <h5 class="h6 fw-semi-bold">
                                             <?php $this->db->select_sum('dep_amt');  

					  $this->db->where("dep_status","Pending"); 

					  $this->db->where("userid",$this->session->userdata("user"));

					  $chk= $this->db->get('deposites');

					 // echo $chk->num_rows();

					  $pending=$chk->row();if($this->db->get_where("deposites",array("userid"=>$this->session->userdata("user")))->num_rows()>0){echo $pending->dep_amt;}else{echo 0;}?>
                                        </h5>
                                    </div>
                                </li>
                                
                            </ul>
                        </div>
                    </div>
                </div>

                <div class="col-12 col-lg-12 col-xl-9">
                	<div class="text-center">
                	<div class="btn-group btn-group-justified">
                      <button type="button" class="btn btn-primary watchlist allbtn">MCX WATCH</button>
                      <button type="button" class="btn btn-default live allbtn">LIVE TRADES</button>
                      <button type="button" class="btn btn-default exit allbtn">EXIT TRADES</button>
                    </div>
                    </div>
                    <div style="position: relative;">
<script>
var socket=io.connect("https://amplecommodity.com:4002");
$(document).ready(function(){
	<?php if(isset($_GET['type'])){?>
	$(".live").trigger("click");
	<?php }?>
	$("#buynow_form").submit(function(e){e.preventDefault();
		var formdata=$(this).serialize();
		$.ajax({
			 type: "POST",
			 url: '<?php echo base_url();?>user/ajax_buynow_form',
			 data: {formdata:formdata},
			 success: function(res){
				 if(res==0){
					alert("Your Trade Started");
					window.location.href='<?php echo base_url().'user?type=trade';?>';
				 }else{
				 alert(res);
				 }
				 //alert(response);
			 }
		});
	});
	$(".watchlist").click(function(){
		$(".allbtn").removeClass("btn-primary,btn-default");
		$(this).addClass("btn-primary");
		$(".live").addClass("btn-default");
		$(".exit").addClass("btn-default");
		$(".watchlist_div").show();
		$(".exit_div").hide();
		$(".live_trdiv").hide();
	});
	$(".live").click(function(){
		$(".allbtn").removeClass("btn-primary,btn-default");
		$(this).addClass("btn-primary");
		$(".watchlist").addClass("btn-default");
		$(".exit").addClass("btn-default");
		$(".watchlist_div").hide();
		$(".live_trdiv").show();
		$(".exit_div").hide();
	});
	$(".exit").click(function(){
		$(".allbtn").removeClass("btn-primary,btn-default");
		$(this).addClass("btn-primary");
		$(".live").addClass("btn-default");
		$(".watchlist").addClass("btn-default");
		$(".watchlist_div").hide();
		$(".live_trdiv").hide();
		$(".exit_div").show();
	});
	socket.on("tr_emit",function(data){
		console.log(data);
		var dd=data.trdata.split('</td><td>');
		//console.log(dd);
		var ddd=dd[0].split('>');
		console.log(ddd);
		if(ddd[3]){
		$("#"+data.trid+'_rate').html(dd[3]);
		}else{
		$("#"+data.trid+'_rate').html(dd[1]);
		}
		$("#"+data.trid+'_chg').html(dd[2]+' ('+dd[3]+'%)');
		$("#"+data.trid+'_open').html(dd[4]);
		$("#"+data.trid+'_high').html(dd[5]);
		$("#"+data.trid+'_low').html(dd[6]);
		//$("#div"+data.trid).css("border-left","2px solid "+data.color);
		$("#div"+data.trid).css("background-color",data.color);
		setTimeout(function(){ $("#div"+data.trid).css("background","#212F3C"); }, 2000);
	});
	socket.on("tr_emit_user",function(data){
		//console.log(data.trid);
		$("tr#"+data.trid).html(data.tr);
//		$("tr#"+data.trid+" td:nth-child(2)").css("background-color",data.color);
//		setTimeout(function(){ $("tr#"+data.trid+" td:nth-child(2)").css("background","none"); }, 2000);
	});
	setInterval(function(){
				var cur_rate_class=$("#cur_rate").attr("class");
				//console.log(cur_rate_class);
				//$(".GOLD_ticker").text($("tr#GOLD td:nth-child(2)").text());
				if(cur_rate_class!=""){
				var v=$("tr#"+cur_rate_class+" td:nth-child(2)").text();
				}
				$("#cur_rate").val(v);
				}, 1000);
	$("#metal_select").change(function(){
				var vall=$(this).val();
				var t=vall.split("-");
				$("#cur_rate").attr("class",t[0]);
			//	$("#limit").attr("class",t[0]+"_ticker form-control");
			});
			$("#marketprice").click(function(){
				if ($('input[type="checkbox"]').is(':checked')){
					$("#limit").attr('disabled',true);
				}else{
					$("#limit").removeAttr('disabled');
				}
				//
			});
			$(".openmodal").click(function(){
				$('#myModal').modal('show');
			});
			$("#close_trade_modal").click(function(){
				$('#myModal').modal('hide');
			});
			
});
</script>
<style>
.metal_divs{margin-top:5px; background:#212F3C; border:2px solid #85929E;border-radius:10px;}
.metalname{color:#E5E7E9;}
.metalrate{color:#fff;}
.metalchg{color:#E5E7E9;}
.ratess span, {color:#FF0; font-size:12px;}
.ohl{color:#fff; font-size:12px;}
.btn-xs{ padding:3px 5px;min-width:auto; font-size:12px;}
</style>
                        <div>
                        <div class="watchlist_div row">
                            <?php $metals=$this->db->get("metals")->result();
                        foreach($metals as $d){?>
                        <div class="col-md-4">
                    	<div class="metal_divs"  style="padding:10px;"  id="div<?php echo $d->metal;?>">
                        	<h55 style="color:#fff;"><?php echo $d->metal;?><span class="pull-right"><b class="metalrate" id="<?php echo $d->metal;?>_rate">0</b>&nbsp;&nbsp;<button type="button" class="btn btn-xs btn-warning pull-right openmodal">TRD</button></span></h5>
                            <div class="row ratess" style="margin-top:5px;">
                    <div class="col-md-4 col-4 text-center ohl">O <span  id="<?php echo $d->metal;?>_open"></span></div>
                    <div class="col-md-4 col-4 text-center ohl">H <span  id="<?php echo $d->metal;?>_high"></span></div>
                    <div class="col-md-4 col-4 text-center ohl">L <span  id="<?php echo $d->metal;?>_low"></span></div>
                            </div>
                            
                        </div>
                    </div>
                            <?php }?>
                            </div>
                           
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Start Trade</h4>
      </div>
      <div class="modal-body">
        <form  action="<?php echo base_url();?>home/buynow" id="buynow_form" method="post">



                        <div class="row">



                        <div class="form-group col-md-6 col-6">



                        <div class="frb frb-warning">



						<input type="radio" name="type" id="radio-button-0" checked="checked" value="BUY">



						<label for="radio-button-0">



							<span class="frb-title">BUY</span>



						</label>



                        </div>



                        </div>



                        <div class="form-group col-md-6 col-6">



                            <div class="frb frb-primary">



                                <input type="radio" id="radio-button-1" name="type" value="SELL">



                                <label for="radio-button-1">



                                    <span class="frb-title">SELL</span>



                                </label>



                            </div>



                        </div>



                    	<div class="form-group col-md-6">



                        	<label>Asset</label>



                            <select name="metal" class="form-control" required id="metal_select">

					

                            	<option value="">Select Asset</option>

                                

                                <?php $this->db->select("*");

					$this->db->from("metals");

					$this->db->join("user_assets","user_assets.asset_type=metals.metal");

					$this->db->where("user_assets.uid",$this->session->userdata("user"));

					$metals=$this->db->get()->result();

					foreach($metals as $d){?>

                    <option value="<?php echo $d->metal.'-'.$d->lots;?>"><?php echo $d->metal;?></option>

                    <?php }?>



                                



                            </select>



                            <input type="hidden" name="cur_rate" id="cur_rate" value="" class="" />



                        </div>



                        <div class="form-group col-md-6">



                        	<label>Limit</label>





                            <input type="text" name="setlimit" id="limit"  required class="form-control" />

       <label><input id="marketprice" type="checkbox" name="marketprice" value="marketprice" /> Market Rate</label>



                        </div>



                        <?php $lim=$this->db->get_where("users",array("id"=>$this->session->userdata("user")))->row();



						//print_r($lim);?>



                        <div class="form-group col-md-6 col-6">



                        	<label>Lot Size</label>



                            <input type="text" name="lotsize" required class="form-control" />



                            <!--<select class="form-control" name="lotsize" required>



                            <?php 



							 //for($i=0;$i<$lim->trade_limit+1;$i++){?>



                            <option value="<?php //echo $i+1;?>"><?php //echo $i+1;?> Lot</option>



                            <?php //}?></select>-->



                        </div>



                        <div class="form-group col-md-6 col-6">



                        	<label>Set Target</label>



                            <input type="text" name="target" class="form-control" />



                        </div>



                        <div class="form-group col-md-6 col-6">



                        	<label>Set StopLoss</label>



                            <input type="text" name="stoploss" class="form-control" />



                        </div>



                        <div class="form-group col-md-6 col-6">



                        	<label>Wallet PIN</label>



                            <input type="text" name="pin" required="" class="form-control">



                        </div>



                        </div>


                         <?php 

						 if($this->db->get_where("users",array("id"=>$this->session->userdata("user"),"usertrade!="=>""))->num_rows()>0){



			?><div class="alert alert-danger">Your Trade is Disabled from company. Please Contact Company.</div><?php 



		}else{?>
        <button type="button" class="btn btn-warning pull-right" id="close_trade_modal" data-dismiss="modal">Close</button><?php 

						 $admin=$this->db->get("admin")->row(); if($wallet>1000 && $admin->trade_enable==""){?>

                        <button type="submit" class="btn btn-success btn-block buybtn btn-block">START TRADE</button>



                        <?php }else{?> 



                        <button type="submit" class="btn btn-success btn-block buybtn btn-block">START TRADE</button>



                        <?php if($wallet<=1000){?> 



                        <label>Please add money to your wallet to start trading.</label>



                        <?php }else if($admin->trade_enable!=""){?>



                        <label>Trade is Closed due to Server Technical issue.</label>



                        <?php }}}?>



                    </form>
      </div>
    </div>

  </div>
</div>
<div class="table-card live_trdiv  border-0 rounded-default  box-shadow    transaction-table" style="display:none;">

                        <div class="card-body p-0">
                        	<script>



//var socket=io.connect("http://localhost:4000");



function edit_trade1(id){



	//var id=$(this).attr("id");



	//alert(id);



	$.ajax({



	 type: "POST",dataType:'json',



	 url: '<?php echo base_url();?>home/edit_trade',



	 data: {id:id},



	 success: function(res){//alert(res.target);



		$("#target").val(res.target);



		$("#stoploss").val(res.stoploss);



		$("#edit_lot").val(res.lotsize);



		$("#bssid").val(res.bsid);



		$("#bstype").val(res.type);



		



		 }



	 });



}



function sq_offtrade(id){



		// alert(id);



		$("#"+id).remove();



		//$(this).remove();



		 $.ajax({



				 type: "POST",



				 url: '<?php echo base_url();?>home/square_off',



				 data: {id:id},



				 success: function(res){



					 alert(res);



					 //$("div#"+id).remove();



					 //alert("Trade Exit");



					 window.location.href="<?php echo base_url();?>user";



					 



					}



				 });



	 }



$(document).ready(function(){



	$("#edit_trade_submit").submit(function(e){e.preventDefault();



								var formdata=$(this).serialize();



								//alert(formdata);



								var id=$("#bssid").val();



								var t=$("#target").val();



								var s=$("#stoploss").val();



								$.ajax({



								 type: "POST",



								 url: '<?php echo base_url();?>home/edit_trade_submit',



								 data: {formdata:formdata},



								 success: function(res){//alert(res);return false;



									 //if(res==0){



										//window.location.href = '<?php //echo base_url();?>home/dashboard'; 



									 //}else{

										if(res!=0){

										 alert(res);

										}else{

										 alert("Trade Updated");

										 $('#myModal2').modal('hide');

									 	}



									 //}



									 //$("#msg").text(res);



//									 if(res=="Trade Updated"){



//										$("#target"+id).text(t);



//										$("#stoploss"+id).text(s); 



//									 }



									 }



								 });



							});



	socket.on("tr_emit",function(data){



		//console.log(data.trdata);



		$("tr#"+data.trid).html(data.trdata);



		$("tr#"+data.trid+" td:nth-child(2)").css("background-color",data.color);



		setTimeout(function(){ $("tr#"+data.trid+" td:nth-child(2)").css("background","none"); }, 2000);



	});



	socket.on("tr_emit_user",function(data){



		//console.log(data.trid);



		$("div#"+data.trid).html(data.tr);



		$("tr#"+data.trid+" div:nth-child(2)").css("background-color",data.color);



//		setTimeout(function(){ $("tr#"+data.trid+" td:nth-child(2)").css("background","none"); }, 2000);



	});



	setInterval(function(){



				var cur_rate_class=$("#cur_rate").attr("class");



				//console.log(cur_rate_class);



				//$(".GOLD_ticker").text($("tr#GOLD td:nth-child(2)").text());



				if(cur_rate_class!=""){



				var v=$("tr#"+cur_rate_class+" td:nth-child(2)").text();



				}



				$("#cur_rate").val(v);



				}, 1000);



	$("#metal_select").change(function(){



				var vall=$(this).val();



				var t=vall.split("-");



				$("#cur_rate").attr("class",t[0]);



			//	$("#limit").attr("class",t[0]+"_ticker form-control");



			});



			$("#marketprice").click(function(){



				if ($('input[type="checkbox"]').is(':checked')){



					$("#limit").attr('disabled',true);



				}else{



					$("#limit").removeAttr('disabled');



				}



				//



			});



			 $(".exit").click(function(e){e.preventDefault();

 $("#table_exits").html('<div class="col-md-12 text-center"><i class="fa fa-spin fa-spinner"></i> Loading Trades</div>');

			     var id=$(this).attr("id");



				 $.ajax({



				 type: "POST",



				 url: '<?php echo base_url();?>home/exit_positions',



				 data: {},



				 success: function(res){



					 $("#table_exits").html(res);



					}



				 });



			 });



			 $("#refresh").click(function(e){e.preventDefault();



				 $.ajax({



					   url: '<?php echo base_url();?>home/wallet_refresh',



					   type: 'GET',



					   success: function(res) {



						   $("#walletamt").text(res);



					   }



					});



			 });



});



</script>
                      <?php $trades=$this->db->get_where("buysell",array("status!="=>"EXIT","uid"=>$this->session->userdata("user")))->result();	
			 		  $i=1;foreach($trades as $tr){?>
                                    <div class="row" style="margin-top:10px; border-top:2px solid #333;" id="<?php echo $tr->bsid;?>">
                                    <div class="col-md-12 text-center">No Trades</div>
                                    </div>
                                    <?php }?>
                        </div>
                    </div>
                    <div class="exit_div"  style="display:none;">
                    
                    <div id="table_exits">
                                    
                    </div>
                    </div>
                        </div>
                        
                    </div>
                </div>

                
            </div>

            
        </div>
    </section>
    
<div class="copyrights bg-light-200 border-gray-light py-4">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12 col-sm-9 col-md-8">
                <p class="mb-0 text-center text-sm-start text-gray fs-6">
                    © Copyright 2021
                    <a href="#" class="copyright-links fw-bold text-gray ms-1 text-hover-primary">
                        Ample Commodity
                    </a> All Rights Reserved.
                </p>
            </div>
        </div>
    </div>
</div>
</div>


        </div>
    <script src="<?php echo base_url();?>newsite/dashboard.js.download"></script>
<script src="<?php echo base_url();?>newsite/validator-init.js.download"></script>
<script src="<?php echo base_url();?>newsite/waves.min.js.download"></script>
<script src="<?php echo base_url();?>newsite/scrollIt.js.download"></script>
<script src="<?php echo base_url();?>newsite/scrollit-init.js.download"></script>
<script src="<?php echo base_url();?>newsite/helpscout-init.js.download"></script>
<script src="<?php echo base_url();?>newsite/scripts.js.download"></script>
    

</body></html>