<?php

session_start();

include ("../../include/global.inc");

$type = $_GET['type'];

//logowanie do łubudubu - sprawdzenie czy pracownik istnieje
if ($type=='1')
{

    $rcp = $_GET['rcp'];
    
    $rcp_hex = dechex($rcp);
    if (strlen($rcp_hex)=='4')
    {
        $rcp_hex = '0000'.$rcp_hex;
    }
    if (strlen($rcp_hex)=='5')
    {
        $rcp_hex = '000'.$rcp_hex;
    }
    if (strlen($rcp_hex)=='6')
    {
        $rcp_hex = '00'.$rcp_hex;
    }
    if (strlen($rcp_hex)=='7')
    {
        $rcp_hex = '0'.$rcp_hex;
    }
    
    if ($rcp == '')
    {
        $stop = '0000x1';
        echo $stop;
        die;
    }
    
    $query = "SELECT a.id, a.name as f_name, a.surname FROM workers a WHERE (a.rcp_card = '$rcp' OR a.rcp_card = '$rcp_hex') AND a.act = '1'";
    $result = mysqli_query($con, $query);
    $row = mysqli_fetch_array($result);
    $rowcount = mysqli_num_rows($result);

    if ($rowcount == 0)
    {
        $stop = '0000x2';
        echo $stop;
        die;
    }
    if ($rowcount > 1)
    {
        $stop = '0000x3';
        echo $stop;
        die;
    }
    
    //porprawny numer karty, loguję
    if ($rowcount == 1)
    {
        $full_name = $row['f_name'].' '.$row['surname'];
        $worker_id = $row['id'];
        
        $_SESSION['worker_id'] = $worker_id;
        
        echo 'true';
    }
    
}

//zmiana avatara
if ($type=='2')
{
    $wid = $_SESSION['worker_id'];
    $imag = $_GET['imag'];
    
    $query = "UPDATE workers SET avatar_src = '$imag' WHERE id = '$wid'";
    mysqli_query($con, $query);
}

//usuwanie urlopu
if ($type=='3')
{
    $holiday_id = $_GET['holiday_id'];
    
    $query = "SELECT YEAR(a.date_from) as vac_year, a.id, a.workers_id, a.type, a.vacations_days, a.date_from, a.date_to from workers_vacations_calendar a WHERE a.id = '$holiday_id'";
    $result = mysqli_query($con, $query);
    $row = mysqli_fetch_array($result);
    
    $worker_id = $row['workers_id'];
    $htype = $row['type'];
    $vacations_days = $row['vacations_days'];
    $vac_year = $row['vac_year'];
    $date_from = $row['date_from'];
    $date_to = $row['date_to'];
    $datetime = date('Y-m-d H:i:s');
    
    $query2 = "UPDATE workers_vacations SET counter = counter + '$vacations_days' WHERE workers_id = '$worker_id' and year = '$vac_year' and type = '$htype'";
    mysqli_query($con, $query2);
    
    $query3 = "DELETE from workers_vacations_calendar WHERE id = '$holiday_id'";
    mysqli_query($con, $query3);
    
    $query4 = "DELETE from lubudubu_holidays_applications WHERE date_from = '$date_from' AND date_to = '$date_to' AND worker_id = '$worker_id' AND htype = '$htype'";
    mysqli_query($con, $query4);
    
    $description = 'Anulowano wniosek urlopowy (potwierdzony] '.$date_from.' - '.$date_to;
    $query4 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '1', act = '1', first_show = '1', type = '3'";
    $query5 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '189', act = '1', first_show = '1', type = '3'";
    $query6 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '21', act = '1', first_show = '1', type = '3'";
    
    mysqli_query($con, $query4);
    mysqli_query($con, $query5);
    mysqli_query($con, $query6);
    
    $message = 'Anuluwano swój wniosek urlopowy [potwierdzony] '.$date_from.' - '.$date_to;
    $query6 = "INSERT INTO lubudubu_messages SET worker_id_sender = '0', worker_id_receiver = '$worker_id', message = '$message', confirm_read = '0', datetime_send = '$datetime'";
    mysqli_query($con, $query6);
}

