<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en"><head>
	<meta charset="utf-8">
	<title>Welcome to CodeIgniter</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">

<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>
     <link href="https://fonts.googleapis.com/css?family=Arimo" rel="stylesheet"> <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">    
     <script>
	 var socket=io.connect("http://trade-crypto-planet.com:4003");
	 $(document).ready(function(){
		setInterval(function(){ 
		//var gold=$("tr#GOLD td:nth-child(2)").text();
		var gold=$("tr#GOLD td:nth-child(2)").text();
		var goldm=$("tr#GOLDM td:nth-child(2)").text();
		var silver=$("tr#SILVER td:nth-child(2)").text();
		var silverm=$("tr#SILVERM td:nth-child(2)").text();
		var copper=$("tr#COPPER td:nth-child(2)").text();
		var copperm=$("tr#COPPERM td:nth-child(2)").text();
		var aluminium=$("tr#ALUMINIUM td:nth-child(2)").text();
		var alumini=$("tr#ALUMINI td:nth-child(2)").text();
		var crudeoil=$("tr#CRUDEOIL td:nth-child(2)").text();
		var crudeoilm=$("tr#CRUDEOILM td:nth-child(2)").text();
		var lead=$("tr#LEAD td:nth-child(2)").text();
		var leadmini=$("tr#LEADMINI td:nth-child(2)").text();
		var zinc=$("tr#ZINC td:nth-child(2)").text();
		var zincm=$("tr#ZINCMINI td:nth-child(2)").text();
		var nickel=$("tr#NICKEL td:nth-child(2)").text();
		var nickelm=$("tr#NICKELM td:nth-child(2)").text();
		var naturalgas=$("tr#NATURALGAS td:nth-child(2)").text();
		var menthaoil=$("tr#MENTHAOIL td:nth-child(2)").text();
		$.ajax({
		   url: 'https://www.mcxdata.in/',
		   type: 'GET',headers: { 'Access-Control-Allow-Origin': '*' },
		   success: function(res) {
					var data = $.parseHTML(res); 
					$(data).find('#fullMcxPriceTable').each(function(){
					$('#fullMcxPriceTable').html($(this).html());
					
					//Start Removing ad rows here
					$("table#fullMcxPriceTable tr:nth-child(7)").remove();
					$("table#fullMcxPriceTable tr:nth-child(17)").remove();
					$("tr#USDINR").remove();$("tr#EURINR").remove();
					$("tr#JPYINR").remove();$("tr#GBPINR").remove();
					//End Removing ad rows here 
					var tdata=$('#fullMcxPriceTable').html();
					//socket.emit("rate_update",{table:tdata});
					
					var goldnew=$("tr#GOLD td:nth-child(2)").text();
					var goldmnew=$("tr#GOLDM td:nth-child(2)").text();
				    var silvernew=$("tr#SILVER td:nth-child(2)").text();
					var silvermnew=$("tr#SILVERM td:nth-child(2)").text();
					var coppernew=$("tr#COPPER td:nth-child(2)").text();
					var coppermnew=$("tr#COPPERM td:nth-child(2)").text();
					var aluminiumnew=$("tr#ALUMINIUM td:nth-child(2)").text();
					var alumininew=$("tr#ALUMINI td:nth-child(2)").text();
					var crudeoilnew=$("tr#CRUDEOIL td:nth-child(2)").text();
					var crudeoilmnew=$("tr#CRUDEOILM td:nth-child(2)").text();
					var leadnew=$("tr#LEAD td:nth-child(2)").text();
					var leadmininew=$("tr#LEADMINI td:nth-child(2)").text();
					var zincnew=$("tr#ZINC td:nth-child(2)").text();
					var zincmnew=$("tr#ZINCMINI td:nth-child(2)").text();
					var nickelnew=$("tr#NICKEL td:nth-child(2)").text();
					var nickelmnew=$("tr#NICKELM td:nth-child(2)").text();
					var naturalgasnew=$("tr#NATURALGAS td:nth-child(2)").text();
					var menthaoilnew=$("tr#MENTHAOIL td:nth-child(2)").text();
					
					if(gold!=goldnew){
						var newgoldtr=$("tr#GOLD").html();
						check_new("GOLD",gold,goldnew,100);
					}else{socket.emit("tr_emit",{trdata:$("tr#GOLD").html(),trid:"GOLD",color:"none",single_val:goldnew});}
					if(goldm!=goldmnew){
						var newgoldtr=$("tr#GOLDM").html();
						check_new("GOLDM",goldm,goldmnew,10);
					}else{socket.emit("tr_emit",{trdata:$("tr#GOLDM").html(),trid:"GOLDM",color:"none",single_val:goldmnew});}
					if(silver!=silvernew){
						var newsilvertr=$("tr#SILVER").html();
						check_new("SILVER",silver,silvernew,30);
					}else{socket.emit("tr_emit",{trdata:$("tr#SILVER").html(),trid:"SILVER",color:"none",single_val:silvernew});}
					if(silverm!=silvermnew){
						var newsilvertr=$("tr#SILVERM").html();
						check_new("SILVERM",silverm,silvermnew,5);
					}else{socket.emit("tr_emit",{trdata:$("tr#SILVERM").html(),trid:"SILVERM",color:"none",single_val:silvermnew});}
					if(copper!=coppernew){
						var newcoppertr=$("tr#COPPER").html();
						check_new("COPPER",copper,coppernew,1000);
					}else{socket.emit("tr_emit",{trdata:$("tr#COPPER").html(),trid:"COPPER",color:"none",single_val:coppernew});}
					if(copperm!=coppermnew){
						var newcoppertr=$("tr#COPPERM").html();
						check_new("COPPERM",copperm,coppermnew,250);
					}else{socket.emit("tr_emit",{trdata:$("tr#COPPERM").html(),trid:"COPPERM",color:"none",single_val:coppermnew});}
					if(aluminium!=aluminiumnew){
						var newaluminiumtr=$("tr#ALUMINIUM").html();
						check_new("ALUMINIUM",aluminium,aluminiumnew,5000);
					}else{socket.emit("tr_emit",{trdata:$("tr#ALUMINIUM").html(),trid:"ALUMINIUM",color:"none",single_val:aluminiumnew});}
					if(alumini!=alumininew){
						var newaluminitr=$("tr#ALUMINI").html();
						check_new("ALUMINI",alumini,alumininew,1000);
					}else{socket.emit("tr_emit",{trdata:$("tr#ALUMINI").html(),trid:"ALUMINI",color:"none",single_val:alumininew});}
					if(crudeoil!=crudeoilnew){
						var crudeoiltr=$("tr#CRUDEOIL").html();
						check_new("CRUDEOIL",crudeoil,crudeoilnew,100);
					}else{socket.emit("tr_emit",{trdata:$("tr#CRUDEOIL").html(),trid:"CRUDEOIL",color:"none",single_val:crudeoilnew});}
					if(crudeoilm!=crudeoilmnew){
						var crudeoilmtr=$("tr#CRUDEOILM").html();
						check_new("CRUDEOILM",crudeoilm,crudeoilmnew,10);
					}else{socket.emit("tr_emit",{trdata:$("tr#CRUDEOILM").html(),trid:"CRUDEOILM",color:"none",single_val:crudeoilmnew});}
					if(lead!=leadnew){
						var leadtr=$("tr#LEAD").html();
						check_new("LEAD",lead,leadnew,5000);
					}else{socket.emit("tr_emit",{trdata:$("tr#LEAD").html(),trid:"LEAD",color:"none",single_val:leadnew});}
					if(leadmini!=leadmininew){
						var leadminitr=$("tr#LEADMINI").html();
						check_new("LEADMINI",leadmini,leadmininew,1000);
					}else{socket.emit("tr_emit",{trdata:$("tr#LEADMINI").html(),trid:"LEADMINI",color:"none",single_val:leadmininew});}
					
					}else{socket.emit("tr_emit",{trdata:$("tr:nth-child(14)").html(),trid:"ZINKMINI",color:"none",single_val:zincmnew});}
					if(nickel!=nickelnew){
						var nickeltr=$("tr#NICKEL").html();
						check_new("NICKEL",nickel,nickelnew,250);
					}else{socket.emit("tr_emit",{trdata:$("tr#NICKEL").html(),trid:"NICKEL",color:"none",single_val:nickelnew});}
					if(nickelm!=nickelmnew){
						var nickelmtr=$("tr#NICKELM").html();
						check_new("NICKELM",nickelm,nickelmnew,100);
					}else{socket.emit("tr_emit",{trdata:$("tr#NICKELM").html(),trid:"NICKELM",color:"none",single_val:nickelmnew});}
					if(naturalgas!=naturalgasnew){
						var naturalgastr=$("tr#NATURALGAS").html();
						check_new("NATURALGAS",naturalgas,naturalgasnew,1250);
					}else{socket.emit("tr_emit",{trdata:$("tr#NATURALGAS").html(),trid:"NATURALGAS",color:"none",single_val:naturalgasnew});}
					if(menthaoil!=menthaoilnew){
						var menthaoiltr=$("tr#MENTHAOIL").html();
						check_new("MENTHAOIL",menthaoil,menthaoilnew,1);
					}else{socket.emit("tr_emit",{trdata:$("tr#MENTHAOIL").html(),trid:"MENTHAOIL",color:"none",single_val:menthaoilnew});}
					});
			   }
			});
		 }, 1000); 
		 function check_new(metal,oldrate,rate,lotsize){
	     var trdata=$("tr#"+metal).html();
		 var single_val= $("tr#"+metal + " td:nth-child(2)").text();
		 //console.log(single_val);
		// console.log(trdata);
		 if(parseInt(rate)>parseInt(oldrate)){
			var color="green"; 
		 }else{
			var color="red";
		 }
		 //var color=getRandomColor();
		 $("tr#"+metal+" td:nth-child(2)").css("background-color",color);
		 socket.emit("tr_emit",{trdata:trdata,trid:metal,color:color,single_val:single_val});
		 setTimeout(function(){ $("tr#"+metal+" td:nth-child(2)").css("background","none"); }, 5000);
		 $.ajax({
				 type: "POST",
				 url: '<?php echo base_url();?>welcome/check_new',
				 data: {metal:metal,rate:rate,lotsize:lotsize},
				 success: function(res){
					 //console.log(res);
					 //alert(response);
				//	 console.log(response)
				//	$("#rate_return").html(response);
					}
				 });
		}
			$("#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');
				}
				//
			});
			$("#test_trade").click(function(){
				$("#test_trade_form").toggle();
			});
			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);
			//Live Trades
			setInterval(function(){
				$.ajax({
				 type: "POST",
				 url: '<?php echo base_url();?>welcome/live_trades',
				 data: {},
				 success: function(res){
					 $("#live_trades").html(res);
					}
				 });
			 }, 1000);
			 $(".exits").click(function(e){e.preventDefault();
			     var id=$(this).attr("id");
				 var date="<?php echo date("Y-m-d");?>";
				 $.ajax({
				 type: "POST",
				 url: '<?php echo base_url();?>welcome/exit_positions',
				 data: {id:id,date:date},
				 success: function(res){
					 $("#table_"+id).html(res);
					}
				 });
			 });
			 $(".manual_val").click(function(){
				var metal=$("#selected_metal").val().split('-');
				var oldrate=$("#"+metal[0]).text();
				var metal_val=$("#metal_val").val();
				if(metal[0]=="" || metal_val==""){alert("Please Select Metal an Put Rate.");return false;}
				///$("tr#"+metal).text(metal_val);
				$("tr#"+metal[0]+" td:nth-child(2)").text(metal_val);
				check_new(metal[0],oldrate,metal_val,metal[1]);
			 });
			 $("#marketprice").click(function(){
				if ($('input[type="checkbox"]').is(':checked')){
					$("#limit").attr('disabled',true);
				}else{
					$("#limit").removeAttr('disabled');
				}
				//
			});

		 });
		//function getRandomColor() {
