<?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;
$child_id = $_POST['child-id'];
$sign_up_option = validate_sql($_POST['sign-up-option']);
$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']);
$extra_hours = validate_sql($_POST['extra_hours_json']);


if($sign_up_option == 1)
{
    $days_attended = validate_sql($_POST['days_attended_json']);
    $extra_days = '[]';
    $weekly_rate = '-';

}
else
{
    if ($sign_up_option > 1) 
    {
        $extra_days = validate_sql($_POST['extra_days_json']);
        $days_attended = '[]';
        $weekly_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) 
{
    mysqli_query($conn, "UPDATE children_details SET child_name = '$child_name', father_name = '$father_name', father_email = '$father_email', father_phone_1 = '$father_phone_one', father_phone_2 = '$father_phone_two', mother_name ='$mother_name', mother_email ='$mother_email', mother_phone_1 = '$mother_phone_one', mother_phone_2 = '$mother_phone_two', street_address ='$street_address', city = '$city', zipcode = '$zipcode', state = '$state';");
    mysqli_query($conn, "UPDATE rate_list SET weekly_charges = '$weekly_rate', daily_rate = '$daily_rate', hourly_rate = '$hourly_rate', misc_charges = '$comment' WHERE child_id = '$child_id';");
    mysqli_query($conn, "UPDATE charges SET extra_days  = '$extra_days', extra_hours = '$extra_hours', days_attended = '$days_attended'  WHERE child_id = '$child_id';");

    if (!mysqli_commit($conn)) {
        $resp['msg'] = 'Failed to add child details';
        $resp['success'] = false;
    }
    else
    {
        $resp['success'] = true;
        $resp['msg'] = 'Child added successfully';
    }
    echo json_encode($resp);
}

?>