<?php 
require_once 'core.php';

function tampil_user_aktifitas_terakhir($user_id){

    global $link;
    
    $query ="SELECT * FROM tluser_detail 
             WHERE ftusername='$user_id'
             ORDER BY fdlast_activity DESC LIMIT 1 
            ";
    $result = mysqli_query($link,$query);
    while($data = mysqli_fetch_assoc($result)){
  
    return $data['fdlast_activity']; 
  
    }  
  }

function fetch_user_chat_history($from_user_id,$to_user_id)
  {
    global $link;
    
    $querys = "SELECT * FROM txmessage_chat 
              WHERE (ftname_sender ='$from_user_id' 
              AND ftrecipient_msg ='$to_user_id' ) 
              OR
              (ftname_sender = '$to_user_id'
              AND ftrecipient_msg='$from_user_id')
              ORDER BY fdcreated_date asc";
   
    $results = mysqli_query($link,$querys);

    $output = '<ul class="list-unstyled id="animasi">';
 
    while($data = mysqli_fetch_assoc($results)){
     $user_name = '';
     $color ='';
     $posisi ='';
    if($data["ftname_sender"] == $from_user_id){
   
    $user_name = '<b class="text-success"></b>';
    $color = '#f4f5f6';
    $posisi = "align='right'";

    $user = $data['ftfill_msg'].' '. $user_name;
    }else{
     
      $user_name = '<b class="text-danger">'.tampil_nama($data["ftname_sender"]).'</b>';
      $color = '#dfe7f9';
      $user =  $user_name.' - '.$data['ftfill_msg']; 
    }
      $output .= '

      <p '.$posisi.' style="background-color:'.$color.'; border:solid;  border-width:0; border-radius: 13px; padding: 10px 10px 10px 10px;  margin-top:10px; margin-bottom:5px;">
      '.$user.'
        <div '.$posisi.'">
         <small><em>'.$data['fdcreated_date'].'</em></small>
        </div>
      </p>
      </li>
      ';
    }
    $output .= '</ul>';
    
    // $status_update = "UPDATE txmessage_chat 
    //                   SET fnstatus='0' 
    //                   WHERE ftname_sender='$to_user_id' 
    //                   AND ftrecipient_msg='$from_user_id'
    //                   AND fnstatus='1'";

    // mysqli_query($link,$status_update);

    return $output;
} 

function tampil_nama($user_id){
 
 global $link;

 $query = "SELECT ftusername FROM tluser WHERE fnid = '$user_id'"; 

 $result = mysqli_query($link,$query);

 $data = mysqli_fetch_assoc($result);
 
 return $data['ftusername'];
}


function pemberitahuan($dikirim,$pengirim){

  global $link;

  $query = "
      SELECT * FROM txmessage_chat WHERE ftname_sender='$dikirim' 
      AND ftrecipient_msg='$pengirim' AND fnstatus ='1'
  ";

  $result = mysqli_query($link,$query);

  $data = mysqli_num_rows($result);

  return $data;
}



function tampil_chat_guest($email,$email_guest){

  global $link;
  
  $querys = "SELECT * FROM txmessage_guest 
            WHERE (ftemail ='$email' 
            AND ftrecipient_msg ='$email_guest' ) 
            OR
            (ftemail = '$email_guest'
            AND ftrecipient_msg='$email')
            ORDER BY fdcreated_date asc";
  
  $results = mysqli_query($link,$querys);

  $output = '<ul class="list-unstyled id="animasi">';

  while($data = mysqli_fetch_assoc($results)){
   $user_name = '';
   $color ='';
   $posisi ='';
  
  if($data["ftemail"] == $email){
 
  $user_name = '<b class="text-success"></b>';
  $color = '#f4f5f6';
  $posisi = "align='right'";

  $user = $data['ftfill_msg'].''. $user_name;
  }else{
   
    $user_name = '<b class="text-danger">'.tampil_nama_guest($data["ftusername"]).'</b>';
    $color = '#dfe7f9';
    $user =  $user_name.' - '.$data['ftfill_msg']; 
  }
    $output .= '
    <p '.$posisi.' style="background-color:'.$color.'; border:solid;  border-width:0; border-radius: 13px; padding: 10px 10px 10px 10px;  margin-top:10px; margin-bottom:5px;">
    '.$user.'
      <div '.$posisi.'">
       <small><em>'.$data['fdcreated_date'].'</em></small>
      </div>
    </p>
    </li>
    ';
  }
  $output .= '</ul>';

  return $output;

}  

function tampil_nama_guest($nama){

//  $query  = "SELECT ftfirst_name FROM tluser WHERE fnid = '$nama'";
//  $result = mysqli_query($link,$query);
//  $data   = mysqli_fetch_assoc($result);
//  return $data['ftfirst_name']; 
return $nama;
}

?>
