<?php
ob_start();
include("../db/cn.php");
include("common/tx.php");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL); 
 
if(isset($_SESSION['user_name']) && isset($_SESSION['user_role'])&& isset($_SESSION['outlet_name'])
&& isset($_SESSION['outlet_address'])) {
    $userName = $_SESSION['user_name'];
    $userRole = $_SESSION['user_role'];
	$outlet_address = $_SESSION['outlet_address'];
	$outlet_name = $_SESSION['outlet_name'];
}
@$customer_name = $_GET["supplier_name"];
$rec =  mysqli_query($connection,"select * from supplier where supplier_name = '$customer_name'  ");
$row = mysqli_fetch_array($rec);

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["submit2"])) {

    $supplier_name     = $_POST["supplier_name"];
    $supplier_id       = $_POST["supplier_id"];
    $supplier_company  = $_POST["supplier_company"];
    $supplier_category = $_POST["supplier_category"];
    $supplier_product  = $_POST["supplier_product"];
    $openAmount        = $_POST["opening_balance"];
    $date_time         = date("Y-m-d");
    $supplier_phone    = $_POST["supplier_phone"];

    // Update opening amount
    mysqli_query(
        $connection,
        "UPDATE supplier_opening_amount 
         SET open_amount = '$openAmount' 
         WHERE supplier_name = '$supplier_name'"
    );

    // Update supplier
    $query = "
        UPDATE supplier SET
            supplier_name     = '$supplier_name',
            supplier_company  = '$supplier_company',
            supplier_product  = '$supplier_product',
            supplier_phone    = '$supplier_phone',
            supplier_category = '$supplier_category',
            date_time         = '$date_time',
            user_name         = '$userName',
            user_role         = '$userRole',
            outlet_name       = '$outlet_name',
            outlet_address    = '$outlet_address'
        WHERE supplier_id = '$supplier_id'
    ";

    if (mysqli_query($connection, $query)) {
        echo '<script type="text/javascript">window.location.href="supplier_view.php";</script>';
        exit();
    } else {
        echo "Error: " . mysqli_error($connection);
    }
}

?>
<!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, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title><?php echo $tx; ?></title>
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@200;300;400;500;600;700&display=swap" rel="stylesheet">
    <link href="../css/sb-admin-2.min.css" rel="stylesheet">
    <link href="../css/sb-admin-2.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<style>
body {
    font-family: 'Quicksand', sans-serif;
    
}
		    .chart-container {
      position: relative;
      background: rgba(255, 255, 255, 0.9);
      border-radius: 10px;
      padding: 15px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    }

    canvas {
      max-width: 100%;
      height: auto;
    }
.bg-gradient-primary{
    
	background:#0A4657;
	color:#fff;	
	
	}

.btn-primary{
	background:#0A4657;
	color:#fff;
	 border:1px #0A4657 solid;
	
	}  
.btn-primary:hover{
  
  background:#fff;
  border:1px #0A4657 solid;
	color:#0A4657;
	
	}	 
    .chart-container {
      position: relative;
      background: rgba(255, 255, 255, 0.9);
      border-radius: 10px;
      padding: 15px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    }

    canvas {
      max-width: 100%;
      height: auto;
    }
	
	.fa-download:hover{
		color:#953E39;
		}
		
.btn, .form-control{
	font-weight:700;  	
		}
  </style>

<body id="page-top">

    <!-- Page Wrapper -->
    <div id="wrapper">

        <!-- Sidebar -->
        <?php
        include("common/sd.php");
		?>
        <!-- End of Sidebar -->

        <!-- Content Wrapper -->
        <div id="content-wrapper" class="d-flex flex-column">

            <!-- Main Content -->
            <div id="content">

                <!-- Topbar -->
                <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">

                    <!-- Sidebar Toggle (Topbar) -->
                    <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                        <i class="fa fa-bars"></i>
                    </button>

                    <!-- Topbar Search -->
                    
  <?php include('common/log.php');  ?>

                    <!-- Topbar Navbar -->
                    
                </nav>
                <!-- End of Topbar -->

                <div class="container">
