<style>
    #mira-application-update{
        border: 2px solid #dddddd;
    }
    #mira-application-update > thead > tr > th,
    #mira-application-update > tbody > tr > th,
    #mira-application-update > tfoot > tr > th,
    #mira-application-update > thead > tr > td,
    #mira-application-update > tbody > tr > td,
    #mira-application-update > tfoot > tr > td {
        border: 2px solid #dddddd;

    }
    #mira-application-update{
        width: 100%;
        overflow-x: scroll;
    }


</style>
<div class="panel">
    <div class="panel-heading panel-default">
        <h2 class="text-center">Deleted&nbsp;Child&nbsp;List</h2>  
    </div>
    <div class="panel-body" style="margin-top: -16px">


        <table id="mira-application-update" class="table table-bordred table-striped table-hover">
            <thead>
                <?php if (isset($miraList) and $miraList) { ?>
                    <tr>
                        <th colspan="10">
                <div class="row" style="margin-top: 10px">
                    <div class="col-lg-5 col-lg-push-3">
                        <div class="form-group">
                            <div class="input-group">
                                <span class="input-group-addon"><i aria-hidden="true"></i><strong>Select Asha</strong></span>
                                <select name="region_code" id="region_code" class="form-control">
                                    <option value="0" selected="true" disabled=""> Select Asha</option>
                                    <?php
                                    for ($i = 0; $i < count($miraList); $i++) {
                                        if (isset($regionCode)) {
                                            if ($regionCode == $miraList[$i]['region_code']) {
                                                ?>
                                                <option selected="true" value=<?= $miraList[$i]['region_code'] ?> ><strong><?= $miraList[$i]['user_name'] ?></strong></option>
                                            <?php } else { ?>
                                                <option value=<?= $miraList[$i]['region_code'] ?>><strong><?= $miraList[$i]['user_name'] ?></strong></option>
                                                <?php
                                            }
                                        } else {
                                            ?>
                                            <option value=<?= $miraList[$i]['region_code'] ?>><strong><?= $miraList[$i]['user_name'] ?></strong></option>
                                            <?php
                                        }
                                    }
                                    ?>
                                </select>
                                <span class="input-group-addon"><i class="fa fa-level-down"aria-hidden="true"></i></span>
                            </div>                    
                        </div> 
                    </div>
                    <div class="col-lg-1 col-lg-push-3" id="house_list_update_spinner" style="display: none;margin-left: -10px;margin-top: 6px">
                        <i class="fa fa-spinner fa-spin"></i>
                    </div>

                </div>
                </th>
                </tr>
            <?php } ?>
            <tr>
                <th class="text-center">#</th>
                <th class="text-center">Child ID</th>
                <th class="text-center">House No.</th>
                <th class="text-center">Mother Name</th>
                <th class="text-center">Child Name</th>
                <th class="text-center">Gender</th>
                <th class="text-center">Date of Birth</th>
                <th class="text-center">Age</th>
                <th class="text-center text-info">Restore</th>

            </tr>
            </thead>
            <tbody>
                <?php
                if (isset($childList) and $childList) {
                    for ($i = 0; $i < count($childList); $i++) {
                        ?>
                        <tr id="<?='c'.$childList[$i]['child_id']?>">
                            <td class="text-center"><?= ($i + 1) ?></td>
                            <td class="text-center"><?= "CH" . $childList[$i]['child_id'] ?></td>
                            <td class="text-center"><?= $childList[$i]['house_number'] ?></td>
                            <td class="text-center"><?= $childList[$i]['name'] ?></td>
                            <td class="text-center"><?= $childList[$i]['child_name'] ?></td>
                            <?php if ($childList[$i]['gender'] == 1) { ?>
                                <td class="text-center">Male</td>
                            <?php } elseif ($childList[$i]['gender'] == 2) { ?>
                                <td class="text-center">Female</td> 
                            <?php } else { ?>
                                <td class="text-center">Other</td>   
                            <?php } ?>
                            <td class="text-center"><?= date('d-m-Y', strtotime($childList[$i]['dob'])) ?></td>
                            <?php
                            $date1 = new DateTime($childList[$i]['dob']);
                            $date2 = new DateTime(date("Y-m-d"));
                            $diff = $date1->diff($date2);
                            ?>
                            <td class="text-center"><?= $diff->y . " Years, " . $diff->m . " Months, " . $diff->d . " Days" ?></td>
                            <td class="text-center"><p style="margin: 0" data-placement="top" data-toggle="tooltip" title="Deleted data restore on click"><button style="margin: 0" type="button" class="btn btn-info btn-xs child-restore-data" data-title="childRestore" data-toggle="modal" data-target="#childRestore"  value="<?=  $childList[$i]['child_id'] ?>"><span><i class="fa fa-undo"></i></span></button></p></td>
                        </tr>
                        <?php
                    }
                }
                ?>

            </tbody>
        </table>

    </div>
    <div class="panel-footer" style="margin-top: -36px">

        <div class="row">
            <div class="col-lg-6">
                <ul class="pagination pagination-large">
                    <?php
                    if (isset($totalPage) and $totalPage and $totalPage > 1) {
                        for ($i = 0; $i < $totalPage; $i++) {
                            if ($i == 0) {
                                if ($activePage == 1) {
                                    ?>
                                    <li class="disabled"><span>«</span></li>
                                <?php } else { ?>
                                    <li class="page" value="<?= ($activePage - 1) ?>"><a href="<?= base_url('get-restore-child-list/' . ($activePage - 1)) ?>"><span>«</span></a></li>
                                    <?php
                                }
                            }


                            if (($i + 1) == $activePage) {
                                ?>

                                <li class="active page"><span><?= ($i + 1) ?></span></li>  
                            <?php } else { ?>
                                <li class="page"><a href="<?= base_url('get-restore-child-list/' . ($i + 1)) ?>"><span><?= ($i + 1) ?></span></a></li>
                                <?php
                            }


                            if (($i + 1) == $totalPage) {
                                if ($totalPage == $activePage) {
                                    ?>
                                    <li class="disabled page"><span>Next</span></li>
                                <?php } else { ?>
                                    <li class="page"><a href="<?= base_url('get-restore-child-list/' . ($activePage + 1)) ?>"><span>Next</span></a></li>
                                        <?php
                                    }
                                }
                            }
                        }
                        ?>
                </ul>
            </div>

        </div>

    </div>
