<script>

    function documentID(arg) {
        return document.getElementById(arg);
    }
</script>



<div class="col-sm-8">

    <div class="row ">


        <div class="col-sm-12 " style="padding: 0">
            <div class="col-sm-12" >
                <h3 class="pull-right">&nbsp;
                    <a href="<?php echo base_url() ?>send-message"> <span style="font-size: 18px;padding: 10px"><i class="fa fa-envelope" title=" Send Message"></i></span></a>
                    <a href="<?php echo base_url() ?>reply-message"> <span style="font-size: 18px;padding: 10px"><i class="fa  fa-mail-reply" title="Reply Message"></i></span></a>
                    <a href="javascript:;" onclick="dustbin();"> <span style="font-size: 18px;padding: 10px"><i class="fa fa-trash-o" title="Delete "></i></span></a>
                </h3>
            </div>

            <div class="col-sm-12" >


                <div class="col-sm-12 gray_ttc text-left" style="border-bottom: 2px solid #8CC73F;font-weight: bold">
                    <div class="col-sm-4" style="padding:10px 0">
                        <div class="checkbox checkbox-success">
                            <input id="checkbox1" name="checkbox1"class="styled" type="checkbox" ><label for="checkbox1" > <strong>Sender</strong></label>

                        </div>
                    </div>
                    <div class="col-sm-4" style="padding:23px 0">Message</div>

                    <div class="col-sm-4" style="padding:23px 0">Date & Time</div>

                </div>
                <div class="col-sm-12"><br></div> 

<?php 
if($rcv){
foreach ($rcv as $val){
    if($receive_message[$val->message_id]){
     
    ?>
                <div class="col-sm-12 gray_ttc text-left" >

                    <div class="col-sm-4" style="padding:10px 0">
                        <div class="checkbox checkbox-success">
                            <input class="checkbox2" name="checkbox2[]" class="styled" type="checkbox" value="<?php echo $val->message_id ?>" ><label for="checkbox2" >
                                <a href="#"><?php foreach( $person_name[$val->sender_id] as $name){
                                    //print_r($name->person_id);
                                        echo $name->person_email;
                                        $recieve_id =$val->receiver_id; 
                                        $recieve_id_array=explode(",", $recieve_id);
                                        echo "(".count($recieve_id_array).")";
                                
                               
                                }
                                ?></a></label>

                        </div>

                    </div>
                    <div class="col-sm-4" style="padding:23px 0">
                        <p class="text-left col-sm-12">
                            <?php 
                            $message_view=$val->message_viewer_id;
                            $message_view_array=  explode(",",$message_view);
                            //print_r($message_view_array);
                           
                            $user_id = $this->session->userdata('user_id');
                            if(!in_array($user_id,$message_view_array)){ ?>
                            <strong>
                          <?php 
                            $mess= $receive_message[$val->message_id]->message_body; 
                            echo substr($mess, 0,20)."...";
                 
                            ?>
                            </strong>
                            
                            <?php }
                            else { ?>
                           
                          <?php 
                            $mess= $receive_message[$val->message_id]->message_body;
                            echo substr($mess, 0,20)."...";
                            }
                            ?>
                         
                            
                             <?php 
                                  foreach($receive_message_attachment[$val->message_id] as $value){
                                  
                                      if($value->message_receiver_attachment){
                                  
                            ?>
                            <i class="fa fa-paperclip" style="color: #80c12c;"></i>
                                  <?php } break;}?>
                            <a href="<?php echo base_url() ?>view-received-message/<?php $message_id= $this->encrypt->encode($val->message_id);echo str_replace("/","%F2", $message_id); ?>">
                           View
                        </a>
                      
                        </p>
                        
                    </div>

                    <div class="col-sm-4" style="padding:23px 0">
                        <p>
                            
                            <?php                                                        
                         $today_day= date("d m");
                          $message_day=date("d m",strtotime($receive_message[$val->message_id]->message_datetime));
                          $today_year=date("y");
                          //echo $today_year;
                          $message_year=date("y",strtotime($receive_message[$val->message_id]->message_datetime));
                          //echo $message_year;
                          if($message_year==$today_year){
                          if($today_day>$message_day)
                         {
                        echo date('h:i a' , strtotime($receive_message[$val->message_id]->message_datetime)).",";
                        echo date('d F'  , strtotime($receive_message[$val->message_id]->message_datetime));
                             
                         }
                        else {
                        echo date('h:i a' , strtotime($receive_message[$val->message_id]->message_datetime))."<br>";
                        
                        }
                          }
                          else 
                          {
                        echo date('h:i a' , strtotime($receive_message[$val->message_id]->message_datetime)).",";
                        echo date('d F y'  , strtotime($receive_message[$val->message_id]->message_datetime));
                          }
                        ?> 
                        </p>
                    </div>
                </div>
                <div class="col-sm-12"><br></div> 


    <?php } } }?>