<div class="row">
<div class="col-sm-9">
<h3 style="color:#212529; font-weight:700;">Edit Supplier Detail
</h3>
</div>
<div class="col-sm-3">
<a href="supplier_view.php"><button type="button" class="btn btn-dark" style="float:right;"><i class="fa fa-arrow-left"></i></button></a>

</div>
</div>
<br />
<div class="container">
    <form action="supplier_edit.php?supplier_name=<?php echo $customer_name; ?>" method="post">
    <div class="row">
        
        <div class="col-sm-1"></div>
        <div class="col-sm-5">
            <div class="form-group">
            <label>Supplier ID:</label>
            <input type="text" name="supplier_id" class="form-control" value="<?php echo $row["supplier_id"]; ?>">
            </div>
           <?php 
            $supplierName = $row["supplier_name"];
            $sup = mysqli_query($connection,"select * from supplier_opening_amount where supplier_name = '$supplierName'");
            $sp = mysqli_fetch_array($sup);
            
           ?>
            <div class="form-group">
                <label>Supplier Name:</label>
            <input type="text" name="supplier_name" class="form-control" style="background:#0A4657;color:#fff;" 
            value="<?php echo $row["supplier_name"]; ?>" readonly>
            </div>
            
            
             <div class="form-group">
                <label>Opening Balance:</label>
            <input type="text" name="opening_balance" class="form-control" 
            value="<?php echo $sp['open_amount']; ?>" >
            </div>
            <!--<div class="form-group">-->
            <!--    <label>Supplier Company:</label>-->
            <!--<input type="text" name="supplier_company" class="form-control" value="<?php echo $row["supplier_company"]; ?>">-->
            
            <!--</div>-->
        </div>
        <div class="col-sm-5">
            <div class="form-group">
                <label>Supplier Address:</label>
            <input type="text" name="supplier_category" class="form-control" value="<?php echo $row["supplier_category"]; ?>">
            </div>
            
            <div class="form-group">
                <label>Supplier Phone:</label>
            <input type="text" name="supplier_phone" class="form-control" value="<?php echo $row["supplier_phone"]; ?>">
            </div>
            <br>
            <button type="submit" id="updateSupplierBtn" name="submit2" class="btn btn-dark btn-sm form-control">Update Supplier</button>
        </div>
        <div class="col-sm-1"></div>
        
    </div>
    </form>
</div>
</div>

    
            </div>
            <br><br>
            <?php include("common/main_ft.php");  ?>
        </div>
    </div>
    <a class="scroll-to-top rounded" href="#page-top">
        <i class="fas fa-angle-up"></i>
    </a>
    <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
                    <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                    <a class="btn btn-primary" href="">Logout</a>
                </div>
            </div>
        </div>
    </div>
<script>
    document.addEventListener("DOMContentLoaded", function() {
    document.getElementById("search-input").addEventListener("input", function() {
        var searchText = this.value.toLowerCase();
        var rows = document.querySelectorAll(".product-row");

        rows.forEach(function(row) {
            var productName = row.querySelector("td:nth-child(6)").textContent.toLowerCase();
            if (productName.includes(searchText)) {
                row.style.display = "table-row";
            } else {
                row.style.display = "none";
            }
        });
    });
});
</script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js"></script>
    <script src="../vendor/jquery/jquery.min.js"></script>
    <script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="../vendor/jquery-easing/jquery.easing.min.js"></script>
    <script src="../js/sb-admin-2.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
document.getElementById('updateSupplierBtn').addEventListener('click', function () {
    Swal.fire({
        title: 'Are you sure?',
        text: "Do you want to update this supplier?",
        icon: 'question',
        showCancelButton: true,
        confirmButtonColor: '#0A4657',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, update it!'
    }).then((result) => {
        if (result.isConfirmed) {
            document.querySelector('form').submit(); // ✅ Form submit hoga
        }
    });
});
</script>
</body>

</html>