<style>
    .table thead tr th{
        border-top:2px solid #eee;
        border-bottom:2px solid #eee;
        background: #fcfcfc;
    }
    .table tfoot tr th{
        border-top:2px solid #eee;
        border-bottom:2px solid #eee;
        background: #fcfcfc;
    }
</style>
<div class="wrapper">
    <div class="container">
        <div class="row m-t-20">
            <div class="col-sm-12 ">
                <div class="btn-group pull-right m-t-15">
                </div>
                <h4 class="page-title">Service Lapse</h4>

            </div>
        </div>

        
        
        <div class="row ">
            <div class="col-md-12">
                <div class="panel panel-default m-t-20">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-12 hidden-print m-t-20">
                                <form method="POST" action='<?php echo base_url(); ?>service_lapse'>
                                    <input type="hidden" name="csrf_test_name" id="daily_sheet_csrf" value=""/>
                                    <div class="col-md-6">
                                        <select onchange="getdate($('#s_query option:selected').attr('days')); $('#days').val($('#s_query option:selected').attr('days'))"  class="form-control inline m-r-5" name="s_query" id="s_query">
                                            <option>select</option>
                                            <?php foreach($tasks as $task){ $word='TEST'; if(strpos(strtoupper($task['task_name']), $word)===false && strpos(strtoupper($task['task_name']), 'TEXT')===false){?>
                                            <option days="<?php echo $task['day_difference'] ;?>" <?php if($task['id_scheduled_tasks']==$s_query){echo "selected='selected'";} ?> value="<?php echo  $task['id_scheduled_tasks'];?>"><?php echo strtoupper($task['task_name']);?></option>
                                            
                                            <?php }} ?>
                                        </select>
                                    </div>
                                    <div class="col-md-2 hidden">
                                        <input type="number" id="days" name="days" class="form-control numeric" value="<?php echo $tasks[0]['day_difference'];?>">
                                    </div>
                                    <div class="col-md-2">
                                        <input id="datepicker" name="datepicker" class="form-control datepicker-dropdown" value="<?php echo $posteddate;?>">
                                    </div>
                                    
                                    <div class="col-md-2">
                                        <button type='submit' onclick="$('#daily_sheet_csrf').val($('#cook').val());" class="btn btn-pink btn-bordred waves-effect w-md waves-light m-b-5">Run</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div> <!--row end-->
        <div class="clearfix"></div>


        <div class="row">

            <div class="col-md-12">
                <div class="panel panel-default m-t-20">
                    <div class="panel-body">

                        <table class="table m-t-10"  id='sheet1'>
                            <tr>
                                <td ><strong>Service Lapse Print Date : <u> <?php echo isset($date) && !empty($date) ? date('d-M-Y', strtotime($date)) : date('d-M-Y'); ?></u></strong></td>
                               </tr>
                            <tr>
                                <td>
                                    <table class="table" id='tblInvoices'>
                                        <thead>
                                            <tr>
                                                <th colspan='8'><strong><?php echo strtoupper($task_title); ?></strong> - As On : <?php if(null!=$posteddate){echo strtoupper($posteddate);} ?></th>
                                            </tr>
                                            <tr>
                                                <?php $count=array();
                                                if (isset($feilds) && !empty($feilds)) {
                                                    foreach ($feilds as $feild) {
                                                        echo "<th>" . str_replace('_',' ',strtoupper($feild->name)) . "</th>";
                                                        array_push($count, $feild->name);
                                                    }
                                                    //print_r($count); exit();
                                                }
                                                ?>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            if (isset($invoices) && !empty($invoices)) {                                                
                                                foreach ($invoices as $invoice) {?>
                                                <tr>
                                                <?php  for($x=0; $x < sizeof($count); $x++){?>
                                                    <?php if($count[$x]!='followupcall' && $count[$x]!='followupsms'){?>
                                                        <td><?php echo $invoice[$count[$x]]; ?></td>
                                                    <?php } else { ?>
                                                        <?php if($invoice[$count[$x]] == null){?>
                                                        <td><button class="btn btn-sm">Mark</button></td>
                                                        <?php } else { ?>
                                                        <td><?php echo $invoice[$count[$x]]; ?></td>
                                                        <?php } ?>
                                                    <?php } ?>
                                                <?php  } ?>
                                                </tr>    
                                            <?php } } ?>
                                        </tbody>
                                        <tfoot>

                                        </tfoot>
                                    </table>
                                </td>
                                
                            </tr>

                        </table>
                    </div>
                </div>
            </div>      
        </div>
        <!-- end row -->
    </div>




    <script>


    $(document).ready(function(){

        $('#datepicker').datepicker({
            autoclose: true,
            todayHighlight: true,
            format: 'yyyy-mm-dd'            
        });

        //$('#datepicker').datepicker("setDate", new Date() );

        $('#tblInvoices').DataTable({
                    lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
                    stateSave: true, fixedHeader: {header: true},
                <?php if(DOWNLOAD == true || $this->session->userdata('role') == 'Admin' || $this->session->userdata('role')=='Super User'){ ?>
                    dom: "Bfrtlip",
                    buttons: [{extend: "copy", className: "btn-sm btn-custom btn-trans"},
                        {extend: "csv", className: "btn-sm btn-pink btn-trans"},
                        {extend: "pdf", className: "btn-sm btn-purple btn-trans"},                    
                        {extend: "print", className: "btn-sm btn-warning btn-trans"}
                ],
                <?php } else { ?>
                    dom: "frtlip",
                <?php } ?> 
                    responsive: 0,
                    ordering: false
                });
    
    });

    function getdate(days){
        
        
        var mdate = subtractDays(days, new Date());
        
        console.log('Converted date: '+ dateFormat(mdate, 'yyyy-mm-dd'));
        
        $('#datepicker').datepicker("setDate", dateFormat(mdate, 'yyyy-mm-dd') );
    }

    function subtractDays(numOfDays, date = new Date()) {
        date.setDate(date.getDate() - numOfDays);
        
        return date;
      }
      

      //a simple date formatting function
    function dateFormat(inputDate, format) {
        //parse the input date
        const date = new Date(inputDate);

        //extract the parts of the date
        const day = date.getDate();
        const month = date.getMonth() + 1;
        const year = date.getFullYear();    

        //replace the month
        format = format.replace("mm", month.toString().padStart(2,"0"));        

        //replace the year
        if (format.indexOf("yyyy") > -1) {
            format = format.replace("yyyy", year.toString());
        } else if (format.indexOf("yy") > -1) {
            format = format.replace("yy", year.toString().substr(2,2));
        }

        //replace the day
        format = format.replace("dd", day.toString().padStart(2,"0"));

        return format;
    }

    function mark_call(id){

        $.ajax({
            type: 'POST',            
            url: "<?php echo base_url() . 'marketing_controller/update_followupcall'; ?>",
            data:{invoice_detail_id: id},
            dataType: "json",
            cache: false,
            async: true,
            success: function(data) {
                toastr.success('Updated', 'Done!');
                location.reload();
            }
        });

    }

    function mark_sms(id){

        $.ajax({
            type: 'POST',            
            url: "<?php echo base_url() . 'marketing_controller/update_followupsms'; ?>",
            data:{invoice_detail_id: id},
            dataType: "json",
            cache: false,
            async: true,
            success: function(data) {
                toastr.success('Updated', 'Done!');
                location.reload();
            }
        });

    }


    </script>