<?php 
include 'header.php'; 
$msg = "";

// Database connection (assuming $link is defined in header.php)
if(!isset($link)) {
    die("Database connection not established");
}

if(isset($_POST['delete'])){
    $uid = trim($_POST['userId']);
    
    $delete_query = mysqli_query($link, "DELETE FROM coin_staking_plans WHERE id = '$uid'");
    if($delete_query) {
        echo '<script>alert("Staking plan was deleted successfully");window.location.href = "stakeplan.php" </script>';
    } else {
        echo '<script>alert("Error deleting staking plan: '.mysqli_error($link).'");</script>';
    }
}

// Handle edit form submission
if(isset($_POST['update_plan'])){
    $id = mysqli_real_escape_string($link, $_POST['id']);
    $coin_name = mysqli_real_escape_string($link, $_POST['coin_name']);
    $symbol = mysqli_real_escape_string($link, $_POST['symbol']);
    $icon_url = mysqli_real_escape_string($link, $_POST['icon_url']);
    $duration_days = mysqli_real_escape_string($link, $_POST['duration_days']);
    $apr = mysqli_real_escape_string($link, $_POST['apr']);
    $min_stake = mysqli_real_escape_string($link, $_POST['min_stake']);
    $max_stake = mysqli_real_escape_string($link, $_POST['max_stake']);
    
    $sql = "UPDATE coin_staking_plans SET 
            coin_name = '$coin_name',
            symbol = '$symbol',
            icon_url = '$icon_url',
            duration_days = '$duration_days',
            apr = '$apr',
            min_stake = '$min_stake',
            max_stake = '$max_stake'
            WHERE id = '$id'";
            
    if(mysqli_query($link, $sql)){
        echo '<script>alert("Staking plan updated successfully");window.location.href = "stakeplan.php"</script>';
    } else {
        echo '<script>alert("Error updating staking plan: '.mysqli_error($link).'");</script>';
    }
}

// Handle add new plan form submission
if(isset($_POST['add_plan'])){
    $coin_name = mysqli_real_escape_string($link, $_POST['coin_name']);
    $symbol = mysqli_real_escape_string($link, $_POST['symbol']);
    $icon_url = mysqli_real_escape_string($link, $_POST['icon_url']);
    $duration_days = mysqli_real_escape_string($link, $_POST['duration_days']);
    $apr = mysqli_real_escape_string($link, $_POST['apr']);
    $min_stake = mysqli_real_escape_string($link, $_POST['min_stake']);
    $max_stake = mysqli_real_escape_string($link, $_POST['max_stake']);
    
    $sql = "INSERT INTO coin_staking_plans (coin_name, symbol, icon_url, duration_days, apr, min_stake, max_stake) 
            VALUES ('$coin_name', '$symbol', '$icon_url', '$duration_days', '$apr', '$min_stake', '$max_stake')";
            
    if(mysqli_query($link, $sql)){
        echo '<script>alert("New staking plan added successfully");window.location.href = "stakeplan.php"</script>';
    } else {
        echo '<script>alert("Error adding staking plan: '.mysqli_error($link).'");</script>';
    }
}
?>

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.jqueryui.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.5.6/css/buttons.jqueryui.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.5.6/css/buttons.bootstrap.min.css">

<script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/dataTables.jqueryui.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.jqueryui.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.colVis.min.js"></script>

<style>
.table-responsive {
    overflow-x: hidden;
}
@media (max-width: 8000px) {
    .table-responsive {
        overflow-x: auto;
    }
}
.modal-lg {
    max-width: 800px;
}
</style>

<?php 
if ($msg != "") {
    echo "<div class='alert alert-success' style='padding:20px;background-color:#dce8f7;color:black'>$msg</div>"; 
}
?>

