<?php include_once "header.php" ?>
<?php

$mailid = ew_RemoveXSS(ew_StripSlashes($_GET['id']));
//die("mailid=".$mailid);

$tmpid = $user_id;

// UPDATE MAIL AS READ
$sql = "UPDATE MAILBOX
           SET MAILREAD=1
         WHERE (ID      =:mailid
            OR PARENTID =:parentid)
       ";

//if ($_SESSION[EW_SESSION_USER_LEVEL]==50){
if ($_SESSION[EW_SESSION_USER_LEVEL]==-999 || $_SESSION[EW_SESSION_USER_LEVEL]==-1 || $_SESSION[EW_SESSION_USER_LEVEL]==50){
    $sql   .= "AND (MAILTO IN (SELECT USERID 
                                 FROM WEBUSER
                                WHERE USERLEVEL IN (-999, -1, 50)
                              )
                    OR MAILTO=:uid)";
    $tmpid  = 'JHEKP';
} else {
    $sql .= "AND MAILTO   =:uid";
}

$stmt = $DB_con->prepare($sql);
$stmt->bindparam(":uid",      $tmpid);
$stmt->bindparam(":mailid",   $mailid);
$stmt->bindparam(":parentid", $mailid);
$stmt->execute();


?>

<!-- Full Width Column -->
<div class="content-wrapper">
    <div class="container">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            Mailbox
            <small><?php echo $totalData;?> new message(s)</small>
          </h1>
          <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i>Home</a></li>
            <li>Mailbox</li>
            <li class="active">Read Mail</li>
          </ol>
        </section>

        <!-- Main content -->
        <section class="content">
          <div class="row">
            <div class="col-md-3">
              <a href="mailbox-compose.php" class="btn btn-primary btn-block margin-bottom">Compose</a>

              <div class="box box-solid">
                <div class="box-header with-border">
                  <h3 class="box-title">Folders</h3>

                  <div class="box-tools">
                    <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
                    </button>
                  </div>
                </div>
                <div class="box-body no-padding">
                  <ul class="nav nav-pills nav-stacked">
                    <li><a href="mailbox.php"><i class="fa fa-inbox"></i> Inbox
                      <span class="label label-primary pull-right"><?php echo $totalData;?></span></a></li>
                    <!--<li><a href="#"><i class="fa fa-envelope-o"></i> Sent</a></li>-->
                  </ul>
                </div>
                <!-- /.box-body -->
              </div>
              <!-- /. box -->
              <!--
              <div class="box box-solid">
                <div class="box-header with-border">
                  <h3 class="box-title">Labels</h3>

                  <div class="box-tools">
                    <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
                    </button>
                  </div>
                </div>
                <div class="box-body no-padding">
                  <ul class="nav nav-pills nav-stacked">
                    <li><a href="#"><i class="fa fa-circle-o text-red"></i> Important</a></li>
                    <li><a href="#"><i class="fa fa-circle-o text-yellow"></i> Promotions</a></li>
                    <li><a href="#"><i class="fa fa-circle-o text-light-blue"></i> Social</a></li>
                  </ul>
                </div>
              </div>
              -->
              <!-- /.box -->
            </div>
            <!-- /.col -->
            <div class="col-md-9">
              <div class="content-wrapper">
                <div class="box-header with-border">
                  <h3 class="box-title">Read Mail</h3>

                  <div class="box-tools pull-right">
                    <a href="#" class="btn btn-box-tool" data-toggle="tooltip" title="Previous"><i class="fa fa-chevron-left"></i></a>
                    <strong>Ticket ID: #<?php echo $mailid;?></strong>
                    <a href="#" class="btn btn-box-tool" data-toggle="tooltip" title="Next"><i class="fa fa-chevron-right"></i></a>
                  </div>
                </div>
                <!-- /.box-header -->
                <div class="box-body no-padding">
                  <!-- ALL MAIL CONVERSATIONS HERE -->
                  <ul class="timeline">
                      <?php

                      $dateprevious = "";

                      // retrieve MAILBOX table
                      $sql  =  "
                                SELECT  A.*
                                       ,B.FULLNAME AS MAILFROMNAME
                                       ,C.FULLNAME AS MAILTONAME
                                  FROM MAILBOX A
                                  LEFT OUTER JOIN WEBUSERPROFILE B
                                    ON B.USERID = A.MAILFROM
                                  LEFT OUTER JOIN WEBUSERPROFILE C
                                    ON C.USERID = A.MAILTO
                                 WHERE A.ID      =:mailid
                                    OR A.PARENTID=:parentid
                                   ORDER BY A.DATECREATED DESC  
                               ";
                      $stmt = $DB_con->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
                      $stmt->bindparam(":mailid",   $mailid);
                      $stmt->bindparam(":parentid", $mailid);
                      $stmt->execute();
                      while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                          $parentid     = trim($row["PARENTID"   ]);
                          $mailfrom     = $row["MAILFROM"    ];
                          $mailfromname = $row["MAILFROMNAME"];
                          $mailto       = $row["MAILTO"      ];
                          $mailtoname   = $row["MAILTONAME"  ];
                          $mailsubject  = $row["MAILSUBJECT" ];
                          $mailmessage  = $row["MAILMESSAGE" ];
                          $datecreated  = $row["DATECREATED" ];
                          $datecreated2 = new DateTime($datecreated);
                          $dateprevious = $dateonly;
                          $dateonly     = $datecreated2->format('d M. Y');

                          //$datecreated  = new DateTime($datecreated);
                          //$datecreated  = $datecreated->format(DateTime::ATOM);

                          if ($mailfrom==$user_id){
                              $title = "You sent <strong>$mailtoname ($mailto)</strong> an email";
                              $inputMailto = $mailto;
                          } else {
                              $title = "<strong>$mailfromname ($mailfrom)</strong> sent you an email";
                              $inputMailto = $mailfrom;
                          }

                          // check the main mail, to determine whether when click reply, set
                          // correct replyto
                          if ($parentid==""){
                              $parentid=$mailid;
                              if ($mailfrom==$user_id){
                                  $inputMailto = $mailto;
                              } else {
                                  $inputMailto = $mailfrom;
                              }
                          }

                          if ($dateprevious=="" || $dateprevious!==$dateonly){
                      ?>

                          <!-- timeline time label -->
                          <li class="time-label">
                                <span class="bg-red">
                                  <?php echo $dateonly;?>
                                </span>
                          </li>
                          <!-- /.timeline-label -->
                      <?php
                          }
                      ?>

                          <!-- timeline item -->
                          <li>
                            <i class="fa fa-envelope bg-blue"></i>

                            <div class="timeline-item">
                              <span class="time"><i class="fa fa-clock-o"></i> <time class="timeago" datetime="<?php echo $datecreated;?>"><?php echo $datecreated;?></time></span>
                              <!--<span class="time"><i class="fa fa-clock-o"></i> <time class="timeago" datetime="2017-03-21T18:00:00Z">2017-03-21T15:00:00Z</time></span>-->

                              <h3 class="timeline-header"><?php echo $title;?></h3>
                              <h3 class="timeline-header bg-aqua"><?php echo $mailsubject;?></h3>

                              <div class="timeline-body">
                                <?php echo $mailmessage;?>
                              </div>
                              <div class="timeline-footer">
                                <form id="mailcompose-form" action="mailbox-compose.php" role="form" method="POST">
                                <input type="hidden" name="inputSubject"  value="<?php echo $mailsubject;?>">
                                <input type="hidden" name="inputTo"       value="<?php echo $inputMailto;?>">
                                <input type="hidden" name="inputParentID" value="<?php echo $parentid;?>">
                                <button type="submit" name="inputReply" class="btn btn-primary btn-xs">Reply</button>
                                </form>
                                <!--<a class="btn btn-danger btn-xs">Delete</a>-->
                              </div>
                            </div>
                          </li>
                          <!-- END timeline item -->

                      <?php
                      }
                      ?>

                  </ul>
                  <!-- ALL MAIL CONVERSATIONS END  -->
                </div>
                <!-- /.box-body -->
                <!--
                <div class="box-footer">
                  <div class="pull-right">
                    <button type="button" class="btn btn-default"><i class="fa fa-reply"></i> Reply</button>
                    <button type="button" class="btn btn-default"><i class="fa fa-share"></i> Forward</button>
                  </div>
                  <button type="button" class="btn btn-default"><i class="fa fa-trash-o"></i> Delete</button>
                  <button type="button" class="btn btn-default"><i class="fa fa-print"></i> Print</button>
                </div>
                -->
                <!-- /.box-footer -->
              </div>
              <!-- /. box -->
            </div>
            <!-- /.col -->
          </div>
          <!-- /.row -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
</div>

<?php include_once "footer.php" ?>