<?php
header('Access-Control-Allow-Origin: *');
header(
    'Access-Control-Allow-Methods: HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS'
);
header(
    'Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization'
);
header('Content-Type: application/json');
$shortDescription = "";
include '../../../../config/db.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = json_decode(file_get_contents('php://input'));
    if (
        !empty($data->clockId) &&
        !empty($data->clockDate) &&
        !empty($data->dayClockFrom) &&
        !empty($data->dayClockTo) &&
        !empty($data->nightClockFrom) &&
        !empty($data->nightClockTo) &&
        !empty($data->sunrise) &&
        !empty($data->sunset)
    ) {
        $clockId = $data->clockId;
        $clockDate = $data->clockDate;
        $dayClockFrom = $data->dayClockFrom;
        $dayClockTo = $data->dayClockTo;
        $nightClockFrom = $data->nightClockFrom;
        $nightClockTo = $data->nightClockTo;
        $sunrise = $data->sunrise;
        $sunset = $data->sunset;
        $dateTime = date("Y-m-d H:i:s");
        $status = 1;

        // $dateTime = date('Y-m-d H:i:s');
        $isExistQuery = "SELECT * FROM Clock WHERE ClockDate = '$clockDate' ";
        if (mysqli_num_rows(mysqli_query($con, $isExistQuery)) != 0) {
            $query = "UPDATE `Clock` SET `ClockDate`= '$clockDate',`DayClockFrom`='$dayClockFrom',`DayClockTo`= '$dayClockTo',`NightClockFrom`= '$nightClockFrom',`NightClockTo`= '$nightClockTo',`Sunrise`= '$sunrise',`Sunset`='$sunset' WHERE ClockId= '$clockId'";
            if (mysqli_query($con, $query)) {
                if (mysqli_affected_rows($con) == 1) {
                        echo json_encode([
                            'status' => 1,
                            'message' => 'Data updated successfully.',
                        ]);
                        http_response_code(200);
                } else {
                    echo json_encode([
                        'status' => 0,
                        'message' => 'Could not insert data.',
                    ]);
                    http_response_code(200);
                }
            } else {
                echo json_encode([
                    'status' => 0,
                    'message' => mysqli_error($con),
                ]);
                http_response_code(200);
            }
        } else {
            echo json_encode([
                'status' => -2,
                'message' => 'Data not exists',
            ]);
            http_response_code(200);
        }
    } else {
        echo json_encode([
            'status' => -1,
            'message' => 'Required fields are missing.',
        ]);
        http_response_code(200);
    }
} else {
    echo json_encode([
        'status' => -1,
    ]);
    http_response_code(200);
}
