<?php
require '../config/function.php';

if(isset($_POST['saveUser']))
{
    $fname = validate($_POST['fname']);
    $lname = validate($_POST['lname']);
    $email = validate($_POST['email']);
    $password = validate($_POST['password']);
    $role = validate($_POST['role']);
    $employeeRate = validate($_POST['employeeRate']);
    $joiningDate = validate($_POST['joiningDate']);
    $budgetedRGL = validate($_POST['budgetedRGL']);
    $workingDays = validate($_POST['workingDays']);

    if($fname!=''||$lname!=''||$email!=''||$password!=''||$role!=''||$employeeRate!=''||$joiningDate!=''||$budgetedRGL!=''||$workingDays!='')
    {
        $query = "INSERT INTO users (fname,lname,email,password,role,employeeRate,joiningDate,budgetedRGL,workingDays) 
                VALUES ('$fname','$lname','$email','$password','$role','$employeeRate','$joiningDate','$budgetedRGL','$workingDays')";
        $result = mysqli_query($conn, $query);

        if($result)
        {
            redirect('users.php','User Added Successfully');
        }
        else
        {
            redirect('users-create.php','Something Went Wrong');
        }
    }
    else
    {
        redirect('users-create.php','Please fill all the input fields');
    }
}

if(isset($_POST['updateUser']))
{
    $fname = validate($_POST['fname']);
    $lname = validate($_POST['lname']);
    $email = validate($_POST['email']);
    $password = validate($_POST['password']);
    $role = validate($_POST['role']);
    $employeeRate = validate($_POST['employeeRate']);
    $joiningDate = validate($_POST['joiningDate']);
    $budgetedRGL = validate($_POST['budgetedRGL']);
    $workingDays = validate($_POST['workingDays']);
    $UID = validate($_POST['UID']);
    $users = getById('users',$UID);
    if($users['status']!=200){
        redirect('users-edit.php?userID='.$UID,'No such ID Found');
    }

    if($fname!=''||$lname!=''||$email!=''||$password!=''||$role!=''||$employeeRate!=''||$joiningDate!=''||$budgetedRGL!=''||$workingDays!='')
    {
        $query = "UPDATE users SET
                 fname='$fname',
                 lname='$lname',
                 email='$email',
                 password='$password',
                 role='$role',
                 employeeRate='$employeeRate',
                 joiningDate='$joiningDate',
                 budgetedRGL='$budgetedRGL',
                 workingDays='$workingDays'
                 WHERE userID='$UID' ";
        $result = mysqli_query($conn, $query);

        if($result)
        {
            redirect('users-edit.php?userID='.$UID,'User Updated Successfully');
        }
        else
        {
            redirect('users-create.php','Something Went Wrong');
        }
    }
    else
    {
        redirect('users-create.php','Please fill all the input fields');
    }
}





if(isset($_POST['saveClient']))
{
    $clientName = validate($_POST['clientName']);

    if($clientName!='')
    {
        $query = "INSERT INTO clients (clientName) 
                VALUES ('$clientName')";
        $result = mysqli_query($conn, $query);

        if($result)
        {
            redirect('clients.php','Client Added Successfully');
        }
        else
        {
            redirect('clients-create.php','Something Went Wrong');
        }
    }
    else
    {
        redirect('clients-create.php','Please fill all the input fields');
    }
}

if(isset($_POST['updateClient']))
{
    $clientName = validate($_POST['clientName']);
    $CID = validate($_POST['CID']);
    $clients = getBycId('clients',$CID);
    if($clients['status']!=200){
        redirect('clients-edit.php?clientID='.$CID,'No such ID Found');
    }

    if($clientName!='')
    {
        $query = "UPDATE clients SET
                 clientName='$clientName'
                 WHERE clientID='$CID' ";
        $result = mysqli_query($conn, $query);

        if($result)
        {
            redirect('clients-edit.php?clientID='.$CID,'Client Updated Successfully');
        }
        else
        {
            redirect('clients-edit.php?clientID='.$CID,'Something Went Wrong');
        }
    }
    else
    {
        redirect('clients-edit.php','Please fill all the input fields');
    }
}



if(isset($_POST['saveProject']))
{
    $projectName = validate($_POST['projectName']);
    $clientID = validate($_POST['clientID']);
    $contractValue = validate($_POST['contractValue']);

    if($projectName!=''||$clientID!=''||$contractValue!='')
    {
        $query = "INSERT INTO projects (projectName, clientID, contractValue) VALUES ('$projectName', '$clientID','$contractValue')";;
        $result = mysqli_query($conn, $query);

        if($result)
        {
            redirect('projects.php','User Added Successfully');
        }
        else
        {
            redirect('projects-create.php','Something Went Wrong');
        }
    }
    else
    {
        redirect('projects-create.php','Please fill all the input fields');
    }
}


if(isset($_POST['saveMilestone']))
{
    $milestoneName = validate($_POST['milestoneName']);
    $milestoneValue = validate($_POST['milestoneValue']);
    $startDate = validate($_POST['startDate']);
    $endDate = validate($_POST['endDate']);
    $completionDate = validate($_POST['completionDate']);

    
    

    if($milestoneName!=''||$milestoneValue!=''||$startDate!=''||$endDate!=''||$completionDate!='')
    {
        $query = "INSERT INTO milestones (milestoneName,milestoneValue,startDate,endDate,completionDate) 
                VALUES ('$milestoneName','$milestoneValue','$startDate','$endDate','$completionDate')";
        $result = mysqli_query($conn, $query);

        if($result)
        {
            redirect('milestones-view.php','Milestone Added Successfully');
        }
        else
        {
            redirect('milestones-view.php','Something Wents Wrong');
        }
    }
    else
    {
        redirect('milestones-view.php','Please fill all the input fields');
    }
}


if(isset($_POST['saveTimesheet']))
{
    $tentryDate = validate($_POST['tenrtyDate']);
    $clientID = validate($_POST['clientID']);
    $projectID = validate($_POST['projectID']);
    $milestoneID = validate($_POST['milestoneID']);
    $hrs = validate($_POST['hrs']);
    $descp = validate($_POST['descp']);

    if($tentryDate!=''||$clientID!=''||$projectID!=''||$milestoneID!=''||$hrs!=''||$descp!='')
    {
        $query = "INSERT INTO timesheet (tenrtyDate, clientID, projectID,milestoneID, hrs,descp) VALUES ('$tentryDate', '$clientID','$projectID','$milestoneID', '$hrs','$descp')";
        $result = mysqli_query($conn, $query);

        if($result)
        {
            redirect('timesheet-view.php','Timesheet Added Successfully');
        }
        else
        {
            redirect('timesheet.php','Something Went Wrong');
        }
    }
    else
    {
        redirect('timesheet.php','Please fill all the input fields');
    }
}


?>