<?php
require_once("dbconnection.php");   
session_start();
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    $s_name = $_POST['s_name'];
    $s_fname = $_POST['s_fname'];
    $s_phone = $_POST['s_phone'];
    $s_tazkera = $_POST['s_tazkera'];

    $q1 = "INSERT INTO student(s_name, s_fname, s_phone, s_tazkera) 
           VALUES ('$s_name', '$s_fname', '$s_phone', '$s_tazkera')";  

    if (mysqli_query($con, $q1)) {
        // Get the last inserted student ID
        $s_id = mysqli_insert_id($con);

        // Redirect to add_payment.php with the new student ID
        header("Location: add_payment.php?id=$s_id");
        exit();
    } else {
        echo "Error inserting student: " . mysqli_error($con);
    }
}


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

    <meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="keywords" content="admin, dashboard">	
	<meta name="robots" content="index, follow">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!-- PAGE TITLE HERE -->
	<title>اضافه شاگرد جدید</title>
	<!-- FAVICONS ICON -->
	<link rel="shortcut icon" type="image/png" href="images/favicon.png">
	
	<link href="vendor/jquery-nice-select/css/nice-select.css" rel="stylesheet">
	<link rel="stylesheet" href="vendor/nouislider/nouislider.min.css">
	<!-- Style css -->
    <link href="css/style.css" rel="stylesheet">
    <link href="vendor/sweetalert2/dist/sweetalert2.min.css" rel="stylesheet">
		<link href="vendor/jquery-nice-select/css/nice-select.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <link rel="stylesheet" href="dist/sweetalert.css"> 
    
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
     <script>
  document.addEventListener("keydown", function(event) {
    if (event.key === "Escape") {
      // Use a short delay to ensure smooth behavior
      setTimeout(() => {
        window.history.back();
      }, 100);
    }
  });
