 <!--start main wrapper-->
    <main class="main-wrapper">
        <div class="main-content">


            <div class="row">
                <div class="col-md-6 my-2">
                    <div class="d-flex align-items-center gap-2 mb-2">

                        <h3 class="fmon fw-bolder m-0">Messages</h3>
                    </div>
                </div>
                <div class="col-md-6 d-flex gap-2 justify-content-end align-items-center">
                    <a href="<?php echo base_url().'site/addtemplte'; ?>" class="btn addBtn d-flex gap-2 align-items-center"><img
                            src="<?php echo base_url(); ?>assets/images/megaIcons/aplu.png" alt="">Add
                        Template </a>
                    <div class="btn bg-white d-flex gap-5 align-items-center px-3 border-15">
                        Filter <img src="<?php echo base_url(); ?>assets/images/e.png " alt="" width="20px">
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-8">

                    <div class="row my-2 ">
                        <div class="col-md-12 my-3">
                            <div class="custom-toolbar px-3 py-2">
                                <p class="mb-0 ft8">Create New Message</p>
                            </div>
                            <div class="card border-15 my-2">
                                <div class="card-body">
                                    <div class="row g-2 flex-nowrap overflow-auto ">
                                        <div class="col-md-3">
                                            <div class="messageBox position-relative">
                                                <!-- <button class="  btncross p-0"><i
                                                        class="fa-solid fa-xmark"></i></button> -->
                                                <div>
                                                    <div class="boxheader">
                                                        <img src="<?php echo base_url(); ?>assets/images/messIcon.png" alt="">

                                                    </div>
                                                    <div class="boxbody">
                                                        <a href="<?php echo base_url().'site/newmessage'; ?>">
                                                            <p class="ft12 m-0 fmon">New Message</p>
                                                        </a>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <?php foreach ($templates as $key => $template) { ?>
                                        <div class="col-md-3">
                                            <div class="messageBox position-relative">
                                                <button class="  btncross p-0"><i
                                                        class="fa-solid fa-xmark"></i></button>
                                                <div>
                                                    <div class="boxheader">
                                                        <img src="<?php echo base_url(); ?>assets/images/messIcon.png" alt="">

                                                    </div>
                                                    <div class="boxbody">
                                                        <a href="<?php echo base_url().'site/usetemplate/'.$key; ?>">
                                                            <p class="ft12 m-0 fmon"><?php echo $template['name'] ?></p>
                                                        </a>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <?php } ?>
                                      

                                    </div>
                                </div>
                            </div>
                            <div class="col-md-12 my-3">
                                <div class="custom-toolbar px-3 py-2">
                                    <p class="mb-0 ft8">Drafts</p>
                                </div>
                                <div class="card un-card border-15 mt-3">
                                    <div class="list-group  border-15">
                                        <?php foreach ($draftmessages as $key => $message) { 
                                            if($message['status'] == 'draft'){
                                            // echo "<pre>";
                                            // print_r($message);
                                            // echo "</pre>";
                                            
                                            ?>
                                        <li class="list-group-item list-group-item-action ">
                                            <div class="d-flex justify-content-between  align-items-center">


                                                <div>
                                                    <p class="ft6 fmon mb-2"><?php echo $message['subject'] ?></p>
                                                    <p class="ft7 fmon mb-0 ">
                                                        <?php
                                                    $createdAtSec = (int)($message['createdAt'] / 1000); 
                                                    $createdDateStr = date('Y-m-d', $createdAtSec); 
                                                    $startOfWeek = strtotime('monday this week'); 
                                                    $endOfWeek = strtotime('sunday this week 23:59:59'); 
                                                    if ($createdAtSec >= $startOfWeek && $createdAtSec <= $endOfWeek) { 
                                                        echo "Draft created this week"; 
                                                    } else { 
                                                        echo "Draft created on " . date('Y-M-d', $createdAtSec); }
?>
                                                    </p>
                                                    <p class="ft7">
                                                        <?php echo date('Y-M-d H:i:s', $createdAtSec); ?>
                                                    </p>

                                                </div>


                                                <div class="d-flex align-items-center gap-1">

                                                    <a href="<?php echo base_url().'site/editdraftmessage/'.$message['id']; ?>" class="btn"><img
                                                            src="<?php echo base_url(); ?>assets/images/ed1.png" alt=""></a>
                                                    <button class="btn" data-bs-toggle="modal"
                                                        data-bs-target="#deleteConfirmationModal">
                                                        <img src="<?php echo base_url(); ?>assets/images/de1.png" alt="">
                                                    </button>
                                                </div>
                                            </div>

                                        </li>
                                        <?php } } ?>


                                    </div>
                                </div>


                            </div>
                            <div class="col-md-12 my-3">
                                <h4 class="fmon">Schedule Messages of the week</h4>
                                <!-- <div class="custom-toolbar px-3 py-2 d-flex justify-content-between">

                                    <p class="mb-0 ft8">Su</p>
                                    <p class="mb-0 ft8">Mo</p>
                                    <p class="mb-0 ft8">Tu</p>
                                    <p class="mb-0 ft8">We</p>
                                    <p class="mb-0 ft8">Th</p>
                                    <p class="mb-0 ft8">Fr</p>
                                    <p class="mb-0 ft8">Sa</p>
                                </div>
                                <div class="card border-15 my-2">
                                    <div class="card-body">
                                        <div class="d-flex justify-content-between">
                                            <div class="sentCard">
                                                <div class="time">9am</div>
                                                <div class="p-1">
                                                    <p class="ft14 fmon">New <br>
                                                        Security <br>
                                                        Personnel</p>
                                                    <p class="ft13 fmon">Sent</p>
                                                </div>
                                            </div>
                                            <div class="sentCard">
                                                <div class="time">9am</div>
                                                <div class="p-1">
                                                    <p class="ft14 fmon">New <br>
                                                        Security <br>
                                                        Personnel</p>
                                                    <p class="ft13 fmon">Sent</p>
                                                </div>
                                            </div>
                                            <div></div>
                                            <div></div>
                                            <div></div>
                                            <div></div>
                                            <div class="sentCard">
                                                <div class="time fmon">9am</div>
                                                <div class="p-1">
                                                    <p class="ft14 fmon">New <br>
                                                        Security <br>
                                                        Personnel</p>
                                                    <p class="ft13 fmon">Sent</p>
                                                </div>
                                            </div>
                                        </div>

                                    </div>
                                </div> -->

                                <div id="calendar"></div>
                            </div>

                        </div>
                    </div>

                </div>
                <div class="col-md-4 my-2">
                    <div class="col-md-12 my-3">
                        <div class="custom-toolbar px-3 py-2 d-flex justify-content-between align-items-center">
                            <p class="mb-0 ft8">Sent</p>
                            <a href="<?php echo base_url().'site/allsentmessage'; ?>" class="mb-0 ft10">
                                <p class="mb-0">see all</p>
                            </a>
                        </div>
                        <div class="card border-15 my-2">
                            <div class="list-group  border-15">
                                  <?php foreach ($messages as $key => $message) {
                                    $totalsent=0;
                                    $unread=0;
                                    $read=0;
                                if($message['status'] == 'sent'){
                                            // echo "<pre>";
                                            // print_r($message);
                                        // echo "</pre>";
                                        $recipientuserlistid=$message['recipientuserlistid'];

                                        foreach($recipientuserlistid as $key => $recipientuserlistid){
                                            if($recipientuserlistid==true){
                                                $read++;
                                            }else{
                                                $unread++;
                                            }
                                        }
                                        $totalsent=$read+$unread;
                                    //  exit;
                                     ?> 

                                <li class="list-group-item list-group-item-action  list-group-item2">
                                    <div class="row">
                                        <div class="col-md-5">
                                            <p class="fmon ft15 m-0"><?php echo $message['subject'] ?></p>
                                            <p class="fmon ft16 m-0"> <?php echo date('Y-M-d H:i:s', $message['sentAt']/1000); ?></p>
                                        </div>
                                        <div class="col-md-7">
                                            <p class="fmon ft16 m-0"><?php echo round(($read/$totalsent)*100,2); ?>% <span>Vistor</span></p>
                                            <div class="progress" role="progressbar" aria-label="Basic example"
                                                aria-valuenow="<?php echo round(($read/$totalsent)*100,2); ?>" aria-valuemin="0" aria-valuemax="100">
                                                <div class="progress-bar" style="width: <?php echo round(($read/$totalsent)*100,2); ?>%"></div>
                                            </div>
                                        </div>
                                    </div>

                                </li>
                                <?php } } ?>

                                
                            </div>

                        </div>
                    </div>
                </div>
            </div>

        </div>


        <!-- Bootstrap Confirmation Modal -->
        <div class="modal fade" id="deleteConfirmationModal" tabindex="-1"
            aria-labelledby="deleteConfirmationModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content">
                    <!-- Modal Header -->
                    <div class="modal-header">
                        <h5 class="modal-title" id="deleteConfirmationModalLabel">Confirm Delete</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>

                    <!-- Modal Body -->
                    <div class="modal-body">
                        Are you sure you want to delete this item?
                    </div>

                    <!-- Modal Footer -->
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="button" class="btn btn-danger" onclick="confirmDelete()">Delete</button>
                    </div>
                </div>
            </div>
        </div>


    </main>
    <!--end main wrapper-->
