<?php

function editProfile()
{
  global $tblUser;
  global $user;

  $filter=['id'=>$user['id']];

  $cnt=$tblUser->count($filter);

  if($cnt>0)
  {
    $rec=$tblUser->findOne($filter);
    if(array_key_exists("operation",$_REQUEST))
    {
      if($_REQUEST['operation']=="add-dancer")
      {
        editProfileAddDancer($rec);
        return 0;
      }

      if($_REQUEST['operation']=="remove-dancer")
      {
        editProfileRemoveDancer($rec);
        return 0;
      }

      if($_REQUEST['operation']=="edit-dancer")
      {
        editProfileEditDancer($rec);
        return 0;
      }

      if($_REQUEST['operation']=="contact-preferences")
      {
        editProfileContactPreferences($rec);
        return 0;
      }
    }

    displayProfile($rec);
  }
  else
  {
    echo "Could not find user";
  }
}

function displayProfile($rec)
{
  global $tblUser;
  global $user;

  echo "<h1 style=\"padding-top: 30px; padding-bottom: 10px;\">Edit Profile</h1>";
  echo "<table class=\"with-borders\">";
  echo "<tr><th>Picture:</th><td><img src=\"".$rec['picture']."\" style=\"width: 40px; height: 40px;\"></td></tr>";
  echo "<tr><th>Name:</th><td>".$rec['name']."</td></tr>";

  if(array_key_exists("role",$rec))
  {
    echo "<tr><th>Roles:</th><td>";
    foreach($rec['role'] as $role=>$val)
    {
      echo $role."<br />";
    }
    echo "</td></tr>";
  }

  echo "<tr><th>Email:</th><td>".$rec['email']."</td></tr>";

  echo "<tr><th>Dancers:</th><td>";
  if(array_key_exists('dancer',$rec))
  {
    foreach($rec['dancer'] as $ck=>$dancer)
    {
      echo $dancer['name']." ";
      if(strlen($dancer['email'])>0) echo "(".$dancer['email'].") ";
      echo "<a class=\"button\" href=\"profile.php?operation=edit-dancer&ck=$ck\"><i class=\"fas fa-pencil-alt\"></i> Edit</a>";
      echo "<a class=\"button\" href=\"profile.php?operation=remove-dancer&ck=$ck\"><i class=\"fas fa-trash\"></i> Remove</a>";
      echo "<br />";
    }
  }
  echo "<a class=\"button\" href=\"profile.php?operation=add-dancer\"><i class=\"fas fa-plus\"></i> Add Dancer</a>";
  echo "</td></tr>";

  echo "<tr><th>Contact Preferences:</th><td>";
  if(array_key_exists('preferences',$rec))
  {
    echo $rec['preferences']."<br />";
  }

  echo " <a class=\"button\" href=\"profile.php?operation=contact-preferences\">Update</a>";
  echo "</td></tr>";

  echo "</table>";
}

function editProfileContactPreferences($rec)
{
  global $tblUser;
  global $user;

  $filter=['id'=>$user['id']];

  $pref="!"; if(array_key_exists("preferences",$_REQUEST)) $pref=$_REQUEST['preferences'];
  $val1="ok"; 
  $updated=0;

  if($pref!="!")
  {
    if(strlen($pref)==0) $val1="Preferences Not Supplied";

    if($val1=='ok')
    {
      $op=['$set'=>["preferences"=>$pref,"preferencescaptured"=>time()]];
      $result=$tblUser->updateOne($filter,$op); 
      
      $updated=1;
      $rec=$tblUser->findOne($filter);
    }
    else
    {
      echo "<b style=\"color: red;\">$val1</b>";
    }
  }

  if($updated==1)
  {
    displayProfile($rec);
    return 0;
  }
  else
  {
    $currPref="!"; if(array_key_exists('preferences',$rec)) $currPref=$rec['preferences'];

    echo "<h2 style=\"padding-top: 30px; padding-bottom: 10px;\">Add Dancer</h2>";
    echo "<form method=\"POST\" action=\"profile.php\">";
    echo "<table class=\"with-borders\">";
    echo "<tr><th>Name:</th><td>".$rec['name'];
    echo "</td></tr>";
    echo "<tr><th>Email:</th><td>".$rec['email'];
    echo "</td></tr>";
    echo "<tr><th>Current Choice:</th><td>".$rec['preferences'];
    echo "</td></tr>";
         
    echo "<tr><th>Contact Preferences:</th><td><select name=\"preferences\">";
    echo "<option value=\"\">-- Contact Preferences --</option>";
    opt("Add me to the 'Dance for Passion' mailing list so that I can keep up to date with events.",$currPref);
    opt("Only contact me to tell me when a new video is available.",$currPref);
    opt("Don't contact me using this email address",$currPref);
    echo "</select>";
    echo "</td></tr>";
    echo "</table>";
    echo "<button type=\"submit\" name=\"operation\" value=\"contact-preferences\">Update</button>";
    echo "<a class=\"button\" href=\"profile.php\">Cancel</a>";
    echo "</form>";
    return 0;
  }
}