//dodawanie urlopu
if ($type=='4')
{
    $htype = $_GET['t'];
    $counter = $_GET['c'];
    
    echo '<div style="padding-top: 5px; height: 25px; width: 97%; margin: 1%; float: left; background-color: lightblue; font-weight: bold; border-radius:5px; border: 1px solid #ccc;">';
        echo 'Od:';
    echo '</div>';
    
    $date = date('Y-m-d');
    
    echo '<div style="padding-top: 15px; height: 50px; width: 97%; margin: 1%; float: left; background-color: white; border-radius:5px; border: 1px solid #ccc;">';
        echo '<input style="display:none;" class="date-input-native" id="date_from" type="date" name="date" min="1950-01-01" max="2050-01-01" >';
        echo '<input style="font-size:32px; text-align: center;" class="date-input-fallback" id="alt" type="text" placeholder="Wybierz datę">';
        echo '<div id="picker" hidden></div>';
    echo '</div>';

    
    echo '<div style="width: 8%; margin: 1%; float: left;">';
        echo '';
    echo '</div>';
    
    echo '<div style="padding-top: 5px; height: 25px; width: 97%; margin: 1%; float: left; background-color: lightblue; font-weight: bold; border-radius:5px; border: 1px solid #ccc;">';
        echo 'Do:';
    echo '</div>';
    
    echo '<div style="padding-top: 15px; height: 50px; width: 97%; margin: 1%; float: left; background-color: white; border-radius:5px; border: 1px solid #ccc;">';
       echo '<input style="display:none;" class="date-input-native" id="date_to" type="date" name="date" min="1950-01-01" max="2050-01-01" >';
        echo '<input style="font-size:32px; text-align: center;" class="date-input-fallback" id="alt_to" type="text" placeholder="Wybierz datę" >';
        echo '<div id="picker_to" hidden></div>';
    echo '</div>';
    
    echo '<div style="padding-top: 5px; height: 25px; width: 97%; margin: 1%; float: left; background-color: lightblue; font-weight: bold; border-radius:5px; border: 1px solid #ccc;">';
        echo 'Podsumowanie:';
    echo '</div>';
    
    echo '<div style="padding-top: 15px; height: 50px; width: 97%; margin: 1%; float: left; background-color: white; border-radius:5px; border: 1px solid #ccc;">';
        echo '<div id="innerCompleteInfo"></div>';
    echo '</div>';
    
    echo '<div style="cursor: pointer; padding-top: 25px; height: 40px; width: 60%; margin: 1%; float: left; background-color: white; border-radius:5px; border: 1px solid #ccc; margin-left:19%;" onclick="send_application();">';
        echo 'wyślij wniosek';
    echo '</div>';
    
    echo '<input type="hidden" id="htype" value="'.$htype.'" />';
    echo '<input type="hidden" id="counter" value="'.$counter.'" />';
    
}

