<!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 - New Partnerships</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 np_partners 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 np_partners where id = '$id'");
    if ($delete_query) {
        header('location:new-partnerships-db.php');
    }
}

if (isset($_GET['ce_id'])) {
    $contentE_id = $_GET['ce_id'];
    $query_for_edit_content       = mysqli_query($con, "select * from np_content where id = '$contentE_id'");
    $query_fetch_for_edit_content = mysqli_fetch_array($query_for_edit_content);
}

if (isset($_GET['cd_id'])) {
    $contentD_id = $_GET['cd_id'];
    $delete_query_content = mysqli_query($con, "DELETE from np_content where id = '$contentD_id'");
    if ($delete_query_content) {
        header('location:new-partnerships-db.php');
    }
}
?>

<body>
    <div class="dashboard-main">
        <?php include './dashboard-nav.php' ?>
        <div class="dashboard-options">
            <div class="dashboard-options-header">
                <h1>New Partnerships</h1>
                <a href="../new-partnerships.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>Write-up</legend>
                    <div class="form-field-two-col">
                        <div class="form-field">
                            <label>Title</label>
                            <input type="text" name="title" value="<?php echo $query_fetch_for_edit_content['title'] ?? null ?>" placeholder="e.g. New Partnerships">
                        </div>
                        <div class="form-field">
                            <label>Date</label>
                            <input type="text" name="date" value="<?php echo $query_fetch_for_edit_content['date'] ?? null ?>" placeholder="e.g. 19 Aug 2021">
                        </div>
                    </div>
                    <div class="form-field">
                        <label>Description</label>
                        <textarea type="text" name="description" placeholder="To highlight the text, do this: <mark>Your Text</mark>"><?php echo $query_fetch_for_edit_content['description'] ?? null ?></textarea>
                    </div>
                    <div class="form-field-one-col">
                        <button type="submit" name="submit_partnership"><?php if (isset($_GET['ce_id'])) {
                                                                            echo 'Update';
                                                                        } else {
                                                                            echo 'Save';
                                                                        } ?></button>
                        <?php if (isset($_GET['ce_id'])) { ?>
                            <button type="submit" class="cancel-btn" name="cancel">Cancel</button>
                        <?php } ?>
                    </div>
                    <div class="dashboard-data-list">
                        <h2>Current Write-up</h2>
                        <ul class="single-item-ul">
                            <li>
                                <span>Title</span>
                                <span>Description</span>
                                <span>Date</span>
                                <span>Edit</span>
                                <span>Delete</span>
                            </li>
                            <?php
                            $query_partnerships_values = mysqli_query($con, "SELECT * from np_content");
                            $fetch_partnerships_values = mysqli_fetch_array($query_partnerships_values);

                            $full_desc = $fetch_partnerships_values['description'] ?? NULL;
                            $short_desc = substr($full_desc, 0, 100);

                            if ($fetch_partnerships_values) {


                            ?>
                                <li>
                                    <span> <?php echo $fetch_partnerships_values['title'] ?? NULL; ?> </span>

                                    <span> <?php echo $short_desc;
                                            if (strlen($full_desc) >= 100) {
                                                echo "..." ?? NULL;
                                            }  ?> </span>

                                    <span> <?php echo $fetch_partnerships_values['date'] ?? NULL; ?> </span>
                                    <a href="new-partnerships-db.php?ce_id=<?php echo $fetch_partnerships_values['id'] ?>"><i class="ri-pencil-line"></i></a>
                                    <a href="new-partnerships-db.php?cd_id=<?php echo $fetch_partnerships_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_partnership'])) {
                $title       = $_POST['title'];
                $description = $_POST['description'];
                $date       = $_POST['date'];

                if (isset($_GET['ce_id'])) {
                    $sql_query = mysqli_query($con, "UPDATE np_content SET title = '$title', description = '$description', date = '$date' where id = '$contentE_id'");
                    header('location:new-partnerships-db.php');
                } else {
                    $insert = mysqli_query($con, "REPLACE into np_content(id,title,description,date)values('1', '$title','$description','$date')");
                    header('location:new-partnerships-db.php');
                }
            }

            ?>

            <!-- add-exchange form -->
            <form class="add-exchange-form" method="post" enctype=multipart/form-data>
                <fieldset>
                    <legend>Add Partners</legend>
                    <div class="form-field-two-col">
                        <div class="form-field">
                            <label>Name</label>
                            <input type="text" name="coin_name" value="<?php echo $query_fetch__for_edit['coin_name'] ?? Null; ?>" placeholder="e.g. Bitcoin">
                        </div>
                        <div class="form-field">
                            <label>Coin Logo<small> ( .jpg, .jpeg, .png, .svg )</small></label>
                            <input type="file" name="coin_logo">
                        </div>
                    </div>
                    <div class="form-field-two-col">
                        <div class="form-field">
                            <label>Coin Placeholder Logo<small> ( .jpg, .jpeg, .png, .svg )</small></label>
                            <input type="file" name="coin_placeholder_logo">
                        </div>
                        <div class="form-field">
                            <label>Coin Shortname</label>
                            <input type="text" name="coin_placeholder_shortname" value="<?php echo $query_fetch__for_edit['coin_placeholder_shortname'] ?? Null; ?>" placeholder="e.g. BTC">
                        </div>
                    </div>
                    <div class="form-field-two-col">
                        <div class="form-field">
                            <label>Twitter ID</label>
                            <input type="text" name="twitter_id" value="<?php echo $query_fetch__for_edit['twitter_id'] ?? Null; ?>" placeholder="e.g. bitcoin">
                        </div>
                    </div>
                    <div class="form-field-one-col">
                        <?php
                        $query_values = mysqli_query($con, "SELECT * from np_partners limit 2");
                        $row_count = mysqli_num_rows($query_values);
                        ?>

                        <button type="submit" name="submit" <?php if ($row_count >= 2 && empty($_GET['e_id'])) {
                                                                echo 'disabled';
                                                            } ?>><?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 } ?>
                        <?php
                        if ($row_count >= 2 && empty($_GET['e_id'])) {
                        ?>
                            <div class="info-msg">
                                <small>(Cannot add more than 2 partners, please delete or edit the ones listed below.)</small>
                            </div>
                        <?php } ?>
                    </div>
                    <?php

                    if (isset($_POST['submit'])) {
                        $coin_name                   = $_POST['coin_name'];
                        $coin_logo                   = $_FILES['coin_logo']['name'];
                        $coin_placeholder_logo       = $_FILES['coin_placeholder_logo']['name'];
                        $coin_placeholder_shortname  = $_POST['coin_placeholder_shortname'];
                        $twitter_id                  = $_POST['twitter_id'];
                        $tmp_dir_coin_logo           = $_FILES['coin_logo']['tmp_name'];
                        $tmp_dir_placeholder_logo    = $_FILES['coin_placeholder_logo']['tmp_name'];
                        $dir                         = 'uploads/';

                        if (!empty($coin_logo)) {
                            move_uploaded_file($tmp_dir_coin_logo, $dir . $coin_logo);
                        }

                        if (!empty($coin_placeholder_logo)) {
                            move_uploaded_file($tmp_dir_placeholder_logo, $dir . $coin_placeholder_logo);
                        }

                        if (isset($_GET['e_id'])) {
                            $sql_query = mysqli_query($con, "UPDATE np_partners SET coin_name = '$coin_name', coin_placeholder_shortname = '$coin_placeholder_shortname', twitter_id = '$twitter_id' where id = '$id'");
                            if (!empty($coin_logo)) {
                                $sql_query_img = mysqli_query($con, "UPDATE np_partners SET coin_logo = '$coin_logo' where id = '$id'");
                            }
                            if (!empty($coin_placeholder_logo)) {
                                $sql_query_img = mysqli_query($con, "UPDATE np_partners SET coin_placeholder_logo = '$coin_placeholder_logo' where id = '$id'");
                            }
                            header('Refresh: 0, url=new-partnerships-db.php');
                        } else {
                            if ($row_count < 2) {
                                $insert    = mysqli_query($con, "INSERT into np_partners(coin_name, coin_logo, coin_placeholder_logo, coin_placeholder_shortname, twitter_id)values('$coin_name', '$coin_logo', '$coin_placeholder_logo ', '$coin_placeholder_shortname', '$twitter_id')");
                                header('Refresh: 0, url=new-partnerships-db.php');
                            }
                        }
                    }
                    if (isset($_POST['cancel'])) {
                        header('location:new-partnerships-db.php');
                    }
                    ?>
                    <!-- exchange list -->
                    <div class="dashboard-data-list">
                        <h2>New Partners</h2>
                        <ul>
                            <li>
                                <span>#</span>
                                <span>Coin Name</span>
                                <span>Coin Short Name</span>
                                <span>Twitter</span>
                                <span>Edit</span>
                                <span>Delete</span>
                            </li>

                            <?php
                            $serial_num = 0;
                            $query_values = mysqli_query($con, "SELECT * from np_partners limit 2");
                            $row_count = mysqli_num_rows($query_values);
                            while ($fetch_values = mysqli_fetch_array($query_values)) {
                                $serial_num++;
                            ?>
                                <li>
                                    <span><?php echo $serial_num ?>.</span>
                                    <span><?php echo $fetch_values['coin_name'] ?></span>
                                    <span><?php echo $fetch_values['coin_placeholder_shortname'] ?></span>
                                    <span>@<?php echo $fetch_values['twitter_id'] ?></span>
                                    <a href="new-partnerships-db.php?e_id=<?php echo $fetch_values['id'] ?>"><i class="ri-pencil-line"></i></a>
                                    <a href="new-partnerships-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>