<?_code

 LJ::set_active_crumb('supportact');
 
 my $cmd = BML::get_query_string();
 if ($cmd =~ /^(\w+);(\d+);(\w{15})(?:;(\d+))?$/) {
     ($action, $spid, $authcode, $splid) = ($1, $2, $3, $4);
 }
 $title = "Error";
 if ($action =~ /(?:touch|close|unlock|lock)/) {
     $title = "Request #$spid";
 }
 return;
 
_code?><?page
title=><?_code return $title; _code?>
body<=
<?_code

 if ($title eq "Error") { 
     return "<?h1 Error h1?><?p Improper arguments. p?>";
 }
 my $remote = LJ::get_remote();
 
 LJ::Support::init_remote($remote);
 my $sp = LJ::Support::load_request($spid);

 if ($sp->{'authcode'} ne $authcode) {
     return "<?h1 Error h1?><?p Invalid authcode. p?>";
 }

 if ($action eq "touch") {
     return "<?h1 Error h1?><?p That request has been locked and cannot be reopened. p?>"
         if LJ::Support::is_locked($sp);

     LJ::Support::touch_request($spid)
       or return "<?h1 Error h1?><?p Touch failed. p?>";

     $ret .= ("<?h1 Touched h1?><?p Your support request ".
              ($sp->{'state'} eq "open" ? "will stay open now."
                                        : "has been re-opened now.").
              " Please comment back telling us why you've touched this request:".
              " p?>");

     my $auth = LJ::Support::mini_auth($sp);

     $ret .= '<br /><form method="post" action="append_request.bml">';
     # hidden values
     $ret .= "<input type=\"hidden\" name=\"spid\" value=\"$spid\" />\n";
     $ret .= "<input type=\"hidden\" name=\"auth\" value=\"$auth\" />\n";
     $ret .= "<table border='0'>\n";
 
     $ret .= '<tr valign="middle"><td align="right">From:</td><td>';
     if ($remote && $remote->{'userid'}) {
         $ret .= "<?ljuser $remote->{'user'} ljuser?>";
     } else {
         $ret .= "(not logged in)";
     }
     $ret .= "</td></tr>\n";

     $ret .= "<tr><td align='right'>Reply Type:</td><td>";
     $ret .= '<input type="hidden" name="replytype" value="comment" />';
     $ret .= "<b>More Information</b>";

     $ret .= "</td></tr>";

     # textarea for their message body
     $ret .= '<tr valign="top"><td align="right">Message:</td><td colspan="2">';
     $ret .= '<textarea rows="10" cols="50" name="body"></textarea><br />';
     $ret .= "\n<?de No HTML allowed, so don't worry about about escaping &lt; and &gt;<br />URLs are automatically link-ified, so just reference those. de?><br />\n";
     $ret .= '<input type="submit" name="submitpost" value="Post Comment/Solution" />';
     $ret .= "\n</td></tr></table></form>";

     return $ret;
 }

 if ($action eq 'lock') {
     return "<?h1 Error h1?><?p You are not allowed to lock this request. p?>"
         unless $remote && LJ::Support::can_lock($sp, $remote);
     return "<?h1 Error h1?><?p That request has already been locked. p?>"
         if LJ::Support::is_locked($sp);

     # close this request and IC on it
     LJ::Support::lock($sp);
     LJ::Support::append_request($sp, {
         body => '(Locking request.)',
         remote => $remote,
         type => 'internal',
     });
     return "<?h1 Success h1?><?p The <a href='/support/see_request.bml?id=$sp->{spid}'>request</a> has been locked. p?>";
 }

 if ($action eq 'unlock') {
     return "<?h1 Error h1?><?p You are not allowed to unlock this request. p?>"
         unless $remote && LJ::Support::can_lock($sp, $remote);
     return "<?h1 Error h1?><?p That request is not locked. p?>"
         unless LJ::Support::is_locked($sp);

     # reopen this request and IC on it
     LJ::Support::unlock($sp);
     LJ::Support::append_request($sp, {
         body => '(Unlocking request.)',
         remote => $remote,
         type => 'internal',
     });
     return "<?h1 Success h1?><?p This <a href='/support/see_request.bml?id=$sp->{spid}'>request</a> has been unlocked. p?>";
 }

 if ($action eq "close") {
     if ($sp->{'state'} eq "open") {
         my $dbh = LJ::get_db_writer();
         $splid += 0;
         if ($splid) {
             $sth = $dbh->prepare("SELECT userid, timelogged, spid, type FROM supportlog WHERE splid=$splid");
             $sth->execute;
             my ($userid, $timelogged, $aspid, $type) = $sth->fetchrow_array;

             if ($aspid != $spid) {
                 return "<?h1 Error h1?><?p The answer you credited for helping you out was not a response to your support request. If you copied a URL to get here, make sure you copied the entire URL correctly.p?>";
             }

             ## can't credit yourself.
             if ($userid != $sp->{'requserid'} && $type eq "answer") {
                 my $cats = LJ::Support::load_cats($sp->{'spcatid'});
                 my $secold = $timelogged - $sp->{'timecreate'};
                 my $points = LJ::Support::calc_points($sp, $secold);
                 LJ::Support::set_points($spid, $userid, $points);
             }
         }
         $dbh->do("UPDATE support SET state='closed', timeclosed=UNIX_TIMESTAMP() WHERE spid=$spid");
     }

     my $remote = LJ::get_remote();
     my $is_admin = LJ::check_priv($remote, "supportclose");
     if ($is_admin)
     {
         my $dbr = LJ::get_db_reader();
         my $catid = $sp->{'_cat'}->{'spcatid'};
         my $sql = "SELECT MIN(spid) FROM support WHERE spcatid=$catid AND state='open' AND timelasthelp>timetouched AND spid>$spid";
         my $sth = $dbr->prepare($sql);
         $sth->execute;
         my $next = $sth->fetchrow_array;
         if ($next) {
             return BML::redirect("$LJ::SITEROOT/support/see_request.bml?id=$next");
         } else {
             return "<?h1 Closed h1?>
<ul>
   <li>Go back to <a href='see_request.bml?id=$sp->{'spid'}'>Request #$sp->{'spid'}</a></li>
   <li>Go back to the <a href='help.bml'>open support requests</a></li>
   <li>Go back to the <a href='help.bml?cat=$sp->{'_cat'}->{'catkey'}'>open support requests in the the same category</a></li>
   <li>Go to <a href='see_request.bml?id=$sp->{'spid'}&amp;find=prev'>previous</a> /
             <a href='see_request.bml?id=$sp->{'spid'}&amp;find=next'>next</a> open request</li>
   <li>Go to <a href='see_request.bml?id=$sp->{'spid'}&amp;find=cprev'>previous</a> /
             <a href='see_request.bml?id=$sp->{'spid'}&amp;find=cnext'>next</a> open request in the same category</li>
</ul>";
         }
     } 
     
     return "<?h1 Closed h1?><?p The support request has been closed. p?>";
 }

 return;

_code?>

<=body
page?><?_c <LJDEP>
link: htdocs/support/see_request.bml
</LJDEP> _c?>
