<?php 
session_start();
include("dbConfig.php");
$customleave_from = $_POST["customleave_from"];
$customleave_to = $_POST["customleave_to"];
$company_id_sel = $_POST["company_id_sel"];
$lang = $_POST["lang"];
$company_id = $_POST["company_id"];
$messag_add = $_POST["messag_add"];
$shift_id = $_POST["shift_id"];

$shift_start_date = $_POST["shift_start_date"];
$shift_end_date = $_POST["shift_end_date"];


$add_user_id = $_POST["add_user_id"];
date_default_timezone_set("Asia/Riyadh");
$created_date_time = date("Y-m-d h:i:sa");

function GetSeriaother() {	
    $strings = '123456789';	
    $stringLength = strlen($strings);
    $newStrings = '';
    for ($i = 0; $i < 6; $i++) {
        $newStrings .= $strings[rand(0, $stringLength - 1)];
    }
    return $newStrings;
}
$j = 0;
if(empty($customleave_to)){
    $j++;
    $sql_em="SELECT * FROM hr_waseet_employ_shift_tbl where system_id='2222' AND company_id='$company_id_sel' and shift_id='$shift_id' and waseet_stat='0' and deleted='0' and stop='0'";
    $result_em = $conn->query($sql_em);
    if ($result_em->num_rows > 0) {
    while($row_em = $result_em->fetch_assoc()) {
        $sql = "UPDATE hr_waseet_employ_shift_tbl SET waseet_stat='1' where system_id='2222' and company_id='$company_id_sel' and shift_id='$shift_id' ";
        if ($conn->query($sql) === TRUE ) {
    
        }else{
    
        }
    }}
}else{
   
$ids = join("','",$customleave_to); 
$sql_em="SELECT * FROM hr_waseet_employ_shift_tbl where employ_id not in ('$ids') and system_id='2222' AND company_id='$company_id_sel' and shift_id='$shift_id' and waseet_stat='0' and deleted='0' and stop='0'";
$result_em = $conn->query($sql_em);
if ($result_em->num_rows > 0) {
while($row_em = $result_em->fetch_assoc()) {
    $sql = "UPDATE hr_waseet_employ_shift_tbl SET waseet_stat='1' where system_id='2222' and company_id='$company_id_sel' and shift_id='$shift_id' ";
    if ($conn->query($sql) === TRUE ) {
$j++;
    }else{

    }
}}

for($i=0;$i<count($customleave_to);$i++){
    $sql_em="SELECT * FROM hr_waseet_employ_shift_tbl where system_id='2222' AND company_id='$company_id_sel' and shift_id='$shift_id' and employ_id='".$_POST["customleave_to"][$i]."' and deleted='0' and stop='0'";
    $result_em = $conn->query($sql_em);
    if ($result_em->num_rows > 0) {
        $sql = "UPDATE hr_waseet_employ_shift_tbl SET waseet_stat='0' where system_id='2222' and company_id='$company_id_sel' and shift_id='$shift_id' and employ_id='".$_POST["customleave_to"][$i]."' ";
        if ($conn->query($sql) === TRUE ) {
    $j++;
        }else{
    
        }
    }else{
        $sql_insert = "INSERT INTO hr_waseet_employ_shift_tbl (waseet_id,system_id,company_id,employ_id,shift_id,createdDate_time,add_user_id)
        VALUES ('".GetSeriaother()."','2222','$company_id_sel','".$_POST["customleave_to"][$i]."','$shift_id','$created_date_time','$add_user_id')";
        
        if ($conn->query($sql_insert) === TRUE ) {
            $j++;
        }else{
    
        }
    }
}
}

if($j>0){
    $_SESSION['message'] =$messag_add;
    header("Refresh:0; url=hr_shiftlist_connect.php?lang=$lang&company_id=$company_id");
}else{

}
?>