<?php
include_once('controllers/helper_functions.php');

$child_id = $_GET['id'];
$sql = "SELECT * FROM children_details LEFT JOIN rate_list ON children_details.child_id = rate_list.child_id WHERE children_details.child_id = '$child_id' AND rate_list.child_id = '$child_id'";
$result = mysqli_query($conn, $sql);
// if result is zero, then no child with that id exists
if (mysqli_num_rows($result) == 0) {
    echo '<div class="nothing-found"><h1 class="emoji">🤷</h1><h2>No Child Found!</h2></div>';
    die;
}

$row = mysqli_fetch_assoc($result);
$child_name = validate_xxs($row['child_name']);
$father_name = validate_xxs($row['father_name']);
$father_email = validate_xxs($row['father_email']);
$father_phone_one = validate_xxs($row['father_phone_1']);
$father_phone_two = validate_xxs($row['father_phone_2']);
$mother_name = validate_xxs($row['mother_name']);
$mother_email = validate_xxs($row['mother_email']);
$mother_phone_one = validate_xxs($row['mother_phone_1']);
$mother_phone_two = validate_xxs($row['mother_phone_2']);
$street_address = validate_xxs($row['street_address']);
$city = validate_xxs($row['city']);
$zipcode = validate_xxs($row['zipcode']);
$state = validate_xxs($row['state']);
$sign_up_option = validate_xxs($row['sign_up_option_id']);
$sql_signup = "SELECT * FROM signup_types WHERE id = '$sign_up_option'";
$result_signup = mysqli_query($conn, $sql_signup);
$row_signup = mysqli_fetch_assoc($result_signup);
$sign_up_type = validate_xxs($row_signup['option_name']);
$weekly_rate = validate_xxs($row['weekly_charges']);
$daily_rate = validate_xxs($row['daily_rate']);
$hourly_rate = validate_xxs($row['hourly_rate']);
$comments = validate_xxs($row['misc_charges']);
$sql_charges = "SELECT * FROM charges WHERE child_id = '$child_id'";
$result_charges = mysqli_query($conn, $sql_charges);
$row_charges = mysqli_fetch_assoc($result_charges);
$extra_hours = $row_charges['extra_hours'];
$extra_days = $row_charges['extra_days'];
$days_attended =  $row_charges['days_attended'];

