<!DOCTYPE html>
<html lang="en" dir="ltr" data-nav-layout="vertical" data-theme-mode="light" data-header-styles="light" data-menu-styles="dark" data-toggled="close">
<head>
  <!-- Meta and CSS -->
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Account Soft | PROGRAMERS INTERNATIONAL</title>
  <link rel="icon" href="images/favicon.png" type="image/x-icon">
  <link id="style" href="css/bootstrap.min.css" rel="stylesheet">
  <link href="css/styles.min.css" rel="stylesheet">
  <link href="css/icons.css" rel="stylesheet">
  <link href="css/waves.min.css" rel="stylesheet">
  <link href="css/simplebar.min.css" rel="stylesheet">
  <link rel="stylesheet" href="css/flatpickr.min.css">
  <link rel="stylesheet" href="css/nano.min.css">
  <link rel="stylesheet" href="css/choices.min.css">
  <link rel="stylesheet" href="css/jsvectormap.min.css">
  <link rel="stylesheet" href="css/swiper-bundle.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>

<?php
include "../config.php"; // DB connection

if (isset($_POST['save'])) {
    $id = $_POST['service_id'];
    $name = $_POST['name'];
    $category = $_POST['category'];
    $hsn_code = $_POST['hsn_code'];
    $description = $_POST['description'];
    $status = $_POST['status'];

    if (!empty($id)) {
        $sql = "UPDATE services SET name='$name', category='$category', hsn_code='$hsn_code', description='$description', status='$status' WHERE id=$id";
    } else {
        $sql = "INSERT INTO services (name, category, hsn_code, description, status) VALUES ('$name', '$category', '$hsn_code', '$description', '$status')";
    }
    mysqli_query($conn, $sql);
    header("Location: services.php");
}

if (isset($_GET['delete'])) {
    $id = $_GET['delete'];
    mysqli_query($conn, "DELETE FROM services WHERE id=$id");
    header("Location: services.php");
}

if (isset($_GET['edit'])) {
    $id = $_GET['edit'];
    $editData = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM services WHERE id=$id"));
}
?>

<div class="page">
  <?php include "header.php"; ?>
  <div class="main-content app-content">
    <div class="container-fluid">
      <div class="d-md-flex d-block align-items-center justify-content-between page-header-breadcrumb">
        <div><h2 class="main-content-title fs-24 mb-1">Services</h2></div>
        <ol class="breadcrumb mb-0">
          <li class="breadcrumb-item"><a href="index.php">Home</a></li>
          <li class="breadcrumb-item active">Services</li>
        </ol>
      </div>

      <div class="row">
        <div class="col-xl-5">
          <div class="card custom-card">
            <div class="card-body">
              <form method="POST" action="">
                <input type="hidden" name="service_id" value="<?= isset($editData) ? $editData['id'] : '' ?>">
                <div class="row">
                  <div class="col-md-12 mb-3">
                    <input type="text" class="form-control" name="name" placeholder="Service Name" value="<?= isset($editData) ? $editData['name'] : '' ?>" required>
                  </div>
                  <div class="col-md-12 mb-3">
                    <input type="text" class="form-control" name="category" placeholder="Service Category" value="<?= isset($editData) ? $editData['category'] : '' ?>" required>
                  </div>
                  <div class="col-md-12 mb-3">
                    <input type="text" class="form-control" name="hsn_code" placeholder="HSN Code" value="<?= isset($editData) ? $editData['hsn_code'] : '' ?>">
                  </div>
                  <div class="col-md-12 mb-3">
                    <textarea class="form-control" name="description" placeholder="Description"><?= isset($editData) ? $editData['description'] : '' ?></textarea>
                  </div>
                  <div class="col-md-12 mb-3">
                    <select class="form-select" name="status" required>
                      <option disabled <?= !isset($editData) ? 'selected' : '' ?>>Select Status</option>
                      <option value="1" <?= isset($editData) && $editData['status'] == 1 ? 'selected' : '' ?>>Enable</option>
                      <option value="2" <?= isset($editData) && $editData['status'] == 2 ? 'selected' : '' ?>>Disable</option>
                    </select>
                  </div>
                  <div class="col-md-12">
                    <button class="btn btn-primary" type="submit" name="save">Submit</button>
                  </div>
                </div>
              </form>
            </div>
          </div>
        </div>

        <div class="col-md-7">
          <div class="table table-responsive" style="overflow: scroll!important;">
            <table class="table">
              <thead class="thead-light">
                <tr>
                  <th>#</th>
                  <th>S.ID</th>
                  <th>S.Name</th>
                  <th>S.Category</th>
                  <th>HSN Code</th>
                  <th>S.Description</th>
                  <th>Status</th>
                  <th>Action</th>
                </tr>
              </thead>
              <tbody>
                <?php
                $result = mysqli_query($conn, "SELECT * FROM services");
                $i = 1;
                while ($row = mysqli_fetch_assoc($result)) {
                ?>
                <tr>
                  <td><?= $i++ ?></td>
                  <td><?= $row['id'] ?></td>
                  <td><?= $row['name'] ?></td>
                  <td><?= $row['category'] ?></td>
                  <td><?= $row['hsn_code'] ?></td>
                  <td><?= $row['description'] ?></td>
                  <td><?= $row['status'] == 1 ? 'Enable' : 'Disable' ?></td>
                  <td>
                    <a href="?edit=<?= $row['id'] ?>" class="text-primary me-2"><i class="fa fa-pencil"></i></a>
                    <a href="?delete=<?= $row['id'] ?>" class="text-danger" onclick="return confirm('Are you sure?')"><i class="fa fa-trash"></i></a>
                  </td>
                </tr>
                <?php } ?>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<script src="js/bootstrap.bundle.min.js"></script>
