<?php

//Victor Chen CH13 Web 153 Assignment

//this is outputting errors if it is error_reporting(E_ALL)
error_reporting(0);
$dbh = new PDO('mysql:host=localhost;dbname=somedb', 'root', '');

if($_SERVER['REQUEST_METHOD'] == 'POST' && $_SERVER['HTTP_REFERER'] == 'http://localhost:8888/ch13/contactcrud.php') {
	//these are the data that comes from the form
	$fullname = $_POST['fullname'];
	$email = $_POST['email'];
	$phone = $_POST['phone'];
	//this gets the id to update from the hidden fields!!!
	$idtoupdate = $_POST['idtoupdate'];
}
if($_SERVER['REQUEST_METHOD'] == 'GET' && $_SERVER['HTTP_REFERER'] == 'http://localhost:8888/ch13/contactcrud.php') {
	//this means that the deleteid was clicked
	$deleteid = $_GET['deleteid'];
	//this is if the edit is clicked!!! 
	$editid = $_GET['editid'];
}

if ($_POST['submit'] == 'Submit' && $fullname != ''){
    //insert data!!!!
    if (!$idtoupdate){
        /*this could be 
        if (empty($idtoupdate))
        if (!isset($idtoupdate))
        if ($idtoupdate == '')
        */
        //if no idtoupdate
      $inssql = $dbh->prepare("insert into contacts (fullname,email,phone) values (:fullname,:email,:phone)");
	  $inssql->bindValue(':fullname', $fullname, PDO::PARAM_STR);
	  $inssql->bindValue(':email', $email, PDO::PARAM_STR);
	  $inssql->bindValue(':phone', $phone, PDO::PARAM_STR);
  
  	  $inssql->execute(); 
      echo "Your record has been inserted!";
		
      //header("location: crud.php?message=insert");
  
    }
  // print_r($inssql->errorInfo()); <!-- use this when your stuff doesn't work.-->
}

//delete
if ($deleteid){
    $delsql= $dbh->prepare("delete from contacts where id = :deleteid");
	$delsql->bindValue(':deleteid', $deleteid, PDO::PARAM_INT);
    $delsql->execute();
}

if ($idtoupdate){
    //IMPORTANT must have where statement of entire database will update to the new data!!!!
	$upsql = $dbh->prepare("UPDATE contacts SET fullname = :fullname, email = :email, phone = :phone WHERE id = :idtoupdate");//update fullname
	$upsql->bindValue(':fullname', $fullname, PDO::PARAM_STR);
	$upsql->bindValue(':email', $email, PDO::PARAM_STR);
	$upsql->bindValue(':phone', $phone, PDO::PARAM_STR);	
	$upsql->bindValue(':idtoupdate', $idtoupdate, PDO::PARAM_INT);
	$upsql->execute();  //exit
}

//need to display our results so that we see what's working and what's in our database!!!
$selsql = $dbh->prepare("select id,fullname from contacts");
$selsql->execute(); 
while ($row = $selsql->fetch()){
    //outputs fullname
    echo $row['fullname'];
    //output links to delete with confirmation popup!
    echo '<a href="contactcrud.php?deleteid='.$row['id'].'"';
    //this is the popup
    echo ' onclick="return confirm(\'Are you sure you want to delete '.$row['fullname'].'\')" ';
    //close the delete link
    echo '>Delete</a>';
    //now output a link that lets them edit with editid !!!!
    echo ' <a href ="contactcrud.php?editid='.$row['id'].'">Edit</a><br>'; //delete fullname --------->
   }

if (isset($editid)){
    $getupsql = $dbh->prepare("select fullname,email,phone from contacts where id = :editid");
	$getupsql->bindValue(':editid', $editid, PDO::PARAM_INT);
	$getupsql->execute();
    $row = $getupsql->fetch();
    $upfullname = $row['fullname'];
    $upemail = $row['email'];
    $upphone = $row['phone'];
}

?>

<form action ="contactcrud.php" method="post" name="contact_form">
<input type="text" name="fullname"  required placeholder="Full Name Here" value="<?php echo $upfullname;?>"/><br>
<input type="text" name="email"  required placeholder="Your Email Here" value="<?php echo $upemail;?>"/><br>
<input type="text" name="phone"  required placeholder="Your Phone Here" value="<?php echo $upphone;?>"/><br>
<input type="hidden" name="idtoupdate" value="<?php echo $editid;?>"/>    
<input type="submit" name="submit" value="Submit"/>    
</form>