<?php 
include('../dbconnect.php');
if(isset($_POST['addCto'])){

    $query = $conn->prepare("INSERT INTO `cto`(`date`, `Holiday Name`, `Holiday Type`, `Marvin`, `Yves`) 
VALUES (?,?,?,?,?)");
$query->bind_param('sssss', $_POST['date'], $_POST['holiday_name'], $_POST['type'], $_POST['marvin'], $_POST['yves']);
$query->execute();



    $cto = $conn->query("SELECT * FROM pto WHERE name = 'Compensatory Time Off' AND empid IN ('ATV18-001','ATV18-027')");
    while ($cto_row =  $cto->fetch_assoc()) {
       $ptoearned=  $row['ptoearned'] + 1;

       if($_POST['marvin'] == 'Yes' && $cto_row['empid'] == 'ATV18-001'){

        $updatecto = $conn->query("UPDATE pto SET ptoearned='$ptoearned' WHERE name='Compensatory Time Off' AND empid= '".$cto_row['empid']."'");
        echo "UPDATE pto SET ptoearned='$ptoearned' WHERE name='Compensatory Time Off' AND empid= '".$cto_row['empid']."'";

       }
       if($_POST['yves'] == 'Yes' && $cto_row['empid'] == 'ATV18-027'){
        $updatecto = $conn->query("UPDATE pto SET ptoearned='$ptoearned' WHERE name='Compensatory Time Off' AND empid= '".$cto_row['empid']."'");
        echo "UPDATE pto SET ptoearned='$ptoearned' WHERE name='Compensatory Time Off' AND empid= '".$cto_row['empid']."'";

       }
      


     
    }
    

  //  echo $conn->error;





header('location: cto.php?success');
}

if($tbl_data == 1){
    $query = $conn->query("SELECT * FROM cto ");
    while ($row = $query->fetch_assoc()) {
        echo'<tr> 
        <td>'.date('m/d/Y',strtotime($row['date'])).'</td>
        <td>'.$row['Holiday Name'].'</td>
        <td>'.$row['Holiday Type'].'</td>
        <td>'.$row['Marvin'].'</td>
        <td>'.$row['Yves'].'</td>
        <td> <button class="btn btn-danger deleteCTO" id="'.$row['id'].'" onclick="delCTO()">Delete</button></td>
        </tr>';
        # code...
    }
}

if(isset($_POST['delCTO'])){
    $query = $conn->query("DELETE FROM `cto` WHERE id='".$_POST['id']."'");

}
if($cto_sum==1){

    

    $emp = $conn->query("SELECT *, empinfo.empid AS 'empinfo_id' FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid WHERE empinfo.empid IN ('ATV18-001','ATV18-027')");
    while($row = $emp->fetch_assoc()){

        if($row['empid'] == 'ATV18-001'){
            $ctoearned = $conn->query("SELECT * FROM cto WHERE marvin='Yes'")->num_rows;
            $qusedcto = $conn->query("SELECT SUM(duration) AS 'used_cto' FROM `leave` WHERE type = 'Compensatory Time Off' AND empid='ATV18-001' AND status IN('3')");

        }else{
            $ctoearned = $conn->query("SELECT * FROM cto WHERE yves='Yes'")->num_rows;
            $qusedcto = $conn->query("SELECT SUM(duration) AS 'used_cto' FROM `leave` WHERE type = 'Compensatory Time Off' AND empid='ATV18-027' AND status IN('3','2')");

        }
        $usedcto = $qusedcto->fetch_assoc();
        echo '<tr> 
        <td>'.$row['empinfo_id'].'</td>
        <td>'.$row['firstname'].' '.$row['surname'].'</td>
        <td>'.$row['team'].'</td>
       
        <td>'.$ctoearned.'</td>
        <td>'.($usedcto['used_cto'] == null? '0': $usedcto['used_cto']).'</td>
        </tr>';

    }
}
?>