//wysyłanie wniosku
if ($type=='5')
{

//wszystkie daty z zakresu
function getDatesFromRange($start, $end, $format = 'Y-m-d') 
{ 
    $array = array(); 
    $interval = new DateInterval('P1D'); 
    $realEnd = new DateTime($end); 
    $realEnd->add($interval); 
  
    $period = new DatePeriod(new DateTime($start), $interval, $realEnd); 
    foreach($period as $date) {                  
        $array[] = $date->format($format);  
    } 
    
    return $array; 
} 

function getFreeDays($dat)
{
    global $con;
    $query = "SELECT a.id FROM graphic_holidays a WHERE a.date = '$dat'";
    $result = mysqli_query($con, $query);
    $rowcount = mysqli_num_rows($result);
    
    return $rowcount;
}

function getSunSat($dat)
{
    $day_of_week = date('N', strtotime($dat));
    
    return $day_of_week;
}

function typeofholiday($t)
{
    if ($t == 1) $txt = 'L4';
    if ($t == 2) $txt = 'Urlop wypoczynkowy długi';
    if ($t == 3) $txt = 'Urlop opiekuńczy';
    if ($t == 4) $txt = 'Urlop wypoczynkowy';
    if ($t == 5) $txt = 'Urlop okolicznościowy';
    if ($t == 6) $txt = 'Urlop na żądanie';
    if ($t == 7) $txt = 'Urlop tacierzyński';
    if ($t == 8) $txt = 'Do odpracowania';
    if ($t == 9) $txt = 'Urlop bezpłatny';
    if ($t == 10) $txt = 'Badania lekarskie';
    if ($t == 11) $txt = 'Szkolenie';
    if ($t == 12) $txt = 'Urlop siła wyższa';
    
    return $txt;
}

function getExist($dat, $worker_id)
{
    global $con;
    $query = "SELECT
            	a.id,
            	a.date_from,
            	a.date_to
            FROM
            	lubudubu_holidays_applications a
            WHERE
            	a.worker_id = '$worker_id'
            AND (
            	a.master_accept = '0'
            	OR a.master_accept = '1'
            )
            AND (
            	'$dat' BETWEEN a.date_from
            	AND a.date_to
            )
            AND
            a.act = '1'";
    $result = mysqli_query($con, $query);
    $counter = mysqli_num_rows($result);
    
    return $counter;

}


    $htype = $_GET['htype'];
    $date_from = $_GET['date_from'];
    $date_to = $_GET['date_to'];
    $worker_id = $_SESSION['worker_id'];
    $datetime = date('Y-m-d H:i:s');
    $counter = $_GET['counter'];
    
    $txt_type = typeofholiday($htype);
    
    
    $date1=date_create($date_from);
    $date2=date_create($date_to);
    $diff=date_diff($date1,$date2);
    
    $days_counter = $diff->format("%a");
    $days_counter = $days_counter+1;
    
    $Date = getDatesFromRange($date_from, $date_to); 
  
    $day_minus = 0;
    $exist_all = 0;
    foreach ($Date as &$value) 
    {
        $free =  getFreeDays($value);
        $sunsat = getSunSat($value);
        $exist = getExist($value, $worker_id);
        
        $exist_all = $exist_all + $exist;
        
        if ( ($free == '1') or ($sunsat == '7') or ($sunsat == '6') )
        {
           $day_minus++; 
        }
        
    }

    
    if ($htype == 7)
    {
        $OnlyWeeksDays = $days_counter;
    }
    else
    {
        $OnlyWeeksDays = $days_counter - $day_minus;
    }
    
    if ($OnlyWeeksDays>$counter)
    {
        echo '0001x1';
        die;
    }
    /*
    if ($exist_all>0)
    {
        echo '0002x1';
        die;
    }
    */
    
    $query = "INSERT INTO lubudubu_holidays_applications SET datetime_add = '$datetime', worker_id = '$worker_id', date_from = '$date_from', date_to = '$date_to', htype = '$htype', workdays = '$OnlyWeeksDays', alldays = '$days_counter', master_accept = '0', worker_accept = '0', act = '1'";
    echo $query;
    die;
    mysqli_query($con, $query);
    
    $res = mysqli_insert_id($con);
        
    $description = 'Wypisano wniosek urlopowy '.$date_from.' - '.$date_to;
    $query2 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '1', act = '1', first_show = '1', type = '3', vin = '$res'";
    $query3 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '189', act = '1', first_show = '1', type = '3', vin = '$res'";
    $query4 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '21', act = '1', first_show = '1', type = '3', vin = '$res'";
    
    mysqli_query($con, $query2);
    mysqli_query($con, $query3);
    mysqli_query($con, $query4);
    
    $message = 'Twój wniosek urlopowy został wysłany. Początek urlopu: '.$date_from.' Koniec urlopu: '.$date_to.' Typ urlopu: '.$txt_type.'. Oczekuj na wiadomość zwrotną z potwierdzeniem akceptacji wniosku przez Dyrektora.';
    $query4 = "INSERT INTO lubudubu_messages SET worker_id_sender = '0', worker_id_receiver = '$worker_id', message = '$message', confirm_read = '1', datetime_send = '$datetime', datetime_read= '$datetime'";
    mysqli_query($con, $query4);
    
}

