<?php
include 'config.php';


$company = [];
$edit_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$edit_mode = false;

if ($edit_id > 0) {
    $edit_result = $con->query("SELECT * FROM companies WHERE id=$edit_id");
    if ($edit_result && $edit_result->num_rows > 0) {
        $company = $edit_result->fetch_assoc();
        $edit_mode = true;
    }
}

// }

// --- INSERT / UPDATE LOGIC ---
if (isset($_POST['submit'])) {
    $company_name   = $_POST['company_name'];
    $address        = $_POST['address'];
    $contact_person = $_POST['contact_person'];
    $contact_number1= $_POST['contact_number1'];
    $contact_number2= $_POST['contact_number2'];
    $email          = $_POST['email'];
    $district       = $_POST['district'];
    $state          = $_POST['state'];
    $country        = $_POST['country'];

    // Handle logo
    $logo_name = $edit_mode ? $company['logo'] : "";
    if (isset($_FILES['logo']) && $_FILES['logo']['error'] == 0) {
        $target_dir = "uploads/";
        if (!file_exists($target_dir)) {
            mkdir($target_dir, 0777, true);
        }
        $logo_name = time() . "_" . basename($_FILES['logo']['name']);
        $target_file = $target_dir . $logo_name;

        if ($_FILES['logo']['size'] <= 51200) {
            move_uploaded_file($_FILES['logo']['tmp_name'], $target_file);
        } else {
            echo "<p style='color:red;'>Logo too large. Max size is 50kb.</p>";
            $logo_name = $edit_mode ? $company['logo'] : "";
        }
    }

    if ($edit_mode) {
        // Update
        $sql = "UPDATE companies SET 
                company_name='$company_name',
                address='$address',
                contact_person='$contact_person',
                contact_number1='$contact_number1',
                contact_number2='$contact_number2',
                email='$email',
                district='$district',
                state='$state',
                country='$country',
                logo='$logo_name'
                WHERE id=$edit_id";
        if ($con->query($sql)) {
            header("Location: add_company.php?success=updated"); 
            exit;
        }
    } else {
        // Insert
        $sql = "INSERT INTO companies 
                (company_name, address, contact_person, contact_number1, contact_number2, email, district, state, country, logo) 
                VALUES 
                ('$company_name', '$address', '$contact_person', '$contact_number1', '$contact_number2', '$email', '$district', '$state', '$country', '$logo_name')";
        if ($con->query($sql)) {
            header("Location: add_company.php?success=added"); 
            exit;
        }
    }
}

// --- FETCH TABLE DATA ---
$sql = "SELECT * FROM companies ORDER BY id DESC";
$result = $con->query($sql);
?>



<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Pipscart</title>
    <!-- plugins:css -->
    <link rel="stylesheet" href="assets/vendors/mdi/css/materialdesignicons.min.css">
    <link rel="stylesheet" href="assets/vendors/ti-icons/css/themify-icons.css">
    <link rel="stylesheet" href="assets/vendors/css/vendor.bundle.base.css">
    <link rel="stylesheet" href="assets/vendors/font-awesome/css/font-awesome.min.css">
    <!-- endinject -->
    <!-- Plugin css for this page -->
    <link rel="stylesheet" href="assets/vendors/font-awesome/css/font-awesome.min.css" />
    <link rel="stylesheet" href="assets/vendors/bootstrap-datepicker/bootstrap-datepicker.min.css">
    <!-- End plugin css for this page -->
    <!-- inject:css -->
    <!-- endinject -->
    <!-- Layout styles -->
    <link rel="stylesheet" href="assets/css/style.css">
    <!-- End layout styles -->
    <!-- <link rel="shortcut icon" href="assets/images/favicon.png" /> -->
  </head>
  <body>
    <div class="container-scroller">
      <!-- partial:partials/_navbar.html -->
      <!-- partial:partials/_navbar.html -->
         <?php
            $title = "Php Header Footer";
            include "header.php";
            ?>
         <!-- partial -->
         <div class="container-fluid page-body-wrapper">
            <!-- partial:partials/_sidebar.html -->
            <?php
               $title = "Php Header Footer";
               include "sidebar.php";
               ?>
            <!-- partial -->
        <!-- partial -->
        <div class="main-panel">
          <div class="content-wrapper">
            <div class="row">
              <div class="col-md-4">
                <div class="card">
                  <div class="card-body">
                    <h4 class="card-title">Add Company</h4>
                    <form class="forms-sample" method="POST" action="" enctype="multipart/form-data">
                    <div class="form-group">
                        <input type="text" name="company_name" class="form-control" 
                              value="<?= $edit_mode ? htmlspecialchars($company['company_name']) : '' ?>" 
                              placeholder="Company Name" required>
                    </div>
                    <div class="form-group">
                        <textarea name="address" class="form-control" rows="4" placeholder="Address" required><?= $edit_mode ? htmlspecialchars($company['address']) : '' ?></textarea>
                    </div>
                    <div class="form-group">
                        <input type="text" name="contact_person" class="form-control" 
                              value="<?= $edit_mode ? htmlspecialchars($company['contact_person']) : '' ?>" 
                              placeholder="Contact Person" required>
                    </div>
                    <div class="form-group">
                        <input type="text" name="contact_number1" class="form-control" 
                              value="<?= $edit_mode ? htmlspecialchars($company['contact_number1']) : '' ?>" 
                              placeholder="Contact Number 1" required>
                    </div>
                    <div class="form-group">
                        <input type="text" name="contact_number2" class="form-control" 
                              value="<?= $edit_mode ? htmlspecialchars($company['contact_number2']) : '' ?>" 
                              placeholder="Contact Number 2">
                    </div>
                    <div class="form-group">
                        <input type="email" name="email" class="form-control" 
                              value="<?= $edit_mode ? htmlspecialchars($company['email']) : '' ?>" 
                              placeholder="Email Address" required>
                    </div>
                    <div class="form-group">
                        <input type="text" name="district" class="form-control" 
                              value="<?= $edit_mode ? htmlspecialchars($company['district']) : '' ?>" 
                              placeholder="District">
                    </div>
                    <div class="form-group">
                        <input type="text" name="state" class="form-control" 
                              value="<?= $edit_mode ? htmlspecialchars($company['state']) : '' ?>" 
                              placeholder="State">
                    </div>
                    <div class="form-group">
                        <input type="text" name="country" class="form-control" 
                              value="<?= $edit_mode ? htmlspecialchars($company['country']) : '' ?>" 
                              placeholder="Country">
                    </div>
                    <div class="form-group">
                        <small>Logo</small><br>
                        <?php if ($edit_mode && !empty($company['logo'])): ?>
                            <img src="uploads/<?= $company['logo'] ?>" width="60" style="margin-bottom:10px;"><br>
                        <?php endif; ?>
                        <input type="file" name="logo" class="form-control">
                        <small style="float: right;color: red;font-size: 12px;">*File size upto 50kb</small>
                    </div>
                    <button type="submit" name="submit" class="btn btn-gradient-primary w-100">
                        <?= $edit_mode ? 'Update' : 'Submit' ?>
                    </button>
                </form>

                  </div>
                </div>
              </div>
              <div class="col-md-8">
                <div class="card">
                  <div class="card-body">
                    <div class="div d-flex justify-content-between">
                    <h4 class="card-title">COMPANY</h4>
                   
                  </div>
                    
                    <div class="table-responsive">
  
                    <!-- Display Companies Table -->
