<!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 - All Exchanges</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');
// edit/delete for nft collectibles
if (isset($_GET['nc_e_id'])) {
    $id               = $_GET['nc_e_id'];
    $query_for_edit       = mysqli_query($con, "select * from nc_exchangeslist where id = $id");
    $query_fetch__for_edit = mysqli_fetch_array($query_for_edit);
}

if (isset($_GET['nc_d_id'])) {
    $id = $_GET['nc_d_id'];
    $delete_query = mysqli_query($con, "DELETE from nc_exchangeslist where id = '$id'");
    if ($delete_query) {
        header('location:all-exchanges-db.php');
    }
}

// edit/delete for perpetual contract
if (isset($_GET['pc_e_id'])) {
    $id               = $_GET['pc_e_id'];
    $query_for_edit       = mysqli_query($con, "select * from pcl_exchangeslist where id = $id");
    $query_fetch__for_edit = mysqli_fetch_array($query_for_edit);
}

if (isset($_GET['pc_d_id'])) {
    $id = $_GET['pc_d_id'];
    $delete_query = mysqli_query($con, "DELETE from pcl_exchangeslist where id = '$id'");
    if ($delete_query) {
        header('location:all-exchanges-db.php');
    }
}

?>

<body>
    <div class="dashboard-main">
        <?php include './dashboard-nav.php' ?>
        <div class="dashboard-options">
            <div class="dashboard-options-header">
                <h1>All Exchanges</h1>
            </div>
            <div class="two-col">
                <fieldset>
                    <legend>NFT Collectible</legend>
                    <!-- exchange list -->
                    <div class="dashboard-data-list">
                        <?php if (isset($_GET['nc_e_id'])) { ?>

                            <form method="POST" enctype="multipart/form-data">
                                <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. Binance" class="exchange-name-input">
                                    </div>
                                    <div class="form-field">
                                        <label>Logo <small>( .jpg, .jpeg, .png, .svg )</small></label>
                                        <input type="file" name="image" class="exchange-image-input">
                                    </div>
                                </div>
                                <div class="form-field-one-col">
                                    <button type="submit" name="nc_submit"><?php if (isset($_GET['nc_e_id'])) {
                                                                                echo 'Update';
                                                                            } else {
                                                                                echo 'Submit';
                                                                            } ?></button>
                                    <?php if (isset($_GET['nc_e_id'])) { ?>
                                        <button type="submit" class="cancel-btn" name="nc_cancel">Cancel</button>
                                    <?php } ?>
                                </div>

                                <?php
                                if (isset($_POST['nc_submit'])) {
                                    $name           = $_POST['name'];
                                    $logo           = $_FILES['image']['name'];
                                    $tmp_dir        = $_FILES['image']['tmp_name'];
                                    $dir            = 'uploads/';

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

                                    if (isset($_GET['nc_e_id'])) {
                                        $sql_query = mysqli_query($con, "UPDATE nc_exchangeslist SET name = '$name' where id = '$id'");
                                        if (!empty($logo)) {
                                            $sql_query_img = mysqli_query($con, "UPDATE nc_exchangeslist SET logo = '$logo' where id = '$id'");
                                        }
                                        header('location:all-exchanges-db.php');
                                    }
                                }
                                if (isset($_POST['nc_cancel'])) {
                                    header('location:all-exchanges-db.php');
                                }
                                ?>

                            </form>
                        <?php } ?>
                        <ul class="allexchanges-ul">
                            <li>
                                <span>#</span>
                                <span>Name</span>
                                <span>Logo</span>
                                <span>Edit</span>
                                <span>Delete</span>
                            </li>

                            <?php
                            $serial_num = 0;
                            $query_nc_exchangeslist_values = mysqli_query($con, "SELECT * from nc_exchangeslist");
                            $row_count = mysqli_num_rows($query_nc_exchangeslist_values);

                            if ($row_count > 0) {
                                while ($fetch_nc_exchangeslist_values = mysqli_fetch_array($query_nc_exchangeslist_values)) {
                                    $serial_num++;
                            ?>
                                    <li>
                                        <span><?php echo $serial_num ?>.</span>
                                        <span><?php echo $fetch_nc_exchangeslist_values['name'] ?></span>
                                        <span class="allexchanges-logo-span"><img src="./uploads/<?php echo $fetch_nc_exchangeslist_values['logo']; ?>" alt="<?php echo $fetch_nc_exchangeslist_values['name']; ?>"></span>
                                        <a href="all-exchanges-db.php?nc_e_id=<?php echo $fetch_nc_exchangeslist_values['id'] ?>"><i class="ri-pencil-line"></i></a>
                                        <a href="all-exchanges-db.php?nc_d_id=<?php echo $fetch_nc_exchangeslist_values['id'] ?>"><i class="ri-delete-bin-6-line"></i></a>
                                    </li>
                                <?php } ?>
                            <?php } else {
                                echo '<li class="empty-state-li"><span>The list is empty</span></li>';
                            } ?>
                        </ul>
                    </div>
                    <!-- exchange list -->
                </fieldset>

                <fieldset>
                    <legend>Perpetual Contract</legend>
                    <!-- exchange list -->
                    <div class="dashboard-data-list">
                        <?php if (isset($_GET['pc_e_id'])) { ?>

                            <form method="POST" enctype="multipart/form-data">
                                <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. Binance" class="exchange-name-input">
                                    </div>
                                    <div class="form-field">
                                        <label>Logo <small>( .jpg, .jpeg, .png, .svg )</small></label>
                                        <input type="file" name="image" class="exchange-image-input">
                                    </div>
                                </div>
                                <div class="form-field-one-col">
                                    <button type="submit" name="pc_submit"><?php if (isset($_GET['pc_e_id'])) {
                                                                                echo 'Update';
                                                                            } else {
                                                                                echo 'Submit';
                                                                            } ?></button>
                                    <?php if (isset($_GET['pc_e_id'])) { ?>
                                        <button type="submit" class="cancel-btn" name="pc_cancel">Cancel</button>
                                    <?php } ?>
                                </div>

                                <?php
                                if (isset($_POST['pc_submit'])) {
                                    $name           = $_POST['name'];
                                    $logo           = $_FILES['image']['name'];
                                    $tmp_dir        = $_FILES['image']['tmp_name'];
                                    $dir            = 'uploads/';

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

                                    if (isset($_GET['pc_e_id'])) {
                                        $sql_query = mysqli_query($con, "UPDATE pcl_exchangeslist SET name = '$name' where id = '$id'");
                                        if (!empty($logo)) {
                                            $sql_query_img = mysqli_query($con, "UPDATE pcl_exchangeslist SET logo = '$logo' where id = '$id'");
                                        }
                                        header('location:all-exchanges-db.php');
                                    }
                                }
                                if (isset($_POST['pc_cancel'])) {
                                    header('location:all-exchanges-db.php');
                                }
                                ?>

                            </form>
                        <?php } ?>
                        <ul class="allexchanges-ul">
                            <li>
                                <span>#</span>
                                <span>Name</span>
                                <span>Logo</span>
                                <span>Edit</span>
                                <span>Delete</span>
                            </li>

                            <?php
                            $serial_num = 0;
                            $query_exchangeslist_values = mysqli_query($con, "SELECT * from pcl_exchangeslist");
                            $row_count = mysqli_num_rows($query_exchangeslist_values);

                            if ($row_count > 0) {
                                while ($fetch_exchangeslist_values = mysqli_fetch_array($query_exchangeslist_values)) {
                                    $serial_num++;
                            ?>
                                    <li>
                                        <span><?php echo $serial_num ?>.</span>
                                        <span><?php echo $fetch_exchangeslist_values['name'] ?></span>
                                        <span class="allexchanges-logo-span"><img src="./uploads/<?php echo $fetch_exchangeslist_values['logo']; ?>" alt="<?php echo $fetch_exchangeslist_values['name'] ?>"></span>
                                        <a href="all-exchanges-db.php?pc_e_id=<?php echo $fetch_exchangeslist_values['id'] ?>"><i class="ri-pencil-line"></i></a>
                                        <a href="all-exchanges-db.php?pc_d_id=<?php echo $fetch_exchangeslist_values['id'] ?>"><i class="ri-delete-bin-6-line"></i></a>
                                    </li>
                                <?php } ?>
                            <?php } else {
                                echo '<li class="empty-state-li"><span>The list is empty</span></li>';
                            } ?>
                        </ul>
                    </div>
                    <!-- exchange list -->
                </fieldset>
            </div>

        </div>
    </div>

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

</html>