<?php
include 'branch_connection.php';
session_start();
if (!isset($_SESSION['userid'])) {
    header("Location: ../index.php");
    exit();
}

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$userid = $_SESSION['userid'];
$message = "";

// Handle delete
if (isset($_GET['delete_id'])) {
    $delete_id = $_GET['delete_id'];
    $sql = "DELETE FROM tb_terms_master WHERE id='$delete_id'";
    if (mysqli_query($conn, $sql)) {
        $message = "Deleted successfully.";
    } else {
        $message = "Error deleting record.";
    }
    header("Location: terms-condition.php?msg=" . urlencode($message));
    exit;
}

// Handle edit retrieval
$editMode = false;
$editRemarks = "";
$editStatus = "";
$editId = 0;

if (isset($_GET['edit_id'])) {
    $editId = $_GET['edit_id'];
    $result = mysqli_query($conn, "SELECT * FROM tb_terms_master WHERE id='$editId'");
    if ($row = mysqli_fetch_assoc($result)) {
        $editRemarks = $row['remarks'];
        $editStatus = $row['status'];
        $editMode = true;
    }
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $remarks = mysqli_real_escape_string($conn, $_POST['remarks']);
    $status = mysqli_real_escape_string($conn, $_POST['status']);
    $edit_id = $_POST['edit_id'];

    if ($edit_id > 0) {
        $sql = "UPDATE tb_terms_master SET remarks='$remarks', status='$status' WHERE id='$edit_id'";
        if (mysqli_query($conn, $sql)) {
            $message = "Updated successfully.";
        } else {
            $message = "Error updating record.";
        }
    } else {
        $sql = "INSERT INTO tb_terms_master (remarks, status) VALUES ('$remarks', '$status')";
        if (mysqli_query($conn, $sql)) {
            $message = "Added successfully.";
        } else {
            $message = "Error adding record.";
        }
    }
    header("Location: terms-condition.php?msg=" . urlencode($message));
    exit;
}

if (isset($_GET['msg'])) {
    $message = htmlspecialchars($_GET['msg']);
}

$result = mysqli_query($conn, "SELECT * FROM tb_terms_master ORDER BY id DESC");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Leads Programers International</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="author" content="DexignLab">
    <meta name="robots" content="index, follow">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" type="image/png" href="assets/images/favicon.png">
    <link href="assets/vendor/bootstrap-select/dist/css/bootstrap-select.min.css" rel="stylesheet">
    <link href="assets/vendor/bootstrap-datepicker-master/css/bootstrap-datepicker.min.css" rel="stylesheet">
    <link href="assets/vendor/owl-carousel/owl.carousel.css" rel="stylesheet">
    <link href="assets/css/jquery.localizationTool.css" rel="stylesheet">
    <link class="main-css" href="assets/css/style.css" rel="stylesheet">
</head>
<body>
    <div id="preloader">
        <div class="lds-ripple"><div></div><div></div></div>
    </div>
    <div id="main-wrapper">
        <div class="nav-header">
            <a href="index.html" class="brand-logo"><img src="assets/images/logo-02.svg" alt=""></a>
            <div class="nav-control"><div class="hamburger"><span class="line"></span><span class="line"></span><span class="line"></span></div></div>
        </div>
        <div class="header">
            <div class="header-content">
                <nav class="navbar navbar-expand">
                    <div class="collapse navbar-collapse justify-content-between">
                        <div class="header-left">
                            <div class="dashboard_bar">Add Terms & Condition</div>
                        </div>
                        <ul class="navbar-nav header-right">
                            <li class="nav-item dropdown notification_dropdown">
                                <a class="nav-link bell dlab-theme-mode p-0" href="javascript:void(0);">
                                    <i id="icon-light" class="fas fa-sun"></i>
                                    <i id="icon-dark" class="fas fa-moon"></i>
                                </a>
                            </li>
                            <?php include "logout.php"; ?>
                        </ul>
                    </div>
                </nav>
            </div>
        </div>
        <?php include "header.php"; ?>
        <div class="content-body">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-xl-12">
                        <div class="row">
                            <div class="col-xl-5">
                                <div class="card">
                                    <div class="card-body">
                                        <div class="basic-form">
                                            <form method="POST" action="">
                                                <input type="hidden" name="edit_id" value="<?php echo $editMode ? $editId : 0; ?>">
                                                <div class="row">
                                                    <div class="mb-2 col-md-12">
                                                        <textarea name="remarks" class="form-control" placeholder="Add terms & conditions" required rows="3"><?php echo $editMode ? htmlspecialchars($editRemarks) : ''; ?></textarea>
                                                    </div>
                                                    <div class="mb-2 col-md-12">
                                                        <select id="inputStatus" class="default-select form-control wide" name="status" required>
                                                            <option value="" disabled <?php echo !$editMode ? 'selected' : ''; ?>>Select Status</option>
                                                            <option value="Active" <?php echo ($editMode && $editStatus == 'Active') ? 'selected' : ''; ?>>Active</option>
                                                            <option value="Inactive" <?php echo ($editMode && $editStatus == 'Inactive') ? 'selected' : ''; ?>>Inactive</option>
                                                        </select>
                                                    </div>
                                                </div>
                                                <button type="submit" class="btn btn-primary w-100"><?php echo $editMode ? 'Update' : 'Submit'; ?></button>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-xl-7">
                                <div class="table-responsive">
                                    <table class="table table-hover table-responsive-sm">
                                        <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>Terms & Condition</th>
                                                <th>Status</th>
                                                <th style="text-align:left!important;">Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $sl = 1;
                                            while ($row = mysqli_fetch_assoc($result)) {
                                                echo "<tr>
                                                    <th>{$sl}</th>
                                                    <td>{$row['remarks']}</td>
                                                    <td>{$row['status']}</td>
                                                    <td style='text-align:center;'>
                                                        <span class='d-flex' style='gap:2px;width:100%;text-align:center;'>
                                                            <a href='terms-condition.php?edit_id={$row['id']}' title='Edit' class='text-warning me-3'><i class='fa fa-edit'></i></a>
                                                            <a href='terms-condition.php?delete_id={$row['id']}' title='Delete' style='color:#dc3545;' onclick=\"return confirm('Are you sure you want to delete this?');\"><i class='fa fa-trash'></i></a>
                                                        </span>
                                                    </td>
                                                </tr>";
                                                $sl++;
                                            }
                                            ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php include "footer.php"; ?>
    </div>

    <script src="assets/vendor/global/global.min.js"></script>
    <script src="assets/vendor/bootstrap-select/dist/js/bootstrap-select.min.js"></script>
    <script src="assets/vendor/bootstrap-datepicker-master/js/bootstrap-datepicker.min.js"></script>
    <script src="assets/vendor/apexchart/apexchart.js"></script>
    <script src="assets/vendor/chartjs/chart.bundle.min.js"></script>
    <script src="assets/vendor/peity/jquery.peity.min.js"></script>
    <script src="assets/js/dashboard/dashboard-1.js"></script>
    <script src="assets/vendor/owl-carousel/owl.carousel.js"></script>
    <script src="assets/js/jquery.localizationTool.js"></script>
    <script src="assets/js/translator.js"></script>
    <script src="assets/js/custom.min.js"></script>
    <script src="assets/js/dlabnav-init.js"></script>
    <script src="assets/js/demo.js"></script>
</body>
</html>

<?php
$conn->close();
?>