<table class="table">
    <thead>
        <tr>
            <th>Co.Name</th>
            <th>Address</th>
            <th>Contact Person</th>
            <th>Contact No 1</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <?php if ($result->num_rows > 0): ?>
            <?php while($row = $result->fetch_assoc()): ?>
                <tr>
                    <td><?= htmlspecialchars($row['company_name']) ?></td>
                    <td><?= htmlspecialchars($row['address']) ?></td>
                    <td><?= htmlspecialchars($row['contact_person']) ?></td>
                    <td><?= htmlspecialchars($row['contact_number1']) ?></td>
                    <td>
                        <span style="display:flex;gap: 5px;align-items: center;">
                            <a href="add_company.php?id=<?= $row['id'] ?>"><i class="fa fa-edit text-warning"></i></a>
                            <!-- Delete -->
                            <!-- <a href="add_company.php?delete=<?= $row['id'] ?>" 
                              onclick="return confirm('Are you sure you want to delete this company?');">
                              <i class="fa fa-trash-o text-danger"></i>
                            </a> -->
                        </span>
                    </td>
                </tr>
            <?php endwhile; ?>
        <?php else: ?>
            <tr><td colspan="5" style="text-align:center;">No companies found</td></tr>
        <?php endif; ?>
    </tbody>
</table>
</div>
<!-- <nav aria-label="Page navigation example">
  <ul class="pagination">
    <li class="page-item"><a class="page-link" href="#"><i class="fa fa-angle-left"></i></a></li>
    <li class="page-item"><a class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
    <li class="page-item"><a class="page-link" href="#">3</a></li>
    <li class="page-item"><a class="page-link" href="#"><i class="fa fa-angle-right"></i>  </a></li>
  </ul>
</nav> -->
                  </div>
                </div>
              </div>
            </div>
            <div class="col-md-8"></div>
          </div>
          <!-- content-wrapper ends -->
          <!-- partial:partials/_footer.html -->
          <?php
         $title = "Php Header Footer";
         include "footer.php";
         ?>
          <!-- partial -->
        </div>
        <!-- main-panel ends -->
      </div>
      <!-- page-body-wrapper ends -->
    </div>
    <style>
    .hover-preview-container {
      position: relative;
      display: inline-block;
    }
.table th img, .table td img {
    width: 36px;
    height: 36px;
    border-radius: 0!important;
}
    .hover-image {
      display: none;
      position: absolute;
      bottom: 100%; /* puts image above the link */
      left: 0;
      margin-bottom: 10px;
      width: 200px;
      border: 1px solid #ccc;
      background: #fff;
      box-shadow: 0 0 10px rgba(0,0,0,0.2);
      z-index: 1000;
    }

    .hover-preview-container:hover .hover-image {
      display: block;
    }
  </style>
    <!-- container-scroller -->
    <!-- plugins:js -->
    <script src="assets/vendors/js/vendor.bundle.base.js"></script>
    <!-- endinject -->
    <!-- Plugin js for this page -->
    <script src="assets/vendors/chart.js/chart.umd.js"></script>
    <script src="assets/vendors/bootstrap-datepicker/bootstrap-datepicker.min.js"></script>
    <!-- End plugin js for this page -->
    <!-- inject:js -->
    <script src="assets/js/off-canvas.js"></script>
    <script src="assets/js/misc.js"></script>
    <script src="assets/js/settings.js"></script>
    <script src="assets/js/todolist.js"></script>
    <script src="assets/js/jquery.cookie.js"></script>
    <!-- endinject -->
    <!-- Custom js for this page -->
    <script src="assets/js/dashboard.js"></script>
    <!-- End custom js for this page -->
  </body>
</html>