<link rel="stylesheet" href="<?php echo base_url();?>assets/js/validationEngine.jquery.css" type="text/css" />
<link rel="stylesheet" href="<?php echo base_url();?>assets/css/cust_paginition.css" type="text/css" />
<div class="layout-app">
<div class="container">
<div class="row row-app">
    <div class="col-md-12">
        <div class="col-md-12">
            <button type="submit" class="btn btn-primary pull-right advanced_search">Advanced Search</button>
            <div style="clear: both"></div>
                                <br />
                <?php
                    $disply = 'display:none;';
                    if(count($_GET) > 0){
                        $disply = 'display:block;';
                    }
                ?>
                <div class="widget allMagazines">
                    <div class="widget-head">
                        <h4 class="heading">Downloads</h4>
                    </div>
                    <div class="widget-body innerAll inner-2x">
                        <?php $this->load->view('admin/flashdata_admin'); ?>
                        <form action="" method="GET" id="Singpost_Listing_form">
                            <h5>Date range to download Stickers</h5>
                            <table class="table table-white table-vertical-center margin-none footable" align="left">
                                <tr>
                                    <td class="col-md-2">From : &nbsp; <input type="text" readonly data-date-format="dd-mm-yyyy" class="form-control date validate[required] text-input" name="from_date" id ="from_date" value="<?php if (!empty($_GET['from_date'])){echo $_GET['from_date'];} ?>"/></th>
                                    <td class="col-md-2">To : &nbsp; <input type="text" readonly data-date-format="dd-mm-yyyy" class="form-control date validate[required] text-input" name="end_date_singList" id="end_date" value="<?php if (!empty($_GET['end_date_singList'])){echo $_GET['end_date_singList'];} ?>"/></th>
                                    <td class="col-md-2">
                                        Select Country for Sticker.&nbsp;
                                        <?php
                                            $options = array(''=>'--Select country for stickers','1'=>'Singapore','2'=>'Malaysia','3'=>'Hong Kong','4'=>'Thiland');
                                            $stf = 'class="cust_select" id="country_name" required="required"';
                                            echo form_dropdown('country_name', $options,$_GET['country_name'],$stf);
                                        ?>
                                    </td>
                                    <td class="col-md-6">
                                        Status : <br/>
                                        <?php
                                            $checked1 = '';$checked0 = ''; $checked2= ''; $checked3= ''; $checked4= ''; $checked5= '';
                                            if(!empty($_GET['status']) && is_array($_GET['status'])){
                                                if(in_array(1, $_GET['status'])){$checked1 = 'checked="checked"';}
                                                if(in_array(0, $_GET['status'])){$checked0 = 'checked="checked"';}
                                                if(in_array(2, $_GET['status'])){$checked2 = 'checked="checked"';}
                                                if(in_array(3, $_GET['status'])){$checked3 = 'checked="checked"';}
                                                if(in_array(4, $_GET['status'])){$checked4 = 'checked="checked"';}
                                                if(in_array(5, $_GET['status'])){$checked5 = 'checked="checked"';}
                                            }
                                        ?>
                                        <label class="col-md-3" for="status_active">
                                            <input type="checkbox" class="chkStatus" id="status_active" name="status[]" value="1" <?php echo $checked1; ?>/> Active
                                        </label>

                                        <label class="col-md-3" for="status_Expired">
                                            <input type="checkbox" class="chkStatus" id="status_Expired" name="status[]" value="0" <?php echo $checked0; ?>/> Expired
                                        </label>

                                        <label class="col-md-6" for="status_terminate_refund">
                                            <input type="checkbox" class="chkStatus" id="status_terminate_refund" name="status[]" value="2" <?php echo $checked2; ?>/> Terminate & Refund
                                        </label>
                                        
                                        <label class="col-md-3" for="status_Transfer">
                                            <input type="checkbox" class="chkStatus" id="status_Transfer" name="status[]" value="3" <?php echo $checked3; ?>/> Transfer
                                        </label>
                                        
                                        <label class="col-md-3" for="status_Deleted">
                                            <input type="checkbox" class="chkStatus" id="status_Deleted" name="status[]" value="4" <?php echo $checked4; ?>/> Deleted
                                        </label>

                                        <label class="col-md-4" for="status_Renewed">
                                            <input type="checkbox" class="chkStatus" id="status_Renewed" name="status[]" value="5" <?php echo $checked5; ?>/> Renewed
                                        </label>
                                        
                                    </th>
                                    
                                    <!-- <td>&nbsp;&nbsp;<button type="submit" class="btn btn-primary">Search</button></td> -->
                                </tr>
                            </table>
                             
                            <hr />
                            <br />
                            <button type="submit" id="sing_list" class="btn btn-primary">SEARCH</button>
                        </form>
                    </div>
                </div>
                 <?php if ($allMagazines && count($allMagazines) > 0) { ?>
                 <form name="search_sub" action="" method="GET">
                    <div class="col-md-12 selectBox">
                        <div class="col-sm-5 bot-mar">
                            <?php
                                foreach ($_GET as $key => $get_value) {
                                    if($key != 'search_value'){
                                        if(is_array($get_value)){
                                            foreach ($get_value as $get_value_value) {
                                                echo '<input type="hidden" value="'.$get_value_value.'" name="status[]"/>';
                                            }
                                        }else{
                                            echo '<input type="hidden" value="'.$get_value.'" name="'.$key.'"/>';
                                        }
                                    }
                                }
                            ?>
                            <input class="form-control" name="search_value" value="<?php echo $_GET['search_value'];?>" placeholder="Enter Subscriber Number / Magezine id / Subscriber Name" style="background-color: #A9A9A9;color: white" type="text">
                            <input class="form-control" id="current_page" value="2" type="hidden">
                        </div>

                        <div class="col-sm-2 bot-mar">
                            
                            <div>
                                <button type="submit" class="btn btn-primary">Search</button>
                            </div>
                        </div>
                        <label>&nbsp;</label>
                        <button type="button" id="donwExls" class="btn btn-primary pull-right"><i class="fa fa-2x fa-download" title="SINGPOST Download"></i></button>
                    </div>
                </form>
                <div>
                    <input type="hidden" id="sys_base_url" value="<?php echo base_url();?>" />
                    <input type="hidden" id="from_date_" value="<?php echo $_GET['from_date'];?>" />
                    <input type="hidden" id="end_date_singList" value="<?php echo $_GET['end_date_singList'];?>" />
                    <input type="hidden" id="status_" value="<?php echo implode(',', $_GET['status']);?>" />
                    <input type="hidden" id="country_name_" value="<?php echo $_GET['country_name'];?>" />
                </div>
                <table class="table table-white table-vertical-center margin-none table-striped footable">
                    <thead>
                        <tr>
                            <th data-hide="phone,tablet" class="text-center">Subscriber Number</th>
                            <th data-hide="Magezine id" class="text-center">Magezine id</th>
                            <th class="text-center">Subscriber Name</th>
                            <th data-class="highlight" class="text-center">Subscription Date</th>
                            <th data-class="highlight" class="text-center">Expiry Date</th>
                            <th data-hide="Status" class="text-center">Status</th>
                            <th data-class="highlight" class="text-center">No Of Issues</th>
                            <th data-hide="Status" class="text-center">Amount</th>
                            
                            <th data-hide="Status" class="text-center">Transaction Date</th>
                            <th data-hide="Status" class="text-center">Payment Mode</th>
                            
                            
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                            $mode_of_payment = array(
                                        '1'=>'Cash',
                                        '2'=>'Cheque',
                                        '3'=>'Credit Card',
                                        '4'=>'Online Banking',
                                        '5'=>'Club Membership',
                                        '6'=>'Complementary',
                                        '7'=>'Replacement',
                                        '8'=>'Invoice',
                                        '9'=>'Nets');
                        foreach ($allMagazines as $user) {?>
                            <tr>
                                <td class="text-center"><?php echo $user['memberId'];?></td>
                                <td class="text-center"><?php echo $user['magezine_id'];?></td>
                                <td class="text-left"><a href="<?php echo base_url();?>index.php/subscriber/preview/?user_id=<?php echo base64_encode($user['subscriber_id']);?>"><?php echo $user['last_name'].', '.$user['first_name'];?></a></td>
                                <td class="text-center"><?php echo date("d/m/Y",strtotime($user['subscribed_on']));?></td>
                                <td class="text-center"><?php echo date("d/m/Y",strtotime($user['subscription_end_on']));?></td>
                                <?php
                                     switch($user['is_active']) {
                                        case 0:
                                            $strThisTransactionStatus = 'Expired';
                                             
                                            break;
                                        case 1:
                                            $strThisTransactionStatus = 'Active';
                                            
                                            break;
                                        case 2:
                                            $strThisTransactionStatus = 'Terminated'; //Refunded
                                             
                                            break;
                                        case 3:
                                            $strThisTransactionStatus = 'Transferred';
                                            
                                            break;
                                        case 4:
                                            $strThisTransactionStatus = 'Deleted';
                                             
                                            break;
                                        case 5:
                                            $strThisTransactionStatus = 'Renewed';
                                            
                                            break;
                                    }
                                ?>
                                <td class="text-center">

                                    <?php
                                    $strDate =  date('d M Y',strtotime($user['subscription_end_on']));
                                    $expiryData = date('d-m-Y', strtotime($strDate));
                                    $toDay = date('d-m-Y');
                                    if($user['is_active'] == 5) { ?>
                                        <?php if(strtotime($toDay) > strtotime($expiryData)) { ?>
                                            //echo 'expired';
                                                <?php echo $strThisTransactionStatus;?> 
                                        <?php }else { ?> 
                                                SingPost Active 
                                        <?php } ?>
                                    <?php }else { ?> 
                                            <?php echo $strThisTransactionStatus;?> 
                                    <?php } ?>
                                    
                                    <?php //echo $strThisTransactionStatus;?>
                                        
                                </td>
                                <td class="text-center"><?php echo $user['no_of_issue'];?></td>
                                <td class="text-right"><?php echo number_format($user['amount'],2);?></td>
                                <td class="text-center"><?php echo date('d/m/Y', strtotime($user['transaction_date']));?></td>
                                <td class="text-center"><?php echo $mode_of_payment[$user['mode_of_payment']];?></td>
                            </tr>
                        <?php } } ?>
                    </tbody>
                </table>
        </div>
        <hr />
        <div class="text-center innerAll border-top pagination margin-none" align = "center">
            <?php echo $pagination; ?>
        </div>
    </div>
