<?php
include_once "../../includes/connectConfig.php";


		//CHECK IF IT A FETCH SET , THEN FETCH ;
		if(isset($_POST['fetch_rec_edit']))
		{
			$postid = test_input($_POST['postid']);
			
			$snd = $db_con->query("SELECT * FROM tks_users  WHERE tus_id = '$postid' ");
			$snd->execute();
			$result = $snd->fetch();			
			
			$ch_gm = ($result['gender'] == "Male") ? "selected" : "";
			$ch_gf = ($result['gender'] == "Female") ? "selected" : "";
			
			$output["fname"] = $result['firstname'];
			$output["lname"] = $result['lastname'];
			$output["genda"] = '<option value="Male" '.$ch_gm.'>Male</option> <option value="Female" '.$ch_gf.'>Female</option>';
			$output["email"] = $result['email'];
			$output["phone"] = $result['phone'];
			
			$output["recid"] = $result['tus_id'];
			echo json_encode($output);
			exit;
		}
		
		else
		{
			$f_name = test_input($_POST['fname']);
			$l_name = test_input($_POST['lname']);
			$e_mail = test_input($_POST['email']);
			$ph_onenum = test_input($_POST['phonenum']);
			$g_enda = test_input($_POST['genda']);
			$catnumu = test_input($_POST['catnumu']);
			
			$passdan = $st_hasher->HashPassword('password');
			
			/***************************************CHECK IF INPUT FIELD EMPTY , THEN INSERT INTO ************************/		
			if($catnumu == "")
			{
				$chk_email = $db_con->prepare("SELECT email,delete_status FROM tks_users  WHERE email=:email AND delete_status = 0");
				$chk_email->execute(array(":email"=>$e_mail));
				$count_mail = $chk_email->rowCount();
				
				if($count_mail == 0)
				{
					$ins_usr = $db_con->prepare("INSERT INTO tks_users (firstname, lastname, gender, email, phone, password, user_type, acc_stp) VALUES(:flname, :lfname, :gend, :mail, :phone, :acpass, 1, 0)");
					$ins_usr->bindParam(':flname',$f_name);
					$ins_usr->bindParam(':lfname',$l_name);
					$ins_usr->bindParam(':gend',$g_enda);
					$ins_usr->bindParam(':mail',$e_mail);
					$ins_usr->bindParam(':phone',$ph_onenum);
					$ins_usr->bindParam(':acpass',$passdan);
					
					if($ins_usr->execute())
					{
						$output["runstt"] = "grant";
						$output["errorreason"] = "Account Saved";
						echo json_encode($output);
						exit;
					}
					else
					{
						$output["runstt"] = "warant";
						$output["errorreason"] = "Failed to Save Account";
						echo json_encode($output);
						exit;
					}
					
				}
				else
				{
					$output["runstt"] = "warant";
					$output["errorreason"] = "Email already registered";
					echo json_encode($output);
					exit;
				}
			}
			else
			{
				$chk_email = $db_con->prepare("SELECT email,delete_status FROM tks_users  WHERE email='$e_mail' AND delete_status = 0 AND tus_id <> '$catnumu' ");
				$chk_email->execute();
				$count_mail = $chk_email->rowCount();
				
				if($count_mail == 0)
				{
					$ins_usr = $db_con->prepare("UPDATE tks_users  SET firstname=:flname, lastname=:lfname, gender=:gend, email=:mail, phone=:phone WHERE tus_id=:catnumu");
					$ins_usr->bindParam(':flname',$f_name);
					$ins_usr->bindParam(':lfname',$l_name);
					$ins_usr->bindParam(':gend',$g_enda);
					$ins_usr->bindParam(':mail',$e_mail);
					$ins_usr->bindParam(':phone',$ph_onenum);
					$ins_usr->bindParam(':catnumu',$catnumu);
					
					if($ins_usr->execute())
					{
						$output["runstt"] = "grant";
						$output["errorreason"] = "Account Updated";
						echo json_encode($output);
						exit;
					}
					else
					{
						$output["runstt"] = "warant";
						$output["errorreason"] = "Failed to Update Account Detailed";
						echo json_encode($output);
						exit;
					}
				}
				else
				{
					$output["runstt"] = "warant";
					$output["errorreason"] = "Invalid Account Record";
					echo json_encode($output);
					exit;
				}
			}
			
			exit;
		}
		
?>