//oznaczanie wiadomości jako przeczytanej
if ($type=='6')
{
    $mid = $_GET['mid'];
    $datetime = date('Y-m-d H:i:s');
    
    $query = "UPDATE lubudubu_messages SET confirm_read = '1', datetime_read = '$datetime' WHERE id = '$mid'";
    mysqli_query($con, $query);
    
}
//wycofanie wniosku niezaakceptowanego
if ($type=='7')
{
    $aid = $_GET['aid'];
    $worker_id = $_SESSION['worker_id'];
    $datetime = date('Y-m-d H:i:s');
    
    $query = "UPDATE lubudubu_holidays_applications SET act = '0' WHERE id = '$aid'";
    mysqli_query($con, $query);
    
    $description = 'Anulowano wniosek urlopowy [niepotwierdzony]';
    $query2 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '1', act = '1', first_show = '1', type = '3'";
    $query3 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '189', act = '1', first_show = '1', type = '3'";
    $query3 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '21', act = '1', first_show = '1', type = '3'";
    
    mysqli_query($con, $query2);
    mysqli_query($con, $query3);
    mysqli_query($con, $query4);
    
    $message = 'Anuluwano swój wniosek urlopowy.';
    $query4 = "INSERT INTO lubudubu_messages SET worker_id_sender = '0', worker_id_receiver = '$worker_id', message = '$message', confirm_read = '1', datetime_send = '$datetime', datetime_read = '$datetime'";
    mysqli_query($con, $query4);
    
}

if ($type=='8')
{
    $worker_id = $_SESSION['worker_id'];
    $datetime = date('Y-m-d H:i:s');
    
    $amount_0 = $_GET['amount_0'];
    $amount_1 = $_GET['amount_1'];
    $amount_2 = $_GET['amount_2'];
    $amount_3 = $_GET['amount_3'];
    $amount_4 = $_GET['amount_4'];
    
    $query = "INSERT INTO lubudubu_clothes SET worker_id = '$worker_id', amount_0 = '$amount_0', amount_1 = '$amount_1', amount_2 = '$amount_2', amount_3 = '$amount_3', datetime_add = '$datetime', act = '1', amount_4 = '$amount_4'";
    mysqli_query($con, $query);
    
    $message = 'Wysłano zgłoszenie braków ubrań w ilości sztuk: koszule: '.$amount_0.', spodnie: '.$amount_1.', bluzy: '.$amount_2.', koszulki: '.$amount_3.', buty: '.$amount_4.'.';
    $query2 = "INSERT INTO lubudubu_messages SET worker_id_sender = '0', worker_id_receiver = '$worker_id', message = '$message', confirm_read = '1', datetime_send = '$datetime', datetime_read = '$datetime'";
    mysqli_query($con, $query2);
    
    $description = 'Wysłano zgłoszenie braków ubioru';
    $query3 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '1', act = '1', first_show = '1', type = '3'";
    $query4 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '189', act = '1', first_show = '1', type = '3'";
    
    mysqli_query($con, $query3);
    mysqli_query($con, $query4);
    
    
}
if ($type=='9')
{
    //wszystkie daty z zakresu
function getDatesFromRange($start, $end, $format = 'Y-m-d') 
{ 
    $array = array(); 
    $interval = new DateInterval('P1D'); 
    $realEnd = new DateTime($end); 
    $realEnd->add($interval); 
  
    $period = new DatePeriod(new DateTime($start), $interval, $realEnd); 
    foreach($period as $date) {                  
        $array[] = $date->format($format);  
    } 
    
    return $array; 
} 

function getFreeDays($dat)
{
    global $con;
    $query = "SELECT a.id FROM graphic_holidays a WHERE a.date = '$dat'";
    $result = mysqli_query($con, $query);
    $rowcount = mysqli_num_rows($result);
    
    return $rowcount;
}

function getSunSat($dat)
{
    $day_of_week = date('N', strtotime($dat));
    
    return $day_of_week;
}

function typeofholiday($t)
{
    if ($t == 1) $txt = 'L4';
    if ($t == 2) $txt = 'Urlop wypoczynkowy długi';
    if ($t == 3) $txt = 'Urlop opiekuńczy';
    if ($t == 4) $txt = 'Urlop wypoczynkowy';
    if ($t == 5) $txt = 'Urlop okolicznościowy';
    if ($t == 6) $txt = 'Urlop na żądanie';
    if ($t == 7) $txt = 'Urlop tacierzyński';
    if ($t == 8) $txt = 'Do odpracowania';
    if ($t == 9) $txt = 'Urlop bezpłatny';
    if ($t == 10) $txt = 'Badania lekarskie';
    if ($t == 11) $txt = 'Szkolenie';
    
    return $txt;
}


    $htype = $_GET['htype'];
    $date_from = $_GET['date_from'];
    $date_to = $_GET['date_to'];
    $worker_id = $_SESSION['worker_id'];
    $datetime = date('Y-m-d H:i:s');
    $counter = $_GET['counter'];
    
    $txt_type = typeofholiday($htype);
    
    
    $date1=date_create($date_from);
    $date2=date_create($date_to);
    $diff=date_diff($date1,$date2);
    
    $days_counter = $diff->format("%a");
    $days_counter = $days_counter+1;

    $Date = getDatesFromRange($date_from, $date_to); 
  
    $day_minus = 0;
    foreach ($Date as &$value) 
    {
        $free =  getFreeDays($value);
        $sunsat = getSunSat($value);
        
        if ( ($free == '1') or ($sunsat == '7') or ($sunsat == '6') )
        {
           $day_minus++; 
        }
        
    }
    
    if ($htype == 7)
    {
        $OnlyWeeksDays = $days_counter;
    }
    else
    {
        $OnlyWeeksDays = $days_counter - $day_minus;
    }
    
    if ($OnlyWeeksDays>$counter)
    {
        echo '0001x1';
    }
    else
    {
        echo $OnlyWeeksDays;
    }
}

