  <?php 
include 'includes/config.php';
session_start();

if (!isset($_SESSION['email'])) {
    header('location:login.php');
    exit();
}

$successMessage = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Retrieve form data
    $contact_id = $_POST['contact_id'] ?? '';
    $date = $_POST['date'] ?? '';
    $customer_image = $_FILES['customer_image']['name'] ?? '';
    $name = $_POST['name'] ?? '';
    $father_name = $_POST['father_name'] ?? '';
    $acc_no = $_POST['acc_no'] ?? '';
    $cnic = $_POST['cnic'] ?? '';
    $dob = $_POST['dob'] ?? '';
    $check_no = $_POST['check_no'] ?? '';
    $reference_no = $_POST['reference_no'] ?? '';
    $status = $_POST['status'] ?? '';
    $home_address = $_POST['home_address'] ?? '';
    $phone_no = $_POST['phone_no'] ?? '';
    $house_type = $_POST['house_type'] ?? '';
    $business_address = $_POST['business_address'] ?? '';
    $occupation = $_POST['occupation'] ?? '';
    $position = $_POST['position'] ?? '';
    $time_period = $_POST['time_period'] ?? '';
    $monthly_income = $_POST['monthly_income'] ?? '';
        $opening_balance = $_POST['opening_balance']?? '';


    // Handle file upload for Customer Image
    $imagePath = '';
    if (!empty($customer_image)) {
        $targetDir = "uploads/customers/";
        if (!is_dir($targetDir)) mkdir($targetDir, 0777, true);
        $imagePath = $targetDir . basename($customer_image);
        move_uploaded_file($_FILES['customer_image']['tmp_name'], $imagePath);
    }

    // Insert into contacts table
    $sql = "INSERT INTO contacts 
            (contact_id, created_at, customer_image, opening_balance, name, father_name, acc_no, cnic, dob, check_no, reference_no, status, home_address, phone_no, house_type, business_address, occupation, position, time_period, monthly_income, type)
            VALUES 
            (:contact_id, :date, :customer_image, :opening_balance, :name, :father_name, :acc_no, :cnic, :dob, :check_no, :reference_no, :status, :home_address, :phone_no, :house_type, :business_address, :occupation, :position, :time_period, :monthly_income, 'customer')";

    $stmt = $conn->prepare($sql);
    $stmt->execute([
        ':contact_id' => $contact_id,
        ':date' => $date,
        ':customer_image' => $imagePath,
        ':opening_balance' => $opening_balance,
        ':name' => $name,
        ':father_name' => $father_name,
        ':acc_no' => $acc_no,
        ':cnic' => $cnic,
        ':dob' => $dob,
        ':check_no' => $check_no,
        ':reference_no' => $reference_no,
        ':status' => $status,
        ':home_address' => $home_address,
        ':phone_no' => $phone_no,
        ':house_type' => $house_type,
        ':business_address' => $business_address,
        ':occupation' => $occupation,
        ':position' => $position,
        ':time_period' => $time_period,
        ':monthly_income' => $monthly_income
    ]);

    $successMessage = "New customer added successfully.";
}
?>


<!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>Add Customers</title>
    <!-- Favicon -->
    <link rel="shortcut icon" href="images/favicon.ico" />
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <!-- Typography CSS -->
    <link rel="stylesheet" href="css/typography.css">
    <!-- Style CSS -->
    <link rel="stylesheet" href="css/style.css">
    <!-- Responsive CSS -->
    <link rel="stylesheet" href="css/responsive.css">
    <style>
    ion-icon {
        font-size: 20px;
        margin-bottom: -5px;
    }
    </style>
</head>

<body>
    <!-- loader Start -->
    <div id="loading">
        <div id="loading-center">
        </div>
    </div>
    <!-- loader END -->
    <!-- Wrapper Start -->
    <div class="wrapper">
        <!-- Sidebar  -->
        <?php 
            include 'includes/sidebar.php';
        ?>
        <!-- TOP Nav Bar -->
        <?php include 'includes/header.php';?>
        <!-- TOP Nav Bar END -->
        <!-- Page Content  -->
        <div id="content-page" class="content-page">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-sm-12">
                        <div class="iq-card">
                            <div class="iq-card-header d-flex justify-content-between row">
                                <div class="iq-header-title col-lg-6">
                                    <h4 class="card-title"><a href="list_supliers.php">Customers</a> > Add Customers
                                    </h4>
                                </div>
                            </div>
                            <div class="iq-card-body" style="overflow:auto;">
                                <?php if ($successMessage): ?>
                                <div class="alert alert-success" role="alert">
                                    <?php echo htmlspecialchars($successMessage); ?>
                                </div>
                                <?php endif; ?>
                           