</div>

<script src="<?php echo base_url(); ?>assets/library/jquery/jquery.min.js"></script>
<script src="<?php echo base_url();?>assets/js/custom.js" type="text/javascript"></script>
<script src="<?php echo base_url();?>assets/plugins/forms_elements_multiselect/js/jquery.multi-select.js?v=v2.0.0-rc1&sv=v0.0.1.2"></script>
<script src="<?php echo base_url();?>assets/plugins/forms_elements_bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
<script src="<?php echo base_url();?>assets/components/forms_elements_bootstrap-select/bootstrap-select.js"></script>
<script src="<?php echo base_url();?>assets/components/forms_elements_bootstrap-select/bootstrap-select.init.js"></script>
<script src="<?php echo base_url();?>assets/components/forms_elements_fuelux-radio/fuelux-radio.init.js"></script>
<script src="<?php echo base_url();?>assets/components/forms_elements_multiselect/multiselect.init.js?v=v2.0.0-rc1&sv=v0.0.1.2"></script>
<script src="<?php echo base_url();?>assets/js/jquery.validationEngine-en.js" type="text/javascript"></script>
<script src="<?php echo base_url();?>assets/js/jquery.validationEngine.js" type="text/javascript"></script>
<script src="<?php echo base_url(); ?>assets/js/subscriber.js"></script>
<script>
    $(document).ready(function() {
        $("#Singpost_Listing_form").validationEngine();
        $('#select_mag').click(function(event) {  //on click
            if(this.checked) { // check select status
                $('.checkbox1').each(function() { //loop through each checkbox
                    this.checked = true;  //select all checkboxes with class "checkbox1"
                });
            }else{
                $('.checkbox1').each(function() { //loop through each checkbox
                    this.checked = false; //deselect all checkboxes with class "checkbox1"
                });
            }
        });

        
        $(".advanced_search").click(function(){
            $('.allMagazines').slideDown();
        });

        $("#sticker").click(function(){
            var chkId = '';
            $('.checkbox1:checked').each(function() {
                chkId += $(this).val() + ",";
            });
            var country_id = parseInt($("#country_name").val());
            chkId = chkId.slice(0,-1);
            $("#ids").val(chkId);
            $("#st_date").val($("#start_date").val());
            $("#end_date").val($("#end_date").val());
            $("#select-country").val($("#country_name").val());

            var all_ids = $("#all_ids").val();
            if (chkId == null || chkId == "") {
                chkId = all_ids;
            }
            var start_date = btoa($("#from_date").val());
            var ids = btoa(chkId);
            var end_date = btoa($("#end_date").val());

            /*var system_base_url = $("#sys_base_url").val();
            var strRedirectPath = system_base_url+"index.php/postings/print_sticker?ids="+ids+"&st_date="+start_date+"&end_date="+end_date+"&country="+country_id;
            window.location.replace(strRedirectPath);*/
        });
        
        $("#donwExls").click(function(){
            var from_date = $('#from_date_').val();
            var status = $('#status_').val();
            var country_name_ = $('#country_name_').val();
            var system_base_url = $("#sys_base_url").val();
            var strRedirectPath = system_base_url+"index.php/postings/saveExls?from_date="+from_date+"&status="+status+"&country_name="+country_name_;
            window.location.replace(strRedirectPath);
        });

        $("#sing_list").click(function(){
            var chkId = null;
            var count = 0;
            $('.chkStatus:checked').each(function() {
                chkId += $(this).val() + ",";
                count++;
            });
            if (chkId == null) {
                alert("Please select at least one magazine to generate listing");
                return false;
            }
            if (count > 5) {
                alert("You can select maximum 5 magazine to generate report");
                return false;
            }
        });

        $("#excel_report").click(function(){
            var chkId = '';
            $('.checkbox1:checked').each(function() {
                chkId += $(this).val() + ",";
            });
            chkId = chkId.slice(0,-1);
            $("#ids").val(chkId);
            $("#st_date").val($("#start_date").val());
            $("#end_date").val($("#end_date").val());

            var all_ids = $("#all_ids").val();
            if (chkId == null || chkId == "") {
                chkId = all_ids;
            }
            var start_date = btoa($("#from_date").val());
            var ids = btoa(chkId);
            var end_date = btoa($("#end_date").val());
            var system_base_url = $("#sys_base_url").val();
            var strRedirectPath = system_base_url+"index.php/postings/GenerateExcel?ids="+ids+"&st_date="+start_date+"&end_date="+end_date;
            window.location.replace(strRedirectPath);
        });

        $("#csv_report").click(function(){
            var chkId = '';
            $('.checkbox1:checked').each(function() {
                chkId += $(this).val() + ",";
            });
            chkId = chkId.slice(0,-1);
            $("#ids").val(chkId);
            $("#st_date").val($("#start_date").val());
            $("#end_date").val($("#end_date").val());

            var all_ids = $("#all_ids").val();
            if (chkId == null || chkId == "") {
                chkId = all_ids;
            }
            var start_date = btoa($("#from_date").val());
            var ids = btoa(chkId);
            var end_date = btoa($("#end_date").val());
            var system_base_url = $("#sys_base_url").val();
            var strRedirectPath = system_base_url+"index.php/postings/GenerateCSV?ids="+ids+"&st_date="+start_date+"&end_date="+end_date;
            window.location.replace(strRedirectPath);
        });
    });
</script>