<?php
	include'session.php';
	include'../connection.php';
	include'../functions.php';
?>
<html>
<head>
	<title>Add Operator</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:right;
			text-align:center;
			padding-top:20px;
		}
		.div1 i{
			font-size:300px;
			color:#6b5127;
			margin:0 10px;
			
		}
		.div1 input, select{
			width:90%;
			height:40px;
			margin:5px 10px;
			padding:2px 10px;
			border-radius:5px;
			border:1px solid #6b5127;
			font-size:16px;
		}
		input[type=submit]{
			background:#6b5127;
			color:white;
			font-size:20px;
			transition:1s;
			width:150px;
			float:right;
			margin-right:5%;
		}
		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;
			}
		}
	</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';
			
	if(isset($_POST['submit']))
	{
		$opName      = $_POST['opName'];
		$opType      = $_POST['opType'];
		$ServiceType = $_POST['ServiceType'];
		$OpNameType  = $_POST['OpNameType'];
		$MainOPId    = $_POST['MainOPId'];
		
		if($opType=="--Select--" || $ServiceType=="--Select--")
		{
		    echo "<div class='msg-div'>
			        <form method='post'>
				        <h3>Select Option Please !</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>";
		}
		elseif(empty($opName))
		{
		    echo "<div class='msg-div'>
			        <form method='post'>
				        <h3>Insert Operator Please !</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>";
		}
		else
		{
		    $quDupOp="SELECT * FROM operator WHERE Name='$opName'";
		    $reDupOp=mysqli_query($con,$quDupOp);
		    $countDupOp=mysqli_num_rows($reDupOp);
		    
		    if($countDupOp>0)
		    {
		        echo "<div class='msg-div'>
			        <form method='post'>
				        <h3>Duplicate Operator Not Allowed !</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>";
		    }
		    else
		    {
		        $quAddOp="INSERT INTO operator (Name,ServiceType,OpNameType,Type) VALUES ('$opName','$ServiceType','$OpNameType','$opType')";
			    if(mysqli_query($con,$quAddOp))
			    {
			        $newOpCode=mysqli_insert_id($con);
			        if($OpNameType=="SUB")
			        {
			            $quApi="SELECT * FROM api";
			            $reApi=mysqli_query($con,$quApi);
			            while($dataApi=mysqli_fetch_array($reApi))
			            {
			                $apiName=$dataApi['ApiName'];
			                
			                $quMainApiOpCode="SELECT $apiName FROM operator WHERE Id='$MainOPId'";
			                $reMainApiOpCode=mysqli_query($con,$quMainApiOpCode);
			                $dataApiOpCode=mysqli_fetch_array($reMainApiOpCode);
			                $apiOpCode=$dataApiOpCode[$apiName];
			                
			                $updateApiOpCode="UPDATE operator SET $apiName='$apiOpCode' WHERE Id='$newOpCode'";
			                mysqli_query($con,$updateApiOpCode);
			                
			                if($apiName=="M_ROBO")
			                {
			                    $mRoboOpCode = $apiOpCode;
			                }
			            }
			        }
			        
			        if($opType=="API")
			        {
			            $quType = "WHERE Type='API' OR Type='ADMIN'";
			        }
			        else
			        {
			            $quType="";
			        }
			        $quUser = "SELECT * FROM user $quType";
			        $reUser = mysqli_query($con,$quUser);
			        while($dataUser=mysqli_fetch_array($reUser))
			        {
			            $usr = $dataUser['Userid'];
			            
			            $insertCommSlab="INSERT INTO comm_slab (Userid,OpName,OpCode) VALUES ('$usr','$opName','$newOpCode')";
			            if(mysqli_query($con,$insertCommSlab))
			            {
			                if($dataUser['Type']=="API"  || $dataUser['Type']=="MASTER")
			                {
			                    $quSetApi = "INSERT INTO set_api (Userid,OpCode,UseApi,ApiOpCode) VALUES ('$usr','$newOpCode','M_ROBO','$mRoboOpCode')";
			                    mysqli_query($con,$quSetApi);
			                }
	       		            echo "<div class='msg-div'>
		        		    	<form method='post'>
				            	    <h3>Operator Added 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>";
			                
			            }
			        }
			    }
		    }
			
		}
	}
		?>
		<div class="content">
			<div class="head">
				<div class="title"><p>Add New Operator :-</p></div>
				<div class="msg"><marquee><?php //include'msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
				<div class="div1">
				</div>
				<div class="div1">
				    <form method="post">
						<select name='opType'>
						    <option>--Select--</option>
						    <option>ALL</option>
						    <option>API</option>
						</select>
						<select name='ServiceType'>
						    <option>--Select--</option>
						    <option>DTH</option>
						    <option>PREPAID</option>
						</select>
						<select name='OpNameType'>
						    <option>--Select--</option>
						    <option>MAIN</option>
						    <option>SUB</option>
						</select>
						<select name='MainOPId'>
						    <option>--Select--</option>
						    <?php
						        $quMainOp="SELECT Id,Name FROM operator WHERE OpNameType='MAIN'";
						        $reMainOp=mysqli_query($con,$quMainOp);
						        while($dataMainOp=mysqli_fetch_array($reMainOp))
						        {
						            echo "<option value='".$dataMainOp['Id']."'>".$dataMainOp['Name']."</option>";
						        }
						    ?>
						</select>
						<input type="text" name="opName" placeholder="Operator Name.." >
						<input type="submit" name="submit" value="Submit"  onclick='showLoader()'>
				    </form>
				</div>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>