<form method="POST" action="" enctype="multipart/form-data">
    <div class="row">
        <!-- Date -->
        <div class="col-lg-3">
            <div class="form-group">
                <label>Date*</label>
                <input type="date" class="form-control" name="date" required>
            </div>
        </div>

        <!-- Customer Image -->
        <div class="col-lg-3">
            <div class="form-group">
                <label>Customer Image</label>
                <input type="file" class="form-control" name="customer_image" accept="image/*">
            </div>
        </div>

        <!-- Customer Name -->
        <div class="col-lg-3">
            <div class="form-group">
                <label>Customer Name*</label>
                <input type="text" class="form-control" name="name" required>
            </div>
        </div>

        <!-- Father Name -->
        <div class="col-lg-3">
            <div class="form-group">
                <label>Father Name</label>
                <input type="text" class="form-control" name="father_name" >
            </div>
        </div>

        <!-- Customer Acc No -->
        <div class="col-lg-3" style="display:none;">
            <div class="form-group">
                <label>Customer Acc No.</label>
                <input type="text" class="form-control" name="acc_no">
            </div>
        </div>
     <!-- Opening Balance -->
                                        <div class="col-lg-3">
                                            <div class="form-group">
                                                <label for="opening_balance" class="label-control">Opening
                                                    Balance:</label>
                                                <input type="number" class="form-control" name="opening_balance"
                                                    placeholder="0" id="opening_balance" step="0.01">
                                            </div>
                                        </div>
        <!-- CNIC -->
        <div class="col-lg-3" style="display:none;">
            <div class="form-group">
                <label>CNIC*</label>
                <input type="text" class="form-control" name="cnic">
            </div>
        </div>

        <!-- Date of Birth -->
        <div class="col-lg-3" style="display:none;">
            <div class="form-group">
                <label>Date of Birth</label>
                <input type="date" class="form-control" name="dob">
            </div>
        </div>

        <!-- Check No -->
        <div class="col-lg-3" style="display:none;">
            <div class="form-group">
                <label>Check No</label>
                <input type="text" class="form-control" name="check_no">
            </div>
        </div>

        <!-- Reference No -->
        <div class="col-lg-3" style="display:none;">
            <div class="form-group">
                <label>Reference No</label>
                <input type="text" class="form-control" name="reference_no">
            </div>
        </div>

        <!-- Status -->
        <div class="col-lg-3" style="display:none;">
            <div class="form-group">
                <label>Status</label>
                <select class="form-control" name="status">
                    <option value="">--Select--</option>
                    <option value="Married">Married</option>
                    <option value="Unmarried">Unmarried</option>
                </select>
            </div>
        </div>

        <!-- Home Address -->
        <div class="col-lg-6">
            <div class="form-group">
                <label>Home Address</label>
                <input type="text" class="form-control" name="home_address">
            </div>
        </div>

        <!-- Phone No -->
        <div class="col-lg-3">
            <div class="form-group">
                <label>Phone No</label>
                <input type="text" class="form-control" name="phone_no">
            </div>
        </div>

        <!-- House Type -->
        <div class="col-lg-3" style="display:none;">
            <div class="form-group">
                <label>House Type</label>
                <select class="form-control" name="house_type">
                    <option value="">--Select--</option>
                    <option value="Owned">Owned</option>
                    <option value="Rented">Rented</option>
                </select>
            </div>
        </div>

        <!-- Business Address -->
        <div class="col-lg-6" style="display:none;">
            <div class="form-group">
                <label>Business Address</label>
                <input type="text" class="form-control" name="business_address">
            </div>
        </div>

        <!-- Occupation -->
        <div class="col-lg-3" style="display:none;">
            <div class="form-group">
                <label>Occupation</label>
                <select class="form-control" name="occupation">
                    <option value="">--Select--</option>
                    <option value="Salaried">Salaried</option>
                    <option value="Business">Business</option>
                </select>
            </div>
        </div>

        <!-- Position -->
        <div class="col-lg-3" style="display:none;">
            <div class="form-group">
                <label>Position</label>
                <input type="text" class="form-control" name="position">
            </div>
        </div>

        <!-- Time Period -->
        <div class="col-lg-3" style="display:none;">
            <div class="form-group">
                <label>Time Period</label>
                <input type="text" class="form-control" name="time_period">
            </div>
        </div>

        <!-- Monthly Income -->
        <div class="col-lg-3" style="display:none;">
            <div class="form-group">
                <label>Monthly Income</label>
                <input type="number" class="form-control" name="monthly_income" step="0.01">
            </div>
        </div>

        <!-- Submit Button -->
        <div class="col-lg-12">
            <button class="btn btn-primary">Submit</button>
        </div>
    </div>
