<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard - AMA Series</title>
    <link rel="stylesheet" href="../css/dashboard.css?v=1901221355">
    <link href="https://cdn.jsdelivr.net/npm/remixicon@2.5.0/fonts/remixicon.css" rel="stylesheet">

</head>

<?php
ob_start();
include('db_connection.php');
if (isset($_GET['e_id'])) {
    $id               = $_GET['e_id'];
    $ama_series_edit       = mysqli_query($con, "select * from ama_series where id = $id");
    $query_fetch_for_edit = mysqli_fetch_array($ama_series_edit);
}

if (isset($_GET['d_id'])) {
    $id = $_GET['d_id'];
    $ama_series_delete = mysqli_query($con, "DELETE from ama_series where id = '$id'");
    if ($ama_series_delete) {
        header('location:ama-series-db.php');
    }
}

?>

<body>
    <div class="dashboard-main">
        <?php include './dashboard-nav.php' ?>
        <div class="dashboard-options">
            <div class="dashboard-options-header">
                <h1>AMA Series</h1>
                <a href="../ama-series.php" target="_blank">
                    <button>
                        <span>Preview</span>
                        <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAQElEQVR42qXKwQkAIAxDUUdxtO6/RBQkQZvSi8I/pL4BoGw/XPkh4XigPmsUgh0626AjRsgxHTkUThsG2T/sIlzdTsp52kSS1wAAAABJRU5ErkJggg==" alt="" style="filter: invert(1);">
                    </button>
                </a>
            </div>

            <!-- add max value and gat between value form -->
            <form method="post" enctype=multipart/form-data>
                <fieldset>
                    <legend>Content</legend>
                    <div class="form-field-two-col">
                        <div class="form-field">
                            <label>Date</label>
                            <input type="text" name="date" value="<?php echo $query_fetch_for_edit['date'] ?? null ?>" placeholder="e.g. 19 Aug 2021">
                        </div>
                        <div class="form-field">
                            <label>Time</label>
                            <input type="text" name="time" value="<?php echo $query_fetch_for_edit['time'] ?? null ?>" placeholder="e.g. 5:00">
                        </div>
                    </div>
                    <div class="form-field-two-col">
                        <div class="form-field select-field">
                            <label>Telegram account</label>
                            <select name="telegram_acc">
                                <option value="@satoshi_club" <?php if (isset($_GET['e_id']) && $query_fetch_for_edit['telegram_acc'] == '@satoshi_club') {
                                                                    echo 'selected';
                                                                } ?>>@satoshi_club</option>

                                <option value="@satoshi_club_ru" <?php if (isset($_GET['e_id']) && $query_fetch_for_edit['telegram_acc'] == '@satoshi_club_ru') {
                                                                        echo 'selected';
                                                                    } ?>>@satoshi_club_ru</option>

                                <option value="@satoshi_club_spanish" <?php if (isset($_GET['e_id']) && $query_fetch_for_edit['telegram_acc'] == '@satoshi_club_spanish') {
                                                                            echo 'selected';
                                                                        } ?>>@satoshi_club_spanish</option>

                                <option value="@satoshi_club_indonesia" <?php if (isset($_GET['e_id']) && $query_fetch_for_edit['telegram_acc'] == '@satoshi_club_indonesia') {
                                                                            echo 'selected';
                                                                        } ?>>@satoshi_club_indonesia</option>

                                <option value="@satoshi_club_cn" <?php if (isset($_GET['e_id']) && $query_fetch_for_edit['telegram_acc'] == '@satoshi_club_cn') {
                                                                        echo 'selected';
                                                                    } ?>>@satoshi_club_cn</option>
                            </select>
                        </div>
                        <div class="form-field">
                            <label>Project logo</label>
                            <input type="file" name="project_logo" />
                        </div>
                    </div>
                    <div class="form-field-two-col">
                        <div class="form-field">
                            <label>Project name <small>(Leave empty if name is not necessary)</small></label>
                            <input type="text" name="project_name" value="<?php echo $query_fetch_for_edit['project_name'] ?? null ?>" placeholder="e.g. Meta stake">
                        </div>
                        <div class="form-field">
                            <label>Prize pool</label>
                            <input type="text" name="prize_pool" value="<?php echo $query_fetch_for_edit['prize_pool'] ?? null ?>" placeholder="e.g. 500">
                        </div>
                    </div>
                    <div class="form-field-one-col">
                        <button type="submit" name="submit"><?php if (isset($_GET['e_id'])) {
                                                                echo 'Update';
                                                            } else {
                                                                echo 'Save';
                                                            } ?></button>
                        <?php if (isset($_GET['e_id'])) { ?>
                            <button type="submit" class="cancel-btn" name="cancel">Cancel</button>
                        <?php } ?>
                    </div>
                    <div class="dashboard-data-list ama-dashboard-data-list">
                        <h2>Current Content</h2>
                        <ul class="single-item-ul">
                            <li>
                                <span>Date</span>
                                <span>Time</span>
                                <span>Telegram</span>
                                <span>Prize Pool</span>
                                <span>Edit</span>
                                <span>Delete</span>
                            </li>
                            <?php
                            $ama_series_values = mysqli_query($con, "SELECT * from ama_series");
                            $fetch_ama_series_values = mysqli_fetch_array($ama_series_values);

                            if ($fetch_ama_series_values) {
                            ?>
                                <li>
                                    <span><?php echo $fetch_ama_series_values['date'] ?? NULL; ?></span>
                                    <span><?php echo $fetch_ama_series_values['time'] ?? NULL; ?></span>
                                    <span><?php echo $fetch_ama_series_values['telegram_acc'] ?? NULL; ?></span>
                                    <span>$<?php echo $fetch_ama_series_values['prize_pool'] ?? NULL; ?></span>
                                    <a href="ama-series-db.php?e_id=<?php echo $fetch_ama_series_values['id'] ?>"><i class="ri-pencil-line"></i></a>
                                    <a href="ama-series-db.php?d_id=<?php echo $fetch_ama_series_values['id'] ?>"><i class="ri-delete-bin-6-line"></i></a>
                                </li>
                            <?php } ?>
                        </ul>

                    </div>
                </fieldset>

            </form><!-- add max value and gat between value form -->
            <?php

            if (isset($_POST['submit'])) {
                $date          = $_POST['date'];
                $time          = $_POST['time'];
                $telegram_acc  = $_POST['telegram_acc'];
                $project_logo  = $_FILES['project_logo']['name'];
                $prize_pool    = $_POST['prize_pool'];
                $tmp_dir       = $_FILES['project_logo']['tmp_name'];
                $dir           = 'uploads/';
                $project_name  = $_POST['project_name'];

                if (!empty($project_logo)) {
                    move_uploaded_file($tmp_dir, $dir . $project_logo);
                }

                if (isset($_GET['e_id'])) {
                    $sql_query = mysqli_query($con, "UPDATE ama_series SET date = '$date', time = '$time', telegram_acc = '$telegram_acc', prize_pool = '$prize_pool', project_name = '$project_name' where id = '$id'");
                    if (!empty($project_logo)) {
                        $sql_query_img = mysqli_query($con, "UPDATE ama_series SET project_logo = '$project_logo' where id = '$id'");
                    }
                    header('location:ama-series-db.php');
                } else {
                    $insert = mysqli_query($con, "REPLACE into ama_series(id,date,time,telegram_acc,project_logo,project_name,prize_pool)values('1', '$date','$time','$telegram_acc','$project_logo','$project_name','$prize_pool')");
                    header('location:ama-series-db.php');
                }
            }
            if (isset($_POST['cancel'])) {
                header('location:ama-series-db.php');
            }

            ?>
        </div>
    </div>

    <!-- custom js -->
    <script src="../js/dashboard.js?v=1901221355"></script>
</body>

</html>