</script>
</head>
<body>

    <!--*******************
        Preloader start
    ********************-->
    <div id="preloader">
        <div class="waviy">
        <span style="--i:1">N</span>
		   <span style="--i:2">A</span>
		   <span style="--i:3">I</span>
		   <span style="--i:4">M</span>
		   <span style="--i:5">-</span>
		   <span style="--i:5">A</span>
		   <span style="--i:5">R</span>
		   <span style="--i:6">I</span>
           <span style="--i:6">A</span>
		   <span style="--i:7">-</span>
           <span style="--i:8">E</span>
		   <span style="--i:9">D</span>
           <span style="--i:10">U</span>
		</div>
    </div>
    <!--*******************
        Preloader end
    ********************-->

    <!--**********************************
        Main wrapper start
    ***********************************-->
    <div id="main-wrapper">

        <!--**********************************
            Nav header start
        ***********************************-->
        <div class="nav-header">
            <a href="index.html" class="brand-logo">
            <img src="images/logo1.png" alt="" height="70" width="70">
            </a>
            <div class="nav-control">
                <div class="hamburger">
                    <span class="line"></span><span class="line"></span><span class="line"></span>
                </div>
            </div>
        </div>
        <!--**********************************
            Nav header end
        ***********************************-->
		
		<!--**********************************
            Header start
        ***********************************-->
        <div class="header">
            <div class="header-content">
                <nav class="navbar navbar-expand">
                    <div class="collapse navbar-collapse justify-content-between">
                        <div class="header-left">
							<div class="dashboard_bar">
                            اضافه شاگرد
                            </div>
                        </div>
       
                    </div>
				</nav>
			</div>
		</div>
        <!--**********************************
            Header end ti-comment-alt
        ***********************************-->

        <!--**********************************
            Sidebar start
        ***********************************-->
        <div class="dlabnav">
            <div class="dlabnav-scroll">
				<ul class="metismenu" id="menu">
					<li class="dropdown header-profile">
						<a class="nav-link" href="javascript:void(0);" role="button" data-bs-toggle="dropdown">
							<img src="images/profile/profile.png" width="20" alt="">
							<div class="header-info ms-3">
								<span class="font-w600 "><?php echo $_SESSION['emp_name']; ?></span>
								<small class="text-end font-w400"><?php echo $_SESSION['emp_email']; ?></small>
							</div>
						</a>
						<div class="dropdown-menu dropdown-menu-end">
							<a href="app-profile.php" class="dropdown-item ai-icon">
								<svg id="icon-user1" xmlns="http://www.w3.org/2000/svg" class="text-primary" width="18" height="18" viewbox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg>
								<span class="ms-2">پروفایل</span>
							</a>
							<a href="logout.php" class="dropdown-item ai-icon">
								<svg id="icon-logout" xmlns="http://www.w3.org/2000/svg" class="text-danger" width="18" height="18" viewbox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"></path><polyline points="16 17 21 12 16 7"></polyline><line x1="21" y1="12" x2="9" y2="12"></line></svg>
								<span class="ms-2">خروج از سیتم </span>
							</a>
						</div>
					</li>
              
					<li><a class="ai-icon" href="index.php" aria-expanded="false" style='font-size:20px;font-weight: bold;'>
						<i class="flaticon-025-dashboard"></i>
						<span class="nav-text">صفحه اصلی</span>
					</a>
				
				</li>
                 	</li>
                    <li><a class="has-arrow ai-icon" href="javascript:void()" aria-expanded="false" style='font-size:20px;font-weight: bold;'>
						<i class="icon-user"></i>
							<span class="nav-text">لیست محصلین</span>
							
						</a>
                        <ul aria-expanded="false">
                            <li><a href="add_student.php" style='font-size:20px;font-weight: bold;'>اضافه محصل</a></li>
							<li><a href="view_student.php" style='font-size:20px;font-weight: bold;'>مشاهده محصل</a></li>
							<li><a href="view_all_student.php" style='font-size:20px;font-weight: bold;'>مشاهده محصلین</a></li>
							
                        </ul>
                    </li>
                  

  
                    <li><a class="has-arrow ai-icon" href="javascript:void()" aria-expanded="false">
							<i class="fa fa-book"></i>
							<span class="nav-text" style='font-size:20px;font-weight: bold;'>بخش صنف</span>
						</a>
                        <ul aria-expanded="false">
							<li><a href="add_course.php" style='font-size:20px;font-weight: bold;'>اضافه صنف جدید </a></li>
							<li><a href="view_course.php" style='font-size:20px;font-weight: bold;'>مشاهده صنوف</a></li>

                        </ul>
                    </li>
					<li><a  class="has-arrow ai-icon" aria-expanded="false">
							<i class="fa fa-file-invoice-dollar"></i>
							<span class="nav-text" style='font-size:20px;font-weight: bold;'>بخش پول صیف</span>
						</a>
					<ul aria-expanded="false">
                            
                            <li><a href="view_cash_point_details.php" style='font-size:20px;font-weight: bold;'>راپور صیف</a></li>
							
							
                        </ul>
					</li>

					<li><a  class="has-arrow ai-icon" aria-expanded="false">
							<i class="fa fa-calendar"></i>
							<span class="nav-text" style='font-size:20px;font-weight: bold;'>مشاهده راپور ها</span>
						</a>
	<ul aria-expanded="false">
                            
                            <li><a href="cash_point_report.php" style='font-size:20px;font-weight: bold;'>راپور صیف</a></li>
							<li><a href="student_report.php" style='font-size:20px;font-weight: bold;'>راپور محصلین</a></li>

							
                        </ul>
					</li>

                </ul>
			<div class="copyright">
					<p><strong>Student MIS Admin Dashboard</strong> © 2025 All Rights Reserved</p>
					<p class="fs-12">Made with <span class="heart"></span> by Dynamic & Cryptic</p>
				</div>
			</div>
        </div>
        <!--**********************************
            Sidebar end
        ***********************************-->


		
		<!--**********************************
            Content body start
        ***********************************-->
			<div class="content-body" >
            <div class="container-fluid">
				<div class="row page-titles">
					<ol class="breadcrumb">
						<li class="breadcrumb-item active" style='font-size:20px;font-weight: bold;'><a href="index.php">صفحه اصلی</a></li>
						<li class="breadcrumb-item" style='font-size:20px;font-weight: bold;'><a id="date-link"></a></li>
					</ol>
                </div>
				
                <div class="row" >
                    <div class="col-lg-12">
                        <div class="card">
                          
                            <div class="card-body">
                               
							<div class="form-validation">
                                    <form id="form1" name="form1" method="post" class="needs-validation" novalidate="" dir="rtl">
                                      
                                    <div class="row">
                                            <div class="col-xl-6">
                                            
                                                
                                            
												
                                                <div class="mb-3 row">
                                                    <label class="col-lg-4 col-form-label" for="validationCustom06" style="font-size:20px;">اسم محصل
                                                        <span class="text-danger">*</span>
                                                    </label>
                                                    <div class="col-lg-6">
                                                        <input style='font-size:20px;' type="text" class="form-control" style="font-size:20px;" id="s_name" name="s_name" placeholder="" required="">
														
                                                    </div>
                                                </div>
                                                <div class="mb-3 row">
                                                    <label class="col-lg-4 col-form-label" for="validationCustom07" style="font-size:20px;">اسم پدر<span class="text-danger">*</span>
                                                    </label>
                                                    <div class="col-lg-6">
                                                        <input style='font-size:20px;' type="text" style="font-size:20px;" class="form-control" id="s_fname" name="s_fname" placeholder="" required="">
														
                                                    </div>
                                                </div>
                                               

                                                 <div class="mb-3 row">
                                                    <label class="col-lg-4 col-form-label" for="validationCustom07" style="font-size:20px;">اسم مکتب<span class="text-danger">*</span>
                                                    </label>
                                                    <div class="col-lg-6">
                                                        <input style='font-size:20px;' type="text" style="font-size:20px;" class="form-control" id="s_school" name="s_school" placeholder="" required="">
														
                                                    </div>
                                                </div>
                                                 <div class="mb-3 row">
                                                    <label class="col-lg-4 col-form-label" for="validationCustom07" style="font-size:20px;">شماره تماس<span class="text-danger">*</span>
                                                    </label>
                                                    <div class="col-lg-6">
                                                        <input style='font-size:20px;' type="text" style="font-size:20px;" class="form-control" id="s_phone" name="s_phone" placeholder="" required="">
														
                                                    </div>
                                                </div>

                                                <div class="mb-3 row">
                                                    <label class="col-lg-4 col-form-label" for="validationCustom07" style="font-size:20px;">یاداشت<span class="text-danger">*</span>
                                                    </label>
                                                    <div class="col-lg-6">
                                                        <textarea class="form-control" name="note" id="note"></textarea>
														
                                                    </div>
                                                </div>

                                                              <label class="col-lg-4 col-form-label" for="validationCustom10">
                                                    </label>
                                                    
                                                    <button style="font-size:10px;" type="button" class="btn btn-rounded btn-warning" name="send" id="butsend"><span class="btn-icon-start text-info"><i class="fa fa-plus color-info"></i>
                                                        </span >اضافه به لیست</button>
                                            </div>
                                  
                                       
                                        <div class="col-lg-9">
                            <div class="card-body">
                                <div class="">
                                    <table class="table header-border table-responsive-sm" id="table1" name="table1">
                                        <thead >  
                                            <tr style="color:black;">
                                              
                                                <th style='font-size:15px;'>نمبر</th>
                                                <th style='font-size:15px;'>اسم محصل</th>
                                                <th style='font-size:15px;'>اسم پدر</th>
                                                <th style='font-size:15px;'>اسم مکتب</th>
                                                <th style='font-size:15px;'>شماره تماس</th>
                                                <th style='font-size:15px;'>یاداشت</th>
                                                <th style='font-size:15px;'>حذف</th>

                                            </tr>
                                        </thead>
                                    <tbody>
                                    </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    <br>
                    
                                            <div class="col-xl-4">
                                         
                                         
                                               <div class="mb-3 row">
                                                    <label class="col-lg-4 col-form-label" for="validationCustom03">
                                                        
                                                    </label>
                                                    <div class="col-lg-8">
                                                    <input type="button" value="ثبت سیستم" class="btn btn-primary btn sweet-success" name="save" id="butsave"></input>
														
                                                    </div>
                                                </div>
                                            </div>
                
                        
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
			
            </div>
        </div>
        <!--**********************************
            Content body end
        ***********************************-->
		
        <!--**********************************
            Footer start
        ***********************************-->
    <div class="footer">
		
            <div class="copyright">

            </div>
        </div>
        <!--**********************************
            Footer end
        ***********************************-->

		


	</div>
    <!--**********************************
        Main wrapper end
    ***********************************-->

    <!--**********************************
        Scripts
    ***********************************-->
    <!-- Required vendors -->
    <script src="vendor/global/global.min.js"></script>
	<script src="vendor/chart.js/Chart.bundle.min.js"></script>
	<script src="vendor/jquery-nice-select/js/jquery.nice-select.min.js"></script>
	
	
	
	<!-- Dashboard 1 -->
	<script src="js/dashboard/dashboard-1.js"></script>
    <script src="js/custom.min.js"></script>
	<script src="js/dlabnav-init.js"></script>
	<script src="js/demo.js"></script>
    <script src="js/styleSwitcher.js"></script>
    <!--Sweet Alert--->
    <script src="vendor/sweetalert2/dist/sweetalert2.min.js"></script>
    <script src="js/plugins-init/sweetalert.init.js"></script>
    <script src="dist/sweetalert.min.js"></script>
	<script src="vendor/jquery-nice-select/js/jquery.nice-select.min.js"></script>
    <script>



