<?php 
  if(isset($_REQUEST['btnPrg'])){
    $userRec = $_REQUEST['userRec'];
    $recAppoint = $_REQUEST['recAppoint'];
    $Deptrec = $_REQUEST['Deptrec'];
    $prjc = $_REQUEST['prjc'];
    $progdate = date('Y-m-d', strtotime($_REQUEST['progdate'])); 
    $prgss = $_REQUEST['prgss'];
    $cmnt = $_REQUEST['cmnt'];
    ##### WPS ####
    $todate = date('Y-m-d H:i:s');

    $statement = $db->prepare("INSERT INTO tbl_work_progress(prj_progress, wrkp_num, dueprogress,recprog,deptprog_rec, stswp, ProgrecAppoint, userprog, prog_cmnt)
    VALUES(?,?,?,?,?,?,?,?,?)");
    $statement->execute(array($prjc,$prgss,$progdate,$todate,$Deptrec,1,$recAppoint,$userRec,$cmnt));
    
    $sql = "UPDATE tbl_projects SET wps=? WHERE projid =?";
    $stmt= $db->prepare($sql);
    $stmt->execute([$prgss, $prjc]);

    echo'<script type="text/javascript">
        jQuery(function validation(){
        swal("Progress Updated", "Successfully", "success", {
        button: "Continue",
        });
        });
    </script>';  
    
echo'<meta http-equiv="refresh"'.'content="2;URL=">'; 
}

?>

 <br>
 
<form id="" action="" method="POST" enctype="multipart/form-data" autocomplete="off">
<div class="row">
    <div class="col-md-1"></div>
   
    <div class="col-md-10" style="background-image: url(../img/placeholders/inner-texture-bg.png); border-radius: 8px;">
      <div style='border-radius: 8px;border: 2px solid #7E9350;width:100%;margin-top:1%;margin-bottom:1%;'>
          <center>
          <b style="font-family: century gothic;font-size: 23px;"><i class="fa fa-edit"></i> Physical Progress</b>
          </center>
          <br /><br />

            <div class="form-group row">
                <input type="hidden" name="userRec" value="<?php echo $userid;?>" required style="width: 90%;">
                <input type="hidden" name="recAppoint" value="<?php echo $tle_id;?>" required style="width: 90%;">
                <input type="hidden" name="Deptrec" value="<?php echo $role_id;?>" required style="width: 90%;">

                <label class="col-form-label col-2 col-lg-2 form-label text-lg-right">Project Code<span class="required" style="color:red">*</span></label>
                <div class="col-3 col-lg-3">
                
                <select class="select2 form-control w-100" name="prjc" id="prjc" data-placeholder="-select project-" required>
                    <option></option>
                        <?php
                            $sth = $db->query('SELECT * FROM tbl_projects WHERE projid="'.$pID.'" ORDER BY projid DESC');
                            try
                            {
                                while ($rr2 = $sth->fetch(PDO::FETCH_ASSOC)) {
                                ?>
                                    <option value="<?php echo $rr2['projid']; ?>"><?php echo $rr2['codeproj']; ?></option>
                                <?php
                                 }
                                }
                                catch (PDOException $ex) {
                                //Something went wrong rollback!
                                echo $ex->getMessage();
                             }
                        ?>	
                    </select>

                </div>

                <label class="col-form-label col-2 col-lg-2 form-label text-lg-right">Progress Date<span class="required" style="color:red">*</span></label>
                <div class="col-3 col-lg-3">
                   <input type="text" class="form-control" name="progdate" id="datepicker-1" placeholder="choose date" required>
                </div>
            </div>
            
            <div class="form-group row">
                <label class="col-form-label col-2 col-lg-2 form-label text-lg-right">Progress %<span class="required" style="color:red">*</span></label>
                <div class="col-3 col-lg-3">
                <input type="text" name="prgss" id="prgss" class="form-control" required placeholder="0.0" 
                 oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');">                  
                </div>

                
                <label class="col-form-label col-2 col-lg-2 form-label text-lg-right">Comment</label>
                <div class="col-3 col-lg-3">
                   <textarea type="text" name="cmnt" rows="2" class="form-control" placeholder="Write Here..." required></textarea>
                </div>
            </div>

            <hr>
            <center>
                <button type="reset" class="btn btn-default btn-sm" style="border-radius: 4px;">Reset</button>
                <button class="btn btn-primary btn-sm" name="btnPrg" style="border-radius: 4px;" type="submit" id="">Save Changes</button>
            </center>
         <br>
      </div> <!--border -->    
    </div>    
  </div>
  
  </form>

  <br>
  <div class="row">
    <div class="col-md-1"></div>

    <div class="col-md-10">
            
    <div class="table-responsive">
        <table class="table tmt-5">
            <thead class="bg-primary" style="color: #fff;">
            <tr>
                <td colspan="2">
                    <b>Progress History:</b>
                </td>
                <td>Remarks</td>
            </tr>
            </thead>

            <tbody>
                <?php 
                $wpQue=$db->prepare('SELECT *FROM tbl_work_progress WHERE prj_progress="'. $pID.'" ORDER BY workp_id DESC');
                $wpQue->execute();
                while($fff=$wpQue->fetch()){
                ?>

                <tr>
                    <td class="header">
                        <b><?php echo date('d/m/Y',strtotime($fff['dueprogress']));?></b>
                    </td>

                    <td class="header">
                        <b><?php echo $fff['wrkp_num']." %";?></b>
                    </td>

                    <td class="header">
                    <?php echo $fff['prog_cmnt'];?>
                    </td>
                </tr>
                <?php }?>
            </tbody>
        </table>
    </div>

</div>
</div>