<?php
	include'session.php';
	include'../connection.php';
	include'../functions.php';
?>
<html>
<head>
	<title>Edit Generate Amount</title>
	<!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    
	<link rel="icon" href="images/logo.png" type="image/gif" sizes="16x16">
	
	<!-- Admin CSS File Link -->
	<link rel="stylesheet" href="css/style.css">
	
	<!-- User JAVA SCRIPT File Link -->
	<script type="text/javascript" src="js/script.js"></script>

	
	<!-- Font Awesome Icone -->
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
	
	<style>
		.div1{
			background:#f7f7f7;
			width:50%;
			height:500px;
			float:left;
			text-align:center;
		}
		.subDiv1{
			background:#6b5127;
			color:white;
			width:30%;
			height:40px;
			line-height:40px;
			margin:15px 0px 0px 10px;
			border-radius:;
			font-size:17px;
			float:left;
		}
		.subDiv1Add{
			background:#6b5127;
			color:white;
			width:30%;
			height:100px;
			line-height:100px;
			margin:10px 0px 10px 10px;
			border-radius:;
			font-size:17px;
			float:left;
		}
		.subDiv2{
			color:white;
			width:64%;
			height:40px;
			line-height:40px;
			margin:15px 0px 0px 0px;
			border-radius:;
			font-size:19px;
			float:left;
		}
		
		.subDiv2Add{
			color:white;
			width:64%;
			height:100px;
			line-height:100px;
			margin:10px 0px 10px 0px;
			border-radius:;
			font-size:19px;
			float:left;
		}
		.div1 input, .div1 select, .div1 p{
			color:black;
			text-align:left;
			width:100%;
			height:40px;
			margin:0px;
			padding:2px 10px;
			border:1px solid #6b5127;
			font-size:17px;
		}
		input[type=file]
		{
			border:none;
		}
		.div1 textarea{
			width:100%;
			height:100px;
			padding:2px 10px;
			border:1px solid #6b5127;
			font-size:19px;
		}
		.divImg{
			background:white;
			width:94%;
			height:100px;
			margin:15px 10px;
			padding:8px;
			border:1px solid #6b5127;
			font-size:19px;
			text-align:left;
		}
		.divImg img{
			width:19%;
			height:80px;
			float:left;
		}
		.divImg input{
			width:70%;
			float:right;
			font-size:15px;
		}
		input[type=submit]{
			background:#6b5127;
			width:94%;
			margin:15px 10px 10px 0px;
			padding:2px;
			color:white;
			font-size:25px;
			transition:1s;
			text-align:center;
		}
		input[type=submit]:hover{
			background:white;
			color:#6b5127;
			border:1px solid #6b5127;
			cursor:pointer;
			transition:1s;
		}
		
		@media(max-width:768px)
		{
			.div1{
				width:100%;
				height:auto;
			}
			.nav,.container{
				height:auto;
			}
			.subDiv1{width:35%; font-size:15px;}
			.subDiv2{width:59%; font-size:15px;}
			.subDiv1Add{width:35%; font-size:15px;}
			.subDiv2Add{width:59%; font-size:15px;}
			input[type=submit]{width:94%; margin:15px 2%;}
		}
	</style>
	
	<link rel="stylesheet" href="css/mediaStyle.css">