if ($type=='10')
{
    $register_date = $_GET['register_date'];
    $worker_id = $_SESSION['worker_id'];
    $datetime = date('Y-m-d H:i:s');
    $new_status = $_GET['new_status'];
    
    /**
     * jeśli nowy wpis to insert
     */
     
     if ($new_status == 1)
     {
        $query = "INSERT INTO lubudubu_saturday_workers SET worker_id = '$worker_id', status = '1', datetime_add = '$datetime', date_saturday = '$register_date'";
        mysqli_query($con, $query);
     }
     if ($new_status == 0)
     {
        $query = "UPDATE lubudubu_saturday_workers SET status = 1, datetime_add = '$datetime' WHERE worker_id = '$worker_id' and date_saturday = '$register_date'";
        mysqli_query($con, $query);
     }
     
     /**
      * logi
      */
      
    $message = 'Wykonano zapis na sobotę';
    $query2 = "INSERT INTO lubudubu_messages SET worker_id_sender = '0', worker_id_receiver = '$worker_id', message = '$message', confirm_read = '1', datetime_send = '$datetime', datetime_read = '$datetime'";
    mysqli_query($con, $query2);
    
    $description = 'Zapisano na sobotę';
    $query3 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '1', act = '1', first_show = '1', type = '10'";
    $query4 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '189', act = '1', first_show = '1', type = '10'";
    
    mysqli_query($con, $query3);
    mysqli_query($con, $query4);
    
    
    /**
     * sprawdzam czy jest wpis w saturday_master, jak nie ma, to dodaję
     */
     
     $query = "SELECT a.id FROM lubudubu_saturday_master a WHERE a.date_saturday = '$register_date'";
     $result = mysqli_query($con, $query);
     $rowcount = mysqli_num_rows($result);

     if ($rowcount == 0)
     {
        $query = "INSERT INTO lubudubu_saturday_master SET date_saturday = '$register_date', decision = -1";
        mysqli_query($con, $query);
     }
     
    
}

