<?php
//start of Header, DB, priviledges// 
$path = pathinfo(__FILE__);
$ROOT='../';
$ROOTfixed = '../fixedPages';
include  $ROOTfixed . '/access.php';

//end of Header, DB, priviledges// 
?>
<button style="float: right; margin:1% " onclick="window.location='project_search.php'">Project Search
    List</button>
<br>
<h2> Project Timesheet</h2> <br>

<div class="">
    <div class="table-responsive">

    <table class="table re_column_width_table table-bordered table-striped" id="myTable">
        <thead>
            <tr>
                <th>
                    period
                </th>
                     <th style="width: 8rem;">
                    Project Name
                </th>
                <th hidden> hidden popup</th>
               
                        <th style="width: 8rem;">
                    Item
                </th>
                <th style="width: 10rem;">
                    Supplier
                </th>
                
                <th>
                    Start Date
                </th>
                <th>
                    End Date
                </th>
                <th>
                    Working Days
                </th>
                 <th>
                    Absent Days
                </th>
                 <th>
                    Weekend Days
                </th>
                 <th>
                    &#128206;
                </th>
                <th>confirm</th>
            </tr>

            <tr class="row_id" id="">
                <form action="link\project_timesheet_update.php" method="post" enctype="multipart/form-data">

                    <!----------------------this should be added in each form------------>
                    <?php fixedformdata() ?>
                    <!----------------------End of Code that should be added in each form------------>

                    <?php if (isset($GET_['id'])) {
                        $formID = $GET_['id'];
                    } else {
                        $formID = '';
                    } ?>
                    <input type="hidden" name="row_id" class="row_id" id="" value="">
                    <th><input type hidden name="period" id="period" value="<?php echo $currentFPID ?>">
                            <?php echo $currentFP ?>
                    </th>
                    <th><select  class="proj_id" name="proj_id" id="proj_id" required >
                            <option value="">Select Project</option>
                            <?php foreach ($allactiveprj as $activeprj) {
                                echo "<option value='" . $activeprj['ID'] . "' >" . $activeprj['project_name'] . "</option>";
                            }; ?>

                        </select>
                        <div id="show_row_details"></div>
                    </th>
                   
                    <th>

                       
                        <input name="item_id"  id="item_id" required>
                               
                         
                       

                    </th>
                    <th>
                       
                        <?php echo"
                        <select class='sup supl0' name='supl'  id='supl0".$tenderBudgetitem["ID"]."' required>
               <option value=''>Select Supplier</option>
               ";
  foreach ($activesuppliers as $suppliers) {
                              
                                echo "
                <option  value='" . $suppliers['suid'] . "-". $suppliers['subid'] . "'>" . $suppliers['supName'] . "</option>";
                
                    };
                    echo "
                </select> ";?>
                    </th>
                   
                    <th><input type="date" name="t_sh_start_date" id="t_sh_start_date" required ></th>
                    <th><input type="date" name="t_sh_end_date" id="t_sh_end_date" required ></th>
                    <th><input type="number" name="prj_item_work_days" id="prj_item_work_days" required step="0.01" ></th>
                    <th><input type="number" name="absent_days" id="absent_days" required  step="0.01" ></th>
                       <th><input type="number" name="weekend_days" id="weekend_days" required  step="0.01" ></th>
                       <th>
                       <label class="fa fa-clipboard" for="fileToUploadNew"></label>
        <input type="file" name="fileToUploadNew" id="fileToUploadNew" required >
                                                                                
                      </th>
                    <th><button type="submit" name="save" id="save">Save</button></th>
                </form>
            </tr>
        </thead>
        <tbody>
        
            <?php foreach ($projectTS as $ts) { 
            $i=0;
            ?>
                <tr class="row_id" id="<?php if (isset($ts['ID'])) {
                                            echo $ts['ID'];
                                        } ?>">
                    <form action="link\project_timesheet_update.php" method="post" enctype="multipart/form-data">

                        <!----------------------this should be added in each form------------>
                        <?php fixedformdata() ?>
                        <!----------------------End of Code that should be added in each form------------>

                        <?php if (isset($GET_['id'])) {
                            $formID = $GET_['id'];
                        } else {
                            $formID = '';
                        } ?>
                        <td hidden>
                        <input type="text" name="row_id" value="<?php echo $ts['ID']; ?>">
                        </td>
                       
                        <td>
                               
                                <?php foreach ($allfinancialPeriods as $activeyears) {
                                    $selected = '';
                                    if ($ts['period'] == $activeyears['ID']) {
                                          echo '<input type="hidden" name="period" id="period'.$ts['ID'].' value="' . $activeyears['ID'] . '">' . $activeyears['period_name'];
                                    }
                                 
                                }
                                ?>
                      
                        </td>
                        <td><select  name='proj_id' id='proj_id<?php echo $ts['ID']; ?>' required>
                                <option value="">Select Project</option>
                                <?php foreach ($allactiveprj as $activeprj) {
                                    $selected = '';
                                    if ($ts['proj_id'] == $activeprj['ID']) {
                                        $selected = 'selected';
                                    }
                                    echo "<option $selected value='" . $activeprj['ID'] . "'>" . $activeprj['project_name'] . "</option>";
                                }; ?>

                            </select>
                            <div id="show_row_details<?php echo $ts['ID']; ?>"></div>
                        </td>
                        <!--<td>-->
                        <!--  <input id="proj_alloc_numb<?php echo $ts['ID']; ?>" name="proj_alloc_numb" value="<?php $ts['proj_alloc_numb'] ?>">-->
                         
                        <!--</td>-->
                        <td>
                   
                    
  <select name="item_id"  id="item_id<?php echo $ts['ID']; ?>" required>
                                <option value="">Select Item</option>
                                <?php
                                
                                foreach ($timesheetItems as $item) {
                                    $itemID = $item['ID'];
                                    $itemName = $item['item_nme_db'];
                                    
if($ts['item_id']==$item['ID']){
    $selected ='selected';
}else{

    $selected ='';  
}
                                    echo "<option ".$selected." value='" . $itemID . "'>" . $itemName . "</option>";
                                } ?>
                            </select>
                        </td>
                        <td>
                          <?php echo"
                        <select class='sup supl".$i."' required name='supl' id='supl".$ts['ID']."'>";
              foreach ($activesuppliers as $suppliers) {
                                $selected = '';
                                if ($suppliers['suid']."-".$suppliers['subid'] == $ts["supl"]."-".$ts["supl_brnch"]) {
                                    $selected = 'selected';
                                }

                                echo "
                <option  $selected value='" . $suppliers['suid'] . "-". $suppliers['subid'] . "'>" . $suppliers['supName'] . "</option>";
                            };
                            echo "
                </select> ";?>
                       

                        </td>
                      
                        <td><input type="date" required name="t_sh_start_date" id="t_sh_start_date<?php echo $ts['ID']; ?>" value="<?php echo $ts['t_sh_start_date']; ?>"></td>

                        <td><input type="date" required name="t_sh_end_date" id="t_sh_end_date<?php echo $ts['ID']; ?>" value="<?php echo $ts['t_sh_end_date']; ?>"></td>

                        <td><input type="number" step=".01" required name="prj_item_work_days" id="prj_item_work_days<?php echo $ts['ID']; ?>" value="<?php echo $ts['prj_item_work_days']; ?>"></td>
                        <td><input type="number" step=".01" required name="absent_days" id="absent_days<?php echo $ts['ID']; ?>" value="<?php echo $ts['absent_days']; ?>"></td>
 
                       <td><input type="number" step=".01" required name="weekend_days" id="weekend_days<?php echo $ts['ID']; ?>" value="<?php echo $ts['weekend_days']; ?>"></td>
                       <td>
               
         <input type="file" id="fileToUpload" name="fileToUpload">
  <label for="fileToUpload"> <span class="fas fa-file-upload"></span></label>
  <?php
  if(($ts['uploads']) <>''){echo 
  '<a href="link/uploads/'.$ts['uploads'].'" target="_blank">
  <i class="fa fa-download" aria-hidden="true" ></i></a>';};
               ?>
               
           
          </td> 
                        <td>
                          <?php  
                               $permissioncheck=explode(',',$permissionrow['permissions']);
             if(in_array('edit',$permissioncheck)){ ?>
                <button type="submit" name="edit" id="edit" value="<?php echo $ts['ID']; ?>"><i class="fa fa-save"></i></button>
                <?php };
                
                   $permissioncheck=explode(',',$permissionrow['permissions']);
             if(in_array('delete',$permissioncheck)){ ?>
                <button type="submit" name="delete" id="delete" value="<?php echo $ts['ID']; ?>"><i class="fa fa-trash"></i></button>
                <?php };
                ?>
                           
                   
                    </td>
                    </form>
                </tr><?php } $i++ ?>
        </tbody>
    </table>