</div>
<div class="modal fade" id="childDelete" tabindex="-1" role="dialog" aria-labelledby="Delete" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close btn-danger" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
                <h4 class="modal-title custom_align text-center" id="Heading">Delete Child</h4>
            </div>
            <div class="modal-body">
                <input id="child_id" type="hidden" name="child_id" value="">
                <h4 class="text-center text-danger"><strong>Are you sure you want to delete this Record?</strong></h4>
            </div>
            <div class="modal-footer ">
                <button id="yes_child" type="button" class="btn btn-success" ><span class="glyphicon glyphicon-ok-sign"></span> Yes</button>
                <button id="no_child" type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> No</button>
            </div>
        </div>

    </div>

</div>
<script type="text/javascript">
    $(document).ready(function (){
        
        
        $('.child-restore-data').click(function () {
            var child_id=$(this).val();
            $.ajax({
                url: '<?= base_url('update-restore-child')?>',
                data: {
                    child_id: child_id
                },
                type: "POST",
                success: function (data, textStatus, jqXHR) {
                    var child_update_data = JSON.parse(data);
                    if (child_update_data['status']) {
                         $('#c'+child_id ).remove();
                    }
                }
            });
           


        });
        ////////////////////********************////////////
       $('.child-delete-data').click(function () {
            $('#child_id').val($(this).val());
           
           var posY = $(this).position().top;
            $('#childDelete').css({'margin-top' :300});
            $('#childDelete').css({'margin-left' :530});
            

        });
        //***********************************************//
        $('#yes_child').click(function () {
            var childId = $('#child_id').val();

            $.ajax({
                url: '<?= base_url('child-delete') ?>',
                data: {
                    child_id: childId
                },
                type: "POST",
                success: function (data, textStatus, jqXHR) {
                    var child_update_data = JSON.parse(data);
                    if (child_update_data['status']) {
                        $('#c' + child_update_data['child_id']).remove();
                        $('#childDelete').modal('hide');
                      
                    }
                }
            });
        });

    });
    $(document).ready(function () {
        $('#region_code').change(function () {
            var region_code = parseInt($('#region_code').val());
            $('#house_list_update_spinner').css('display', 'block');
            $('#mira-application-update tbody tr').remove();
            $('#pagination li').remove();
            $.ajax({
                url: '<?= base_url('child_list_view_jquery') ?>',
                data: {
                    region_code: region_code
                },
                type: "POST",
                success: function (data, textStatus, jqXHR) {
                    $('#house_list_update_spinner').css('display', 'none');
                    var child_data = JSON.parse(data);
                    if (child_data['childList']) {


                        for (var i = 0; i < child_data['childList'].length; i++) {
                            $("#mira-application-update tbody").append('<tr></tr>');
                            var date_string = getDateFormat(child_data['childList'][i]['dob']);
                            $('#mira-application-update tbody tr:last').append('<td class="text-center">' + (parseInt(i) + 1) + '</td>');
                            $('#mira-application-update tbody tr:last').append('<td class="text-center">' + "CH" + child_data['childList'][i]['child_id'] + '</td>');
                            $('#mira-application-update tbody tr:last').append('<td class="text-center">' + child_data['childList'][i]['house_number'] + '</td>');
                            $('#mira-application-update tbody tr:last').append('<td class="text-center">' + child_data['childList'][i]['name'] + '</td>');
                            $('#mira-application-update tbody tr:last').append('<td class="text-center">' + child_data['childList'][i]['child_name'] + '</td>');
                            if(parseInt(child_data['childList'][i]['gender'])==1){
                               $('#mira-application-update tbody tr:last').append('<td class="text-center">Male</td>'); 
                            }else if(parseInt(child_data['childList'][i]['gender'])==2){
                                $('#mira-application-update tbody tr:last').append('<td class="text-center">Female</td>');
                            }else{
                                $('#mira-application-update tbody tr:last').append('<td class="text-center">Other</td>');
                            }
                           
                            $('#mira-application-update tbody tr:last').append('<td class="text-center">' + date_string + '</td>');

                            }
                    }
                    // This block is working for pagination.
                    if (child_data['totalPage'] && parseInt(child_data['totalPage']) > 1) {
                        var current_page = parseInt(child_data['activePage']);
                        var total_page = parseInt(child_data['totalPage']);
                        var region_code = parseInt(child_data['regionCode']);
                        previousPage(current_page, region_code, 'pagination');
                        forLoop(current_page, total_page, region_code, 'pagination');
                        nextPage(current_page, total_page, region_code, 'pagination');
                    }
                }
            });
        });
    });

    function nextDoubleClick(startingindex, regioncode, selectorid) {
        $('#' + selectorid).append('<li class ="disabled"><span>. .. ...</span></li>');
        $('#' + selectorid).append('<li class="page"><a href =' + '<?= base_url('house_list_view') ?>' + '/' + (startingindex + 5) + '/' + regioncode + '><span><i class="fa fa-angle-double-right" aria-hidden="true"></i><i class="fa fa-angle-double-right" aria-hidden="true"></i></span></a></li>');
    }
    function previousDoubleClick(startingindex, regioncode, selectorid) {

        $('#' + selectorid).append('<li class="page"><a href =' + '<?= base_url('house_list_view') ?>' + '/' + (startingindex - 5) + '/' + regioncode + '><span><i class="fa fa-angle-double-left" aria-hidden="true"></i><i class="fa fa-angle-double-left" aria-hidden="true"></i></span></a></li>');
        $('#' + selectorid).append('<li class ="disabled"><span>... .. .</span></li>');

    }
    function previousPage(startingindex, regioncode, selectorid) {

        $('#' + selectorid).append('<li class ="disabled"><span><i class="fa fa-angle-double-left" aria-hidden="true"></i></span></li>');

    }
    function nextPage(startingindex, lastindex, regioncode, selectorid) {

        $('#' + selectorid).append('<li class="page"><a href =' + '<?= base_url('house_list_view') ?>' + '/' + (startingindex + 1) + '/' + regioncode + '><span>Next</span></a></li>');



    }
    function forLoop(startingindex, lastindex, regioncode, selectorid) {
        for (var i = 1; i <= lastindex; i++) {
            if (i === startingindex) {
                $('#' + selectorid).append('<li class="active page"><span>' + i + '</span></li>');
            } else {
                $('#' + selectorid).append('<li class="page"><a href=' + '<?= base_url('house_list_view') ?>' + '/' + i + '/' + regioncode + '><span>' + i + '</span></a></li>');
            }

        }
    }
    function getDateFormat(date) {
        var split_date_array1 = date.split('-');
        var split_date_array2 = split_date_array1[2].split(' ');
        return (split_date_array2[0] + '-' + split_date_array1[1] + '-' + split_date_array1[0]);
    }

</script>


