<?php
include_once('../components/connect.php');

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: POST");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
include_once('helper_functions.php');

$resp['msg'] = '';
$resp['success'] = false;

$sign_up_type = validate_sql($_POST['sign-up-type']);
$child_name = validate_sql($_POST['child-name']);
$father_name = validate_sql($_POST['father-name']);
$father_email = validate_sql($_POST['father-email']);
$father_phone_one = validate_sql($_POST['father-phone-one']);
$father_phone_two = validate_sql($_POST['father-phone-two']);
$mother_name = validate_sql($_POST['mother-name']);
$mother_email = validate_sql($_POST['mother-email']);
$mother_phone_one = validate_sql($_POST['mother-phone-one']);
$mother_phone_two = validate_sql($_POST['mother-phone-two']);
$street_address = validate_sql($_POST['street-address']);
$city = validate_sql($_POST['city']);
$zipcode = validate_sql($_POST['zipcode']);
$state = validate_sql($_POST['state']);

if($sign_up_type == "drop-in")
{
    $sign_up_option = 1;
}
else
{
    if ($sign_up_type == "regular") 
    {
        $sign_up_option = validate_sql($_POST['sign-up-option']);
    }
}

$weeky_rate = validate_sql($_POST['weekly-rate']);
$daily_rate = validate_sql($_POST['daily-rate']);
$hourly_rate = validate_sql($_POST['hourly-rate']);
$comment = validate_sql($_POST['comments']);

$successful_validation = true;

if ($successful_validation) 
{
    $sql = "INSERT INTO children_details (child_name, father_name, father_email, father_phone_1, father_phone_2, mother_name, mother_email, mother_phone_1, mother_phone_2, street_address, city, zipcode, state, sign_up_option_id,is_invoiced, invoice_due) VALUES ('$child_name', '$father_name', '$father_email', '$father_phone_one', '$father_phone_two', '$mother_name', '$mother_email', '$mother_phone_one', '$mother_phone_two', '$street_address', '$city', '$zipcode', '$state', '$sign_up_option', 0, '0000-00-00')";
    if (mysqli_query($conn, $sql))
    {
        $last_id = mysqli_insert_id($conn);
        if($last_id)
        {
            $sql_query = "INSERT INTO rate_list (child_id, signup_id, weekly_charges, daily_rate, hourly_rate, misc_charges) VALUES ('$last_id', '$sign_up_option', '$weeky_rate', '$daily_rate', '$hourly_rate', '$comment')";
            if (mysqli_query($conn, $sql_query))
            {
                $extra_hours = '[]';
                $extra_days = '[]';
                $days_attended = '[]';
                $sql_query2 = "INSERT INTO charges (child_id, extra_hours, extra_days, days_attended) VALUES ('$last_id', '$extra_hours', '$extra_days', '$days_attended')";
                if (mysqli_query($conn, $sql_query2))
                {
                    $resp['success'] = true;
                    $resp['msg'] = 'Child added successfully';
                }
                else
                {
                    $resp['success'] = false;
                    $resp['msg'] = 'Error: ' . mysqli_error($conn);
                }
            }
            else
            {
                $resp['success'] = false;
                $resp['msg'] = 'Error: ' . mysqli_error($conn);
            }
        }
        else
        {
            $resp['msg'] = 'Failed to add child details';
            $resp['success'] = false;
        }
    }
    else
    {
        $resp['msg'] = 'Failed to add child details';
        $resp['success'] = false;
    }
    echo json_encode($resp);
}

?>