</head>
<body>
	<!--Header Divison-->
	<?php
		require'header.php';
	?>
	
	<!--Container Division-->
	<div class="container">
	    <?php
			include'menu.php';
			$TxnId = $_GET['id'];
			
			$data1    = "t.SellerId, t.FirmId, t.Amount, t.TxnWith, t.BankWalletId,";
			$data2    = "s.Name,";
			$data3    = "f.FirmName,";
			$data4    = "b.BankName";
			
			$IJstNs   = "INNER JOIN Seller As s ON t.SellerId=s.Id";
			$IJstNf   = "INNER JOIN seller_firm As f ON t.FirmId=f.Id";
			$IJstNb   = "INNER JOIN bank As b ON t.TxnWith=b.Id";
			
			
			$quTxn    = "SELECT $data1 $data2 $data3 $data4 FROM seller_txn AS t $IJstNs $IJstNf $IJstNb WHERE t.Id='$TxnId'";
			$reTxn    = mysqli_query($con,$quTxn);
			$dataTxn  = mysqli_fetch_array($reTxn);
			
			$Seller_id = $dataTxn[0];
			$firm_id   = $dataTxn[1];
			$amount    = $dataTxn[2];
			$bank_id   = $dataTxn[3];
			$bank_W_id = $dataTxn[4];
			$S_Name    = $dataTxn[5];
			$f_Name    = $dataTxn[6];
			$bank_Name = $dataTxn[7];
			
			
			if(isset($_POST['submit']))
			{
			    $amount = $_POST['amount'];
			    $Seller = $_POST['Seller'];
			    $firm   = $_POST['firm'];
			    $bankId = $_POST['bankId'];
				    
			    $UpdateTxn = "UPDATE seller_txn SET SellerId='$Seller', FirmId='$firm', Amount='$amount', TxnWith='$bankId' WHERE Id='$TxnId'";
			    if(mysqli_query($con,$UpdateTxn))
			    {
			        if($amount<0)
			        {
			            $UpdateBankWallet = "UPDATE bank_wallet SET BankId='$bankId', Txn='$amount', TxnWith='$Seller' WHERE Id='$bank_W_id'";
			        }
			        else
			        {
			            $UpdateBankWallet = "UPDATE bank_wallet SET BankId='$bankId', Txn='-$amount', TxnWith='$Seller' WHERE Id='$bank_W_id'";
			        }
			        if(mysqli_query($con,$UpdateTxn))
			        {
			            if($amount>0)
			            {
			                $UpdateAdminWallet = "UPDATE wallet SET Txn='$amount', TxnWith='$Seller' WHERE TxnId='$TxnId'";
			            }
			            else
			            {
			                $UpdateAdminWallet = "UPDATE wallet SET Txn='-$amount', TxnWith='$Seller' WHERE TxnId='$TxnId'";
			            }
			            if(mysqli_query($con,$UpdateAdminWallet))
			            {
			            
			                echo "<div class='msg-div'>
			    	            <form method='post'>
		       			            <h3>Txn Updated Successfully !</h3><br>
	    				            <i class='fa fa-check-circle' aria-hidden='true' id='okIcon'></i><br><br>
					                <button type='submit' class='btnPopup'>OK</button><br><br>
				                </form>
		                    </div>";
			            }
			        }
				}
				else
				{
				    echo "<div class='msg-div'>
			            <form method='post'>
				            <h3>Something Error !</h3><br>
				            <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				            <button type='submit' class='btnPopup'>OK</button><br><br>
			            </form>
		            </div>";
				}
			}	
			   
		?>
		<div class="content">
			<div class="head">
				<div class="title"><p>Edit Generate Amount :-</p></div>
				<div class="msg"><marquee><?php include'../msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
				<form method="post" enctype="multipart/form-data">
					<div class="div1">
						<div class="subDiv1">Txn Id. : </div><div class="subDiv2"><p><?php echo $TxnId; ?></p></div>
						<div class="subDiv1">Amount. : </div><div class="subDiv2"><input type="text" name="amount" value="<?php echo $amount; ?>"></div>
						<div class="subDiv1">Seller : </div>
						    <div class="subDiv2">
						        <select name='Seller' onchange="getFirmBySellerId('--Select Firm--')" id="SellerId2">
						            <option>--Select Seller--</option>
						        <?php
						            $quSeller="SELECT * FROM Seller WHERE Status='ACTIVE'";
						            $reSeller=mysqli_query($con,$quSeller);
						            while($dataSeller=mysqli_fetch_array($reSeller))
						            {
						                if($dataSeller['Id']==$Seller_id)
						                {
						                    echo"<option value='".$dataSeller['Id']."' selected>".$dataSeller['Name']."- ".$dataSeller['City']."</option>";
						                }
						                else
						                {
						                    echo"<option value='".$dataSeller['Id']."' >".$dataSeller['Name']."- ".$dataSeller['City']."</option>";
						                }
						            }
						        ?>
						        </select>
						</div>
						<div class="subDiv1">Firm : </div>
						    <div class="subDiv2">
						        <select name='firm' id='FirmPlace2'>
						            <option value="<?php $firm_id ?>"><?php echo $f_Name; ?></option>
						        </select>
						</div>
						<div class="subDiv1">Bank : </div>
						    <div class="subDiv2">
						        <select name="bankId" >
						            <option>--Select Bank--</option>
						        <?php
						            $quBank ="SELECT * FROM bank WHERE Status='ACTIVE'";
						            $reBank =mysqli_query($con,$quBank);
						            while($dataBank = mysqli_fetch_array($reBank))
						            {
						                if($dataBank['Id']==$bank_id)
						                {
						                    echo"<option value='".$dataBank['Id']."' selected>".$dataBank['BankName']." - ".$dataBank['AccName']."</option>";
						                }
						                else
						                {
						                    echo"<option value='".$dataBank['Id']."' >".$dataBank['BankName']." - ".$dataBank['AccName']."</option>";
						                }
						            }
						        ?>
						        </select>
						</div>
						<div class="subDiv2"><input type="submit" name="submit" value="Submit"  onclick='showLoader()'></div>
					</div>
				</form>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>