<?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']!="") {
        if($_GET['startDate']<=$_GET['endDate']){
            require('../connect.php');
    
            $conn = new mysqli($host, $user, $pass, $database);
    
            $checkBilling = mysqli_query($conn, "SELECT * FROM `billings` WHERE (`start_date` >= '{$_GET['startDate']}' AND `end_date` <= '{$_GET['endDate']}') OR (`end_date` >= '{$_GET['startDate']}' AND `start_date` <= '{$_GET['endDate']}')");
            
            if(mysqli_num_rows($checkBilling)==0){
                // $sql = "UPDATE `energy` SET `kWh_price`='{$_GET['price']}' WHERE `date` >= '{$_GET['startDate']}' AND `date` <= '{$_GET['endDate']}'";
                $price = str_replace(",",".",$_GET['price']);
                $sql = "INSERT INTO `billings` VALUES (NULL,'{$_GET['startDate']}','{$_GET['endDate']}','{$price}')";
                echo $sql;
                mysqli_query($conn, $sql);
            }else{
                echo "Dane nakładają się";
            }
    
    
            // foreach ($checkBilling as $billing) {
            //     print_r($billing);
            //     echo "<br>";
            // }
    
            mysqli_close($conn);
        }else{
            echo "Błąd daty";
        }
    }
?>