<?php
     include_once('connect.php');
    session_start(); 
    
    // Check if the user is logged in
    if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
         echo "<script type='text/javascript'>window.location.href = 'index.php';</script>"; // Redirect to login page
        exit();
    }
    
    
    include_once('head.php');
?>
<head>
  <style type="text/css">
    textarea{
      width: 400px;
      height: 110px;
      border-radius: var(--bs-border-radius);
    }
  </style>
</head>
<?php
    
    if(isset($_POST["btnDocumentInfoAdd"]))
    {
        // $filename = $_FILES["fileImg"]["name"]; 
        // $tempname = $_FILES["fileImg"]["tmp_name"];     

        // if($filename==""){
        //      $folder=$_POST['imagepath'];
        // }
        // else {
        //     $folder ="../uploads/".$filename;
        // }
        // $oldfile=$_POST['imagepath'];
        // $newfile ="../uploads/".$filename;

        // $filename1 = $_FILES["fileImg1"]["name"]; 
        // $tempname1 = $_FILES["fileImg1"]["tmp_name"];     

        // if($filename1==""){
        //      $folder1=$_POST['imagepath1'];
        // }
        // else {
        //     $folder1 ="../uploads/".$filename1;
        // }
        // $oldfile1=$_POST['imagepath1'];
        // $newfile1 ="../uploads/".$filename1;

        // $filename2 = $_FILES["fileImg2"]["name"]; 
        // $tempname2 = $_FILES["fileImg2"]["tmp_name"];     

        // if($filename2==""){
        //      $folder2=$_POST['imagepath2'];
        // }
        // else {
        //     $folder2 ="../uploads/".$filename2;
        // }
        // $oldfile2=$_POST['imagepath2'];
        // $newfile2 ="../uploads/".$filename2;

       $hid=$_POST['txtHid'];

      // FILE 1
      $filename = $_FILES["fileImg"]["name"];
      $tempname = $_FILES["fileImg"]["tmp_name"];
      $folder = $filename == "" ? $_POST['imagepath'] : "../uploads/" . $filename;
      if ($filename != "") {
          move_uploaded_file($tempname, $folder);
      }

      // FILE 2
      $filename1 = $_FILES["fileImg1"]["name"];
      $tempname1 = $_FILES["fileImg1"]["tmp_name"];
      $folder1 = $filename1 == "" ? $_POST['imagepath1'] : "../uploads/" . $filename1;
      if ($filename1 != "") {
          move_uploaded_file($tempname1, $folder1);
      }

      // FILE 3
      $filename2 = $_FILES["fileImg2"]["name"];
      $tempname2 = $_FILES["fileImg2"]["tmp_name"];
      $folder2 = $filename2 == "" ? $_POST['imagepath2'] : "../uploads/" . $filename2;
      if ($filename2 != "") {
          move_uploaded_file($tempname2, $folder2);
      }

      // FILE 4
      $filename3 = $_FILES["fileImg3"]["name"];
      $tempname3 = $_FILES["fileImg3"]["tmp_name"];
      $folder3 = $filename3 == "" ? $_POST['imagepath3'] : "../uploads/" . $filename3;
      if ($filename3 != "") {
          move_uploaded_file($tempname3, $folder3);
      }

      // FILE 5
      $filename4 = $_FILES["fileImg"]["name"];
      $tempname4 = $_FILES["fileImg"]["tmp_name"];
      $folder4 = $filename4 == "" ? $_POST['imagepath4'] : "../uploads/" . $filename4;
      if ($filename4 != "") {
          move_uploaded_file($tempname4, $folder4);
      }

      // FILE 6
      $filename5 = $_FILES["fileImg5"]["name"];
      $tempname5 = $_FILES["fileImg5"]["tmp_name"];
      $folder5= $filename5 == "" ? $_POST['imagepath5'] : "../uploads/" . $filename5;
      if ($filename5 != "") {
          move_uploaded_file($tempname5, $folder5);
      }

      // FILE 7
      $filename6 = $_FILES["fileImg6"]["name"];
      $tempname6 = $_FILES["fileImg6"]["tmp_name"];
      $folder6 = $filename6 == "" ? $_POST['imagepath6'] : "../uploads/" . $filename6;
      if ($filename6 != "") {
          move_uploaded_file($tempname6, $folder6);
      }

       // FILE 8
      $filename7 = $_FILES["fileImg7"]["name"];
      $tempname7 = $_FILES["fileImg7"]["tmp_name"];
      $folder7 = $filename7 == "" ? $_POST['imagepath7'] : "../uploads/" . $filename7;
      if ($filename7 != "") {
          move_uploaded_file($tempname7, $folder7);
      }

        

        $insert1 = mysqli_query($con,"UPDATE `tbl_documentinfo` SET `affiliation_letter`='$folder',`company_renewal`='$folder1',`noc`='$folder2',`recognition_cert`='$folder3',`building_safety`='$folder4',`fire_safety`='$folder5',`self_certification`='$folder6',`water_health_sani`='$folder7' WHERE documentinfo_id=$hid");

         if($insert1) {
                
                echo "<script>alert('Document Information Updated');</script>";

            }
            else{
                echo "<script>alert('Updation Failed!');</script>";
            }    

    }
    ?>

  <main id="main" class="main">
    <div class="pagetitle">   
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="index.html">Home</a></li>
          <li class="breadcrumb-item active">Update Document Information</li>
        </ol>
      </nav>
    </div><!-- End Page Title -->    
     <section class="section">
      <div class="row">
        <div class="col-lg-1">
        </div>
        <div class="col-lg-10">
          <div class="card">
            <div class="card-body">
              <div class="row mb-3  bord-bottom">
             <div class="col-lg-5 "><h5 class="card-title">Update Document Information</h5></div>
              <div class="col-lg-5">
                <!-- <h5 class="card-title"><a href="view_documentinfo.php" class="btn btn-secondary">View Document Information</a></h5> -->
              </div>
              </div>           
                <?php
                        $selres = mysqli_query($con,"SELECT * FROM tbl_documentinfo");
                         while($rowres = mysqli_fetch_array($selres)) {                                
                ?>

                <!-- Document Form Elements -->
                <form name="FormPromoter" method="post" enctype="multipart/form-data" class="pt-20">  
                  <div class="row mb-3">
                  <label for="inputText" class="col-sm-4 col-form-label">COPIES OF AFFILIATION/UPGRADATION LETTER AND RECENT EXTENSION OF AFFILIATION</label>
                  <div class="col-sm-5">
                   <!--  <input class="form-control" type="file"  name="fileImg" id="fileImg"  > -->
                    <input type="text" id="txtHid" name="txtHid" class="form-control" value="<?php echo $rowres['documentinfo_id']; ?>" hidden>

                    <input type="text" id="image_u" name="imagepath" class="form-control" value="<?php echo $rowres['affiliation_letter']; ?>" hidden>
                    <label><?php echo $rowres['affiliation_letter']; ?></label><br>
                    <input type="file" name="fileImg" id="fileImg" height="100"  value="">   
                  </div>
                </div>    
                 <div class="row mb-3">
                  <label for="inputText" class="col-sm-4 col-form-label">COPIES OF SOCIETIES/TRUST/COMPANY REGISTRATION/RENEWAL CERTIFICATE</label>
                  <div class="col-sm-5">
                    <input type="text" id="image_u" name="imagepath1" class="form-control" value="<?php echo $rowres['company_renewal']; ?>" hidden>
                    <label><?php echo $rowres['company_renewal']; ?></label><br>
                    <input class="form-control" type="file"  name="fileImg1" id="fileImg1"  >
                  </div>
                </div>  
                 <div class="row mb-3">
                  <label for="inputText" class="col-sm-4 col-form-label">COPY OF NO OBJECTION CERTIFICATE (NOC) ISSUED, IF APPLICABLE, BY THE STATE GOVT./UT</label>
                  <div class="col-sm-5">
                    <input type="text" id="image_u" name="imagepath2" class="form-control" value="<?php echo $rowres['noc']; ?>" hidden>
                    <label><?php echo $rowres['noc']; ?></label><br>
                    <input class="form-control" type="file"  name="fileImg2" id="fileImg2"  >
                  </div>
                </div>  
                 <div class="row mb-3">
                  <label for="inputText" class="col-sm-4 col-form-label">COPIES OF RECOGNITION CERTIFICATE UNDER RTE ACT, 2009, AND IT'S RENEWAL IF APPLICABLE</label>
                  <div class="col-sm-5">
                    <input type="text" id="image_u" name="imagepath3" class="form-control" value="<?php echo $rowres['recognition_cert']; ?>" hidden>
                    <label><?php echo $rowres['recognition_cert']; ?></label><br>
                    <input class="form-control" type="file"  name="fileImg3" id="fileImg3"  >
                  </div>
                </div>  
                 <div class="row mb-3">
                  <label for="inputText" class="col-sm-4 col-form-label">COPY OF VALID BUILDING SAFETY CERTIFICATE AS PER THE NATIONAL BUILDING CODE</label>
                  <div class="col-sm-5">
                    <input type="text" id="image_u" name="imagepath4" class="form-control" value="<?php echo $rowres['building_safety']; ?>" hidden>
                    <label><?php echo $rowres['building_safety']; ?></label><br>
                    <input class="form-control" type="file"  name="fileImg4" id="fileImg4"  >
                  </div>
                </div>  
                 <div class="row mb-3">
                  <label for="inputText" class="col-sm-4 col-form-label">COPY OF VALID FIRE SAFETY CERTIFICATE ISSUED BY THE COMPETENT AUTHORITY</label>
                  <div class="col-sm-5">
                    <input type="text" id="image_u" name="imagepath5" class="form-control" value="<?php echo $rowres['fire_safety']; ?>" hidden>
                    <label><?php echo $rowres['fire_safety']; ?></label><br>
                    <input class="form-control" type="file"  name="fileImg5" id="fileImg5"  >
                  </div>
                </div>   
                 <div class="row mb-3">
                  <label for="inputText" class="col-sm-4 col-form-label">COPY OF THE SELF CERTIFICATION SUBMITTED BY THE SCHOOL FOR AFFILIATION/UPGRADATION/EXTENSION OF AFFILIATION*</label>
                  <div class="col-sm-5">
                    <input type="text" id="image_u" name="imagepath6" class="form-control" value="<?php echo $rowres['self_certification']; ?>" hidden>
                    <label><?php echo $rowres['self_certification']; ?></label><br>
                    <input class="form-control" type="file"  name="fileImg6" id="fileImg6"  >
                  </div>
                </div>  
                <div class="row mb-3">
                  <label for="inputText" class="col-sm-4 col-form-label">COPIES OF VALID WATER, HEALTH AND SANITATION CERTIFICATES</label>
                  <div class="col-sm-5">
                    <input type="text" id="image_u" name="imagepath7" class="form-control" value="<?php echo $rowres['water_health_sani']; ?>" hidden>
                    <label><?php echo $rowres['water_health_sani']; ?></label><br>
                    <input class="form-control" type="file"  name="fileImg7" id="fileImg7" >
                  </div>
                </div>  
                <div class="row mb-3">
                  <label class="col-sm-3 col-form-label"></label>
                  <div class="col-sm-5 ">
                     <input type="submit" class="btn btn-primary btn-center" name="btnDocumentInfoAdd" id="btnDocumentInfoAdd" value="Update">
                    
                  </div>
                </div>

              </form><!-- End Document Form Elements -->

              
                 <?php
                }
                ?>
            </div>
          </div>

        </div>
        <div class="col-lg-1">
        </div>
      </div>
    </section>

  </main><!-- End #main -->
  <?php
        include_once('footer.php');
   ?>