function opt($opt,$current)
{
  echo "<option value=\"$opt\"";
  if ($opt==$current) echo " selected";
  echo ">$opt</option>";
}

function editProfileAddDancer($rec)
{
  global $tblUser;
  global $user;

  $filter=['id'=>$user['id']];

  $name=""; if(array_key_exists("name",$_REQUEST)) $name=$_REQUEST['name'];
  $cksum=cksum($name);
  $email=""; if(array_key_exists("email",$_REQUEST)) $email=$_REQUEST['email'];
  $val1="ok"; 
  $updated=0;

  if(array_key_exists("name",$_REQUEST))
  {
    if(strlen($name)==0) $val1="Name Not Supplied";
    if(array_key_exists($cksum,$rec['dancer'])) $val1="A dancer with the name '$name' is already on your account. Please supply a different name.";

    $ck=$val1;

    if($ck=='ok')
    {
      $dancer=array();
      $dancer['name']=$name;
      $dancer['email']=$email;
      $dancer['cksum']=$cksum;
  
      $op=['$set'=>["dancer.".$cksum=>$dancer]];
      $result=$tblUser->updateOne($filter,$op); 
      
      $updated=1;
      $rec=$tblUser->findOne($filter);
    }
  }

  if($updated==1)
  {
    displayProfile($rec);
    return 0;
  }
  else
  {
    if(array_key_exists('dancer',$rec))
    {
      echo "<h2 style=\"padding-top: 30px; padding-bottom: 10px;\">Existing Dancers on Your Profile</h2>";
      echo "<table class=\"with-borders\">";
      foreach($rec['dancer'] as $ck=>$dancer)
      {
        echo "<tr><th>Dancer Name:</th><td>".$dancer['name']."</td><td>".$dancer['email']."</td></tr>";
      }
      echo "</table>";
    }
   
    if($val1!="ok") echo "<b style=\"color: red;\">$val1</b>";

    echo "<h2 style=\"padding-top: 30px; padding-bottom: 10px;\">Add Dancer</h2>";
    echo "<form method=\"POST\" action=\"profile.php\">";
    echo "<table class=\"with-borders\">";
    echo "<tr><th>Dancer Name:</th><td><input type=\"text\" name=\"name\" value=\"$name\"></td><td>";
    echo "</td></tr>";
         
    echo "<tr><th>Dancer Email:</th><td><input type=\"text\" name=\"email\" value=\"$email\"></td><td>(Optional)</td></tr>";
    echo "</table>";
    echo "<button type=\"submit\" name=\"operation\" value=\"add-dancer\">Add Dancer</button>";
    echo "<a class=\"button\" href=\"profile.php\">Cancel</a>";
    echo "</form>";
    return 0;
  }
}

