<?php
//header("Location: https://bcrlangkawi-empire.com/");
    $billcode = $_GET['billcode'];
    $transactionid = $_GET['transaction_id'];
    $statusid = $_GET['status_id'];
    
    $servername = "localhost";
    $username = "bcrlangk_bcrlangkawi";
    $password = "bcrlangkawi1234*";
    $dbname = "bcrlangk_bcrlangkawi";
    
    if($statusid == 1){
        
        $test='{"available":0,"bind":0,"hours":{"00:00":{"available":0,"bind":0,"info":"","notes":"","price":0,"promo":0,"status":"none"}},"hours_definitions":[{"value":"00:00"}],"info":"","notes":"","price":"","promo":"","status":"booked"}';
    
        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
        }
        
        
        
        $sql = "UPDATE wp_dopbsp_reservations SET payment_status='paid', transaction_id='$transactionid', status='approved', token='' WHERE stripe_user='$billcode'";
    
        if ($conn->query($sql) === TRUE) {
          //echo "Record updated successfully";
        } else {
          //echo "Error updating record: " . $conn->error;
        }
        //$conn->close();
        
        $sql1 = "SELECT calendar_id, check_in, check_out FROM wp_dopbsp_reservations WHERE stripe_user='$billcode'";
        $result = $conn->query($sql1);
        
        if ($result->num_rows > 0) {
          while($row = $result->fetch_assoc()) {
            $calendar_id=$row['calendar_id'];
            $check_in=$row['check_in'];
            $check_out=$row['check_out'];
            
            //echo $calender_id;
            $begin = new DateTime($check_in);
            $end = new DateTime($check_out);
            $end = $end->modify( '+1 day' );
            
            $interval = DateInterval::createFromDateString('1 day');
            $period = new DatePeriod($begin, $interval, $end);
            
            foreach ($period as $dt) {
                $dt=$dt->format("Y-m-d");
                $uniqueid = $calendar_id.'_'.$dt;
                $sql2 = "UPDATE wp_dopbsp_days SET data='$test' WHERE unique_key='$uniqueid'";
                
                if ($conn->query($sql2) === TRUE) {
                  //echo "Record updated successfully";
                } else {
                  //echo "Error updating record: " . $conn->error;
                }
            }
          }
        }
        $conn->close();
        
        echo "<script>
              alert('Your Booking has been registered and approved');
              window.location.href='https://bcrlangkawi-empire.com/';
              </script>";
    }else{
        echo "<script>
              alert('Your Payment is unsuccessful. Please try again or call our support, thank you.');
              window.location.href='https://bcrlangkawi-empire.com/';
              </script>";
    }

  //echo $result;
    
die();
?>