<div class="page-content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h4 class="card-title mb-0">Staking Plans</h4>
                        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addPlanModal">
                            Add New Plan
                        </button>
                    </div><!-- end card header -->

                    <div class="card-body">
                        <div id="customerList">
                            <div class="table-responsive table-card mt-3 mb-1">
                                <table id="myTable" class="table table-striped table-bordered" style="width:100%">  
                                    <thead class="table-light">
                                        <tr>
                                            <th>Coin Name</th>
                                            <th>Symbol</th>
                                            <th>Duration (Days)</th>
                                            <th>APR (%)</th>
                                            <th>Min Stake</th>
                                            <th>Max Stake</th>
                                            <th>Action</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>  
                                        <?php 
                                        $sql = "SELECT * FROM coin_staking_plans ORDER BY id DESC";
                                        $result = mysqli_query($link, $sql);
                                        if($result && mysqli_num_rows($result) > 0) {
                                            while($row = mysqli_fetch_assoc($result)) {  
                                        ?>
                                        <tr>
                                            <form method="post">
                                                <input type="hidden" name="userId" value="<?php echo $row['id']; ?>">
                                                <td><?php echo htmlspecialchars($row['coin_name']); ?></td>
                                                <td><?php echo htmlspecialchars($row['symbol']); ?></td>
                                                <td><?php echo htmlspecialchars($row['duration_days']); ?></td>
                                                <td><?php echo htmlspecialchars($row['apr']); ?></td>
                                                <td><?php echo htmlspecialchars($row['min_stake']); ?></td>
                                                <td><?php echo htmlspecialchars($row['max_stake']); ?></td>
                                                <td>
                                                    <button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#editPlanModal<?php echo $row['id']; ?>">
                                                        Edit
                                                    </button>
                                                </td>
                                                <td>
                                                    <button type="submit" onclick="return confirm('Are you sure you want to delete this staking plan?')" name="delete" class="btn btn-danger">
                                                        Delete
                                                    </button>
                                                </td>
                                            </form>
                                        </tr>

                                        <!-- Edit Plan Modal for each row -->
                                        <div class="modal fade" id="editPlanModal<?php echo $row['id']; ?>" tabindex="-1" aria-labelledby="editPlanModalLabel<?php echo $row['id']; ?>" aria-hidden="true">
                                            <div class="modal-dialog modal-lg">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <h5 class="modal-title" id="editPlanModalLabel<?php echo $row['id']; ?>">Edit Staking Plan</h5>
                                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                    </div>
                                                    <form method="post">
                                                        <div class="modal-body">
                                                            <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
                                                            
                                                            <div class="row mb-3">
                                                                <div class="col-md-6">
                                                                    <label for="coin_name" class="form-label">Coin Name</label>
                                                                    <input type="text" class="form-control" id="coin_name" name="coin_name" value="<?php echo htmlspecialchars($row['coin_name']); ?>" required>
                                                                </div>
                                                                <div class="col-md-6">
                                                                    <label for="symbol" class="form-label">Symbol</label>
                                                                    <input type="text" class="form-control" id="symbol" name="symbol" value="<?php echo htmlspecialchars($row['symbol']); ?>" required>
                                                                </div>
                                                            </div>
                                                            
                                                            <div class="row mb-3">
                                                                <div class="col-md-6">
                                                                    <label for="icon_url" class="form-label">Icon URL</label>
                                                                    <input type="text" class="form-control" id="icon_url" name="icon_url" value="<?php echo htmlspecialchars($row['icon_url']); ?>">
                                                                </div>
                                                                <div class="col-md-6">
                                                                    <label for="duration_days" class="form-label">Duration (Days)</label>
                                                                    <input type="number" class="form-control" id="duration_days" name="duration_days" value="<?php echo htmlspecialchars($row['duration_days']); ?>" required>
                                                                </div>
                                                            </div>
                                                            
                                                            <div class="row mb-3">
                                                                <div class="col-md-6">
                                                                    <label for="apr" class="form-label">APR (%)</label>
                                                                    <input type="number" step="0.01" class="form-control" id="apr" name="apr" value="<?php echo htmlspecialchars($row['apr']); ?>" required>
                                                                </div>
                                                                <div class="col-md-6">
                                                                    <label for="min_stake" class="form-label">Minimum Stake</label>
                                                                    <input type="number" step="0.000001" class="form-control" id="min_stake" name="min_stake" value="<?php echo htmlspecialchars($row['min_stake']); ?>" required>
                                                                </div>
                                                            </div>
                                                            
                                                            <div class="row mb-3">
                                                                <div class="col-md-6">
                                                                    <label for="max_stake" class="form-label">Maximum Stake</label>
                                                                    <input type="number" step="0.000001" class="form-control" id="max_stake" name="max_stake" value="<?php echo htmlspecialchars($row['max_stake']); ?>" required>
                                                                </div>
                                                            </div>
                                                        </div>
                                                        <div class="modal-footer">
                                                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                                                            <button type="submit" name="update_plan" class="btn btn-primary">Save Changes</button>
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>
                                        <?php 
                                            }
                                        } else {
                                            echo "<tr><td colspan='8' class='text-center'>No staking plans found</td></tr>";
                                        }
                                        ?>
                                    </tbody>  
                                </table>
                            </div>
                        </div>
                    </div><!-- end card -->
                </div>
                <!-- end col -->
            </div>
            <!-- end col -->
        </div>
        <!-- end row -->
    </div>
</div>

<!-- Add New Plan Modal -->
<div class="modal fade" id="addPlanModal" tabindex="-1" aria-labelledby="addPlanModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addPlanModalLabel">Add New Staking Plan</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form method="post">
                <div class="modal-body">
                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label for="new_coin_name" class="form-label">Coin Name</label>
                            <input type="text" class="form-control" id="new_coin_name" name="coin_name" required>
                        </div>
                        <div class="col-md-6">
                            <label for="new_symbol" class="form-label">Symbol</label>
                            <input type="text" class="form-control" id="new_symbol" name="symbol" required>
                        </div>
                    </div>
                    
                    <div class="row mb-3">
                       <div class="col-md-6">
                            <label for="new_icon_url" class="form-label">Icon URL</label>
                            <input type="url" class="form-control" id="new_icon_url" name="icon_url" placeholder="https://example.com" pattern="https?://.+" required>
                        </div>

                        <div class="col-md-6">
                            <label for="new_duration_days" class="form-label">Duration (Days)</label>
                            <input type="number" class="form-control" id="new_duration_days" name="duration_days" required>
                        </div>
                    </div>
                    
                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label for="new_apr" class="form-label">APR (%)</label>
                            <input type="number" step="0.01" class="form-control" id="new_apr" name="apr" required>
                        </div>
                        <div class="col-md-6">
                            <label for="new_min_stake" class="form-label">Minimum Stake</label>
                            <input type="number" step="0.000001" class="form-control" id="new_min_stake" name="min_stake" required>
                        </div>
                    </div>
                    
                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label for="new_max_stake" class="form-label">Maximum Stake</label>
                            <input type="number" step="0.000001" class="form-control" id="new_max_stake" name="max_stake" required>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="submit" name="add_plan" class="btn btn-primary">Add Plan</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
$(document).ready(function(){
    $('#myTable').DataTable({
        dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]
    });
});
</script>

<?php include 'footer.php'; ?>