<!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 - Events</title>
    <link rel="stylesheet" href="../css/dashboard.css?v=1301221146">
    <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'];
    $query_for_edit       = mysqli_query($con, "select * from ev_events where id = $id");
    $query_fetch__for_edit = mysqli_fetch_array($query_for_edit);
}

if (isset($_GET['d_id'])) {
    $id = $_GET['d_id'];
    $delete_query = mysqli_query($con, "DELETE from ev_events where id = '$id'");
    if ($delete_query) {
        header('location:events-db.php');
    }
}
?>

<body>
    <div class="dashboard-main">
        <?php include './dashboard-nav.php' ?>
        <div class="dashboard-options">
            <div class="dashboard-options-header">
                <h1>Events</h1>
                <a href="../events.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">
                <fieldset>
                    <legend>Event's Time</legend>
                    <div class="form-field-two-col">
                        <div class="form-field select-field">
                            <label>Select month</label>
                            <select name="month">
                                <option value="">--</option>
                                <option value="January">January</option>
                                <option value="February">February</option>
                                <option value="March">March</option>
                                <option value="April">April</option>
                                <option value="May">May</option>
                                <option value="June">June</option>
                                <option value="July">July</option>
                                <option value="August">August</option>
                                <option value="September">September</option>
                                <option value="October">October</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-field-one-col">
                        <button type="submit" name="submit_month">Save</button>
                    </div>
                    <div class="dashboard-data-list">
                        <h2>Current saved Month</h2>
                        <ul class="single-item-ul">
                            <li>
                                <span>Month Name</span>
                            </li>
                            <?php
                            $query_exchange_limit_values = mysqli_query($con, "SELECT * from ev_time");
                            $fetch_exchange_limit_values = mysqli_fetch_array($query_exchange_limit_values);
                            ?>
                            <li>
                                <span> <?php echo $fetch_exchange_limit_values['month'] ?? NULL; ?> </span>
                            </li>
                        </ul>

                    </div>
                </fieldset>

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

            if (isset($_POST['submit_month'])) {
                $month = $_POST['month'];

                $insert = mysqli_query($con, "REPLACE into ev_time(id,month)values('1', '$month')");
                header('location:events-db.php');
            }

            ?>

            <!-- add-exchange form -->
            <form class="add-exchange-form" method="post" enctype=multipart/form-data>
                <fieldset>
                    <legend>Add Event</legend>
                    <div class="form-field-two-col">
                        <div class="form-field">
                            <label>Name</label>
                            <input type="text" name="name" value="<?php echo $query_fetch__for_edit['name'] ?? Null; ?>" placeholder="e.g. Visa Infinite Event">
                        </div>
                        <div class="form-field">
                            <label>Coin Logo<small> ( .jpg, .jpeg, .png, .svg )</small></label>
                            <input type="file" name="image">
                        </div>
                    </div>
                    <div class="form-field-two-col">
                        <div class="form-field">
                            <label>Description</label>
                            <input type="text" name="description" value="<?php echo $query_fetch__for_edit['description'] ?? Null; ?>" placeholder="Amet minim mollit..">
                        </div>
                        <div class="form-field">
                            <label>Date</label>
                            <input type="text" name="date" value="<?php echo $query_fetch__for_edit['date'] ?? Null; ?>" placeholder="e.g. 10.09.21">
                        </div>
                    </div>
                    <div class="form-field-two-col">
                        <div class="form-field">
                            <label>Coin Fullname</label>
                            <input type="text" name="coin_fullname" value="<?php echo $query_fetch__for_edit['coin_fullname'] ?? Null; ?>" placeholder="e.g. Bitcoin">
                        </div>
                        <div class="form-field">
                            <label>Coin Shortname</label>
                            <input type="text" name="coin_shortname" value="<?php echo $query_fetch__for_edit['coin_shortname'] ?? Null; ?>" placeholder="e.g. BTC">
                        </div>
                    </div>
                    <div class="form-field-one-col">
                        <button type="submit" name="submit"><?php if (isset($_GET['e_id'])) {
                                                                echo 'Update';
                                                            } else {
                                                                echo 'Submit';
                                                            } ?></button>
                        <?php if (isset($_GET['e_id'])) { ?>
                            <button type="submit" class="cancel-btn" name="cancel">Cancel</button>
                        <?php } ?>
                    </div>
                    <?php

                    if (isset($_POST['submit'])) {
                        $name           = $_POST['name'];
                        $logo           = $_FILES['image']['name'];
                        $description    = $_POST['description'];
                        $date           = $_POST['date'];
                        $coin_fullname  = $_POST['coin_fullname'];
                        $coin_shortname = $_POST['coin_shortname'];
                        $tmp_dir        = $_FILES['image']['tmp_name'];
                        $dir            = 'uploads/';

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

                        if (isset($_GET['e_id'])) {
                            $sql_query = mysqli_query($con, "UPDATE ev_events SET name = '$name', description = '$description', date = '$date', coin_fullname = '$coin_fullname', coin_shortname = '$coin_shortname'  where id = '$id'");
                            if (!empty($logo)) {
                                $sql_query_img = mysqli_query($con, "UPDATE ev_events SET coin_logo = '$logo' where id = '$id'");
                            }
                            header('location:events-db.php');
                        } else {
                            $insert    = mysqli_query($con, "INSERT into ev_events(name,description,coin_logo,date,coin_fullname,coin_shortname)values('$name', '$description', '$logo ', '$date', '$coin_fullname', '$coin_shortname')");
                            // $sql_query = mysqli_query($con, $insert);
                            header('loacation:events-db.php');
                        }
                    }
                    if (isset($_POST['cancel'])) {
                        header('location:events-db.php');
                    }
                    ?>
                    <!-- exchange list -->
                    <div class="dashboard-data-list">
                        <h2>Added Exchanges</h2>
                        <ul>
                            <li>
                                <span>#</span>
                                <span>Name</span>
                                <span>Coin</span>
                                <span>Date</span>
                                <span>Edit</span>
                                <span>Delete</span>
                            </li>

                            <?php
                            $serial_num = 0;
                            $query_values = mysqli_query($con, "SELECT * from ev_events");
                            while ($fetch_values = mysqli_fetch_array($query_values)) {
                                $serial_num++;
                            ?>
                                <li>
                                    <span><?php echo $serial_num ?>.</span>
                                    <span><?php echo $fetch_values['name'] ?></span>
                                    <span><?php echo $fetch_values['coin_shortname'] ?></span>
                                    <span><?php echo $fetch_values['date'] ?></span>
                                    <a href="events-db.php?e_id=<?php echo $fetch_values['id'] ?>"><i class="ri-pencil-line"></i></a>
                                    <a href="events-db.php?d_id=<?php echo $fetch_values['id'] ?>"><i class="ri-delete-bin-6-line"></i></a>
                                </li>

                            <?php } ?>
                        </ul>
                    </div>
                    <!-- exchange list -->
                </fieldset>
            </form><!-- add-exchange form -->

        </div>
    </div>

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

</html>