</form>

                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
    </div>
    <!-- Wrapper END -->
    <!-- Footer -->
    <footer class="iq-footer">
        <div class="container-fluid">
            <div class="row">
                <div class="col-lg-6">
                    <ul class="list-inline mb-0">
                        <li class="list-inline-item"><a href="privacy-policy.html">Privacy Policy</a></li>
                        <li class="list-inline-item"><a href="terms-of-service.html">Terms of Use</a></li>
                    </ul>
                </div>
                <div class="col-lg-6 text-right">
                    Copyright <span id="copyright">
                        <script>
                        document.getElementById('copyright').appendChild(document.createTextNode(new Date()
                            .getFullYear()))
                        </script>
                    </span> <a href="#">Vito</a> All Rights Reserved.
                </div>
            </div>
        </div>
    </footer>
    <nav class="iq-float-menu">
        <input type="checkbox" href="#" class="iq-float-menu-open" name="menu-open" id="menu-open" />
        <label class="iq-float-menu-open-button" for="menu-open">
            <span class="lines line-1"></span>
            <span class="lines line-2"></span>
            <span class="lines line-3"></span>
        </label>
        <button class="iq-float-menu-item bg-info" data-toggle="tooltip" data-placement="top" title="Direction Mode"
            data-mode="rtl"><i class="ri-text-direction-r"></i></button>
        <button class="iq-float-menu-item bg-danger" data-toggle="tooltip" data-placement="top" title="Color Mode"
            id="dark-mode" data-active="true"><i class="ri-sun-line"></i></button>
        <button class="iq-float-menu-item bg-warning" data-toggle="tooltip" data-placement="top" title="Comming Soon"><i
                class="ri-palette-line"></i></button>
    </nav>
    <!-- Footer END -->
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="js/jquery.min.js"></script>
    <!-- Rtl and Darkmode -->
    <script src="js/rtl.js"></script>
    <script src="js/customizer.js"></script>
    <script src="js/popper.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <!-- Appear JavaScript -->
    <script src="js/jquery.appear.js"></script>
    <!-- Countdown JavaScript -->
    <script src="js/countdown.min.js"></script>
    <!-- Counterup JavaScript -->
    <script src="js/waypoints.min.js"></script>
    <script src="js/jquery.counterup.min.js"></script>
    <!-- Wow JavaScript -->
    <script src="js/wow.min.js"></script>
    <!-- Apexcharts JavaScript -->
    <script src="js/apexcharts.js"></script>
    <!-- Slick JavaScript -->
    <script src="js/slick.min.js"></script>
    <!-- Select2 JavaScript -->
    <script src="js/select2.min.js"></script>
    <!-- Owl Carousel JavaScript -->
    <script src="js/owl.carousel.min.js"></script>
    <!-- Magnific Popup JavaScript -->
    <script src="js/jquery.magnific-popup.min.js"></script>
    <!-- Smooth Scrollbar JavaScript -->
    <script src="js/smooth-scrollbar.js"></script>
    <!-- lottie JavaScript -->
    <script src="js/lottie.js"></script>
    <!-- Chart Custom JavaScript -->
    <script src="js/chart-custom.js"></script>
    <!-- Custom JavaScript -->
    <script src="js/custom.js"></script>
</body>

</html>