?>
<div class="add-form bootstrap-wrapper">
    <form class="add-form-section container" id="edit_child_form">
        <div class="form-group-c row">
            <div class="col-md-6">
                <label for="child-name">Child's Name</label>
                <input type="text" class="form-control" id="child-name" name="child-name" placeholder="Enter Child's Name" value="<?php if (!empty($child_name)) {
                                                                                                                                        echo $child_name;
                                                                                                                                    } ?>">
                <input type="hidden" class="form-control" id="child-id" name="child-id" value="<?php if (!empty($_GET['id'])) {
                                                                                                    echo $_GET['id'];
                                                                                                } ?>">
            </div>
            <div class="col-md-6">
                <span class="sign_up_type"><?php if (!empty($sign_up_type)) {
                                                echo $sign_up_type;
                                            } ?></span>
                <input type="hidden" class="form-control" id="sign-up-type" name="sign-up-option" value="<?php if (!empty($sign_up_option)) {
                                                                                                                echo $sign_up_option;
                                                                                                            } ?>">
            </div>
        </div>
        <div class="form-group-c row">
            <div class="col-md-6">
                <label for="father-name">Father's Name</label>
                <input type="text" class="form-control" name="father-name" placeholder="Enter Father's Name" value="<?php if (!empty($father_name)) {
                                                                                                                        echo $father_name;
                                                                                                                    } ?>">
            </div>
            <div class="col-md-6">
                <label for="father-email">Father's Email</label>
                <input type="text" class="form-control" name="father-email" placeholder="Enter Father's Name" value="<?php if (!empty($father_email)) {
                                                                                                                            echo $father_email;
                                                                                                                        } ?>">
            </div>
            <div class="col-md-6">
                <label for="father-phone-one">Father's Phone</label>
                <input type="text" class="form-control" name="father-phone-one" placeholder="Enter Father's Name" value="<?php if (!empty($father_phone_one)) {
                                                                                                                                echo $father_phone_one;
                                                                                                                            } ?>">
            </div>
            <div class="col-md-6">
                <label for="father-phone-two">Father's Phone</label>
                <input type="text" class="form-control" name="father-phone-two" placeholder="Enter Father's Name" value="<?php if (!empty($father_phone_two)) {
                                                                                                                                echo $father_phone_two;
                                                                                                                            } ?>">
            </div>
        </div>
        <div class="form-group-c row">
            <div class="col-md-6">
                <label for="father-name">Mother's Name</label>
                <input type="text" class="form-control" name="mother-name" placeholder="Enter Father's Name" value="<?php if (!empty($mother_name)) {
                                                                                                                        echo $mother_name;
                                                                                                                    } ?>">
            </div>
            <div class="col-md-6">
                <label for="father-name">Mother's Email</label>
                <input type="text" class="form-control" name="mother-email" placeholder="Enter Father's Name" value="<?php if (!empty($mother_email)) {
                                                                                                                            echo $mother_email;
                                                                                                                        } ?>">
            </div>
            <div class="col-md-6">
                <label for="mother-phone-one">Mother's Phone</label>
                <input type="text" class="form-control" name="mother-phone-one" placeholder="Enter Father's Name" value="<?php if (!empty($mother_phone_one)) {
                                                                                                                                echo $mother_phone_one;
                                                                                                                            } ?>">
            </div>
            <div class="col-md-6">
                <label for="mother-phone-two">Mother's Phone</label>
                <input type="text" class="form-control" name="mother-phone-two" placeholder="Enter Father's Name" value="<?php if (!empty($mother_phone_two)) {
                                                                                                                                echo $mother_phone_two;
                                                                                                                            } ?>">
            </div>
        </div>

        <div class="form-group-c row">
            <div class="col-md-6">
                <label for="street-address">Street Address</label>
                <input type="text" class="form-control" name="street-address" placeholder="Enter Street Address" value="<?php if (!empty($street_address)) {
                                                                                                                            echo $street_address;
                                                                                                                        } ?>">
            </div>
            <div class="col-md-6">
                <label for="city">City</label>
                <input type="text" class="form-control" name="city" placeholder="Enter City" value="<?php if (!empty($city)) {
                                                                                                        echo $city;
                                                                                                    } ?>">
            </div>
            <div class="col-md-6">
                <label for="zipcode">Zipcode</label>
                <input type="text" class="form-control" name="zipcode" placeholder="Enter Zipcode" value="<?php if (!empty($zipcode)) {
                                                                                                                echo $zipcode;
                                                                                                            } ?>">
            </div>
            <div class="col-md-6">
                <label for="state">State</label>
                <input type="text" class="form-control" name="state" placeholder="Enter State" value="<?php if (!empty($state)) {
                                                                                                            echo $state;
                                                                                                        } ?>">
            </div>
        </div>

        <div class="form-group-c row">
            <?php
            if ($sign_up_type == 'Drop-In') {
            ?>
                <div class="col-md-6">
                    <h3>Days Attended</h3>
                    <label for="days_attended">Day</label>
                    <input type="text" id="days_attended">
                </div>
                <div class="col-md-3">
                    <h3>&nbsp;</h3>
                    <label for="daily_rate">Daily Rate</label>
                    <input type="text" class="form-control" id="daily_rate" name="daily_rate" placeholder="Enter Rate">
                </div>
                <div class="col-md-3">
                    <h3>&nbsp;</h3>
                    <label for="">&nbsp;</label>
                    <button onclick="addDays()" class="extra-btn">Add Days</button>
                </div>

                <div class="col-md-12">
                    <div id="days_attended_div" class="dates_wrapper empty-dates">
                    </div>
                    <input type="hidden" name="days_attended_json" id="days_attended_json" value='<?php echo $days_attended ?>'>
                </div>
            <?php
            } else {
            ?>
                <div class="col-md-6">
                    <h3>Extra Days</h3>
                    <label for="extra_days">Day</label>
                    <input type="text" id="extra_days">
                </div>
                <div class="col-md-3">
                    <h3>&nbsp;</h3>
                    <label for="daily_rate">Daily Rate</label>
                    <input type="text" class="form-control" id="daily_rate" name="daily_rate" placeholder="Enter Rate">
                </div>
                <div class="col-md-3">
                    <h3>&nbsp;</h3>
                    <label for="daily_rate">&nbsp;</label>
                    <button onclick="addDays()" class="extra-btn">Add Days</button>
                </div>

                <div class="col-md-12">
                    <div id="extra_days_div" class="dates_wrapper empty-dates">
                    </div>
                    <input type="hidden" name="extra_days_json" id="extra_days_json" value='<?php echo $extra_days ?>'>
                </div>
            <?php
            }
            ?>
            <div class="col-md-3">
                <h3>Extra Hours</h3>
                <label for="extra_days">Date</label>
                <input type="text" id="extra_hours_date">
            </div>
            <div class="col-md-3">
                <h3>&nbsp;</h3>
                <label for="extra_hours">Hours</label>
                <input type="text" id="extra_hours">
            </div>
            <div class="col-md-3">
                <h3>&nbsp;</h3>
                <label for="hourly_rate">Hourly Rate</label>
                <input type="text" id="hourly_rate" class="form-control" name="hourly_rate" placeholder="Enter Rate">
            </div>
            <div class="col-md-3">
                <h3>&nbsp;</h3>
                <label for="">&nbsp;</label>
                <button onclick="addHours()" class="extra-btn">Add Hours</button>
            </div>
            <div class="col-md-12">
                <div id="extra_hours_div" class="hours_wrapper empty-dates">
                </div>
                <input type="hidden" name="extra_hours_json" id="extra_hours_json" value='<?php echo $extra_hours ?>'>
            </div>
        </div>
        <?php
        if ($sign_up_type != 'Drop-In') {
        ?>
            <div class="form-group-c regular-options row">
                <div class="col-md-3">
                    <label for="weekly-rate">Weekly Rate</label>
                    <input type="text" class="form-control" id="weekly-rate" name="weekly-rate" placeholder="Enter Weekly Rate" value="<?php if (!empty($weekly_rate) || $weekly_rate == 0) {
                                                                                                                                            echo $weekly_rate;
                                                                                                                                        } ?>">
                </div>
            </div>
        <?php
        }
        ?>
        <div class="form-group-c drop-in-options row">
            <div class="col-md-3">
                <label for="daily-rate">Daily Rate</label>
                <input type="text" class="form-control" name="daily-rate" placeholder="Enter Value" value="<?php if (!empty($daily_rate) || $daily_rate == 0) {
                                                                                                                echo $daily_rate;
                                                                                                            } ?>">
            </div>
            <div class="col-md-3">
                <label for="hourly-rate">Hourly Rate</label>
                <input type="text" class="form-control" name="hourly-rate" placeholder="Enter Value" value="<?php if (!empty($hourly_rate) || $hourly_rate == 0) {
                                                                                                                echo $hourly_rate;
                                                                                                            } ?>">
            </div>
        </div>
        <div class="form-group-c row">
            <div class="col-md-12  d-flex flex-column justify-content-between">

                <label for="comments">Misc Charges/Extra Charges</label>
                <textarea id=" comments" name="comments" rows="5" placeholder="Enter Comments"><?php if (!empty($comments)) {
                                                                                                    echo $comments;
                                                                                                } ?></textarea>

            </div>
        </div>
        <div class="form-group-c">
            <input type="submit" id="edit_child_btn" class="extra-btn" value="Update">
    </form>
</div>