<?php
	include 'includes/session.php';
	include 'sendsms.php';
	if(isset($_POST['add'])){
		$firstname = $_POST['firstname'];
		$lastname = $_POST['lastname'];
		$position = $_POST['position'];
		$contact = $_POST['contact'];
		$polling_station_code = $_POST['polling_station_code'];
		$polling_station_name = $_POST['polling_station_name'];
		$constituency=$_POST['constituency'];
		$gender = $_POST['gender'];
		$voter_id = $_POST['voter_id'];
		$filename = $_FILES['photo']['name'];
		$msg="Dear $lastname, you have successfully registered as a member of NDC, Western North with voter id $voter_id. Unity, Stability and Development";
		if(!empty($filename)){
			move_uploaded_file($_FILES['photo']['tmp_name'], '../images/'.$filename);	
		}
		$sql1 = "select * from voters where voters_id='$voter_id' or (polling_station='$polling_station_code' and position='$position')";
		$query = $conn->query($sql1);
		if($row=$query->fetch_assoc()>=1){
			if($row['position']!="MEMBER" or $row['voters_id']==$voter_id){
			$_SESSION['error'] = "Executive with Voter ID or Position already Exist!!!";}
			else{
					$sql = "INSERT INTO voters (position, firstname, lastname, photo, polling_station,polling_station_name,constituency,gender,contact,voters_id) VALUES ('$position', '$firstname', '$lastname', '$filename', '$polling_station_code','$polling_station_name','$constituency','$gender','$contact','$voter_id')";
		if($conn->query($sql)){
			sendmsg($msg,$contact);
			$_SESSION['success'] = 'Executive or Member added successfully';
			#$_SESSION['success'] =$msg;
		}
		else{
			$_SESSION['error'] = $conn->error;
		}
			}
		}
		else{
		$sql = "INSERT INTO voters (position, firstname, lastname, photo, polling_station,polling_station_name,constituency,gender,contact,voters_id) VALUES ('$position', '$firstname', '$lastname', '$filename', '$polling_station_code','$polling_station_name','$constituency','$gender','$contact','$voter_id')";
		if($conn->query($sql)){
			sendmsg($msg,$contact);
			$_SESSION['success'] = 'Executive or Member added successfully';
			#$_SESSION['success'] =$msg;
		}
		else{
			$_SESSION['error'] = $conn->error;
		}

	}
	}
	else{
		$_SESSION['error'] = 'Fill up add form first';
	}

	header('location: candidates.php');
?>