function editProfileEditDancer($rec)
{
  global $tblUser;
  global $user;

  $filter=['id'=>$user['id']];

  $name=""; if(array_key_exists("name",$_REQUEST)) $name=$_REQUEST['name'];
  $newcksum=cksum($name);
  $email=""; if(array_key_exists("email",$_REQUEST)) $email=$_REQUEST['email'];
  $oldcksum="!"; if(array_key_exists("ck",$_REQUEST)) $oldcksum=$_REQUEST['ck'];

  $val1="ok"; 
  $updated=0;

  if(array_key_exists("name",$_REQUEST))
  {
    $oper="replace"; if($oldcksum==$newcksum) $oper="update";

    if(strlen($name)==0) $val1="Name Not Supplied";
    if(array_key_exists($newcksum,$rec['dancer']) and $oper=="replace") $val1="A dancer with the name '$name' is already on your account. Please supply a different name.";
    if(!array_key_exists($newcksum,$rec['dancer']) and $oper=="update") $val1="Could not find dancer '$name' on your account. Please Try Again.";
    if(!array_key_exists($oldcksum,$rec['dancer']) and $oper=="replace") $val1="Dancer '$name' might already have been updated. Please Try Again.";

    if($val1!="ok") 
    {
      echo "<b style=\"color: red;\">$val1</b>";
    }
    else
    {
      $dancer=array();
      $dancer['name']=$name;
      $dancer['email']=$email;
      $dancer['cksum']=$cksum;
  
      if($oper=="replace")
      {
        $op=['$unset'=>["dancer.".$oldcksum=>1]];
        $result=$tblUser->updateOne($filter,$op); 
      }

      $op=['$set'=>["dancer.".$newcksum=>$dancer]];
      $result=$tblUser->updateOne($filter,$op); 
      
      $updated=1;
      $rec=$tblUser->findOne($filter);
    }
  }

  if($updated==1)
  {
    displayProfile($rec);
    return 0;
  }
  else
  {
    if(array_key_exists('dancer',$rec))
    {
      echo "<h2 style=\"padding-top: 30px; padding-bottom: 10px;\">Existing Dancers on Your Profile</h2>";
      echo "<table class=\"with-borders\">";
      foreach($rec['dancer'] as $ck=>$dancer)
      {
        if($oldcksum==$ck)
        {
          echo "<tr style=\"background:#FFFF00\"><th>Dancer Name:</th><td>".$dancer['name']."</td><td>".$dancer['email']."</td></tr>";
          if(strlen($name)==0)
          {
            $name=$dancer['name']; 
            $email=$dancer['email'];
          }
        }
        else
        {
          echo "<tr><th>Dancer Name:</th><td>".$dancer['name']."</td><td>".$dancer['email']."</td></tr>";
        }
      }
      echo "</table>";
    }
   

    echo "<h2 style=\"padding-top: 30px; padding-bottom: 10px;\">Edit Dancer</h2>";
    echo "<form method=\"POST\" action=\"profile.php\">";
    echo "<table class=\"with-borders\">";
    echo "<tr><th>Dancer Name:</th><td><input type=\"text\" name=\"name\" value=\"$name\"></td><td>";
    echo "</td></tr>";
         
    echo "<tr><th>Dancer Email:</th><td><input type=\"text\" name=\"email\" value=\"$email\"></td><td>(Optional)</td></tr>";
    echo "</table>";
    echo "<input type=\"hidden\" name=\"ck\" value=\"$oldcksum\">";
    echo "<button type=\"submit\" name=\"operation\" value=\"edit-dancer\">Update</button>";
    echo "<a class=\"button\" href=\"profile.php\">Cancel</a>";
    echo "</form>";
    return 0;
  }
}

function editProfileRemoveDancer($rec)
{
  global $tblUser;
  global $user;

  $filter=['id'=>$user['id']];

  $ck="ok";
  $cksum="!"; if(array_key_exists("ck",$_REQUEST)) $cksum=$_REQUEST['ck'];
  $updated=0;

  if(!array_key_exists($cksum,$rec['dancer'])) $ck="This dancer might already have been deleted";

  if($ck=='ok')
  {
    $op=['$unset'=>["dancer.".$cksum=>1]];
    $result=$tblUser->updateOne($filter,$op); 
      
    $updated=1;

    $rec=$tblUser->findOne($filter);
  }

  if($updated==1)
  {
    displayProfile($rec);
    return 0;
  }
  else
  {
    echo "<p><strong>$ck</strong></p>";
    displayProfile($rec);
    return 1;
  }
}
?>