if ($type=='11')
{
    $register_date = $_GET['register_date'];
    $worker_id = $_SESSION['worker_id'];
    $datetime = date('Y-m-d H:i:s');
    
    $query = "UPDATE lubudubu_saturday_workers SET status = 0, datetime_add = '$datetime' WHERE worker_id = '$worker_id' and date_saturday = '$register_date'";
    mysqli_query($con, $query);
    
    /**
      * logi
      */
      
    $message = 'Zrezygnowano z zapisu na sobotę';
    $query2 = "INSERT INTO lubudubu_messages SET worker_id_sender = '0', worker_id_receiver = '$worker_id', message = '$message', confirm_read = '1', datetime_send = '$datetime', datetime_read = '$datetime'";
    mysqli_query($con, $query2);
    
    $description = 'Rezygnacja z zapisu na sobotę';
    $query3 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '1', act = '1', first_show = '1', type = '3'";
    $query4 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '189', act = '1', first_show = '1', type = '3'";
    
    mysqli_query($con, $query3);
    mysqli_query($con, $query4);
    
}

if ($type=='12')
{
    $cid = $_GET['cid'];
    $worker_id = $_GET['wid'];
    $datetime = date('Y-m-d H:i:s');
    
    $query = "UPDATE lubudubu_clothes SET act = '0', datetime_confirm = '$datetime' WHERE id = '$cid'";
    mysqli_query($con, $query);
    
    $message = 'Twoje zgłoszenie braków ubioru zostało oznaczone jako załatwione.';
    $query2 = "INSERT INTO lubudubu_messages SET worker_id_sender = '$worker_id', worker_id_receiver = '$worker_id', message = '$message', confirm_read = '0', datetime_send = '$datetime'";
    mysqli_query($con, $query2);
}

if ($type=='13')
{
    $worker_id = $_SESSION['worker_id'];
    $datetime = date('Y-m-d H:i:s');
    
    $amount_0 = $_GET['amount_0'];
    $amount_1 = $_GET['amount_1'];
    $amount_2 = $_GET['amount_2'];
    $amount_3 = $_GET['amount_3'];
    $amount_4 = $_GET['amount_4'];
    
    $query = "INSERT INTO lubudubu_clothes_repair SET worker_id = '$worker_id', amount_0 = '$amount_0', amount_1 = '$amount_1', amount_2 = '$amount_2', amount_3 = '$amount_3', datetime_add = '$datetime', act = '1', amount_4 = '$amount_4'";
    mysqli_query($con, $query);
    
    $message = 'Wysłano zgłoszenie naprawy ubrań w ilości sztuk: koszule: '.$amount_0.', spodnie: '.$amount_1.', bluzy: '.$amount_2.', koszulki: '.$amount_3.', buty: '.$amount_4.'.';
    $query2 = "INSERT INTO lubudubu_messages SET worker_id_sender = '0', worker_id_receiver = '$worker_id', message = '$message', confirm_read = '1', datetime_send = '$datetime', datetime_read = '$datetime'";
    mysqli_query($con, $query2);
    
    $description = 'Wysłano zgłoszenie naprawy ubioru';
    $query3 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '1', act = '1', first_show = '1', type = '3'";
    $query4 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '189', act = '1', first_show = '1', type = '3'";
    
    mysqli_query($con, $query3);
    mysqli_query($con, $query4);
    
    
}

