<?php
// ১. ডাটাবেস কানেকশন সরাসরি এখানে দেওয়া হলো (এরর এড়াতে)
$servername = "localhost";
$username = "mobileh1_teams";
$password = 'de*tMX~5[.Ffwb7v'; 
$dbname = "mobileh1_teams";

$conn = new mysqli($servername, $username, $password, $dbname);

// কানেকশন চেক
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// *** বাংলা লেখা (????) ফিক্স করার জন্য এই লাইনটি অত্যন্ত জরুরি ***
$conn->set_charset("utf8mb4"); 

$message = "";

// ২. ফর্ম সাবমিট হলে ডাটা প্রসেসিং
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
    // ডাটা রিসিভ এবং সিকিউরিটি চেক
    $client_name = $conn->real_escape_string($_POST['name']);
    $mobile_number = $conn->real_escape_string($_POST['phone']);
    $email = $conn->real_escape_string($_POST['email']);
    $address = $conn->real_escape_string($_POST['address']);
    $website_name = $conn->real_escape_string($_POST['site_name']);
    $requirement_demo = $conn->real_escape_string($_POST['demo_url']);
    $project_start_date = $_POST['start_date'];
    $project_end_date = $_POST['end_date'];
    $description = $conn->real_escape_string($_POST['description']);
    $total_price = $_POST['total_price'];
    $advance_payment = $_POST['advance_payment'];
    $due_amount = $_POST['due_amount'];
    $payment_method = $_POST['payment_method'];
    $sender_number = $conn->real_escape_string($_POST['sender_number']);
    $transaction_id = $conn->real_escape_string($_POST['transaction_id']);

    // লোগো আপলোড
    $logo_path = "";
    if (isset($_FILES['logo']) && $_FILES['logo']['error'] == 0) {
        $target_dir = "uploads/"; 
        if (!file_exists($target_dir)) { mkdir($target_dir, 0777, true); }
        $file_name = time() . "_" . basename($_FILES["logo"]["name"]);
        $target_file = $target_dir . $file_name;
        if (move_uploaded_file($_FILES["logo"]["tmp_name"], $target_file)) {
            $logo_path = $target_file;
        }
    }

    // ৩. ডাটাবেসে সেভ করা
    $sql = "INSERT INTO orders (client_name, mobile_number, email, address, website_name, requirement_demo, project_start_date, project_end_date, logo_path, description, total_price, advance_payment, due_amount, payment_method, sender_number, transaction_id, status) 
            VALUES ('$client_name', '$mobile_number', '$email', '$address', '$website_name', '$requirement_demo', '$project_start_date', '$project_end_date', '$logo_path', '$description', '$total_price', '$advance_payment', '$due_amount', '$payment_method', '$sender_number', '$transaction_id', 'Pending')";

    if ($conn->query($sql) === TRUE) {
        $message = "<div style='background: #d4edda; color: #155724; padding: 15px; border-radius: 10px; text-align: center; margin-bottom: 20px; font-weight:bold;'>ধন্যবাদ! আপনার অর্ডারটি সফলভাবে জমা হয়েছে।</div>";
    } else {
        $message = "<div style='background: #f8d7da; color: #721c24; padding: 15px; border-radius: 10px; text-align: center; margin-bottom: 20px;'>ডাটাবেস এরর: " . $conn->error . "</div>";
    }
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>প্রজেক্ট অর্ডার ফর্ম - CodexioBD</title>
    <link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@400;600;700&display=swap" rel="stylesheet">
    <style>
        :root { --primary: #ff4d6d; --bg: #f0f2f5; --text: #4a4e69; --shadow-light: #ffffff; --shadow-dark: #d1d9e6; }
        body { font-family: 'Hind Siliguri', sans-serif; background-color: var(--bg); margin: 0; padding: 20px; display: flex; justify-content: center; }
        .container { background: var(--bg); width: 100%; max-width: 900px; padding: 40px; border-radius: 30px; box-shadow: 20px 20px 60px var(--shadow-dark), -20px -20px 60px var(--shadow-light); }
        
        header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
        .logo { font-weight: 800; font-size: 26px; color: #333; text-decoration: none; }
        .logo span { color: var(--primary); }
        .back-home { background: var(--bg); padding: 10px 20px; border-radius: 12px; text-decoration: none; color: #555; font-weight: 600; box-shadow: 5px 5px 10px var(--shadow-dark), -5px -5px 10px var(--shadow-light); }

        .title-section { text-align: center; margin-bottom: 30px; }
        h1 { color: var(--text); font-size: 32px; margin: 0; }
        .agency-info { color: #777; font-size: 15px; margin-top: 10px; line-height: 1.6; }

        .section-header { color: var(--primary); font-size: 18px; font-weight: 700; margin-bottom: 25px; border-bottom: 2px solid #e0e4eb; padding-bottom: 10px; }
        .form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 25px; margin-bottom: 40px; }
        .form-group { display: flex; flex-direction: column; }
        label { margin-bottom: 10px; font-weight: 600; font-size: 14px; color: #555; }
        label span { color: var(--primary); }

        input, textarea, select { padding: 15px 20px; border: none; border-radius: 15px; background: var(--bg); box-shadow: inset 6px 6px 12px var(--shadow-dark), inset -6px -6px 12px var(--shadow-light); outline: none; font-family: inherit; color: #333; }
        
        .payment-display { grid-column: span 2; background: #fff; padding: 20px; border-radius: 20px; margin-bottom: 20px; box-shadow: 10px 10px 20px var(--shadow-dark); display: flex; justify-content: space-between; align-items: center; }
        .copy-btn { background: var(--primary); color: white; padding: 8px 15px; border-radius: 10px; border: none; cursor: pointer; }

        .submit-btn { width: 100%; padding: 18px; background: var(--primary); color: white; border: none; border-radius: 15px; font-size: 18px; font-weight: 700; cursor: pointer; box-shadow: 0 10px 20px #ff4d6d44; }
        
        @media (max-width: 768px) { .form-grid { grid-template-columns: 1fr; } .container { padding: 20px; } .payment-display { flex-direction: column; gap: 10px; text-align: center; } }
    </style>
</head>
<body>

<div class="container">
    <header>
        <a href="#" class="logo">CODEXIO<span>BD</span></a>
        <a href="#" class="back-home">← হোমে ফিরে যান</a>
    </header>


    <div class="title-section">
        <h1> প্রজেক্ট অর্ডার ফর্ম </h1>
        <div class="agency-info">
            <b>CodexioBD Digital Agency</b><br>
           গাজীপুর সদর, গাজীপুর, ঢাকা - ১২১৬ <br>
            ফোন: +88 01912 855989  | ইমেইল: info@codexiobd.com
        </div>
    </div>


    <?php echo $message; ?>

    <form action="" method="POST" enctype="multipart/form-data">
        <div class="section-header">👤 যোগাযোগের বিস্তারিত</div>
        <div class="form-grid">
            <div class="form-group"><label>নাম <span>*</span></label><input type="text" name="name" required></div>
            <div class="form-group"><label>মোবাইল <span>*</span></label><input type="tel" name="phone" required></div>
            <div class="form-group"><label>ইমেইল <span>*</span></label><input type="email" name="email" required></div>
            <div class="form-group"><label>ঠিকানা <span>*</span></label><textarea name="address" required></textarea></div>
        </div>

        <div class="section-header">📰 প্রজেক্টের তথ্য</div>
        <div class="form-grid">
            <div class="form-group"><label>ওয়েবসাইটের নাম <span>*</span></label><input type="text" name="site_name" required></div>
            <div class="form-group"><label>ডেমো URL</label><input type="url" name="demo_url"></div>
            <div class="form-group"><label>শুরুর তারিখ <span>*</span></label><input type="date" name="start_date" required></div>
            <div class="form-group"><label>শেষের তারিখ <span>*</span></label><input type="date" name="end_date" required></div>
            <div class="form-group" style="grid-column: span 2;"><label>লোগো</label><input type="file" name="logo"></div>
        </div>

        <div class="section-header">💳 পেমেন্ট তথ্য</div>
        <div class="payment-display">
            <div>পেমেন্ট নম্বর (বিকাশ/নগদ): <b id="payNum">01912855989</b></div>
            <button type="button" class="copy-btn" onclick="copyNumber()">কপি করুন</button>
        </div>
        <div class="form-grid">
            <div class="form-group"><label>মোট মূল্য <span>*</span></label><input type="number" id="total" name="total_price" oninput="calc()" required></div>
            <div class="form-group"><label>অগ্রিম <span>*</span></label><input type="number" id="adv" name="advance_payment" oninput="calc()" required></div>
            <div class="form-group"><label>বাকি</label><input type="number" id="due" name="due_amount" readonly></div>
            <div class="form-group">
                <label>মেথড <span>*</span></label>
                <select name="payment_method" required>
                    <option value="bKash">bKash</option>
                    <option value="Nagad">Nagad</option>
                    <option value="Rocket">Rocket</option>
                </select>
            </div>
            <div class="form-group"><label>আপনার পেমেন্ট নম্বর <span>*</span></label><input type="tel" name="sender_number" required></div>
            <div class="form-group"><label>Transaction ID <span>*</span></label><input type="text" name="transaction_id" required></div>
        </div>

        <div class="section-header">📝 অতিরিক্ত বর্ণনা</div>
        <textarea name="description" style="width: 100%; margin-bottom: 30px;"></textarea>

        <button type="submit" class="submit-btn">অর্ডার কনফার্ম করুন</button>
    </form>
</div>

<script>
    function copyNumber() {
        const num = document.getElementById('payNum').innerText;
        navigator.clipboard.writeText(num);
        alert("নম্বর কপি হয়েছে: " + num);
    }
    function calc() {
        const t = document.getElementById('total').value || 0;
        const a = document.getElementById('adv').value || 0;
        document.getElementById('due').value = t - a;
    }
</script>

</body>
</html>