<!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 - Perpetual Contract</title>
    <link rel="stylesheet" href="../css/dashboard.css?v=1401221621">
    <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 pcl_exchange 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 pcl_exchange where id = '$id'");
    if ($delete_query) {
        header('location:perpetual-contract-liquidations-db.php');
    }
}

if (isset($_GET['ve_id'])) {
    $ve_id = $_GET['ve_id'];
    $query_for_edit_val       = mysqli_query($con, "select * from pcl_verticalaxis where id = '$ve_id'");
    $query_fetch_for_edit_val = mysqli_fetch_array($query_for_edit_val);
}

if (isset($_GET['vd_id'])) {
    $vd_id = $_GET['vd_id'];
    $delete_query_val = mysqli_query($con, "DELETE from pcl_verticalaxis where id = '$vd_id'");
    if ($delete_query_val) {
        header('location:perpetual-contract-liquidations-db.php');
    }
}
?>

<body>
    <div class="dashboard-main">
        <?php include './dashboard-nav.php' ?>
        <div class="dashboard-options">
            <div class="dashboard-options-header">
                <h1>Perpetual Contract Liquidations</h1>
                <a href="../perpetual-contract-liquidations.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>

            <!-- Time range form -->
            <form method="post">
                <fieldset>
                    <legend>Time Range</legend>

                    <div class="form-field-two-col">
                        <div class="form-field select-field">
                            <label>Select</label>
                            <select name="timerange">
                                <option value="">--</option>
                                <option value="24 Hours">24 Hours</option>
                                <option value="7 Days">7 Days</option>
                                <option value="30 Days">30 Days</option>
                                <option value="All-time">All-time</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-field-one-col">

                        <button type="submit" name="timerange_submit"><?php if (isset($_GET['tr_id'])) {
                                                                            echo 'Update';
                                                                        } else {
                                                                            echo 'Save';
                                                                        } ?></button>
                        <?php if (isset($_GET['tr_id'])) { ?>
                            <button type="submit" class="cancel-btn" name="cancel">Cancel</button>
                        <?php } ?>

                    </div>
                    <div class="dashboard-data-list">
                        <h2>Current saved Time range</h2>
                        <ul class="single-item-ul">
                            <li>
                                <span>Time range</span>
                            </li>
                            <?php
                            $query_timerange_values = mysqli_query($con, "SELECT * from pcl_timerange");
                            $fetch_timerange_values = mysqli_fetch_array($query_timerange_values);
                            if ($fetch_timerange_values) {
                            ?>

                                <li>
                                    <span> <?php echo $fetch_timerange_values['timerange'] ?? NULL; ?> </span>
                                </li>
                            <?php } ?>
                        </ul>

                    </div>
                </fieldset>

            </form><!-- Time range form -->
            <?php

            if (isset($_POST['timerange_submit'])) {
                $timerange = $_POST['timerange'];

                $insert = mysqli_query($con, "REPLACE into pcl_timerange(id,timerange)values('1','$timerange')");
                header('location:perpetual-contract-liquidations-db.php');
            }
            ?>

            <!-- add-exchange form -->
            <form class="add-exchange-form" method="post" enctype=multipart/form-data>
                <fieldset>
                    <legend>Add Exchange</legend>

                    <div class="form-field-two-col">
                        <div class="form-field select-field">
                            <label>Select</label>
                            <select name="name_select" class="addexchange-select" <?php if (isset($_GET['e_id'])) {
                                                                                        echo 'disabled';
                                                                                    } ?>>
                                <option value="">--</option>
                                <?php
                                $exchangeslist_query = mysqli_query($con, "SELECT * from pcl_exchangeslist");
                                while ($fetch_exchangeslist = mysqli_fetch_array($exchangeslist_query)) {
                                ?>
                                    <option value="<?php echo $fetch_exchangeslist['name'] ?>"><?php echo $fetch_exchangeslist['name'] ?></option>

                                <?php } ?>

                            </select>
                        </div>
                        <div class="not-formfield">
                            <?php
                            if (!isset($_GET['e_id'])) {
                                echo '<span>OR&emsp;<span class="plus-or-minus">+</span> <a href="javascript:void(0)" class="addnew-exchange-btn">Add new</a></span>';
                            }
                            ?>
                        </div>
                    </div>

                    <div class="form-field-two-col exchange-nameandlogo" <?php if (isset($_GET['e_id'])) {
                                                                                echo 'style="display:grid"';
                                                                            } ?>>
                        <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-two-col">
                        <div class="form-field">
                            <label>Total Sold</label>
                            <input type="text" name="totalSold" value="<?php echo $query_fetch__for_edit['total_sold'] ?? Null; ?>" placeholder="e.g. 200">
                        </div>
                        <div class="form-field">
                            <label>Percentage <small>(Longs)</small></label>
                            <input type="text" name="percentageChange" value="<?php echo $query_fetch__for_edit['percentage_change'] ?? Null; ?>" placeholder="e.g. 80">
                        </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'])) {
                        if (!empty($_POST['name'])) {
                            $name = $_POST['name'];
                            $logo             = $_FILES['image']['name'];
                        } else {
                            $name = $_POST['name_select'];
                            $exchangeslist_logo_query = mysqli_query($con, "SELECT * from pcl_exchangeslist WHERE name = '$name'");
                            $exchangeslist_logo = mysqli_fetch_array($exchangeslist_logo_query);
                            $logo             = $exchangeslist_logo['logo'];
                        }
                        $totalSold        = $_POST['totalSold'];
                        $percentageChange = $_POST['percentageChange'];
                        $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 pcl_exchange SET name = '$name', total_sold = '$totalSold', percentage_change = '$percentageChange'  where id = '$id'");
                            if (!empty($logo)) {
                                $sql_query_img = mysqli_query($con, "UPDATE pcl_exchange SET logo = '$logo' where id = '$id'");
                            }
                            header('location:perpetual-contract-liquidations-db.php');
                        } else {
                            $insert    = mysqli_query($con, "INSERT into pcl_exchange(name,logo,total_sold,percentage_change)values('$name', '$logo', '$totalSold', '$percentageChange')");

                            if (!empty($_POST['name'])) {
                                $insert    = mysqli_query($con, "INSERT into pcl_exchangeslist(name,logo)values('$name', '$logo')");
                            }

                            header('Refresh: 0, url=perpetual-contract-liquidations-db.php');
                        }
                    }
                    if (isset($_POST['cancel'])) {
                        header('location:perpetual-contract-liquidations-db.php');
                    }
                    ?>
                    <!-- exchange list -->
                    <div class="dashboard-data-list">
                        <h2>Added Exchanges</h2>
                        <ul>
                            <li>
                                <span>#</span>
                                <span>Name</span>
                                <span>Total Sold</span>
                                <span>Percentage change</span>
                                <span>Edit</span>
                                <span>Delete</span>
                            </li>

                            <?php
                            $serial_num = 0;
                            $query_values = mysqli_query($con, "SELECT * from pcl_exchange");
                            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['total_sold'] ?></span>
                                    <span><?php echo $fetch_values['percentage_change'] ?></span>
                                    <a href="perpetual-contract-liquidations-db.php?e_id=<?php echo $fetch_values['id'] ?>"><i class="ri-pencil-line"></i></a>
                                    <a href="perpetual-contract-liquidations-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=1401221621"></script>
</body>

</html>