<?php
  include 'dbconnect.php';
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


  $contactId = $_GET['cid'];
  $deleteId = $_GET['did'];
  $updateId = $_POST['uid'];

  if( isset($updateId) ) {
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $comments = $_POST['comments'];
    
    $sql = $pdo->prepare("UPDATE contacts SET first_name='$first_name', last_name='$last_name', email='$email', phone='$phone', comments='$comments' WHERE id='$updateId'");
    
    $sql->execute();
    
    echo "Record successfully updated.";
  }

  if ( isset($deleteId) ) {
    $sql = $pdo->prepare("DELETE FROM contacts WHERE id ='$deleteId'");
    $sql->execute();
    echo "Deleted!";
  }

  $numPerPage = 2;
  $countsql = $pdo->prepare("SELECT count(id) FROM contacts");
  $countsql->execute();
  $row = $countsql->fetch();
  $numRecords = $row[0];
  $numLinks = ceil($numRecords / $numPerPage);
  $page = $_GET['start'];
  if(!$page) $page = 0;
  $start = $page * $numPerPage;
  

  $sql = $pdo->prepare("SELECT * FROM contacts LIMIT $start, $numPerPage");
  $sql->execute();

  ?>
  
  <table>
    <thead>
      <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
        <th>Phone</th>
        <th>Comments</th>
        <th colspan="2">Actions</th>
      </tr>
    </thead>
    
    <tbody>
     
     <?php
      
      while($row = $sql->fetch()) {
        $first_name = $row['first_name'];
        $last_name = $row['last_name'];
        $email = $row['email'];
        $phone = $row['phone'];
        $comments = $row['comments'];
        
        echo '<tr>';
        
        echo '<td>' . $first_name . '</td>';
        echo '<td>' . $last_name . '</td>';
        echo '<td>' . $email . '</td>';
        echo '<td>' . $phone . '</td>';
        echo '<td>' . $comments . '</td>';
        
        echo '<td>' . '<a href="crud.php?did='. $row['id'] .'">Delete</a>' . '</td>';
        echo '<td>' . '<a href="crud.php?cid='. $row['id'] .'">Update</a>' . '</td>';
        
        echo '</tr>';

      }
      
     ?>
      
    </tbody>
  </table>
  <?php
    echo 'Page: ';
    for($i = 0; $i < $numLinks; $i++) {
      echo '<a href="crud.php?start='.$i.'">'.($i + 1).'</a>&nbsp;';
    }

  ?>
  <?php

  if( isset($contactId) ) {
    $sql = $pdo->prepare("SELECT * FROM contacts WHERE id ='$contactId'");
    
    $sql->execute();
    
    $row = $sql->fetch();
    
    $first_name = $row['first_name'];
    $last_name = $row['last_name'];
    $email = $row['email'];
    $phone = $row['phone'];
    $comments = $row['comments'];
  ?>
<form name="contactform" method="post" action="crud.php">
  <input type="hidden" name="uid" value="<?php echo $contactId;  ?>">

  <fieldset>
    <legend>Contact information</legend>
    <table width="500px">
      <tr>
        <td valign="top">
          <label for="first_name">First Name: *</label>
        </td>
        <td valign="top">
          <input  type="text" name="first_name" maxlength="50" size="30" value="<?php echo $first_name ?>" required>
        </td>
      </tr>

      <tr>
        <td valign="top">
          <label for="last_name">Last Name: *</label>
        </td>
        <td valign="top">
          <input  type="text" name="last_name" maxlength="50" size="30" value="<?php echo $last_name ?>" required>
        </td>
      </tr>

      <tr>
        <td valign="top">
          <label for="email">Email Address: *</label>
        </td>
        <td valign="top">
          <input  type="email" name="email" maxlength="80" size="30" value="<?php echo $email ?>" required>
        </td>
      </tr>

      <tr>
        <td valign="top">
          <label for="phone">Phone: *</label>
        </td>
        <td valign="top">
          <input type="tel" id="phone" name="phone"
                 placeholder="123-456-7890"
                 pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
                 value="<?php echo $phone; ?>"
                 required>
        </td>
      </tr>

      <tr>
        <td valign="top">
          <label for="comments">Comments: *</label>
        </td>
        <td valign="top">
          <textarea  name="comments" maxlength="1000" cols="25" rows="6"><?php echo $comments ?></textarea>
        </td>
      </tr>

      <tr>
        <td colspan="2" style="text-align:center">
          <input type="submit" value="Submit">  
        </td>
      </tr>
    </table>
  </fieldset>
</form>
  <?php
  }
  ?>
