<?php
include 'config.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $full_name = $conn->real_escape_string($_POST['full_name']);
    $phone = $conn->real_escape_string($_POST['phone']);
    $nin = $conn->real_escape_string($_POST['nin']);
    $district = $conn->real_escape_string($_POST['district']);
    $village = $conn->real_escape_string($_POST['village']);
    $address = $conn->real_escape_string($_POST['address']);
    $next_of_kin = $conn->real_escape_string($_POST['next_of_kin']);
    $kin_phone = $conn->real_escape_string($_POST['kin_phone']);
    $payment_method = $conn->real_escape_string($_POST['payment_method']);
    $account_no = 'GOW-' . rand(10000, 99999);
    
    // Handle uploads
    function uploadFile($field){
        if(!empty($_FILES[$field]['name'])){
            $target = "uploads/".time()."_".$field."_".basename($_FILES[$field]['name']);
            move_uploaded_file($_FILES[$field]['tmp_name'], $target);
            return $target;
        }
        return '';
    }
    
    $nin_photo = uploadFile('nin_photo');
    $id_front = uploadFile('id_front');
    $id_back = uploadFile('id_back');
    
    $sql = "INSERT INTO members (full_name, phone, nin, nin_photo, id_front, id_back, district, village, address, next_of_kin, kin_phone, account_no, payment_method) 
            VALUES ('$full_name','$phone','$nin','$nin_photo','$id_front','$id_back','$district','$village','$address','$next_of_kin','$kin_phone','$account_no','$payment_method')";
    
    if ($conn->query($sql)) {
        $success = "SUCCESS! Account No: <b>$account_no</b><br>Fees: 25,000/= (5,000/= Membership + 20,000/= Passbook)<br>Our team will verify your ID and call you on $phone";
    } else {
        $error = "Error: " . $conn->error;
    }
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>God of Wonders SACCO - Open Account</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body style="background:#f5f7fb">
<div class="container p-2">
  <div class="card shadow mx-auto" style="max-width:550px">
    <div class="card-header bg-primary text-white text-center py-3">
      <h5 class="m-0">GOD OF WONDERS SACCO</h5>
      <small>Account Opening - UGX 25,000/=</small><br>
      <small style="background:yellow;color:black;padding:2px 8px;border-radius:10px">Membership 5k + Passbook 20k</small>
    </div>
    <div class="card-body">
      <?php if(isset($success)) echo "<div class='alert alert-success'>$success</div>"; ?>
      <?php if(isset($error)) echo "<div class='alert alert-danger'>$error</div>"; ?>
      
      <form method="POST" enctype="multipart/form-data">
        <label class="fw-bold small">Full Name *</label>
        <input type="text" name="full_name" class="form-control mb-2" placeholder="As on National ID" required>
        
        <label class="fw-bold small">Phone Number *</label>
        <input type="tel" name="phone" class="form-control mb-2" placeholder="07XXXXXXXX" required>
        
        <label class="fw-bold small">NIN Number *</label>
        <input type="text" name="nin" class="form-control mb-2" placeholder="CMXXXXXXXXXXXX" required>
        
        <div class="row">
          <div class="col-6">
            <label class="fw-bold small">NIN Photo (Selfie holding ID) *</label>
            <input type="file" name="nin_photo" class="form-control mb-2" accept="image/*" required>
          </div>
          <div class="col-6">
            <label class="fw-bold small">ID Front Copy *</label>
            <input type="file" name="id_front" class="form-control mb-2" accept="image/*" required>
          </div>
        </div>
        
        <label class="fw-bold small">ID Back Copy *</label>
        <input type="file" name="id_back" class="form-control mb-2" accept="image/*" required>
        
        <label class="fw-bold small">Address / Location *</label>
        <input type="text" name="address" class="form-control mb-2" placeholder="Plot, Street, LC1, Town" required>
        
        <div class="row">
          <div class="col-6">
            <input type="text" name="district" class="form-control mb-2" placeholder="District" required>
          </div>
          <div class="col-6">
            <input type="text" name="village" class="form-control mb-2" placeholder="Village" required>
          </div>
        </div>
        
        <label class="fw-bold small">Next of Kin *</label>
        <input type="text" name="next_of_kin" class="form-control mb-2" placeholder="Next of Kin Full Name" required>
        <input type="tel" name="kin_phone" class="form-control mb-2" placeholder="Kin Phone Number" required>
        
        <label class="fw-bold small">Payment Method</label>
        <select name="payment_method" class="form-control mb-3" required>
          <option value="MTN MoMo">MTN MoMo - Pay 25k</option>
          <option value="Airtel Money">Airtel Money - Pay 25k</option>
          <option value="Cash">Cash at Office</option>
        </select>
        
        <button type="submit" class="btn btn-primary w-100 py-2">SUBMIT APPLICATION - UGX 25,000</button>
        <p class="text-center small mt-2 text-muted">You will receive SACCO Number after ID verification</p>
      </form>
    </div>
  </div>
</div>
</body>
</html>