<!DOCTYPE html>
<html lang="en">

<head>
<?php ob_start(); ?>
<?php   require_once 'head_link.php'; ?>

</head>

<body>
 
<?php   require_once 'header1.php'; ?> 

<?php 
	
	if(isset($_POST['btnsave']))
	{
		$member_id = $_POST['member_id'];   
		$customer_no = $_POST['customer_no'];
		
		$BatchNo = $_POST['BatchNo'];
	    $CourseName = $_POST['CourseName'];
		
		$customer_name = $_POST['customer_name'];
		
		$FatherName = $_POST['FatherName'];
	    $MotherName = $_POST['MotherName'];		
		$Position = $_POST['Position'];

		$gender = $_POST['gender'];
		$address = $_POST['address'];
		$contact_info = $_POST['contact_info'];
		$Email = $_POST['Email'];
		$national_id = $_POST['national_id'];
		
	$MrgInfo = $_POST['MrgInfo'];
	$Religion = $_POST['Religion'];
	$LastEdu = $_POST['LastEdu'];
	$Institute = $_POST['Institute'];
	$GuarPhn = $_POST['GuarPhn'];
	$Refer = $_POST['Refer'];
	$Date = $_POST['Date'];
		
		$imgFile = $_FILES['user_image']['name'];
		$tmp_dir = $_FILES['user_image']['tmp_name'];
		$imgSize = $_FILES['user_image']['size'];
		
		
		if(empty($customer_no)){
			$errMSG = "Please Enter customer_no.";
		}
		else if(empty($customer_name)){
			$errMSG = "Please Enter customer_name.";
		}
		
		else if(empty($contact_info)){
			$errMSG = "Please Enter Phone Number.";
		}
		
		if (isset($_POST['contact_info']) && $_POST['contact_info'] != '') {
		$contact_info = $_POST['contact_info'];
		$checkQuery = mysqli_query($con, "SELECT * FROM `customer` WHERE `contact_info`='$contact_info' ");
		if (mysqli_num_rows($checkQuery) > 0) {
		$errMSG = "This Phone Number Already Exist !";
		}
		
		else
		{
			$upload_dir = 'user_images/'; // upload directory
	
			$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
		
			// valid image extensions
			$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
		
			// rename uploading image
			$userpic = rand(1000,1000000).".".$imgExt;
				
			// allow valid image file formats
			if(in_array($imgExt, $valid_extensions)){			
				// Check file size '5MB'
				if($imgSize < 5000000)				{
					move_uploaded_file($tmp_dir,$upload_dir.$userpic);
				}
				else{
					$errMSG = "Sorry, your file is too large.";
				}
			}
			//else{
			//	$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";		
			//}
		}
		
		
		// if no error occured, continue ....
		if(!isset($errMSG))
		{
			$stmt = $DB_con->prepare('INSERT INTO customer(member_id,customer_no,batch_no,course_id,customer_name,father_name,mother_name,position,gender,address,contact_info,email,national_id, marriage_info,religion,last_edu,school,gar_phn,refers, join_date,status,userPic)
			VALUES(:member_id,:customer_no, :BatchNo,:CourseName,  :customer_name, :FatherName,:MotherName,:Position, :gender, :address,:contact_info,:Email,:national_id, :MrgInfo,:Religion, :LastEdu,:Institute,:GuarPhn,:Refer, :Date,1,:upic)');
		
		$stmt->bindParam(':member_id',$member_id);
			$stmt->bindParam(':customer_no',$customer_no);
			
			$stmt->bindParam(':BatchNo', $BatchNo);
		    $stmt->bindParam(':CourseName', $CourseName); 
			
			$stmt->bindParam(':customer_name',$customer_name);
			
			$stmt->bindParam(':FatherName', $FatherName);
		    $stmt->bindParam(':MotherName', $MotherName);
			
			$stmt->bindParam(':Position',$Position);
			$stmt->bindParam(':gender',$gender);
			$stmt->bindParam(':address',$address);
			$stmt->bindParam(':contact_info',$contact_info);
			$stmt->bindParam(':Email',$Email);
			$stmt->bindParam(':national_id',$national_id);
			
		$stmt->bindParam(':MrgInfo', $MrgInfo);
		$stmt->bindParam(':Religion', $Religion);
		$stmt->bindParam(':LastEdu', $LastEdu);
		$stmt->bindParam(':Institute', $Institute);
		$stmt->bindParam(':GuarPhn', $GuarPhn);
		$stmt->bindParam(':Refer', $Refer);
		$stmt->bindParam(':Date',$Date);
		$stmt->bindParam(':upic',$userpic);
			
			if($stmt->execute()) {
		  ?>
              
          <?php			
		
			}
			else
			{
				$errMSG = "error while inserting....";
			}
		}
	}
	}
?>

<?php 
	
		if(isset($_POST['btnsave']))
	{
		
		$member_id = $_POST['member_id'];
		$CustId = $_POST['CustId'];
		$Date = $_POST['Date'];
		$customer_name = $_POST['customer_name'];
		$contact_info = $_POST['contact_info'];  
		
		if(empty($customer_name)){
			$errMSG = "Please Enter Customer Name.";
		}
		else if(empty($contact_info)){
			$errMSG = "Please Enter Phone Name.";
		}
		
		 
		if(!isset($errMSG))
		{
			$stmt = $DB_con->prepare('INSERT INTO orders_details (order_id,user_id,customer_id,order_date, client_name, client_contact, pre_due,today_total,grand_total,paid,recent_due,cuses,order_type,status,custom_invoice,paym_type) 
			                                              VALUES(0,:member_id,:CustId,:Date,:customer_name,:contact_info,"0", "0", "0", "0", "0","New Customer",1,1,0,0 )');
			
			
			$stmt->bindParam(':member_id',$member_id);
			$stmt->bindParam(':CustId',$CustId);
			$stmt->bindParam(':Date',$Date);
			$stmt->bindParam(':customer_name',$customer_name);
			$stmt->bindParam(':contact_info',$contact_info);  
			
			
			if($stmt->execute())
			{
				?>
              <script> 
				</script>
          <?php	
			}
			else
			{
				$errMSG = "error while inserting....";
			}
		}
	}
?>

<?php 
	
		if(isset($_POST['btnsave']))
	{
		
		$member_id = $_POST['member_id'];
		$CustId = $_POST['CustId'];
		$Date = $_POST['Date'];
		$customer_name = $_POST['customer_name'];
		$contact_info = $_POST['contact_info'];
		
		if(empty($customer_name)){
			$errMSG = "Please Enter Customer Name.";
		}
		else if(empty($contact_info)){
			$errMSG = "Please Enter Phone Name.";
		}
		
		// if no error occured, continue ....
		if(!isset($errMSG))
		{
			$stmt = $DB_con->prepare('INSERT INTO orders_dues (order_id,user_id,customer_id,order_date, last_update, client_name, client_contact,pre_due,today_total,grand_total, paid, recent_due,dues_or_paid,dues_or_paid_status,cuses,custom_invoice,paym_type ) 
			                                              VALUES("0000",:member_id,:CustId,:Date,:Date,:customer_name,:contact_info, "0", "0", "0", "0", "0", "Dues","4", "New Customer",0,0 )');
			
			
			$stmt->bindParam(':member_id',$member_id);
			$stmt->bindParam(':CustId',$CustId);
			$stmt->bindParam(':Date',$Date);
			$stmt->bindParam(':customer_name',$customer_name);
			$stmt->bindParam(':contact_info',$contact_info);
			
			
			if($stmt->execute())
			{
				?>
              <script>
				alert('Data Successfully Add ...');
				window.location.href='student-list';
				</script>
          <?php	
			}
			else
			{
				$errMSG = "error while inserting....";
			}
		}
	}
?>

<?php  require_once 'sidebar.php'; ?>

  <main id="main" class="main">

    <div class="pagetitle bg-light rounded p-2">
      <h1>Add New Student |  <span> <a href="student-list">    <i class="bi bi-table"></i> </a> </span> </h1>
	  
    </div> 
	
	
		<section class="section">
      
          <div class="card">
            <div class="card-body"> 
			
			<h5 class="card-title">
			  <?php
	if(isset($errMSG)){
			?>
            <div class="alert alert-danger">
            	<span class="glyphicon glyphicon-info-sign"></span> <strong><?php echo $errMSG; ?></strong>
            </div>
            <?php
	}
	else if(isset($successMSG)){
		?>
        <div class="alert alert-success">
              <strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG; ?></strong>
        </div>
        <?php
	}
	?>  
			  
</h5>
			
  <form method="post" enctype="multipart/form-data" class="form-horizontal" >
			  
		<div class="row">
        <div class="col-lg-6">	  
		
		<?php
				   $pq=mysqli_query($con,"select * from stuff left join `user` on user.userid=stuff.userid where stuff.userid='".$_SESSION['id']."' ");
				   while($pqrow=mysqli_fetch_array($pq)){
				?>
        <input class="form-control" type="hidden" name="member_id"  value="<?php echo $pqrow['userid']; ?>" />
		<?php }?>
		
		 <?php	//   Order Due  //
				   
				   $pq=mysqli_query($con,"select MAX(customer_id)+1 as max_id from customer ");
				   while($pqrow=mysqli_fetch_array($pq)){
				?>
      
        <input class="form-control" type="hidden" name="CustId"  value="<?php echo $pqrow['max_id']; ?>"  /> 
      <?php }?>
	  
	  <div class="d-none"> 
	  <label class="control-label">Customer No </label> 
	         <?php	// Auto Customer No
				  
				   $pq=mysqli_query($con,"select MAX(customer_no)+1 as max_id from customer  where member_id='".$_SESSION['id']."'");
				   while($pqrow=mysqli_fetch_array($pq)){
				?> 
      <input class="form-control" type="text" name="customer_no"  value="21"  />  
      <?php }?>
	  </div>
			  
                <div class="row mb-3">
                  <label for="BatchNo" class="col-sm-4 col-form-label">Batch Name</label>
                  <div class="col-sm-8">
                    <select style="width:100%;" class="form-control chosen-select" Id="BatchNo" name="BatchNo" required="">
							<option value="#">Select Batch</option>

							<?php
							$sql = "SELECT id,batch_name FROM batch where user_id='".$_SESSION['id']."'  order by id asc";
							$result = $con->query($sql);

							while ($row = $result->fetch_array()) {
								echo "<option value='" . $row[0] . "'>" . $row[1] . "</option>";
							}
							?>
						</select>
                  </div>
                </div>
				
                <div class="row mb-3">
                  <label for="CourseName" class="col-sm-4 col-form-label">Course Name</label>
                  <div class="col-sm-8">
                    <select style="width:100%;" class="form-control chosen-select" Id="CourseName" name="CourseName" required="">
							<option value="#">Select Course</option>

							<?php
							$sql = "SELECT product_id,product_name FROM product where user_id='".$_SESSION['id']."'";
							$result = $con->query($sql);

							while ($row = $result->fetch_array()) {
								echo "<option value='" . $row[0] . "'>" . $row[1] . "</option>";
							}
							?>
						</select>
                  </div>
                </div>
				
                <div class="row mb-3">
                  <label for="customer_name" class="col-sm-4 col-form-label">Student Name</label>
                  <div class="col-sm-8">
                    <input class="form-control" type="text" name="customer_name" id="customer_name" placeholder="Student Name">
                  </div>
                </div>
				
				<div class="row mb-3">
                  <label for="gender" class="col-sm-4 col-form-label"> Gender</label>
                  <div class="col-sm-8">
                   <select style="width:100%;" class="form-select" name="gender" id="gender" > <option>Male </option> <option>Female </option> <option>Other </option> </select> 
                  </div>
                </div>
				
                <div class="row mb-3">
                  <label for="Position" class="col-sm-4 col-form-label"> Profession</label>
                  <div class="col-sm-8">
                  <select style="width:100%;" class="form-control chosen-select" style="width:100%;" name="Position" id="Position">
		          <option value="Position">Select Profession</option>
				      	<?php 
				      	$sql = "SELECT id, position_name FROM cust_position where user_id='".$_SESSION['id']."' order by id asc  ";
								$result = $con->query($sql);

								while($row = $result->fetch_array()) {
									echo "<option value='".$row[0]."'>".$row[1]."</option>";
								} // while
				      	?>
		           </select> 
                  </div>
                </div>
				
                <div class="row mb-3">
                  <label for="FatherName" class="col-sm-4 col-form-label"> Father Name </label>
                  <div class="col-sm-8">
                   <input class="form-control" type="text" name="FatherName" placeholder="Father Name" >
                  </div>
                </div>
				
                <div class="row mb-3">
                  <label for="MotherName" class="col-sm-4 col-form-label">Mother Name</label>
                  <div class="col-sm-8">
                   <input class="form-control" type="text" name="MotherName" placeholder="Mother Name">
                  </div>
                </div>
				
                <div class="row mb-3">
                  <label for="address" class="col-sm-4 col-form-label">Address Name</label>
                  <div class="col-sm-8">
                    <input class="form-control" type="text" id="address" name="address" placeholder="Address" value="<?php echo $pqrow['address']; ?>" >
                  </div>
                </div>
				
                <div class="row mb-3">
                  <label for="contact_info" class="col-sm-4 col-form-label"> Phone Number</label>
                  <div class="col-sm-8">
                    <input class="form-control" type="text" name="contact_info" id="contact_info" placeholder="Phone Number" >
                  </div>
                </div>
				
                <div class="row mb-3">
                  <label for="Email" class="col-sm-4 col-form-label"> Email Address</label>
                  <div class="col-sm-8">
                   <input class="form-control" type="text" name="Email" id="Email" value="Na" >
                  </div>
                </div>
    </div>
		
	<div class="col-lg-6">
	
				<div class="row mb-3">
                  <label for="national_id" class="col-sm-4 col-form-label"> National Id</label>
                  <div class="col-sm-8">
                    <input class="form-control" type="text" name="national_id" id="national_id" value="0" >
                  </div>
                </div>
				 
		        <div class="row mb-3">
                  <label for="MrgInfo" class="col-sm-4 col-form-label"> Marriage Info</label>
                  <div class="col-sm-8">
                    <select style="width:100%;" class="form-select" name="MrgInfo" id="MrgInfo">
							<option value="Yes">Yes</option>
							<option value="No">No </option>
						</select>
                  </div>
                </div>
				 
				<div class="row mb-3">
                  <label for="Religion" class="col-sm-4 col-form-label"> Religion</label>
                  <div class="col-sm-8">
                   <select style="width:100%;" class="form-select" name="Religion" id="Religion">
							<option value="Muslim">Muslim</option>
							<option value="Hindu">Hindu </option>
							<option value="Christian">Christian </option>
							<option value="Buddha">Buddha </option>
							<option value="Other">Other </option>
						</select>
                  </div>
                </div>
				 
				<div class="row mb-3">
                  <label for="LastEdu" class="col-sm-4 col-form-label"> Last Education Qualification</label>
                  <div class="col-sm-8">
                  <select style="width:100%;" class="form-control chosen-select" Id="LastEdu" name="LastEdu">
							<option value="#">Select Education Qualification</option>

							<?php
							$sql = "SELECT deg_id,deg_name FROM degree ";
							$result = $con->query($sql);

							while ($row = $result->fetch_array()) {
								echo "<option value='" . $row[0] . "'>" . $row[1] . "</option>";
							}
							?>
						</select>
                  </div>
                </div>
				 
				<div class="row mb-3">
                  <label for="Institute" class="col-sm-4 col-form-label"> School/College/University</label>
                  <div class="col-sm-8">
                  <input class="form-control" type="text" name="Institute" id="Institute" placeholder="School/College/University" />
				  </div>
                </div>
				 
				 <div class="row mb-3">
                  <label for="GuarPhn" class="col-sm-4 col-form-label"> Guardian / Phone </label>
                  <div class="col-sm-8">
                  <input class="form-control" type="text" name="GuarPhn" id="GuarPhn" placeholder="Guardian /  Phone Number" />
				  </div>
                </div>
				
				 <div class="row mb-3">
                  <label for="Refer" class="col-sm-4 col-form-label"> Reference </label>
                  <div class="col-sm-8">
                  <input class="form-control" type="text" name="Refer" id="Refer" placeholder="Reference (Name/Phone/Address)" />
				  </div>
                </div>
				
				 <div class="row mb-3">
                  <label for="Date" class="col-sm-4 col-form-label"> Date</label>
                  <div class="col-sm-8">
                   <input class="form-control" type="date" name="Date" id="Date" placeholder="" value="<?php date_default_timezone_set("Asia/Dhaka"); echo date("Y-m-d") ;?>" />
                  </div>
                </div>
				
				 <div class="row mb-3">
                  <label for="user_image" class="col-sm-4 col-form-label"> Photo</label>
                  <div class="col-sm-8">
                   <input class="input-group" type="file" name="user_image" id="user_image" accept="image/*" />
                  </div>
                </div>
	</div>
				 
  
				
                <div class="text-center"><hr>				
				<button type="submit" name="btnsave" class="btn btn-primary"><span class="bi bi-check-circle"></span>  &nbsp;Save </button>
                </div>
				
             
			 </form> 

            </div>
          </div>
	  </div>
   </div>
</section>
	 
	 

  </main> 

    <?php  require_once 'footer1.php'; ?>
	
	
<script src = "js/chosen.js"></script>
<script type = "text/javascript">
	$('.chosen-select').chosen({width: "100%"});
</script>

<script>
	$("#loadGroup").on("click", function(){
		
		var groupID = $("#group_no").val();
		if(groupID == "")
		{
			alert("Please enter a valid Group ID!");
		}
		else{
			$.ajax({url: "ajax_c_load_group.php?c=" + groupID, success: function(result){
				var customerGroup = JSON.parse(result);
				$("#group_name1").val(customerGroup.groupName);
				$("#group_name").val(customerGroup.groupName);
				$("#address1").val(customerGroup.villageName);
				$("#address").val(customerGroup.villageName);
				
			}});
		}
	});
</script>