<!--                <nav class="course-pagination">
                    <ul class="pagination">
                        <li>
                            <a href="#" aria-label="Previous">
                                <span aria-hidden="true" title="First Page">&laquo;&laquo;</span>
                            </a>
                        </li>
                        <li>
                            <a href="#" aria-label="Previous">
                                <span aria-hidden="true" title="Previous Page">&laquo;</span>
                            </a>
                        </li>
                        <li><a href="#">1</a></li>
                        <li><a href="#">2</a></li>
                        <li><a href="#">3</a></li>
                        <li><a href="#">4</a></li>
                        <li><a href="#">5</a></li>
                        <li>
                            <a href="#" aria-label="Next">
                                <span aria-hidden="true" title="Next Page">&raquo;</span>
                            </a>
                        </li>
                        <li>
                            <a href="#" aria-label="Next">
                                <span aria-hidden="true" title="Last Page">&raquo;&raquo;</span>
                            </a>
                        </li>
                    </ul>
                </nav>-->

            </div>


        </div><!-- /.row gray -->

    </div>

</div>
<script>
    $(document).ready(function() {
    $('#checkbox1').click(function(event) {  //on click
        if(this.checked) { // check select status
            $('.checkbox2').each(function() { //loop through each checkbox
                this.checked = true;  //select all checkboxes with class "checkbox1"              
            });
        }else{
            $('.checkbox2').each(function() { //loop through each checkbox
                this.checked = false; //deselect all checkboxes with class "checkbox1"                      
            });        
        }
    });
   
});
    
function dustbin(){


swal({
                title: "", text: "Do you really want to delete the entire conversation??",
                showCancelButton: true,
                confirmButtonColor: '#DD6B55',
                confirmButtonText: 'Yes!',
                cancelButtonText: "No", closeOnConfirm: true,
                closeOnCancel: true
            },
            function (isConfirm) {
                if (isConfirm) {
                 
                 var xmlhttp5 = false;
        try {
            xmlhttp5 = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp5 = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp5 = false;
            }
        }
        if (!xmlhttp5 && typeof XMLHttpRequest != 'undefined') {
            xmlhttp5 = new XMLHttpRequest();
        }
      var url= '<?php echo base_url();?>'+'remove-rcv-selected-msg';
       
       
    var vals = [];
$(document).ready(function(){
$('input:checkbox[name="checkbox2[]"]').each(function() {
    if (this.checked) {
        vals.push(this.value);
    }
});

 
});

 var message_id=vals;
 //alert( message_id);
        var formdata = new FormData();
        formdata.append("message_id",message_id);
      
        
        xmlhttp5.addEventListener("load", function () {
          //alert(xmlhttp5.responseText);
        if( xmlhttp5.responseText ===  'redirect')
           {
               location.href = "receive-message";
           }
            
              
            }, false);
            xmlhttp5.addEventListener("error", function () {
                alert("not sent");
            }, false);
            xmlhttp5.addEventListener("abort", function () {
                alert("Aborted");
            }, false);

            xmlhttp5.open("POST", url);
            xmlhttp5.send(formdata);

        
                }
            });

}
</script>