<?php
/**
 * Email Bad Report Script
 * 
 * This script generates a report of SMS-related data based on user-selected filters.
 * It includes DataTables for server-side processing, filter functionality, and report export options.
 * 
 * Author: Aarti
 * Last Modified: 09-24-2024
 */

// Include the database connection file
include("../../config/web_mysqlconnect.php");
include("../web_function.php");
$name = $_SESSION['logged'];

include("bulk_report_function.php");  
// renamed the function to fetch email bulk upload list [vastvikta][07-01-2026]
$list_BULK = new BULK_REPORT();
$bulk_lists =  $list_BULK->get_bulk_upload_list_email(); 
?>
<link rel="stylesheet" type="text/css" href="<?=$SiteURL?>CRM/omnichannel_config/css/bulk_email_report.css">
<div class="col-sm-12" style="padding-left:0">
  <span class="breadcrumb_head" style="height: 37px; padding: 9px 16px;">Email Bulk Report</span>
  <form name="myform" method="post">
    <div class="style2-table ">
      <div class="table">
        <table class="tableview tableview-2 main-form new-customer">
          <tbody>
            <tr>
              <?php
              $startdatetime = ($_REQUEST['sttartdatetime'] != '') ? ($_REQUEST['sttartdatetime']) : date("01-m-Y 00:00:00");
              $enddatetime = ($_REQUEST['enddatetime'] != '') ? ($_REQUEST['enddatetime']) : date("d-m-Y 23:59:59");
              ?>
              <td class="left boder0-left">
                <label>From
                  <input type="text" name="from_date" class="date_class dob1" value="<?= $startdatetime ?>"
                    id="startdatetime">
                </label>
                <label>To
                  <input type="text" name="end_date" class="date_class dob1" value="<?= $enddatetime ?>" id="enddatetime">
                </label>
              </td>
              <td class="left boder0-right">
                <label>Scheduled</label>
                <div class="log-case">
                  <select name="schedule" id="schedule" class="select-styl1">
                    <option value="all" <?php echo ($schedule == 'all') ? 'selected' : '' ?>>All</option>
                    <option value="0" <?php echo ($schedule == '0') ? 'selected' : '' ?>>Scheduled</option>
                    <option value="1" <?php echo ($schedule == '1') ? 'selected' : '' ?>>Unscheduled</option>
                  </select>
                </div>
              </td>
              <td class="left boder0-right">
                <label>Campaign wise</label>
                <div class="log-case">
                  <select name="list_wise" id="agent_n" class="select-styl1">
                    <option value="all">All</option>
                    <?php if (count($bulk_lists) > 0):
                      foreach ($bulk_lists as $key => $list): ?>
                        <option value="<?= $list['id'] ?>" <?php echo ($list_wise == $list['id']) ? 'selected' : '' ?>>
                          <?= $list['list_name'] ?></option>
                      <?php endforeach;
                    endif;
                    ?>
                  </select>
                </div>
              </td>
            </tr>
            <tr>
              <td class="left boder0-left">
                <label>Agents</label>
                <div class="log-case">
                  <?php
                  $sqlagent = "select AtxUserID,AtxUserName from $db.uniuserprofile where AtxUserStatus='1'";
                  $result = mysqli_query($link, $sqlagent);
                  ?>
                  <select name="user_wise" id="user_wise" class="select-styl1">
                    <option value="">Select Agent</option>
                    <?php
                    while ($row = mysqli_fetch_array($result)) {
                      $AtxUserID = $row['AtxUserID'];
                      $AtxUserName = $row['AtxUserName'];
                      $sel = ($AtxUserName == $_POST['agent_n']) ? 'selected' : '';
                      ?>
                      <option value='<?= $AtxUserName ?>' <?= $sel ?>><?= $AtxUserName ?></option>
                    <?php } ?>
                  </select>
                </div>
              </td>
              <td class="left boder0-right">
                <input type='submit' name='sub1' value='Run Report' class="button-8">
                <button type="button" id="reset" name="reset" class="btn button-8" title="Reset filters">Reset</button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <div class="table">
          <div class="wrapper1">
          </div>
          <div class="wrapper2">
             <div class="box-body table-responsive">
          <div class="title_name" style="display: none;">Agent Report</div>
          <div class="report_name" style="display: none;"><?= date('dmY') ?></div>
          <div class="download_label" style="display: none;"><?= $pdf_heading ?></div>
          <table class="tableview tableview-2" id="report-server-side">
            <thead>
              <tr>
                <td># &nbsp;</td>
                <td>Date</td>
                <td>List Name</td>
                <td>Count</td>
                <td>Queue</td>
                <td>Submitted</td>
                <td>Delivered</td>
                <td>Not Delivered</td>
                <td>Message</td>
                <td>Schedule Date</td>
                <td>Schedule Type</td>
                <td>Created By</td>
              </tr>
            </thead>
          </table>
        </div>
          </div>
        </div>
      </div>
    <!-- End Right panel --> 
  </form>
