<?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['updateProject'])) {
    // Retrieve and validate form data
    $projectID = validate($_POST['projectID']);
    $projectName = validate($_POST['projectName']);
    $clientID = validate($_POST['clientID']);
    $contractValue = validate($_POST['contractValue']);

    // Update project details in the database
    $updateQuery = "UPDATE projects SET projectName = '$projectName', clientID = '$clientID', contractValue = '$contractValue' WHERE projectID = '$projectID'";
    $updateResult = mysqli_query($conn, $updateQuery);

    if ($updateResult) {
        // Project details updated successfully, redirect to projects.php
        redirect('projects.php', 'Project Updated Successfully');
    } else {
        // Something went wrong with the database query
        redirect("projects-edit.php?projectID=$projectID", 'Something Went Wrong');
    }
}




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','Project 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'])) {
    // Retrieve and validate form data
    $milestoneName = validate($_POST['milestoneName']);
    $milestoneValue = validate($_POST['milestoneValue']);
    $completionDate = !empty($_POST['completionDate']) ? $_POST['completionDate'] : '0000-00-00';
    $projectID = validate($_POST['projectID']);

    // Insert milestone data into the database
    // Replace the placeholders with your actual SQL query and database logic
    $query = "INSERT INTO milestones (milestoneName, milestoneValue, completionDate, projectID) 
              VALUES ('$milestoneName', '$milestoneValue', '$completionDate', '$projectID')";

    $result = mysqli_query($conn, $query); // Execute the query using your database connection

    if ($result) {
        // Milestone added successfully, redirect to milestones-view.php
        redirect('milestones-view.php?projectID=' . $projectID, 'Milestone Added Successfully');
    } else {
        // Something went wrong with the database query
        redirect('milestones-create.php?projectID=' . $projectID, 'Something Went Wrong');
    }
}


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');
    }
}


if (isset($_POST['updateMilestone'])) {
    // Check if milestoneID and other required fields are set
    if (isset($_POST['milestoneID'], $_POST['milestoneName'], $_POST['milestoneValue'], $_POST['completionDate'])) {
        $milestoneID = $_POST['milestoneID'];
        $milestoneName = $_POST['milestoneName'];
        $milestoneValue = $_POST['milestoneValue'];
        $completionDate = !empty($_POST['completionDate']) ? $_POST['completionDate'] : '0000-00-00';

        // Fetch the projectID from the milestone
        $queryProjectID = "SELECT projectID FROM milestones WHERE milestoneID = $milestoneID";
        $resultProjectID = mysqli_query($conn, $queryProjectID);

        if (!$resultProjectID) {
            die("Query failed: " . mysqli_error($conn));
        }

        $rowProjectID = mysqli_fetch_assoc($resultProjectID);
        $projectID = $rowProjectID['projectID'];

        // Perform any necessary data validation here

        // Update the milestone in the database
        $query = "UPDATE milestones SET 
            milestoneName = '$milestoneName',
            milestoneValue = '$milestoneValue',
            completionDate = '$completionDate'
            WHERE milestoneID = $milestoneID";

        $result = mysqli_query($conn, $query);

        if ($result) {
            // Milestone updated successfully
            $_SESSION['success_message'] = "Milestone updated successfully.";
            header('Location: milestones-view.php?projectID=' . $projectID); // Redirect to milestones-view.php with projectID
            exit();
        } else {
            // Handle the case where the update query failed
            $_SESSION['error_message'] = "Error updating milestone: " . mysqli_error($conn);
            header('Location: milestones-edit.php?milestoneID=' . $milestoneID); // Redirect back to edit page with milestoneID
            exit();
        }
    } else {
        // Handle the case where required fields are missing
        $_SESSION['error_message'] = "All fields except Completion Date are required.";
        header('Location: milestones-edit.php?milestoneID=' . $_POST['milestoneID']); // Redirect back to edit page with milestoneID
        exit();
    }
} else {
    // Handle the case where the form was not submitted
}
?>