<?php
// Database configuration
$servername = "localhost"; // Replace with your server name
$username = "antenicenefather_anf"; // Replace with your database username
$password = "363213119.363213119"; // Replace with your database password
$dbname = "antenicenefather_anf"; // Replace with your database name

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Prepare and bind
$stmt = $conn->prepare("INSERT INTO longform (content_type, content_1, content_2, content_3, content_4, content_5, content_6, content_7, content_8, content_9, content_10, content_11, content_12, content_13, content_14, content_15, content_16, content_17, content_18, content_19, content_20, is_send) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("issssssssssssssssssssi", $content_type, $content_1, $content_2, $content_3, $content_4, $content_5, $content_6, $content_7, $content_8, $content_9, $content_10, $content_11, $content_12, $content_13, $content_14, $content_15, $content_16, $content_17, $content_18, $content_19, $content_20, $is_send);

// Set parameters and execute
$content_type = 1; // Content type is always set to 1
$content_1 = $_POST['content_1'] ?? '';
$content_2 = $_POST['content_2'] ?? '';
$content_3 = $_POST['content_3'] ?? '';
$content_4 = $_POST['content_4'] ?? '';
$content_5 = $_POST['content_5'] ?? '';
$content_6 = $_POST['content_6'] ?? '';
$content_7 = $_POST['content_7'] ?? '';
$content_8 = $_POST['content_8'] ?? '';
$content_9 = $_POST['content_9'] ?? '';
$content_10 = $_POST['content_10'] ?? '';
$content_11 = $_POST['content_11'] ?? '';
$content_12 = $_POST['content_12'] ?? '';
$content_13 = $_POST['content_13'] ?? '';
$content_14 = $_POST['content_14'] ?? '';
$content_15 = $_POST['content_15'] ?? '';
$content_16 = $_POST['content_16'] ?? '';
$content_17 = $_POST['content_17'] ?? '';
$content_18 = $_POST['content_18'] ?? '';
$content_19 = $_POST['content_19'] ?? '';
$content_20 = $_POST['content_20'] ?? '';
$is_send = 1; // Assuming is_send should be 1

$modalToShow = "";

if ($stmt->execute()) {
    $modalToShow = '<script type="text/javascript">
                        $(document).ready(function(){
                            $("#contentReadyModal").modal("show");
                        });
                    </script>';
} else {
    $modalToShow = "Error: " . $stmt->error;
}

$stmt->close();
$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Bootstrap CSS -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>

<!-- Content Ready Modal -->
<div class="modal fade" id="contentReadyModal">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Attention!</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                Your longform content will be generated in a new tab --> Do not close the new tab while the content is being generated or it will be lost!
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal" onclick="redirectToIndex()">Cancel</button>
                <button type="button" class="btn btn-primary" onclick="openContentTab()">Create Content Now</button>
            </div>
        </div>
    </div>
</div>

<script>
function openContentTab() {
    // Close the modal
    $("#contentReadyModal").modal("hide");

    // Redirect the current tab to index.php
    window.location.href = "index.php";

    // Open 1.php in a new tab
    var win = window.open("1.php", "_blank");
    if (win) {
        // Browser has allowed it to be opened
        win.focus();
    } else {
        // Browser has blocked it
        alert('Pop up was blocked! In order to generate your longform content Please allow popups for this website');
    }
}

function redirectToIndex() {
    window.location.href = "index.php";
}
</script>

<?php echo $modalToShow; ?>

</body>
</html>
