<?php require_once('../auth/auth.php');require_once('../auth/function.php'); ?>
<?php 
if(isset($_GET['prid'])){
  $prid= $_GET['prid'];
  $project= mysqli_query($conn,"SELECT prid, pr_coid, pr_brid, pr_name, date(pr_sdate) pr_sdate, date(pr_edate) pr_edate, pr_active FROM project WHERE pr_coid='$coid' AND prid='$prid'")or die(mysqli_error($conn));
  $prr = mysqli_fetch_assoc($project);
  $prid      =   $prr['prid'];  
  $pr_brid   =   $prr['pr_brid']; 
  $pr_name   =   $prr['pr_name'];  
  $pr_sdate  =   $prr['pr_sdate'];
  $pr_edate  =   $prr['pr_edate'];
  $pr_active =   $prr['pr_active'];  

  if ($pr_active=='1') {
    $active_view="Active";
  }else if ($pr_active=='0') {
    $active_view="Deactive";
  }
}
?>
<section class="content">
   <div class="container-fluid">
      <div class="row">
         <div class="col-md-2"></div>
         <div class="col-md-8">
            <div class="card card-primary card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
               <div class="card-header" style="padding: 5px 8px;">
                  <h3 class="card-title">Project Add</h3>
                  <div class="card-tools">
                     <button type="button" class="btn btn-tool" data-card-widget="remove">
                     <i class="fas fa-times"></i>
                     </button>
                  </div>
               </div>
               <div class="card-body cscroll" style="height: 400px;">
                  <form id="project_form" action="#" method="post" autocomplete="off">
                    <input type="hidden" name="pr_brid" value="<?php echo $brid;?>" required="true">
                    <?php if(@$_GET['prid']){  ?>
                      <input type="hidden" name="prid" value="<?php echo @$prid ;?>" required="true">
                    <?php } else { ?>
                      <input type="hidden" name="prid">
                    <?php } ?>
                     <div class="col-md-12">
                        <div class="row">
                           <div class="col-md-4">
                              <div class="form-group">
                                 <label>Project Name</label>
                                 <input type="text" class="form-control form-control-sm" value="<?php echo @$pr_name;?>" name="pr_name" required="true">
                              </div>
                           </div>
                           <div class="col-md-2">
                              <div class="form-group">
                                 <center><label>Start Date</label></center>
                                 <input type="text" name="pr_sdate" class="form-control form-control-sm" onmouseover="lcdate(this)" readonly="true" required="true" value="<?php if(@$_GET['prid']){ echo $pr_sdate;}else{ echo $date;}?>" style="text-align:center;">
                              </div>
                           </div>
                           <div class="col-md-2">
                              <div class="form-group">
                                 <center><label>End date</label></center>
                                 <input type="text" name="pr_edate" class="form-control form-control-sm" onmouseover="stdate(this)" readonly="true" required="true" value="<?php if(@$_GET['prid']){ echo $pr_edate;}else{ echo $date;}?>" style="text-align:center;">
                              </div>
                           </div>   
                            <div class="col-sm-2">
                              <div class="form-group">
                                <label>Status</label>
                                <select name="pr_active" class="form-control select2" style="width: 100%;" required="true">
                                  <?php if(@$_GET['prid']){  ?>
                                    <option value="<?php echo @$pr_active;?>"> <?php echo @$active_view;?></option>
                                  <?php } ?>
                                  <option value="1">Active</option>
                                  <option value="0">Deactive</option>
                                </select>
                            </div>
                          </div>   
                           <div class="col-md-2">
                              <center>
                                 <button type="submit" id="save" class="btn btn-success btn-xs" style="margin-top:18.5px;"><i class="fas fa-circle-plus"></i> Add Project</button>
                              </center>
                           </div>
                           <div class="col-md-12">
                              <hr style="margin-top:-5px;margin-bottom: 7px;">
                           </div>
                        </div>
                     </div>
                  </form>
                     <div class="col-md-12">
                       <table class="ttable" cellpadding="0" cellspacing="0">
                          <thead>
                             <tr>
                                <th class="text-center" style="width:2%;">S/L</th>
                                <th class="text-center" style="width:40%;">Project Name</th>
                                <th class="text-center" style="width:15%;">Start Date</th>
                                <th class="text-center" style="width:15%;">End Date</th>
                                <th class="text-center" style="width:10%;">Status</th>
                             </tr>
                          </thead>
                          <tbody>
                           <?php
                            $resueeelt = mysqli_query($conn,"SELECT * FROM project WHERE pr_coid='$coid' AND pr_brid='$brid'") or die (mysqli_error($conn));
                            $i = 0;
                            while ($row= mysqli_fetch_assoc($resueeelt)){
                              $vprid = $row['prid'];
                              $vpr_name = $row['pr_name'];
                              $pr_sdate = $row['pr_sdate'];
                              $pr_edate = $row['pr_edate'];
                              $pr_active= $row['pr_active'];
                            ?>
                              <tr> 
                                <td class="text-center"><?php echo ++$i;?></td>
                                <td class="btn-outline-dark" onclick="pagecall('../g_purchase/project_add.php?prid=<?php echo $vprid;?>','#mview')"><?php echo $vpr_name;?></td>
                                <td class="text-center"><?php echo date('d-m-Y h:i:s a',strtotime($pr_sdate));?></td>
                                <td class="text-center"><?php echo date('d-m-Y h:i:s a',strtotime($pr_edate));?></td>
                                <td class="text-center">
                                  <?php
                                  if ($pr_active=='0') {
                                    echo "<i class='fa fa-close' style='font-size: 12px; color: red;' aria-hidden='true'></i>";
                                  } else {
                                    echo "<i class='fa fa-check-circle' style='font-size: 12px; color: green;' aria-hidden='true'></i>";
                                  }
                                  ;?>
                              </tr>
                            <?php } ?>
                          </tbody>
                       </table>
                     </div>
               </div>
            </div>
         </div>
         <div class="col-md-2"></div>
      </div>
   </div>
</section>
<script>
   $(document).ready(function() {
   $("select").select2(); 
   $('#project_form').on('submit', function(e){
   e.preventDefault(); 
   $('#save').prop('disabled', true);
   $('#save').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...'); 
        $.post("../g_purchase/project_db.php",$(this).serialize(),function(data){
        toastr.success(Successfully, yourdata);
        $('#project_form')[0].reset();
        $('#save').prop('disabled', false);
        $('#save').html('Add Project');
        pagecall('../g_purchase/project_add.php','#mview');
      });
     });
   });  
</script>