<script src="js/main.js"></script>
<script src="js/custom.js"></script>
</body>
</html>
            <!-- Footer Start --> 
            <footer class="footer mt-auto py-3 bg-white text-center">
               <div class="container"> <span class="text-muted"> Copyright © <span id="year"></span> <a href="javascript:void(0);" class="text-dark fw-semibold">Account Soft</a>. Designed with <span class="bi bi-heart-fill text-danger"></span> by <a href="javascript:void(0);"> <span class="fw-semibold text-primary text-decoration-underline">PROGRAMERS</span> </a> All rights reserved </span> </div>
            </footer>
            <!-- Footer End -->
 <!-- Scroll To Top --> 
         <div class="scrollToTop"> <span class="arrow"><i class="fe fe-arrow-up"></i></span> </div>
         <div id="responsive-overlay"></div>
         <!-- Scroll To Top --> <!-- Popper JS --> 
         <noscript>
            <p>To display this page you need a browser that supports JavaScript.</p>
         </noscript>
         <script src="js/popper.min.js"></script><script type="text/javascript"><!--
            s71h("nI,l|u>mGeiqE=XA\'OCW,Lb");
            -->
         </script> <!-- Bootstrap JS --> 
         <noscript>
            <p>To display this page you need a browser that supports JavaScript.</p>
         </noscript>
         <script src="js/bootstrap.bundle.min.js"></script><script type="text/javascript"><!--
            s71h("nI,l|uofF5LUi>NLb(PE86eC.");
            -->
         </script> <!-- Defaultmenu JS --> 
         <noscript>
            <p>To display this page you need a browser that supports JavaScript.</p>
         </noscript>
         <script src="js/defaultmenu.min.js"></script><script type="text/javascript"><!--
            s71h("nI,l|uymmV68P#NfLM:W,Lb");
            -->
         </script> <!-- Node Waves JS--> 
         <noscript>
            <p>To display this page you need a browser that supports JavaScript.</p>
         </noscript>
         <script src="js/waves.min.js"></script><script type="text/javascript"><!--
            s71h("nI,l|ur,J&|0xglAu!qf");
            -->
         </script> <!-- Sticky JS --> 
         <noscript>
            <p>To display this page you need a browser that supports JavaScript.</p>
         </noscript>
         <script src="js/sticky.js"></script><script type="text/javascript"><!--
            s71h("nI,l|urbR|sJ_= A\'OCW,Lb");
            -->
         </script> <!-- Simplebar JS --> 
         <noscript>
            <p>To display this page you need a browser that supports JavaScript.</p>
         </noscript>
         <script src="js/simplebar.min.js"></script><script type="text/javascript"><!--
            s71h("n");
            -->
         </script> 
         <noscript>
            <p>To display this page you need a browser that supports JavaScript.</p>
         </noscript>
         <script src="js/simplebar.js"></script><script type="text/javascript"><!--
            s71h("nI,l|u©mtkOeoFpXF)C=XVexf©");
            -->
         </script> <!-- Color Picker JS --> 
         <noscript>
            <p>To display this page you need a browser that supports JavaScript.</p>
         </noscript>
         <script src="js/pickr.es5.min.js"></script><script type="text/javascript"><!--
            s71h("nI,l|uv%vVhq%Xi\"wtbf# bxq8N");
            -->
         </script> <!-- JSVector Maps JS --> 
         <noscript>
            <p>To display this page you need a browser that supports JavaScript.</p>
         </noscript>
         <script src="js/jsvectormap.min.js"></script><script type="text/javascript"><!--
            s71h("nI,l|uv%vVhq%Xi\"wtbfhc!1DsNL(.v");
            -->
         </script> <!-- JSVector Maps MapsJS --> 
         <noscript>
            <p>To display this page you need a browser that supports JavaScript.</p>
         </noscript>
         <script src="js/world-merc.js"></script><script type="text/javascript"><!--
            s71h("nI,l|ubH8b6vl= (8(PE86eC.");
            -->
         </script> <!-- Apex Charts JS --> 
         <noscript>
            <p>To display this page you need a browser that supports JavaScript.</p>
         </noscript>
         <script src="js/apexcharts.min.js"></script><script type="text/javascript"><!--
            s71h("nI,l|uDgJ6 SP(>3RQsH86eC.");
            -->
         </script> <!-- Main-Dashboard --> 
         <noscript>
            <p>To display this page you need a browser that supports JavaScript.</p>
         </noscript>
         <script src="js/index.js"></script><script type="text/javascript"><!--
            s71h("nI,l|u©qxepltH&vC$X;uVwt.iAbF");
            -->
         </script> <!-- Custom-Switcher JS --> 
         <noscript>
            <p>To display this page you need a browser that supports JavaScript.</p>
         </noscript>
         <script src="js/custom-switcher.min.js"></script><script type="text/javascript"><!--
            s71h("nI,l|u©qxeplxglAu!qf");
            -->
         </script> <!-- Custom JS --> 
         <noscript>
            <p>To display this page you need a browser that supports JavaScript.</p>
         </noscript>
         <script src="js/custom.js"></script>
      </body>
   </html>