<?php
include("../db/cn.php");
ob_start();
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'];
}
//$cr_id = $_GET["cr_id"];
@$customer_name = $_GET["cr_name"];
//echo $cr_nickname;
$rec =  mysqli_query($connection,"select * from credit_note where cr_name = '$customer_name'  ");
$row = mysqli_fetch_array($rec);


if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST["edit_credits"])) {
    
    // Capture input data and validate/sanitize them
    $cr_name = mysqli_real_escape_string($connection, $_POST["cr_name1"]);
    $cr_nickname = mysqli_real_escape_string($connection, $_POST["cr_nickname"]);
    $cr_id = mysqli_real_escape_string($connection, $_POST["cr_id"]);
    $phone_number = mysqli_real_escape_string($connection, $_POST["phone_number"]);
    $cr_address = mysqli_real_escape_string($connection, $_POST["cr_address"]);
    
    // Check for $customer_name definition
    if (!isset($customer_name)) {
        die('Customer name is not defined');
    }

    // Prepare the query to prevent SQL injection
    $stmt = $connection->prepare("UPDATE credit_note 
                                  SET cr_name = ?, cr_nickname = ?, cr_address = ?, phone_number = ?, cr_id = ?
                                  WHERE cr_name = ?");
    
    // Bind parameters
    $stmt->bind_param("ssssss", $cr_name, $cr_nickname, $cr_address, $phone_number, $cr_id, $customer_name);
    
    // Execute the query          
    if ($stmt->execute()) {
        echo "<script type='text/javascript'>window.location.href = 'view_creditor.php';</script>";
    } else {
        echo "Error updating record: " . $stmt->error;
    }
    
    // Close the statement
    $stmt->close();
}
ob_end_flush();
?>

<!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> Edit-Customer Ledger Detail</title>

    <!-- Custom fonts for this template-->
    <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">

    <!-- Custom styles for this template-->
    <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;
		}
		
	/* Basic styling */
    .tab {
        display: none;
        padding: 20px;
    }
    .active {
        display: block;
    }
    .tab-buttons {
        display: flex;
    }
    .tab-button {
        cursor: pointer;
        padding: 10px 10px;
        background-color: #ffff;
        border: 1px solid #0A5064;
        border-radius: 5px 5px 0 0;
        margin-right: 5px;
		color:#212529;
    }
    .tab-button.active {
        background-color: #212529;
		color:#fff;
    }

    /* Background colors for tab content */
    #tab1 {
        background-color: #fff;
		color:#212529;
		padding:20px;
    }
    #tab2 {
        color:#212529;
		background-color: ;
    }
    #tab3 {
		color:#212529;
        background-color: ;
    }
  </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">
                <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">

                    <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                        <i class="fa fa-bars"></i>
                    </button>

                     <?php include('common/log.php');  ?>
                </nav>
<br />
<div class="container">
<div class="row">
<div class="col-sm-9">
<h3 style="color:#212529; font-weight:700;">Edit Customer Detail
</h3>
</div>
<div class="col-sm-3">
<a href="view_creditor.php"><button type="button" class="btn btn-dark"><i class="fa fa-arrow-left"></i></button></a>

</div>
</div>
<br />
<div class="container">
    <form action="credits_edit.php?cr_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>Customer ID:</label>
            <input type="text" name="cr_id" class="form-control" value="<?php echo $row["cr_id"]; ?>">
            </div>
           
            <div class="form-group">
                <label>Customer Name:</label>
            <input type="text" name="cr_name1" class="form-control" style="background:#0A4657;color:#fff;" value="<?php echo $row["cr_name"]; ?>" readonly>
            </div>
            
            <!--<div class="form-group">-->
            <!--    <label>Creditor Nick Name:</label>-->
            <!--<input type="text" name="cr_nickname" class="form-control" value="<?php echo $row["cr_nickname"]; ?>">-->
            
            <!--</div>-->
        </div>
        <div class="col-sm-5">
            <div class="form-group">
                <label>Customer Address:</label>
            <input type="text" name="cr_address" class="form-control" value="<?php echo $row["cr_address"]; ?>">
            </div>
            
            <div class="form-group">
                <label>Phone Number:</label>
            <input type="text" name="phone_number" class="form-control" value="<?php echo $row["phone_number"]; ?>">
            </div>
            <br>
            <button type="button" id="updateBtn" class="btn btn-dark btn-sm form-control">Update Customer</button>
        </div>
        <div class="col-sm-1"></div>
        
    </div>
    </form>
</div>
</div>   



            </div>
            <br><br>
            <!-- End of Main Content -->

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

        </div>
        <!-- End of Content Wrapper -->

    </div>
    <a class="scroll-to-top rounded" href="#page-top">
        <i class="fas fa-angle-up"></i>
    </a>

    <!-- Logout Modal-->
    <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 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('updateBtn').addEventListener('click', function () {
    Swal.fire({
        title: 'Are you sure?',
        text: "Do you want to update this customer?",
        icon: 'question',
        showCancelButton: true,
        confirmButtonColor: '#0A4657',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, update it!'
    }).then((result) => {
        if (result.isConfirmed) {
            // Agar confirm kare to form submit hoga
            document.querySelector('form').submit();
        }
    });
});
</script>
</body>

</html>