<?php
include "classes/class.loader.php";
$html = new html();
$html->bacground_emoji();
$calls = new functions();
$db = new db();
$user_data_1 = $db->query("SELECT * FROM users WHERE record_id = {$_SESSION['user_id']}");
$user_data = $user_data_1->fetch_assoc();
$user_extended_data_1 = $db->query("SELECT * FROM user_data_1 WHERE user_id = {$_SESSION['user_id']}");
$user_extended_data = $user_extended_data_1->fetch_assoc();
?>
<style>
    body {
        background-color: #bebebee7;
        color: white;
    }
</style>
<div class="header">
    <input type="text" id="username" name="username" value="<?php echo $_SESSION['username']; ?>" readonly />
    <button onclick="window.location.href='home.php'">HOME</button>
    <button onclick="window.location.href='profile.php'" style="border: 2px solid white">MY ACCOUNT</button>
    <button onclick="window.location.href='bookings.php'">BOOKINGS</button>
    <button onclick="window.location.href='news.php'">NEWS</button>
    <button onclick="window.location.href='help.php'">HELP</button>
</div>

<div class="home_container">

    <div class="container_wide">
        <h2>USER INFORMATION</h2>
        <div class="line"></div>
        <label>USERNAME</label>
        <input type="text" id="username" name="username" onchange="change_user_details(this, 'users')"
            value="<?php echo $user_data['username']; ?>" required />
        <label>EMAIL</label>
        <input type="text" id="email" name="email" onchange="change_user_details(this, 'users')"
            value="<?php echo $user_data['email']; ?>" required />
        <br>
        <a href="forgot_password.php">CHANGE PASSWORD?</a>
        <br>

    </div>
    <div class="container_wide">
        <h2>PERSONAL INFORMATION</h2>
        <label>FIRST NAME</label>
        <input type="text" id="first_name" name="first_name" onchange="change_user_info_1(this, 'user_data_1')"
            value="<?php echo $user_extended_data['first_name']; ?>" required />
        <label>LAST NAME</label>
        <input type="text" id="last_name" name="last_name" onchange="change_user_info_1(this, 'user_data_1')"
            value="<?php echo $user_extended_data['last_name']; ?>" required />
        <label>CELL NUMBER</label>
        <input type="text" id="cell_number" name="cell_number" onchange="change_user_info_1(this, 'user_data_1')"
            value="<?php echo $user_extended_data['cell_number']; ?>" required />
        <label>ADDRESS</label>
        <input type="text" id="home_address" onchange="change_user_info_1(this, 'user_data_1')" name="home_address"
            value="<?php echo $user_extended_data['home_address']; ?>" required />
        <script>
            function change_user_info_1(input, table_name) {
                var value = input.value;
                var column = input.id;
                var where = "user_id = <?php echo $_SESSION['user_id']; ?>";

                var xhr = new XMLHttpRequest();
                xhr.open('POST', 'ajax/database_update_inputs.php', true);
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === XMLHttpRequest.DONE) {
                        if (xhr.status === 200) {
                            console.log(xhr.responseText);
                            if (xhr.responseText === '1') {
                                input.style.border = "2px solid green";
                            } else {
                                input.style.border = "2px solid red";

                            }
                        }
                    }
                };
                xhr.send('value=' + encodeURIComponent(value) + '&column=' + encodeURIComponent(column) + '&table_name=' + encodeURIComponent(table_name) + '&where=' + encodeURIComponent(where));
            }
            function change_user_details(input, table_name) {
                var value = input.value;
                var column = input.id;
                var where = "user_id = <?php echo $_SESSION['user_id']; ?>";

                var xhr = new XMLHttpRequest();
                xhr.open('POST', 'ajax/database_update_inputs.php', true);
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === XMLHttpRequest.DONE) {
                        if (xhr.status === 200) {
                            console.log(xhr.responseText);
                            if (xhr.responseText === '1') {
                                input.style.border = "2px solid green";
                            } else {
                                input.style.border = "2px solid red";

                            }
                        }
                    }
                };
                xhr.send('value=' + encodeURIComponent(value) + '&column=' + encodeURIComponent(column) + '&table_name=' + encodeURIComponent(table_name) + '&where=' + encodeURIComponent(where));
            }
        </script>

    </div>


    <div class="container_wide" id="dogs_container">
        <h2>YOUR DOGS</h2>
        <div class="line"></div>
        <div class="data_container_for_adding">
            <?php
            $breeder_datalist_res = $db->query("SELECT * FROM breeder_info WHERE 1");
            $breed_select = "<option value='0'>SELECT BREED</option>";
            while ($breeder_datalist = $breeder_datalist_res->fetch_assoc()) {
                $breed_select .= "<option value='" . $breeder_datalist['record_id'] . "'>" . $breeder_datalist['name'] . "</option>";
            }
            $dog_res = $db->query("SELECT * FROM user_animals WHERE user_id = {$_SESSION['user_id']}");
            if ($dog_res->num_rows > 0) {
                while ($dog = $dog_res->fetch_assoc()) {
                    ?>
                    <div class="dogs_data_row" id="dog_<?php echo $dog['record_id']; ?>" style="height: 25vh;overflow: hidden">
                        <button onclick="show_hide_dogs('dog_<?php echo $dog['record_id']; ?>',this)">SHOW</button>
                        <div class="dogs_data_row_number">
                            <label id="dog_form">NAME</label>
                            <input type="text" id="name"
                                onchange="change_dog_info(this, 'user_animals', '<?php echo $dog['record_id']; ?>')" name="name"
                                value="<?php echo @$dog['name']; ?>" />
                            <label>BREED</label>
                            <input type="text" id="breed"
                                onchange="change_dog_info(this, 'user_animals', '<?php echo $dog['record_id']; ?>')"
                                name="breed" value="<?php echo @$dog['breed']; ?>" />
                            <label>BREEDER</label>
                            <select type="text" id="breeder_id" class="input"
                                onchange="change_dog_info(this, 'user_animals', '<?php echo $dog['record_id']; ?>')"
                                name="breeder_id<?php echo $dog['record_id']; ?>"><?php echo $breed_select; ?></select>
                            <script>
                                document.getElementsByName('breeder_id<?php echo $dog['record_id']; ?>')[0].value = '<?php echo $dog['breeder_id']; ?>';
                            </script>
                            <label>DATE OF BIRTH</label>
                            <input type="date" id="date_of_birth"
                                onchange="change_dog_info(this, 'user_animals', '<?php echo $dog['record_id']; ?>')"
                                name="date_of_birth" value="<?php echo @$dog['date_of_birth']; ?>" />
                            <label>AGE</label>
                            <input type="number" id="age"
                                onchange="change_dog_info(this, 'user_animals', '<?php echo $dog['record_id']; ?>')" name="age"
                                value="<?php echo @$dog['age']; ?>" />
                            <label>SEX</label>
                            <input type="text" id="sex"
                                onchange="change_dog_info(this, 'user_animals', '<?php echo $dog['record_id']; ?>')" name="sex"
                                value="<?php echo @$dog['sex']; ?>" />
                            <label>SPAY NEUTERED</label>
                            <input type="text" id="spay_neutered"
                                onchange="change_dog_info(this, 'user_animals', '<?php echo $dog['record_id']; ?>')"
                                name="spay_neutered" value="<?php echo @$dog['spay_neutered']; ?>" />
                            <label>LAST VACCINATION DATE</label>
                            <input type="date" id="last_vaccination_date"
                                onchange="change_dog_info(this, 'user_animals', '<?php echo $dog['record_id']; ?>')"
                                name="last_vaccination_date" value="<?php echo @$dog['last_vaccination_date']; ?>" />
                            <label>REGULAR VETERINARIAN</label>
                            <input type="text" id="regular_veterinarian"
                                onchange="change_dog_info(this, 'user_animals', '<?php echo $dog['record_id']; ?>')"
                                name="regular_veterinarian" value="<?php echo @$dog['regular_veterinarian']; ?>" />
                            <label>CONTACT NUMBER</label>
                            <input type="text" id="contact_number"
                                onchange="change_dog_info(this, 'user_animals', '<?php echo $dog['record_id']; ?>')"
                                name="contact_number" value="<?php echo @$dog['contact_number']; ?>" />
                            <label>INSURANCE NAME</label>
                            <input type="text" id="insurance_name"
                                onchange="change_dog_info(this, 'user_animals', '<?php echo $dog['record_id']; ?>')"
                                name="insurance_name" value="<?php echo @$dog['insurance_name']; ?>" />
                            <label>INSURANCE CONTACT</label>
                            <input type="text" id="insurance_contact"
                                onchange="change_dog_info(this, 'user_animals', '<?php echo $dog['record_id']; ?>')"
                                name="insurance_contact" value="<?php echo @$dog['insurance_contact']; ?>" />
                        </div>
                        <?php if ($dog['image_file'] == "") { ?>
                            <img class="dog_image" src="assets/dog_generic.jpg" alt="Dog Image">

                        <?php } else { ?>
                            <img class="dog_image" src="<?php echo $dog['image_file']; ?>" alt="Dog Image">
                        <?php } ?>
                    </div>
                <?php }
            } else {
                echo "<h2>NO DOGS ADDED YET</h2>";
            }
            ?>
        </div>
        <br>
        <button onclick="add_row_dogs()">ADD NEW DOG</button>
        <script>
            function show_hide_dogs(id, button) {
                var x = document.getElementById(id);
                if (x.style.height == "25vh") {
                    x.style.height = "auto";
                    button.innerHTML = "HIDE";
                } else {
                    x.style.height = "25vh";
                    button.innerHTML = "SHOW";
                }

            }
            function add_row_dogs() {
                var xhr = new XMLHttpRequest();
                xhr.open('POST', 'ajax/add_new_dog.php', true);
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === XMLHttpRequest.DONE) {
                        if (xhr.status === 200) {
                            console.log(xhr.responseText);
                            if (xhr.responseText == 1) {
                                window.location.assign("profile.php#dogs_container");
                                window.location.reload();
                            } else if (xhr.responseText == 2) {
                                alert("PLEASE FILL IN THE NEW DOGS INFORMATION");
                                document.getElementById("dog_form").focus();

                            } else {
                                alert("SOMETHING WENT WRONG");

                            }
                        }
                    }
                };
                xhr.send();
            }
            function change_dog_info(input, table_name, dog_id) {
                var value = input.value;
                var column = input.id;
                var where = "user_id = <?php echo $_SESSION['user_id']; ?> AND record_id = " + dog_id;

                var xhr = new XMLHttpRequest();
                xhr.open('POST', 'ajax/database_update_inputs.php', true);
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === XMLHttpRequest.DONE) {
                        if (xhr.status === 200) {
                            console.log(xhr.responseText);
                            if (xhr.responseText === '1') {
                                input.style.border = "2px solid green";
                            } else {
                                input.style.border = "2px solid red";

                            }
                        }
                    }
                };
                xhr.send('value=' + encodeURIComponent(value) + '&column=' + encodeURIComponent(column) + '&table_name=' + encodeURIComponent(table_name) + '&where=' + encodeURIComponent(where));
            }

        </script>



    </div>

    <div class="container_wide">
        <h2>PREVIOUS BOOKINGS</h2>
        <div class="line"></div>
        <?php
        $booking_res = $db->query("SELECT * FROM bookings WHERE user_id = " . $_SESSION['user_id'] . " ORDER BY record_id DESC");
        while ($booking = $booking_res->fetch_assoc()) {
            $dog_ids = explode(",", $booking['dog_ids']);
            for ($i = 0; $i < count($dog_ids); $i++) {
                if ($dog_ids[$i] == "") {
                    continue;
                }
                $dog_data = $db->query("SELECT * FROM user_animals WHERE record_id = " . $dog_ids[$i]);
                $dog = $dog_data->fetch_assoc();
                $dog_names[] = $dog['name'];
            }
            $dog_names_string = implode(", ", $dog_names);
            $booking['date_of_booking'] = date("Y-m-d", strtotime($booking['date_of_booking']));
            $start = $booking['time_start'];
            $end = $booking['time_end'];
            ?>
            <div class="data_container_for_adding">
                <div class="dogs_data_row">
                    <h3>DATE: <?php echo $booking['date_of_booking']; ?></h3>
                    <h3>TIME: <?php echo $start . " - " . $end; ?></h3>
                    <h3>DOGS: <?php echo $dog_names_string; ?></h3>
                    <h3>STATUS: <?php echo $booking['status']; ?></h3>
                    <h3>TYPE: <?php echo $booking['type']; ?></h3>
                </div>
            </div>
            <?php
        }
        ?>

    </div>

    <div class="container_wide">
        <h2>PREVIOUS PAYMENTS MADE</h2>
        <div class="line"></div>
        <?php
        $payment_res = $db->query("SELECT * FROM payments WHERE user_id = " . $_SESSION['user_id'] . " ORDER BY record_id DESC");
        while ($payment = $payment_res->fetch_assoc()) {
            ?>
            <div class="data_container_for_adding">
                <div class="dogs_data_row">
                    <h3>DATE: <?php echo $payment['date_time']; ?></h3>
                    <h3>AMOUNT: R <?php echo $payment['zar']; ?></h3>
                </div>
            </div>
            <?php
        }
        ?>


    </div>
</div>