<?php 
include('include/connection.php'); 

if(isset($_POST['submit'])){

  //picture upload functions
  function getExtention($image_name){
      return substr($image_name,strrpos($image_name,'.')+1);
    }
    function getBaseName($image_name){
      return substr($image_name,0,strrpos($image_name,'.'));
    }
//End picture upload functions

  function test_input($data){
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
  }

  $centreName = test_input($_POST['centreName']);
  $centreAddr = test_input($_POST['centreAddr']); 
  $vehMake = test_input($_POST['vehMake']); 
  $vehModel = test_input($_POST['vehModel']); 
  $vehType = test_input($_POST['vehType']); 
  $vehColor = test_input($_POST['vehColor']); 
  $vehChasis = test_input($_POST['vehChasis']); 
  $vehEngine = test_input($_POST['vehEngine']); 
  $vehCylinder = test_input($_POST['vehCylinder']); 
  $vehCapacity = test_input($_POST['vehCapacity']); 
  $vehPurpose = test_input($_POST['vehPurpose']); 
  $prevReg = test_input($_POST['prevReg']); 
  $ownerName = test_input($_POST['ownerName']); 
  $ownerAddr = test_input($_POST['ownerAddr']); 
  $phoneNo = test_input($_POST['phoneNo']); 
  $unionNo = test_input($_POST['unionNo']); 
  // $photo = test_input($_POST['photo']); 
  $today = date("Y-m-d ");

  ////////////picture upload///////////////
        $img = $_FILES["photo"]["name"];
                   
        $random_digit=rand(000000,999999);
        $new_name=$random_digit."_".$img;            
        $tname=$_FILES["photo"]["tmp_name"];
        $size=$_FILES["photo"]["size"];
        $oext=getExtention($img);
        $ext=strtolower($oext);
        $base_name=getBaseName($new_name);
                        

                        if($ext=="jpg" || $ext=="jpeg" || $ext=="bmp" || $ext=="gif" || $ext=="png"){
                          if($size< 1048576){
                            move_uploaded_file($tname,"uploads/".$new_name);
                              $result = 1;

 

      $sql ="INSERT INTO vehReg (regID, centreName, centreAddr, vehMake, vehModel, vehType, vehColor, vehChasis, vehEngine, vehCylinder, vehCapacity, vehPurpose, prevReg, ownerName, ownerAddr, phoneNo, unionNo, photo, datee) VALUES ('', '$centreName', '$centreAddr', '$vehMake', '$vehModel', '$vehType', '$vehColor', '$vehChasis', '$vehEngine', '$vehCylinder', '$vehCapacity', '$vehPurpose', '$prevReg', '$ownerName', '$ownerAddr', '$phoneNo', '$unionNo', '$new_name', '$today')";
      $query = mysqli_query($conn, $sql) or die("Unable to insert record to DB | ".mysqli_error($conn));
      header("Location: success.php");
      }else{
        echo "Record not Inserted: Image upload failed";
      }
    }

}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bio Data Collection</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        :root {
            --primary-color: #4361ee;
            --secondary-color: #3f37c9;
            --accent-color: #4cc9f0;
            --light-color: #f8f9fa;
            --dark-color: #212529;
            --success-color: #4bb543;
            --error-color: #ff3333;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            color: var(--dark-color);
            line-height: 1.6;
            min-height: 100vh;
            padding: 2rem;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }

        .form-header {
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
            color: white;
            padding: 2rem;
            text-align: center;
            position: relative;
        }

        .form-header h1 {
            font-size: 2rem;
            margin-bottom: 0.5rem;
        }

        .form-header p {
            opacity: 0.9;
        }

        .form-header::after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 0;
            width: 100%;
            height: 20px;
            background: url('data:image/svg+xml;utf8,<svg viewBox="0 0 1200 120" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none"><path d="M0,0V46.29c47.79,22.2,103.59,32.17,158,28,70.36-5.37,136.33-33.31,206.8-37.5C438.64,32.43,512.34,53.67,583,72.05c69.27,18,138.3,24.88,209.4,13.08,36.15-6,69.85-17.84,104.45-29.34C989.49,25,1113-14.29,1200,52.47V0Z" opacity=".25" fill="white"/><path d="M0,0V15.81C13,36.92,27.64,56.86,47.69,72.05,99.41,111.27,165,111,224.58,91.58c31.15-10.15,60.09-26.07,89.67-39.8,40.92-19,84.73-46,130.83-49.67,36.26-2.85,70.9,9.42,98.6,31.56,31.77,25.39,62.32,62,103.63,73,40.44,10.79,81.35-6.69,119.13-24.28s75.16-39,116.92-43.05c59.73-5.85,113.28,22.88,168.9,38.84,30.2,8.66,59,6.17,87.09-7.5,22.43-10.89,48-26.93,60.65-49.24V0Z" opacity=".5" fill="white"/><path d="M0,0V5.63C149.93,59,314.09,71.32,475.83,42.57c43-7.64,84.23-20.12,127.61-26.46,59-8.63,112.48,12.24,165.56,35.4C827.93,77.22,886,95.24,951.2,90c86.53-7,172.46-45.71,248.8-84.81V0Z" fill="white"/></svg>');
            background-size: cover;
        }

        .form-body {
            padding: 2rem;
        }

        .form-row {
            display: flex;
            flex-wrap: wrap;
            margin: 0 -1rem;
            margin-bottom: 1.5rem;
        }

        .form-group {
            flex: 1 0 100%;
            padding: 0 1rem;
            margin-bottom: 1.5rem;
        }

        @media (min-width: 768px) {
            .form-group.half {
                flex: 1 0 50%;
                max-width: 50%;
            }
        }

        label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 500;
            color: var(--dark-color);
        }

        .required-field::after {
            content: ' *';
            color: var(--error-color);
        }

        .input-group {
            position: relative;
        }

        .input-group i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: var(--primary-color);
        }

        input, select, textarea {
            width: 100%;
            padding: 12px 15px 12px 45px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-family: inherit;
            font-size: 1rem;
            transition: all 0.3s ease;
        }

        .input-group input, 
        .input-group select {
            padding-left: 45px;
        }

        input:focus, 
        select:focus, 
        textarea:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.2);
        }

        textarea {
            min-height: 120px;
            padding: 15px;
            resize: vertical;
        }

        .form-footer {
            text-align: center;
            margin-top: 2rem;
        }

        .btn {
            display: inline-block;
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
            color: white;
            border: none;
            padding: 12px 30px;
            border-radius: 50px;
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(67, 97, 238, 0.3);
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(67, 97, 238, 0.4);
        }

        .btn:active {
            transform: translateY(0);
        }

        .success-message {
            background-color: rgba(75, 181, 67, 0.1);
            color: var(--success-color);
            padding: 1rem;
            border-radius: 8px;
            margin-bottom: 1.5rem;
            border-left: 4px solid var(--success-color);
            display: flex;
            align-items: center;
        }

        .success-message i {
            margin-right: 10px;
        }

        .error-message {
            background-color: rgba(255, 51, 51, 0.1);
            color: var(--error-color);
            padding: 1rem;
            border-radius: 8px;
            margin-bottom: 1.5rem;
            border-left: 4px solid var(--error-color);
            display: flex;
            align-items: center;
        }

        .error-message i {
            margin-right: 10px;
        }

        .photo-upload {
            text-align: center;
            border: 2px dashed #e9ecef;
            padding: 2rem;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .photo-upload:hover {
            border-color: var(--primary-color);
            background-color: rgba(67, 97, 238, 0.05);
        }

        .photo-upload i {
            font-size: 2rem;
            color: var(--primary-color);
            margin-bottom: 1rem;
        }

        .photo-upload p {
            margin-bottom: 0.5rem;
        }

        .photo-upload small {
            color: #6c757d;
        }

        #photo-preview {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            object-fit: cover;
            display: none;
            margin: 0 auto 1rem;
            border: 4px solid white;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="form-header">
            <h1>Vehicle Registration (Form MVA 1)</h1>
            <p>Please fill out this form with accurate information</p>
        </div>

        <div class="form-body">
            <!-- Success/Error messages would go here -->
            <!-- <div class="success-message">
                <i class="fas fa-check-circle"></i>
                <span>Your information has been submitted successfully!</span>
            </div> -->

            <form id="bioDataForm" action="test.php" method="post" enctype="multipart/form-data">
                <div class="form-row">REGISTRATION CENTRE</div>
                <div class="form-group">
                    <label for="centreName" class="required-field">Name of Centre</label>
                    <div class="input-group">
                        <i class="fas fa-city"></i>
                        <input type="text" id="centreName" name="centreName" required>
                    </div>
                </div>
                <div class="form-group">
                    <label for="centreAddr" class="required-field">Centre Address</label>
                    <div class="input-group">
                        <i class="fas fa-map-marker-alt"></i>
                        <input type="text" id="centreAddr" name="centreAddr" required>
                    </div>
                </div>
                <div class="form-row">VEHICLE DETAILS</div>
                <div class="form-row">
                    <div class="form-group half">
                        <label for="vehMake" class="required-field">Make</label>
                        <div class="input-group">
                            <i class="fas fa-car"></i>
                            <input type="text" id="vehMake" name="vehMake" required>
                        </div>
                    </div>
                    <div class="form-group half">
                        <label for="vehModel" class="required-field">Model</label>
                        <div class="input-group">
                            <i class="fas fa-car"></i>
                            <input type="text" id="vehModel" name="vehModel" required>
                        </div>
                    </div>
                </div>

                <div class="form-row">
                    <div class="form-group half">
                        <label for="vehType" class="required-field">Type</label>
                        <div class="input-group">
                            <i class="fas fa-car"></i>
                            <select id="vehType" name="vehType" required>
                                <option value="">Select Vehicle Type</option>
                                <option value="Saloon">Saloon</option>
                                <option value="Pickup">Pickup</option>
                                <option value="Wagon">Wagon</option>
                                <option value="Bus">Bus</option>
                                <option value="Tanker">Tanker</option>
                                <option value="Trailer">Trailer</option>
                                <option value="Tricycle">Tricycle</option>
                                <option value="Motorcycle">Motorcycle</option>
                                <option value="Truck">Truck</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group half">
                        <label for="vehColor" class="required-field">Color</label>
                        <div class="input-group">
                            <i class="fas fa-car"></i>
                            <input type="text" id="vehColor" name="vehColor" required>
                        </div>
                    </div>
                    
                </div>

                <div class="form-row">
                    <div class="form-group half">
                        <label for="vehChasis" class="required-field">Chasis Number</label>
                        <div class="input-group">
                            <i class="fas fa-car"></i>
                            <input type="text" id="vehChasis" name="vehChasis" required>
                        </div>
                    </div>
                    <div class="form-group half">
                        <label for="vehEngine" class="required-field">Engine Number</label>
                        <div class="input-group">
                            <i class="fas fa-car"></i>
                            <input type="text" id="vehEngine" name="vehEngine" required>
                        </div>
                    </div>
                </div>

                <div class="form-row">
                    <div class="form-group half">
                        <label for="vehCylinder" class="required-field">Number of Cylinder</label>
                        <div class="input-group">
                            <i class="fas fa-car"></i>
                            <select id="vehCylinder" name="vehCylinder" required>
                                <option value="">Select Cylinder Number</option>
                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                                <option value="4">4</option>
                                <option value="5">5</option>
                                <option value="6">6</option>
                                <option value="7">7</option>
                                <option value="8">8</option>
                                <option value="9">9</option>
                                <option value="10">10</option>
                                <option value="11">11</option>
                                <option value="12">12</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group half">
                        <label for="vehCapacity" class="required-field">Engine Capacity</label>
                        <div class="input-group">
                            <i class="fas fa-car"></i>
                            <input type="text" id="vehCapacity" name="vehCapacity" required>
                        </div>
                    </div> 
                </div>

                <div class="form-row">
                    <div class="form-group half">
                        <label for="vehPurpose" class="required-field">Purpose</label>
                        <div class="input-group">
                            <i class="fas fa-car"></i>
                            <select id="vehPurpose" name="vehPurpose" required>
                                <option value="">State Purpose</option>
                                <option value="Private">Private</option>
                                <option value="Commercial">Commercial</option>
                                <option value="Government">Government</option>
                                <option value="Paramilitary">Paramilitary</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group half">
                        <label for="prevReg" >Previous Registration Number (if any)</label>
                        <div class="input-group">
                            <i class="fas fa-car"></i>
                            <input type="text" id="prevReg" name="prevReg" required>
                        </div>
                    </div> 
                </div>

                <div class="form-row">OWNER'S INFORMATION</div>

                <div class="form-group">
                    <label for="ownerName" class="required-field">Name</label>
                    <div class="input-group">
                        <i class="fas fa-user"></i>
                        <input type="text" id="ownerName" name="ownerName" required>
                    </div>
                </div>
                <div class="form-group">
                    <label for="ownerAddr" class="required-field">Address</label>
                    <textarea id="ownerAddr" name="ownerAddr" placeholder="Please provide your address here" required></textarea>
                </div>
                <div class="form-row">
                    <div class="form-group half">
                        <label for="phoneNo" class="required-field">Phone Number</label>
                        <div class="input-group">
                            <i class="fas fa-phone"></i>
                            <input type="text" id="phoneNo" name="phoneNo" required>
                        </div>
                    </div>
                    <div class="form-group half">
                        <label for="unionNo">Union Membership No (if any)</label>
                        <div class="input-group">
                            <i class="fas fa-globe"></i>
                            <input type="text" id="unionNo" name="unionNo" >
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label>Upload Photo</label>
                    <div class="photo-upload" onclick="document.getElementById('photo').click()">
                        <img id="photo-preview" src="#" alt="Photo Preview">
                        <i class="fas fa-camera"></i>
                        <p>Click to upload your photo</p>
                        <small>JPEG or PNG, max 2MB</small>
                        <input type="file" id="photo" name="photo" accept="image/*" style="display: none;" onchange="previewPhoto(event)">
                    </div>
                </div>

                <div class="form-footer">
                    <button type="submit" name="submit" class="btn">
                        <i class="fas fa-paper-plane"></i> Submit Registration
                    </button>
                </div>
            </form>
        </div>
    </div>

    <script>
        function previewPhoto(event) {
            const input = event.target;
            const preview = document.getElementById('photo-preview');
            
            if (input.files && input.files[0]) {
                const reader = new FileReader();
                
                reader.onload = function(e) {
                    preview.src = e.target.result;
                    preview.style.display = 'block';
                    // Hide the camera icon when photo is selected
                    document.querySelector('.photo-upload i').style.display = 'none';
                    document.querySelector('.photo-upload p').style.display = 'none';
                }
                
                reader.readAsDataURL(input.files[0]);
            }
        }

        // // Form submission would be handled here
        // document.getElementById('bioDataForm').addEventListener('submit', function(e) {
        //     e.preventDefault();
        //     // Here you would typically send the form data to the server
        //     alert('Form submission would be handled here in a real implementation');
        // });
    </script>
</body>
</html>