<style type="text/css">
    #searchdiv{
        margin-bottom: 10px;
    }
    .searchfieldslist{
        margin-top: 4px;
    }
</style>
<!-- Page Content -->
<div class="page-content">
    <!-- Page Header -->
    <div class="page-header position-relative">
        <div class="header-title">
            <h1>
                Customer Warning List
            </h1>
        </div>
    </div>
    <!-- /Page Header -->
    <!-- Page Body -->
    <div class="page-body">
        
        <div class="row">
            <div class="col-xs-12 col-md-12">
                <div class="widget">
                    <div class="widget-body">
            <div id="searchdiv">
                <form name="frmsearch" id="frmsearch" method="post">
                    <div class='col-md-4 searchfieldslist'>
                        <input type="text" name="dayssearch" id="dayssearch" class="form-control" placeholder="ENTER LAST TRANSACTION BEFORE"
                               value="<?php if(isset($_POST['dayssearch'])) echo $_POST["dayssearch"]; ?>" />
                    </div>
                    <div class='col-md-4 searchfieldslist'>
                        <select name="custcategory" id="custcategory" class="form-control">
                            <option value="">Select Customer Category</option>
<?php 
if($custArr) {
	foreach($custArr as $custumercategory) {
?>
	<option value="<?php echo $custumercategory->ID; ?>"
 <?php if(isset($_POST["custcategory"])) { if($custumercategory->ID==$_POST["custcategory"]) echo "selected"; } ?>>
<?php echo $custumercategory->NAME; ?></option>
<?php
	}
}
?>
                        </select>
                    </div>
                    <div class="clearfix"></div>
                    <div class='col-md-3 searchfieldslist'>
                        <button type="submit" class="btn btn-primary shiny" name="allsearch"><span class="fa fa-search"></span>Execute</button>
                    </div>
                </form>
            </div>
                <form name="exportexcel" id="exportexcel" action="<?php echo base_url(); ?>reports/customerwarning/exportexceldata" method="post">
                    <?php
                    if(isset($_POST["allsearch"])){
                    ?>
                    <div style="float: right;">

                        <input type="hidden" name="hiddensqlquery" id="hiddensqlquery" value="<?php echo $sqlquery; ?>" />
<input type="hidden" name="hiddendayssearch" id="hiddendayssearch" value="<?php if(isset($_POST['dayssearch'])) echo $_POST["dayssearch"]; ?>" />

                        <div class="btn-group">
                            <button type="submit" class="btn btn-success shiny" name="excelexport"><i class="fa fa-file-excel-o"></i> Excel</button>
                            <button type="submit" class="btn btn-danger shiny" name="pdfxport"><i class="fa fa-file-pdf-o"></i> PDF</button>
                        </div>
                    </div>
                    <?php
                    }
                    ?>
                </form>
<div id="tablecontent" class="table-scrollable">
    <div id="tablecontent" class="table-scrollable">
        <table class="table table-striped table-hover table-bordered" id="reportdatatable">
            <thead>
                <tr role="row">
                    <th style="text-align:left;width:50px">Search Key</th>
                    <th style="text-align:left;width:70px">Category</th>
                    <th style="text-align:left;width:70px">Name</th>
                    <th style="text-align:right;width:70px">Card</th>
                    <th style="text-align:right;width:70px">Max.Debt</th>
                    <th style="text-align:right;width:70px">Last Trn. Date</th>
                    <th style="text-align:right;width:70px">Current Debt</th>
                </tr>
            </thead>
            <tfoot></tfoot>
            <tbody>
    <?php
    if(isset($resultdata)) {
        if($resultdata) {
            foreach($resultdata as $result) {
                $sql1 = "SELECT C.ID AS CUSTID,C.SEARCHKEY,C.NAME,R.DATENEW AS MAXDATENEW ,P.PAYMENT, C.CARD, C.MAXDEBT, C.CURDATE ,"
                    . "C.CURDEBT,P.PAYMENT,C.TAXCATEGORY,TC.NAME AS TAXCATEGORYNAME FROM RECEIPTS R JOIN TICKETS T"
                    ." JOIN CUSTOMERS C ON T.ID=R.ID AND C.ID=T.CUSTOMER "
                    ." JOIN PAYMENTS P ON P.RECEIPT=R.ID AND (P.PAYMENT='debt') LEFT JOIN TAXCUSTCATEGORIES TC ON TC.ID=C.TAXCATEGORY"
                    . " WHERE C.ID='".$result->CUSTID."' AND R.DATENEW>'".$result->MAXDATENEW."'"
                    . " ORDER BY R.DATENEW ASC LIMIT 0,1";
                $custrec = $this->common->customquery($sql1);
                if($custrec) {
                    $now = time();
                    $your_date = strtotime($custrec[0]->MAXDATENEW);
                    $datediff = $now - $your_date;
                    $daysdif    =   round($datediff / (60 * 60 * 24));
                    if($daysdif>=$_POST["dayssearch"]) {
    ?>
        <tr role="row">
            <td style="width: 50px;"> <?php echo $custrec[0]->SEARCHKEY; ?></td>
            <td style="width: 80px;"> <?php echo $custrec[0]->TAXCATEGORYNAME; ?></td>
            <td style="width: 100px;"> <?php echo $custrec[0]->NAME; ?></td>
            <td style="width: 70px;"> <?php echo $custrec[0]->CARD; ?> </td>
            <td style="width: 70px;"> <?php echo $this->formatslist->formatCurrency($custrec[0]->MAXDEBT); ?> </td>
            <td style="width: 70px;"> <?php echo $this->formatslist->datetimeFormat($custrec[0]->CURDATE); ?> </td>
            <td style="width: 70px;"> <?php echo $this->formatslist->formatCurrency($custrec[0]->CURDEBT); ?> </td>
        </tr>
    <?php
                    }
                }
            }
        }
    }
    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- /Page Body -->
    </div>
    <!-- /Page Content -->
</div>
<!-- /Page Container -->
<!-- Main Container -->
</div>
<script src="<?= site_url() ?>assets/js/datetime/bootstrap-datepicker.js"></script>
<script type="text/javascript">
    $("#fromdate").datepicker({format: 'dd-mm-yyyy', setDate: new Date()});
    $("#todate").datepicker({format: 'dd-mm-yyyy', setDate: new Date()});
    $('.datepickertxt').datepicker().on('changeDate', function (ev) { 
        $('.datepicker').hide();
    });
</script>