<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    if (isset($_GET['key'])&&($_GET['key'] === 'pricepassword')&&isset($_GET['startDate'])&&$_GET['startDate']!=""&&isset($_GET['endDate'])&&$_GET['endDate']!=""&&isset($_GET['price'])&&$_GET['price']!=""&&isset($_GET['id'])&&$_GET['id']!="") {
        if($_GET['startDate']<=$_GET['endDate']){
            require('../connect.php');
    
            $conn = new mysqli($host, $user, $pass, $database);

            $checkBillingSql = "SELECT * FROM `billings` WHERE (`id` NOT IN ('{$_GET['id']}')) AND ((`start_date` >= '{$_GET['startDate']}' AND `end_date` <= '{$_GET['endDate']}') OR (`end_date` >= '{$_GET['startDate']}' AND `start_date` <= '{$_GET['endDate']}'))";
    
            $checkBilling = mysqli_query($conn, $checkBillingSql);

            // echo $checkBillingSql;
            
            if(mysqli_num_rows($checkBilling)==0){
                $price = str_replace(",",".",$_GET['price']);
                // $sql = "UPDATE `energy` SET `kWh_price`='{$_GET['price']}' WHERE `date` >= '{$_GET['startDate']}' AND `date` <= '{$_GET['endDate']}'";
                $sql = "UPDATE `billings` SET `start_date`='{$_GET['startDate']}',`end_date`='{$_GET['endDate']}',`price`='{$price}' WHERE `id` = '{$_GET['id']}'";
                mysqli_query($conn, $sql);
                header("Location: ../energyPrice.php");
            }else{
                // echo "Dane nakładają się";
                setcookie("error","Dane nakładają się");
            }
    
    
            // foreach ($checkBilling as $billing) {
            //     print_r($billing);
            //     echo "<br>";
            // }
    
            mysqli_close($conn);
        }else{
            echo "Błąd daty";
            setcookie("error","Błąd daty");
        }
    }
?>