<!-- Main Content -->

<div class="page-wrapper">

    <div class="container-fluid pt-25">

        <div class="row">

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

                <div class="alert alert-success alert-dismissible" role="alert" <?php if($this->session->flashdata('message')==''){ ?> style="display:none" <?php } ?> >

                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>

                    <?php echo $this->session->flashdata('message'); ?>

                </div>

            </div>

        </div>

        <!-- Row -->

        <div class="row">

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

                <div class="panel panel-default card-view">

                    <div class="panel-heading">

                        <div class="pull-left">

                            <h6 class="panel-title txt-dark">Newsletter Subscriber List</h6>

                        </div>
                        <span class="pull-right">
                            <a class="btn btn-info" data-toggle="modal" data-target="#searchModal"><i class="fa fa-search"></i>  Search</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <?php if($this->admin_type=='superadmin' || (in_array('8.1', $this->authority))){ ?>
                                <a href="javascript:void(0);" data-tableid="datable_1-1" data-table="post" id="sendMailMultiple" class="btn btn-success">Send Mail</a>
                            <?php } ?>
                        </span>

                        <div class="clearfix"></div>

                    </div>

                    <div class="panel-wrapper collapse in">

                        <div class="panel-body">

                            <div class="table-wrap">

                                <div class="table-responsive">

                                    <table id="datable_1-1" class="table   table-hover">
                                        <thead align="center">
                                            <tr>
                                                <td colspan="4"><?php echo (isset($h1)?$h1:""); ?></td>
                                                <td colspan="3">
                                                    <div class="col-md-6">&nbsp;</div>
                                                    <div class="col-md-6">
                                                        <select id="admin_length" class="pull-right form-control">
                                                            <option value="10" <?php echo ($this->perPage==10)?'selected':""; ?>>10</option>
                                                            <option value="20" <?php echo ($this->perPage==20)?'selected':""; ?>>20</option>
                                                            <option value="50" <?php echo ($this->perPage==50)?'selected':""; ?>>50</option>
                                                            <option value="100" <?php echo ($this->perPage==100)?'selected':""; ?>>100</option>
                                                            <option value="250" <?php echo ($this->perPage==250)?'selected':""; ?>>250</option>
                                                            <option value="500" <?php echo ($this->perPage==500)?'selected':""; ?>>500</option>
                                                        </select>
                                                    </div>
                                                </td>
                                            </tr>
                                            <tr><td colspan="4" style="color: red;"><?php echo (isset($h2)?$h2:""); ?></td>
                                                <td colspan="3">
                                                </td>
                                            </tr>
                                        </thead>
                                        <thead>

                                            <tr>
                                                <th><input type="checkbox" name="postid" class="check-one"/></th>
                                                <th>Sr. No.</th>

                                                <!-- <th>Ip Address</th>

                                                <th>Name</th> -->
                                                <th>Email</th>

                                                <th>Date</th>

                                                <th>Action</th>

                                            </tr>

                                        </thead>

                                        <tbody>

                                            <?php

                                             if($newsletters){ foreach ($newsletters as $key => $value) : ?>

                                                <tr id="<?php echo "newsletter_".$value['id']; ?>">
                                                    <td><input type="checkbox" data-id="<?php echo $value['id']; ?>" class="check-all"/></td>
                                                    <td><?php echo $k++; ?></td>

                                                    <!-- <td><?php //echo $value['ip_address']; ?></td>

                                                    <td><?php //echo $value['name']; ?></td> -->
                                                    <td><?php echo $value['email']; ?></td>

                                                    <td><?php echo $value['date']; ?></td>

                                                    <td>
                                                        <?php if($this->admin_type=='superadmin' || (in_array('8.2', $this->authority))){ ?>
                                                            <a href="javascript:void(0)" class="text-inverse ajax_delete" data-table="newsletter" data-value="<?php echo "id_".$value['id']; ?>"> 
                                                                <button class="btn btn-danger btn-icon-anim btn-square">
                                                                    <i class="fa fa-trash-o fa-lg"></i>
                                                                </button>
                                                            </a>

                                                        <?php } ?>
                                                    </td>

                                                </tr>

                                            <?php endforeach; } ?>

                                        </tbody>

                                    </table>

                                </div>

                            </div>

                        </div>
                        <div class="panel-footer clearfix">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="text-center"><?php echo (isset($links))?$links:""; ?></div>
                                </div>
                            </div>
                        </div>

                    </div>

                </div>  

            </div>

        </div>

        <!-- /Row -->

    </div>
<div id="searchModal" class="modal fade" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Search</h4>
        </div>
        <form action="<?php echo base_url(A."/newsletter-list"); ?>" method="get" enctype="multipart/form-data">
          <div class="modal-body">
              <div class="form-group">
                <label>Email</label>
                <input type="email" name="email" class="form-control" placeholder="Enter Email" />
              </div>
          </div>
          <div class="modal-footer">
            <input type="submit" class="btn btn-success" value="Submit">
          </div>
        </form>
      </div>

    </div>
</div>

<div class="modal fade" id="mailModal" tabindex="-1" role="dialog" aria-labelledby="mailModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="mailModalLabel">Send Mail</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <form id="addProductFrom" action="<?php echo base_url(A.'/sendInstantMail'); ?>" method="post">
        <input type="hidden" name="ids" value="" id="uid">
        <div class="modal-body">
            <div class="form-group">
                <label>Subject</label>
                <input type="text" name="subject" class="form-control" required />
            </div>
            <div class="form-group">
                <label>Message</label>
                <textarea class="form-control tinymce" name="message"></textarea>
            </div>
        </div>
        <div class="modal-footer">
            <button type="submit" class="btn btn-primary">Send</button>
        </div>
      </form>
    </div>
  </div>
</div>
        
<script type="text/javascript">

    var site_url = "<?php echo base_url(); ?>";
    var urlLink = "<?php echo A; ?>";

    $(".ajax_delete").on("click",function(){

        var id = $(this).attr("data-value");

        var split = id.split("_");

        //alert(split[0]);

        var table = $(this).attr("data-table");

        var image = ($(this).attr("data-image")=="undefined")?"":$(this).attr("data-image");

        var result = confirm("Are you sure you want to delete?");

        if(result) {

            $.ajax({

                url: site_url+urlLink+"/ajaxDelete",

                type: "POST",

                data: split[0]+"="+split[1]+"&table="+table+"&image="+image,

                success: function(result){

                    if(result==1)

                    {

                        $("#"+table+"_"+split[1]).remove();

                    }

                }

            });

        }

    });
</script>
<script>
    $(".check-one").on("click",function(){ 
        if($(this).is(":checked")) { 
            $(this).closest("table").find('.check-all').prop('checked',true); 
        }else{ 
            $(this).closest("table").find('.check-all').prop('checked',false); 
        } 
    });

    $("#sendMailMultiple").on("click",function(){
        var table_id = $(this).data('tableid');
        var table = $(this).data('table');
        var field = $("#"+table_id).find('.check-one').attr('name');
        var id = [];var length=0;
        $("#"+table_id+" tbody").find("input[class='check-all']:checked").each(function(){            
            id.push($(this).data("id"));
            length++;
        });

        var ids = id.join(",");
        if(ids!=""){
            $('#uid').val(ids);
            $('#mailModal').modal('show');
        }else{
            alert("Select at least one user!");
            $(this).val('');
        }
    });
</script>