</div>
</div>

<script>


    $(document).on('click', 'tr', function() {

        var row_id = $(this).attr('id');
        // alert(row_id);
        
        $('#proj_id' + row_id).on('change', function() {

            var proj_id = $(this).find(":selected").val();
            // alert(proj_id);
            // $('#suplierBranchID' + row_id).html('');
            $.ajax({
                url: 'ajax.php',
                method: 'post',
                data: {
                    project_id: proj_id,
                    row_id: row_id,
                },
                dataType: 'text', //data type can be json//
                success: function(data) {

                    $('#show_row_details' + row_id).html(data);
                    $("#modal" + row_id).css("display","block");
                    $(".btn-close"+row_id).on("click",function(){
                        $('#proj_id' + row_id).prop('selectedIndex',0);
                           $("#modal" + row_id).css("display","none");
               
                        // $("#").append($(data).find("#resp_item_id"+row_id));
                        })
                                    $(".modal").on("click","li",function(){
                               var id=$(this).attr("id");
                               var res_item_id_val=$(data).find("#resp_item_id"+id).val();
                               alert(res_item_id_val);
                              $("#item_id"+row_id).val(res_item_id_val);
                           })
                        
                },
            })
        });
  
    
    });
</script>
<?php
include $ROOTfixed.'/footer.php';
?>