<?php
// var_dump($_POST);
// exit;
    
session_start();
$userID=$_SESSION["userID"];
if(empty($userID)){
  $error="You have no permission";
  header("location:login.php");
}

if(isset($_POST)){
    include("config.php");

    $firstName=mysqli_escape_string($conn,$_POST["firstName"]);
    $lastName=mysqli_escape_string($conn,$_POST["lastName"]);
    $emailaddress=mysqli_escape_string($conn,$_POST["email"]);
    $phone=mysqli_escape_string($conn,$_POST["phone"]);
    $gps=mysqli_escape_string($conn,$_POST["gpsaddress"]);
    $location=mysqli_escape_string($conn,$_POST["location"]);
    $housetype=mysqli_escape_string($conn,$_POST["housetype"]);
    $number_room=mysqli_escape_string($conn,$_POST["number_room"]);
    $region=mysqli_escape_string($conn,$_POST["region"]);
    $constituency=mysqli_escape_string($conn,$_POST["constituency"]);
    
    

    $query="SELECT * FROM property WHERE phone='$phone' and gps='$gps'";
    $data=mysqli_query($conn,$query);
    $count=mysqli_num_rows($data);
    if($count>0){
        $error="Property already exists";
        header("location:add_property.php?message=$error");
        exit;
    }
    else{

    $query4="SELECT * FROM prate where typeBuilding='$housetype' and noRooms='$number_room'";
    $data4=mysqli_query($conn,$query4);
    while($row4=mysqli_fetch_assoc($data4)){
        $amount=$row4["amount"];
        $prateID=$row4["prateID"];
    }
    // echo $prateID;
    // echo "<br>";
    // echo $amount;
    // exit;
        $transdate=date("Y-m-d");
  // String of all alphanumeric character
  $str_result = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  $trandate1=date("Ymd");

  // Shuffle the $str_result and returns substring
  // of specified length
  $token= substr(str_shuffle($str_result), 0, 6);
      // $token=random();
      $prid="GA-$token$trandate1";

        $query2="INSERT INTO property SET prateID='$prateID',userID='$userID',propRefID='$prid',fname='$firstName',lname='$lastName',gps='$gps',location='$location',region='$region',constituency='$constituency',phone='$phone',email='$emailaddress',type_house='$housetype',no_rooms='$number_room',date_created='$transdate'";
        $data2=mysqli_query($conn,$query2);

        if($data2){
            $fullName="$firstName $lastName";
            $number="$phone";
            $code="233";
            $rest = substr("$number", 1);
            $number_recipient="$code$rest";
            $text="Hello $fullName your property with property Reference Number $prid has been added."; 
            $from="momoCAD";
            $sender2="MCI";

               // SEND SMS
    $curl = curl_init();
    curl_setopt_array($curl, array(
    CURLOPT_URL => "https://sms.hubtel.com/v1/messages/send?clientsecret=kzgcubkj&clientid=ityrcnbz&from=CAD&to=$number_recipient&content=$text",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 10,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET', ));
    $response = curl_exec($curl);
    curl_close($curl);
    // echo $response;
        }
        $error="Property Added Successfully";
        header("location:add_properties.php?message=$error");
        exit;

    }

}
else{
    $error="You have no right to be here";
    header("location:add_properties.php?message=$error");
    exit;
}





?>