$(document).ready(function() {
   

        
        var s_name1 = new Array(); 
        var s_fname1 = new Array(); 
        var s_school1 = new Array();
        var s_phone1 = new Array();
        var note1 = new Array();
        
        


        


        
    
    var id = 1;  
    
    /* Assigning id and class for tr and td tags for separation. */

    $("#butsend").click(function() {
       
        var s_name=document.form1.s_name.value;
        var s_fname=document.form1.s_fname.value;
        var s_school=document.form1.s_school.value;
        var s_phone=document.form1.s_phone.value;
        var note=document.form1.note.value;
        

       if(s_name == null || s_name == "" ){
      sweetAlert("اخطاریه","اسم شاگر لازمی میباشد","warning");  
      return false;  
      }
      else 
        if(s_fname == null || s_fname == ""){
        sweetAlert("اخطاریه","اسم پدر لازمی میباشد","warning");  
        return false;  
      }
      else 
        if(s_school == null || s_school == ""){
        sweetAlert("اخطاریه","اسم مکتب لازمی میباشد","warning");  
        return false;  
      }
      
      else 
        if(s_phone == null || s_phone == ""){
        sweetAlert("اخطاریه","شماره تماس لازمی میباشد","warning");  
        return false;  
      }

    
            s_name1.push({
                s_name,
                id
            })
            s_fname1.push({
                s_fname,
                id
            })
            s_school1.push({
                s_school,
                id
            })
            s_phone1.push({
                s_phone,
                id
            })
            note1.push({
                note,
                id
            })
            
        var newid = id++;  
        

        $("#table1").append('<tr valign="top" id="'+newid+'">\n\
        <td width="100px" class="">' + newid + '</td>\n\
            <td style="font-size:20px;" width="10px" class="s_name'+newid+'">' + $("#s_name").val() + '</td>\n\
            <td style="font-size:20px;" width="10px" class="s_fname'+newid+'">' + $("#s_fname").val() + '</td>\n\
            <td style="font-size:20px;" width="10px" class="s_school'+newid+'">' + $("#s_school").val() + '</td>\n\
            <td style="font-size:20px;" width="10px" class="s_phone'+newid+'">' + $("#s_phone").val() + '</td>\n\
            <td style="font-size:20px;" width="10px" class="note'+newid+'">' + $("#note").val() + '</td>\n\
            <td style="font-size:20px;" width="10px"><a href="javascript:void(0);" class="remCF btn btn-danger shadow btn-xs sharp"><i class="fa fa-trash"></i></a></td>\n\
        </tr>');

    
    
    
        $('#s_name').val('');
        $('#s_fname').val('');
        $('#s_school').val('');
        $('#s_phone').val('');
        $('#note').val('');
      

        
         

    });
    
    

    var serializedData = $('#form1').serialize();

    $.ajax({
        url: "insert_student.php", 
        type: "post",
        data: serializedData
    });


     


    $("#table1").on('click', '.remCF', function() {
        
        let id =parseInt(this.parentNode.parentNode.id)

        
        s_name1=s_name1.filter((v)=>{
            return v.id !=id;
        })
        s_fname1=s_fname1.filter((v)=>{
            return v.id !=id;
        })
        s_school1=s_school1.filter((v)=>{
            return v.id !=id;
        })
        s_phone1=s_phone1.filter((v)=>{
            return v.id !=id;
        })
        note1=note1.filter((v)=>{
            return v.id !=id;
        })
        $(this).parent().parent().remove();
        
        
    });

   

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

        
        
      
        var lastRowId = $('#table1 tr:last').attr("id"); /* finds id of the last row inside table */
        
        var s_name1_send = new Array();
        var s_fname1_send = new Array();
        var s_school1_send = new Array();
        var s_phone1_send = new Array();
        var note1_send = new Array();
        for ( var i = 1; i <= lastRowId; i++) {
            
         
            
            s_name1_send.push($("#"+i+" .s_name"+i).html());
            s_fname1_send.push($("#"+i+" .s_fname"+i).html());
            s_school1_send.push($("#"+i+" .s_school"+i).html());
            s_phone1_send.push($("#"+i+" .s_phone"+i).html());
            note1_send.push($("#"+i+" .note"+i).html());
            
        }

      
        
        var sends_name = JSON.stringify(s_name1_send);
        var sends_fname = JSON.stringify(s_fname1_send);
        var sends_school = JSON.stringify(s_school1_send);
        var sends_phone = JSON.stringify(s_phone1_send);
        var sendnote = JSON.stringify(note1_send);
        
        
        


        
            if(s_name1_send == null || s_name1_send==""){
            sweetAlert("Warining","لیست خالی میباشد","error");  
            return false;  
            } else {
    

        $.ajax({
            url: "insert_student.php",
            type: "post", 
            data: {
                s_name : sends_name,
                s_fname : sends_fname,
                s_school : sends_school,
                s_phone:sends_phone,
                note:sendnote
            },
          success: function(data) {
                //    alert(data?'success':'failed');
              //console.log(data);
            
              swal("معلومات شما موفقانه ثبت سیستم شد","","success")
                window.setTimeout(function(){ 
                    window.location = "view_student.php";
            } ,1500);
           
                }
              
        }); 
        
    }
    }); 
});

 

  

  
</script>
    
	
</body>
</html>
