<?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["btnCurrAdd"]))
    {
        
       $hid=$_POST['txtHid'];
            
        $filename = $_FILES["fileImg"]["name"]; 
        $tempname = $_FILES["fileImg"]["tmp_name"];     

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

        $insert1 = mysqli_query($con,"UPDATE `tbl_curriculum` SET `curriculum_link`='$folder' WHERE curriculum_id=$hid");

         if($insert1) {
                if($filename!=""){
                    $fold=move_uploaded_file($tempname, $folder);
                     if($oldfile!=$newfile){
                        unlink($oldfile);
                    }
                }
                echo "<script>alert('Curriculum 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 Curriculum</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 Curriculum</h5></div>
              <div class="col-lg-5">
                <!-- <h5 class="card-title"><a href="view_documentinfo.php" class="btn btn-secondary">View Curriculum</a></h5> -->
              </div>
              </div>           
                <?php
                        $selres = mysqli_query($con,"SELECT * FROM tbl_curriculum");
                         while($rowres = mysqli_fetch_array($selres)) {                                
                ?>

                <!-- Document Form Elements -->
                <form name="FormCurri" method="post" enctype="multipart/form-data" class="pt-20">  
                  <div class="row mb-3">
                  <label for="inputText" class="col-sm-4 col-form-label">CURRICULUM FILE</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['curriculum_id']; ?>" hidden>

                    <input type="text" id="image_u" name="imagepath" class="form-control" value="<?php echo $rowres['curriculum_link']; ?>" hidden>
                    <label><?php echo $rowres['curriculum_link']; ?></label><br>
                    <input type="file" name="fileImg" id="fileImg" height="100"  value="">   
                  </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="btnCurrAdd" id="btnCurrAdd" 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');
   ?>