<?
session_start();
?>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.3.0/base-min.css">
<?php
include 'dbconnect.php';
$sql= $dbh-prepare("CREATE TABLE IF NOT EXISTS `mp3s` (
  `id` int(5) NOT NULL auto_increment,
  `songname` varchar(255) NOT NULL,
  `artist` varchar(255) NOT NULL,
  `songlength` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`))");
  $sql->execute();

//this connects to the db with a refenceable object: $dbh
//login section
//needs to get submission and check with database
//need a form with 2 fields

$submit = $_POST['submit'];
if ($submit  ==  'Sign up'){
	$username = $_POST['username'];
	$password = $_POST['password'];
	$sql = $dbh->prepare("select username from contact_control where username = ? and password = ?");

  $sql->execute(array($username,$password));
	
	

	$nummatches = 0;
	while ($row = $sql->fetch()){
	$uname = $row['username'];
	$nummatches++;	
	}
	if ($nummatches > 0) {
	$_SESSION['approved'] = 'yes';
	$_SESSION['uname'] = $uname;	 
	}
	
}


if ($_SESSION['approved'] == 'yes'){ 
echo ' Welcome, '.$_SESSION['uname'].'<br/>';
//$dbh = new PDO("mysql:host=localhost;dbname=mmaaaco_jmoreland", "mmaaaco_jmorelan", "jmoreland361521");
//insert
$status = 'Enter Name';//for the button value 
$deleteid = $_GET['deleteid'];//gets the query string when the user clicks delete lnk
$songname = $_POST['songname'];//songname field from the form via post method
$editid = $_GET['editid'];//if edit link is clicked
$artist = $_POST['artist'];
$songlength = $_POST['songlength'];

//if delete is clicked 
 if (is_numeric($deleteid)){  //is the url value a number? 
 $delsql = $dbh->prepare("delete from mp3s where id = ?");//prepare statement
 $delsql->bindValue(1,$deleteid);//bind the value of the variable to the ?
 $delsql->execute();//run it
if ($delsql) echo 'Record Deleted<br/>';//$delsql is boolean...did it work?
 }



//edit block!
if ($submit == 'Edit Name'){//tests value of dynamic button
$updatesql = $dbh->prepare("update mp3s set songname = ?,artist=?,songlength=? where id = ?");//update statement
$updatesql->execute(array($songname,$artist,$songlength,$editid));//an array because more than one ?
if ($updatesql) echo 'Record updated!<br/>';//confirmation
}

//insert block
if ($submit == 'Enter Name'){
//$insql = $dbh->prepare("insert into mp3s (songname) values ('$songname')"); 
//bind value for security:
$insql = $dbh->prepare("insert into mp3s (songname,artist,songlength) values (?,?,?)"); //prepared statement
$insql->execute(array($songname,$artist,$songlength));//run it

  if ($insql) echo 'Record Inserted <br/>';//confirm
  else print_r($insql->errorInfo());//how to error handle!!!!!!
}

//this is the block to populate the text field with the id of what is being edited...note $editid 
if (is_numeric($editid)){
$status = 'Edit Name';
$upsql = $dbh->prepare("select songname,artist,songlength from mp3s where id = ?");
$upsql->bindValue(1,$editid);
$upsql->execute();
$uprow = $upsql->fetch();
$upsongname = $uprow['songname'];//a variable to put in the text field so the user can edit!
$upartist = $uprow['artist'];
$upsonglength = $uprow['songlength'];
}


//display records!!
$sql = $dbh->prepare("select id,songname from mp3s");
$sql->execute();
while($row = $sql->fetch()){//fetches records in a loop 
$id = $row['id'];//get id to display in links
$songname = $row['songname'];//get id to display name
echo $songname.'   <a href = "cms.php?deleteid='.$id.'" 
onclick="return confirm(\' Are you sure you want to delete '.$songname.'\');">Delete</a> 
 <a href = "cms.php?editid='.$id.'"> Edit</a><br/>';  //what the user sees!!!!!!!
}

?>

<form action = "cms.php?<?= $_SERVER['QUERY_STRING'];//keep query string so we can see record being edited?>" method = "post">
Song Name: <input type = "text" name = "songname" value="<?= $upsongname;?>"/>
artist: <input type = "text" name = "artist" value="<?= $upartist;?>"/>
songlength: <input type = "text" name = "songlength" value="<?= $upsonglength;?>"/>
<input type = "submit" name = "submit" value = "<?= $status;//either edit or add?>"/>
</form> 
<br/>
<a href = "logout.php">Log Out</a><br/>
<?
if ($editid){
?>
<a href = "cms.php">Return to add mode</a> <!-- gets us out of edit mode!-->
<?
}
}
else
{
?>
<form  method="post" class="pure-form pure-form-stacked" action ="cms.php"  >
    <fieldset>
        <legend>Login</legend>

        <input type="text" name = "username" placeholder="Username">
        <input type="password" name="password" placeholder="Password">

        <label for="remember">
            <input id="remember" type="checkbox"> Remember me
        </label>

        <input  type="submit" name = "submit" class="pure-button pure-button-primary " value = "Sign up">
    </fieldset>
</form>
<?
}
?>