//		  var letters = '0123456789ABCDEF';
//		  var color = '#';
//		  for (var i = 0; i < 6; i++) {
//			color += letters[Math.floor(Math.random() * 16)];
//		  }
//		  return color;
//		}
	 </script>
     <style>label{color:#999;}</style>
</head>
<body style="font-family: 'Arimo', sans-serif;">
<div class="container-fluid">
	<div class="row">
    	<div class="col-md-5">
        	<h4 class="text-danger">GuruCommodity Live MCX</h4>
             <form class="form-inline" action="">
              <div class="form-group">
              <select id="selected_metal" required class="form-control">
              	<option value="">Select Metal</option>
                <?php $this->db->select("metal,lots");$metals=$this->db->get("metals")->result();
				foreach($metals as $m){?>
                <option value="<?php echo $m->metal.'-'.$m->lots;?>"><?php echo $m->metal;?></option>
                <?php }?>
              </select>
              </div>
              <div class="form-group">
                <input type="number" placeholder="Enter Value" required class="form-control" id="metal_val">
              </div>
              <button type="button" class="btn btn-danger manual_val">Update</button>
            </form> 
            <table id="fullMcxPriceTable" class="table table-condensed table-bordered table-striped">
                <!--<tr><th>Commodity</th><th>Price</th><th>Chg</th><th>%Chg</th><th>Open</th><th>High</th><th>Low</th><th>Time</th></tr>-->
				<?php //$metals=$this->db->get("metals")->result();
					//foreach($metals as $d){?>
                <!--<tr id="<?php //echo $d->metal;?>" class="metals_tr">
                	<td><?php //echo $d->metal;?></td>
                    <td></td>
               		<td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td></tr>-->
                <?php //}?>
                </table>
        </div>
        <div class="col-md-7">
        <h4 class="text-primary">Customer Trades<button type="button" id="test_trade" class="btn btn-sm btn-warning pull-right">Add Test Trade</button></h4>
   		<form id="test_trade_form" action="<?php echo base_url();?>welcome/buynow" method="post" style="background:#FBFBFB; border:5px solid antiquewhite; padding:10px; display:none;">
                        <div class="row">
                        <div class="form-group col-md-4">
                        	<label>TYPE</label><br/>
                        	<label>
                              <input type="radio" name="type" value="BUY" checked> BUY
                            </label>
                            <label>
                              <input type="radio" name="type" value="SELL"> SELL
                            </label>
                        </div>
                    	<div class="form-group col-md-4">
                        	<label>Asset</label>
                            <select name="metal" class="form-control" required id="metal_select">
                            	<option value="">Select Asset</option>
                                <option value="GOLD-100">GOLD</option>
                                <option value="GOLDM-10">GOLDM</option>
                                <option value="SILVER-30">SILVER</option>
                                <option value="SILVERM-5">SILVERM</option>
                                <option value="COPPER-1000">COPPER</option>
                                <option value="COPPERM-250">COPPERM</option>
                                <option value="ALUMINIUM-5000">ALUMINIUM</option>
                                <option value="ALUMINI-1000">ALUMINI</option>
                                <option value="CRUDEOIL-100">CRUDEOIL</option>
                                <option value="CRUDEOILM-10">CRUDEOILM</option>
                                <option value="ZINC-5000">ZINC</option>
                                <option value="ZINCMINI-1000">ZINCMINI</option>
                                <option value="LEAD-5000">LEAD</option>
                                <option value="LEADMINI-1000">LEADMINI</option>
                                <option value="NICKEL-250">NICKEL</option>
                                <option value="NICKELM-100">NICKELM</option>
                                <option value="NATURALGAS-1200">NATURALGAS</option>
                            </select>
                            <input type="hidden" name="cur_rate" id="cur_rate" value="" class="" />
                        </div>
                        <div class="form-group col-md-4">
                        	<label>Lot Size</label>
                            <input type="number" name="lotsize" required class="form-control" />
                        </div>
                        <div class="form-group col-md-4">
                        	<label>Limit</label>
       <label class="pull-right"><input id="marketprice" type="checkbox" name="marketprice" value="marketprice" /> Market Rate</label>
                            <input type="text" name="setlimit" id="limit"  required class="form-control" />
                        </div>
                        <div class="form-group col-md-4">
                        	<label>Set Target</label>
                            <input type="text" name="target" class="form-control" />
                        </div>
                        <div class="form-group col-md-4">
                        	<label>Set StopLoss</label>
                            <input type="text" name="stoploss" class="form-control" />
                        </div>
                         
                        </div>
                        <button type="submit" class="btn btn-warning buybtn">Submit</button>
                    </form>
        <ul class="nav nav-tabs">
          <li class="active"><a data-toggle="tab" href="#home">Live Trades</a></li>
          <li><a data-toggle="tab" href="#menu1" class="exits" id="BUY">BUY EXIT</a></li>
          <li><a data-toggle="tab" href="#menu2" class="exits" id="SELL">SELL EXIT</a></li>
        </ul>
        
        <div class="tab-content">
          <div id="home" class="tab-pane fade in active">
            <table class="table table-bordered table-condensed table-striped">
            <thead><tr><th>S.No.</th><th>Client</th><th>Metal</th><th>TradeType</th><th>Live</th><th>Limit</th><th>Target</th><th>Stoploss</th><th>Profit/Loss</th><th>Time</th><th>Action</th></tr></thead>
            <tbody id="live_trades">
            <tr><td colspan="10"><h3>Loading Trades...</h3></td></tr>
            </tbody>
        </table>
          </div>
          <div id="menu1" class="tab-pane fade">
            <table class="table table-bordered table-condensed table-striped">
            <thead><tr><th>S.No.</th><th>Client</th><th>Metal</th><th>TradeType</th><th>SqOff</th><th>Limit</th><th>Target</th><th>Stoploss</th><th>Profit/Loss</th><th>Time</th><th>Action</th></tr></thead>
            <tbody id="table_BUY">
            </tbody>
        </table>
          </div>
          <div id="menu2" class="tab-pane fade">
            <table class="table table-bordered table-condensed table-striped">
            <thead><tr><th>S.No.</th><th>Client</th><th>Metal</th><th>TradeType</th><th>SqOff</th><th>Limit</th><th>Target</th><th>Stoploss</th><th>Profit/Loss</th><th>Time</th><th>Action</th></tr></thead>
            <tbody id="table_SELL">
            </tbody>
        </table>
          </div>
        </div>
        </div>
    </div>
</div>

</body>
</html>