</div>
<script type="text/javascript">
   $(function () {
      $("#report-server-side").DataTable({
        "searching": false,
        "responsive": true, 
        "pageLength": 100, 
        "lengthChange": false,
        "autoWidth": true,
        "processing": true,
        "serverSide": true,
        "searchable" : false, 
        "ajax":{
            "url": "Report/bulk_report_function.php",
            "dataType": "json",
            "type": "POST",
            "data" : {report_name : '<?php echo $report_name; ?>',
                      report_in_out : '<?php echo $report_in_out ?>',
                      from_date : '<?php echo $from_date ?>',
                      end_date : '<?php echo $end_date ?>',
                      schedule : '<?php echo $schedule ?>',
                      message_type : '<?php echo $message_type ?>',
                      status : '<?php echo $status ?>',
                      list_wise : '<?php echo $list_wise ?>',
                      user_wise : '<?php echo $user_wise ?>',
                      'action':'bulk_sms_report',
                      'channeltype':'Email'
                    }
        },
        "columns": [
            { "data": "#" },
            { "data": "create_date" },
            { "data": "list_name" },
            { "data": "total_count" },
            { "data": "queue" },
            { "data": "submitted" },
            // { "data": "pending" },
            { "data": "delivered" },
            { "data": "not_delivered" },
            { "data": "message" },
            { "data": "schedule_time" },
            { "data": "schedule_flag" },
            { "data": "create_by" },
        ],
        "columnDefs": [{
            targets: "_all",
            orderable: false
         }],
        "dom": "Bfrtip",
      "buttons": 
        [{
          extend: 'csvHtml5',
          text: '<i class="fa fa-file-excel-o"></i>',
          titleAttr: 'CSV',
          filename: 'Email Bulk Report',
          title: 'Email Bulk Report',
          exportOptions: {
              columns: ':visible'
          }
          },
          {
            extend: 'excelHtml5',
              text: '<i class="fa fa-file-excel-o"></i>',
              titleAttr: 'Excel',
              filename: 'Email Bulk Report',
              title: 'Email Bulk Report',
            exportOptions: {
                  columns: ':visible'
              }
          },
          {
            extend: 'pdfHtml5',
            filename : 'Email Bulk Report',
            messageTop : 'Email Bulk Report',
            orientation: 'landscape',
            pageSize: 'A3',
            text: '<i class="fa fa-file-pdf-o"></i>',
            titleAttr: 'PDF',
            customize: function ( doc ) {
                
                var logoBase64 = document.getElementById('pdf-logo-base64').innerText;
                doc.images = doc.images || {};
                doc.images.logo = logoBase64;
            
            doc.content.splice( 1, 0, {
                margin: [ 0, 0, 0, 5 ],
                    alignment: 'left',
                    image: 'logo', 
                      width: 250
                } );
            },
            title: '.',
            exportOptions: {
                columns: ':visible'
                
            }
          },
          {
            extend: 'colvis',
            text: '<i class="fa fa-columns"></i>',
            titleAttr: 'Columns',
            title: $('.download_label').html(),
            postfixButtons: ['colvisRestore']
          },
        ],
      });


    });
</script>