if ($type=='14')
{
    $cid = $_GET['cid'];
    $worker_id = $_GET['wid'];
    $datetime = date('Y-m-d H:i:s');
    
    $query = "UPDATE lubudubu_clothes_repair SET act = '0', datetime_confirm = '$datetime' WHERE id = '$cid'";
    mysqli_query($con, $query);
    
    $message = 'Twoje zgłoszenie naprawy ubioru zostało oznaczone jako załatwione.';
    $query2 = "INSERT INTO lubudubu_messages SET worker_id_sender = '$worker_id', worker_id_receiver = '$worker_id', message = '$message', confirm_read = '0', datetime_send = '$datetime'";
    mysqli_query($con, $query2);
}
if ($type=='15')
{
    $id = $_GET['id'];
    $state = $_GET['state'];
    
    $worker_id = $_GET['wid'];
    $datetime = date('Y-m-d H:i:s');
    
    if ($state == '2')
    {
        $query = "UPDATE 
                    lubudubu_equipment
                SET
                    act_state = '1',
                    datetime_accept_add = '$datetime' 
                WHERE
                    id = '$id'";
    }
    if ($state == '3')
    {
        $query = "UPDATE 
                    lubudubu_equipment
                SET
                    act_state = '0',
                    datetime_accept_reject = '$datetime' 
                WHERE
                    id = '$id'";
    }
    
    mysqli_query($con, $query);
}

if ($type=='16')
{
    $file_id = $_GET['id'];
    $worker_id = $_SESSION['worker_id'];
    $datetime = date('Y-m-d H:i:s');
    
    $query = "INSERT INTO
                bhp_occupational_risk_sign
            SET
                bhp_occupational_risk_files_id = '$file_id',
                worker_id_sign = '$worker_id',
                datetime_sign = '$datetime'";
                
    mysqli_query($con, $query);
}
if ($type=='17')
{
    $eid = $_GET['eid'];
    $datetime = date('Y-m-d H:i:s');
    $worker_id = $_SESSION['worker_id'];
    
    $query = "UPDATE 
                bhp_work_clothes_equipment
            SET
                clothes_status = '1'
            WHERE
                id = '$eid'";
    mysqli_query($con, $query);
    
    $query2 = "INSERT INTO
                    bhp_work_clothes_repair
                SET
                    bhp_work_clothes_equipment_id = '$eid',
                    event_datetime = '$datetime',
                    act = '1'";
    mysqli_query($con, $query2);
    
    
    $message = 'Wysłano zgłoszenie naprawy ubrań.';
    $query2 = "INSERT INTO lubudubu_messages SET worker_id_sender = '0', worker_id_receiver = '$worker_id', message = '$message', confirm_read = '1', datetime_send = '$datetime', datetime_read = '$datetime'";
    mysqli_query($con, $query2);
    
    $description = 'Wysłano zgłoszenie naprawy ubioru';
    $query3 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '1', act = '1', first_show = '1', type = '3'";
    $query4 = "INSERT INTO sofp_alarms SET description = '$description', datetime = '$datetime', worker_id = '$worker_id', recipient_id = '189', act = '1', first_show = '1', type = '3'";
    
    mysqli_query($con, $query3);
    mysqli_query($con, $query4);
}
if ($type == '18') 
{
    $sid = $_GET['sid'];
    $datetime = date('Y-m-d H:i:s');
    
    $query = "UPDATE 
                lubudubu_trainings_workers
            SET
                confirmed = '1',
                datetime_confirmed = '$datetime'
            WHERE
                id = '$sid'";
    mysqli_query($con, $query);
}
if ($type == '19') {
    $date_from = $_GET['date_from'];
    $date_to = $_GET['date_to'];
    
    $date1 = new DateTime($date_from);
    $date2 = new DateTime($date_to);
    
    $interval = $date1->diff($date2);
    
    $days_counter = $interval->days + 1; // Dodajemy 1, aby uwzględnić dzień początkowy
    
    /**
     * NOWA FUNKCJONALNOŚĆ - SPRAWDZANIE CZY WYPOCZYNKOWY DŁUGI - TJ. min. 14 DNI KALENDARZOWYCH
     */
    $long_add = 0;
    
    $ds = $date1->format('w');
    $de = $date2->format('w');

    if ($ds == 1) $long_add += 2;
    if ($ds == 0) $long_add += 1;
    
    if ($de == 5) $long_add += 2;
    if ($de == 6) $long_add += 1;
    
    $long_counter = $days_counter + $long_add;
   
    echo $long_counter;
}
?>