<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;
    }

    .back-btn-style {
        float: right;
        margin-bottom: -10px;
    }
</style>
<?php
$ashaName = 'Testing';
$level = $this->session->userdata('level');
$asharegion = $this->session->userdata('asharegionId');

?>
<div class="panel">

    <div class="panel-body" style="margin-top: -16px">
        <div class="col text-left bg-info">
            <h3 class="text-info" style=" width: 300px; float:left;">
                <strong>Registered&nbsp;Child&nbsp;List&nbsp;</strong></h3>
            <?php $regionId = $this->session->userdata('regionId'); ?>
            <?php

        //    if ($regionId) {
                if ( isset($flag) && $flag == true) { ?>
                    <a class="btn btn-primary back-btn-style" href="javascript:window.history.back();">Back</a>
                <?php
                }
           // } ?>
        </div>
        <table id="mira-application-update" class="table table-bordred table-striped table-hover">
            <thead>


                <?php
                if ( $level == 1) {
                    if ( $flag != true) { ?>

                        <tr>
                            <th colspan="10">
                                <div class="row" style="margin-left: 2px">
                                    <div class="row">
                                        <div class="col-md-4">
                                            <div class="form-group">
                                                <div class="input-group">
                                                    <span class="input-group-addon"><i aria-hidden="true"></i><strong>Select
                                                            ANM</strong></span>
                                                    <select name="anm_region_code" id="anm_region_code" class="form-control">
                                                        <option value="0" selected="true" disabled=""> Select ANM</option>
                                                        <?php
                                                        for ($i = 0; $i < count($anmList); $i++) {
                                                            if (isset($regionCode)) {
                                                                if ($anmselected == $anmList[$i]['region_code']) {
                                                                    ?>
                                                                    <option selected="true" value=<?= $anmList[$i]['region_code'] ?>>
                                                                        <strong><?= $anmList[$i]['user_name'] ?></strong></option>
                                                                <?php } else { ?>
                                                                    <option value=<?= $anmList[$i]['region_code'] ?>><strong><?= $anmList[$i]['user_name'] ?></strong></option>
                                                                    <?php
                                                                }
                                                            } else {
                                                                ?>
                                                                <option value=<?= $anmList[$i]['region_code'] ?>><strong><?= $anmList[$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>
                                </div>

                                <div class="row" style="margin-top: 10px">
                                    <?php if (isset($miraList) and $miraList and $level == 1) { ?>
                                        <div class="col-md-4">
                                            <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 ($ashaselected == $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>
                                    <?php } else { ?>
                                        <div class="col-md-4">
                                            <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>

                                                    </select>
                                                    <span class="input-group-addon"><i class="fa fa-level-down"
                                                            aria-hidden="true"></i></span>
                                                </div>
                                            </div>
                                        </div>
                                    <?php } ?>
                                    <div class="col-lg-1 col-lg-push-3" id="asha_list_update_spinner"
                                        style="display: none;margin-left: -10px;margin-top: 6px">
                                        <i class="fa fa-spinner fa-spin"></i>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <div class="input-group">
                                                <span class="input-group-addon"><i aria-hidden="true"></i><strong>Search
                                                        By</strong></span>
                                                <select name="searchbydate" id="searchbydate" required class="form-control">
                                                    <option value="0" disabled="true" selected="true"> Select Option</option>
                                                    <option value="1"> <strong>By Mobile Date</strong></option>
                                                    <option value="2"> <strong>By Actual Date</strong></option>
                                                    <option value="3"><strong>By Server Date</strong></option>
                                                </select>
                                                <span class="input-group-addon"><i class="fa fa-level-down"
                                                        aria-hidden="true"></i></span>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="col-md-2-push-2" id="trdspan" style="display: none;">
                                        <label><b>Total Records:</b><span id="trd"></span></label>
                                    </div>
                                    <div class="col-md-2-push-2" style="display: none;" id="frdspan">
                                        <label><b>Filtered Records:</b><span id="frd"></span></label>
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-lg-4">

                                        <div class="form-group">
                                            <div class="input-group date" id="st_date" data-date-fromat="dd-mm-yyyy"
                                                style="color: white">
                                                <span class="input-group-addon"><strong>Start Date</strong></span>
                                                <input class="form-control" type="text" name="st_date" id="st_date_input"
                                                    required placeholder="dd-mm-yyyy">
                                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                            </div>
                                        </div>

                                    </div>
                                    <div class="col-lg-4">

                                        <div class="form-group">
                                            <div class="input-group date" id="en_date" data-date-fromat="dd-mm-yyyy"
                                                style="color: white">
                                                <span class="input-group-addon"><strong>End Date</strong></span>
                                                <input class="form-control" type="text" name="en_date" id="en_date_input"
                                                    required placeholder="dd-mm-yyyy">
                                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                            </div>
                                        </div>

                                    </div>

                                    <div class="col-lg-4">
                                        <input type="button" class="btn btn-primary" id="btnsearch" value="Submit">
                                    </div>

                                </div>
                            </th>
                        </tr>
                    <?php }
                } ?>

                <?php if (isset($miraList) and $miraList and $level > 1) { ?>
                    <tr>
                        <th colspan="10">
                            <div class="row" style="margin-top: 10px">
                                <div class="col-md-4">
                                    <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 class="col-md-4">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <span class="input-group-addon"><i aria-hidden="true"></i><strong>Search
                                                    By</strong></span>
                                            <select name="searchbydate" id="searchbydate" required class="form-control">
                                                <option value="0" disabled="true" selected="true"> Select Option</option>
                                                <option value="1"> <strong>By Mobile Date</strong></option>
                                                <option value="2"> <strong>By Actual Date</strong></option>
                                                <option value="3"><strong>By Server Date</strong></option>
                                            </select>
                                            <span class="input-group-addon"><i class="fa fa-level-down"
                                                    aria-hidden="true"></i></span>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-md-2-push-2" id="trdspan" style="display: none;">
                                    <label><b>Total Records:</b><span id="trd"></span></label>
                                </div>
                                <div class="col-md-2-push-2" style="display: none;" id="frdspan">
                                    <label><b>Filtered Records:</b><span id="frd"></span></label>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-lg-4">

                                    <div class="form-group">
                                        <div class="input-group date" id="st_date" data-date-fromat="dd-mm-yyyy"
                                            style="color: white">
                                            <span class="input-group-addon"><strong>Start Date</strong></span>
                                            <input class="form-control" type="text" name="st_date" id="st_date_input"
                                                required placeholder="dd-mm-yyyy">
                                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                        </div>
                                    </div>

                                </div>
                                <div class="col-lg-4">

                                    <div class="form-group">
                                        <div class="input-group date" id="en_date" data-date-fromat="dd-mm-yyyy"
                                            style="color: white">
                                            <span class="input-group-addon"><strong>End Date</strong></span>
                                            <input class="form-control" type="text" name="en_date" id="en_date_input"
                                                required placeholder="dd-mm-yyyy">
                                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                        </div>
                                    </div>

                                </div>

                                <div class="col-lg-4">
                                    <input type="button" class="btn btn-primary" id="btnsearch" value="Submit">
                                </div>

                            </div>
                        </th>
                    </tr>


                <?php } ?>
                <tr>
                    <th class="text-center">#</th>
                    <th class="text-center">Child ID</th>
                    <th class="text-center">Child Name</th>
                    <th class="text-center">Actual&nbsp;Registration&nbsp;Date</th>
                    <th class="text-center">House No.</th>
                    <th class="text-center">Mother Name</th>
                    <th class="text-center">Gender</th>
                    <th class="text-center">Date of Birth</th>

                </tr>
            </thead>
            <tbody>
                <?php
                if (isset($childrenList)) {
                    $childList = $childrenList;
                }

                if (isset($childList) and $childList and $childList != NULL) {
                    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]['child_name'] ?>
                            </td>
                            <td class="text-center">
                                <?= date('d-m-Y', strtotime($childList[$i]['create_date'])); ?>
                            </td>
                            <td class="text-center">
                                <?= $childList[$i]['house_number'] ?>
                            </td>
                            <td class="text-center">
                                <?= $childList[$i]['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>

                        </tr>
                        <?php
                    }
                } else {
                    if (isset($message) && $message) {
                        ?>
                        <h4 class="text-bold text-danger">
                            <?= $message ?>
                        </h4>
                        <?php
                    } else {
                        echo '';
                    }
                }
                ?>



            </tbody>
        </table>

    </div>
    <div class="panel-footer" style="margin-top: -36px">
        <h4 id="no-data" class="text-center text-danger" style="display: none;">
            <b>* Data not found . . !</b>
        </h4>
        <div class="row">
            <div class="col-lg-6">
                <ul id="pagination" class="pagination pagination-large">
                    
                </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-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();

            $('#st_date_input').val('');
            $('#en_date_input').val('');
            $("#trd").text(0);
            $("#frd").text('0');
            $('#searchbydate option:first').prop('selected', true);

            $.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');
                    $('#no-data').css('display', 'none');
                    let child_data;
                    try{
                        child_data = JSON.parse(data);
                        console.log(child_data['childList'].length);
                        if (child_data['childList']) {
                            $("#trd").text(child_data['totalRecords']);
                            $('#trdspan').css('display', 'block');
                            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']);
                                var ardate_string = getDateFormat(child_data['childList'][i]['actual_register_date']);
                                $('#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]['child_name'] + '</td>');
                                $('#mira-application-update tbody tr:last').append('<td class="text-center">' + ardate_string + '</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>');

                                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>');

                            }
                        }
                        if(child_data['totalPage'] && parseInt(child_data['totalPage']) > 1){

                            let totalPages = parseInt(child_data['totalPage']);
                            const pagination = document.getElementById('pagination');

                            // Create previous button
                            const prevLi = document.createElement('li');
                            prevLi.className = 'page-item';
                            const prevLink = document.createElement('a');
                            prevLink.className = 'page-link';
                            prevLink.href = '#';
                            prevLink.setAttribute('aria-label', 'Previous');
                            const prevSpan = document.createElement('span');
                            prevSpan.setAttribute('aria-hidden', 'true');
                            prevSpan.innerHTML = '&laquo;';
                            prevLink.appendChild(prevSpan);
                            prevLi.appendChild(prevLink);
                            pagination.appendChild(prevLi);

                            // Create numeric page buttons using a for loop
                            for (let i = 1; i <= totalPages; i++) {
                                const pageLi = document.createElement('li');
                                pageLi.className = 'page-item';
                                const pageLink = document.createElement('a');
                                pageLink.className = 'page-link';
                                pageLink.href = '#';
                                pageLink.onclick = function() {
                                    getDataUsingPagination(i, region_code);
                                };
                                pageLink.textContent = i;
                                pageLi.appendChild(pageLink);
                                pagination.appendChild(pageLi);
                            }
                            // Create next button
                            const nextLi = document.createElement('li');
                            nextLi.className = 'page-item';
                            const nextLink = document.createElement('a');
                            nextLink.className = 'page-link';
                            nextLink.href = '#';
                            nextLink.setAttribute('aria-label', 'Next');
                            const nextSpan = document.createElement('span');
                            nextSpan.setAttribute('aria-hidden', 'true');
                            nextSpan.innerHTML = '&raquo;';
                            nextLink.appendChild(nextSpan);
                            nextLi.appendChild(nextLink);
                            pagination.appendChild(nextLi);
                        }
                    }
                    catch{
                        $('#no-data').css('display', 'block');
                    }
                }
            });
        });

        function getDataUsingPagination(pageNumber, regionCode){
            var region_code = parseInt($('#region_code').val());
            $('#house_list_update_spinner').css('display', 'block');
            $('#mira-application-update tbody tr').remove();
            $.ajax({
                url: '<?= base_url('/registrationhousewomenchild/getChildListByRegionIdUsingPagination') ?>',
                data: {
                    pageNumber: pageNumber,
                    regionCode: regionCode
                },
                type: "POST",
                success: function (data, textStatus, jqXHR) {
                    $('#house_list_update_spinner').css('display', 'none');
                    var child_data = JSON.parse(data);
                    if (child_data['childList']) {
                        $('#trdspan').css('display', 'block');

                        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']);
                            var ardate_string = getDateFormat(child_data['childList'][i]['actual_register_date']);
                            $('#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]['child_name'] + '</td>');
                            $('#mira-application-update tbody tr:last').append('<td class="text-center">' + ardate_string + '</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>');

                            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>');

                        }
                    }
                }
            });
        }

        $('#btnsearch').click(function () {
            let stdate = $('#st_date_input').val();
            let endate = $('#en_date_input').val();
            let rcode = parseInt($('#region_code').val());
            let searchby = parseInt($('#searchbydate').val());
            // alert('hi   .....==='+searchby);

            $('#house_list_update_spinner').css('display', 'block');
            $('#mira-application-update tbody tr').remove();
            $('#pagination li').remove();

            $.ajax({
                url: '<?= base_url('child_list_view_jqueryV2') ?>',
                data: {
                    region_code: rcode,
                    start_date: stdate,
                    end_date: endate,
                    search_by: searchby
                },
                type: "POST",
                success: function (data, textStatus, jqXHR) {
                    //console.log(data);
                    $('#house_list_update_spinner').css('display', 'none');
                    $("#frd").text('0');
                    var child_data = JSON.parse(data);
                    if (child_data['childList']) {
                        $("#frd").text(child_data['totalRecords']);
                        $('#frdspan').css('display', 'block');
                        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']);
                            var ardate_string = getDateFormat(child_data['childList'][i]['actual_register_date']);
                            $('#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]['child_name'] + '</td>');
                            $('#mira-application-update tbody tr:last').append('<td class="text-center">' + ardate_string + '</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>');

                            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) {
                        let totalPages = parseInt(child_data['totalPage']);
                        const pagination = document.getElementById('pagination');
                        // Create previous button
                        const prevLi = document.createElement('li');
                        prevLi.className = 'page-item';
                        const prevLink = document.createElement('a');
                        prevLink.className = 'page-link';
                        prevLink.href = '#';
                        prevLink.setAttribute('aria-label', 'Previous');
                        const prevSpan = document.createElement('span');
                        prevSpan.setAttribute('aria-hidden', 'true');
                        prevSpan.innerHTML = '&laquo;';
                        prevLink.appendChild(prevSpan);
                        prevLi.appendChild(prevLink);
                        pagination.appendChild(prevLi);

                        // Create numeric page buttons using a for loop
                        for (let i = 1; i <= totalPages; i++) {
                            const pageLi = document.createElement('li');
                            pageLi.className = 'page-item';
                            const pageLink = document.createElement('a');
                            pageLink.className = 'page-link';
                            pageLink.href = '#';
                            pageLink.onclick = function() {
                                getFilteredChildDataUsingPagination(i, rcode, stdate, endate, searchby);
                            };
                            pageLink.textContent = i;
                            pageLi.appendChild(pageLink);
                            pagination.appendChild(pageLi);
                        }
                        // Create next button
                        const nextLi = document.createElement('li');
                        nextLi.className = 'page-item';
                        const nextLink = document.createElement('a');
                        nextLink.className = 'page-link';
                        nextLink.href = '#';
                        nextLink.setAttribute('aria-label', 'Next');
                        const nextSpan = document.createElement('span');
                        nextSpan.setAttribute('aria-hidden', 'true');
                        nextSpan.innerHTML = '&raquo;';
                        nextLink.appendChild(nextSpan);
                        nextLi.appendChild(nextLink);
                        pagination.appendChild(nextLi);
                    }
                }
            });
        });

        function getFilteredChildDataUsingPagination(pageNumebr, regionCode, startDate, endDate, searchBy){
            event.preventDefault();
            let stdate=$('#st_date_input').val();
            let endate=$('#en_date_input').val();
            let rcode=parseInt($('#region_code').val());
            let searchby=parseInt($('#searchbydate').val());
            var _name = $('#region_code option:selected').text();  
            $('#house_list_update_spinner').css('display', 'block');
            $('#mira-application-update tbody tr').remove();

            $.ajax({
                url: '<?= base_url('/registrationhousewomenchild/getFilteredChildDataUsingPagination')?>',
                data: {
                    page_number: pageNumebr,
                    region_code: rcode,
                    start_date: stdate,
                    end_date: endate,
                    search_by: searchby
                },
                type: "POST",
                success: function (data, textStatus, jqXHR) {
                    console.log(data);
                    $('#house_list_update_spinner').css('display', 'none');
                    var child_data = JSON.parse(data);
                    if (child_data['childList']) {
                        $("#frd").text(child_data['totalRecords']);
                        $('#frdspan').css('display', 'block');
                        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']);
                            var ardate_string = getDateFormat(child_data['childList'][i]['actual_register_date']);
                            $('#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]['child_name'] + '</td>');
                            $('#mira-application-update tbody tr:last').append('<td class="text-center">' + ardate_string + '</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>');

                            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>');

                        }
                    }
                }
            });
        }

        $('#anm_region_code').change(function () {
            var region_code = parseInt($('#anm_region_code').val());
            $('#region_code').children('option:not(:first)').remove();
            $('#region_code').css('display', 'block');

            $('#st_date_input').val('');
            $('#en_date_input').val('');
            $("#trd").text(0);
            $("#frd").text('0');
            $('#searchbydate option:first').prop('selected', true);

            $.ajax({
                url: '<?= base_url('getAshalistByANMId') ?>',
                data: {
                    region_code: region_code
                },
                type: "POST",
                success: function (json) {
                    var states = $.parseJSON(json);
                    for (var i = 0; i < states['miraList'].length; i++) {
                        $("#region_code").append($("<option></option>").val(states['miraList'][i]['region_code']).html(states['miraList'][i]['user_name']));
                    }
                    $('#asha_list_update_spinner').css('display', 'none');
                },
                error: function (xhr, status, errorThrown) {
                    $('#asha_list_update_spinner').css('display', 'none');
                },
                complete: function (xhr, status) {
                    $('#asha_list_update_spinner').css('display', 'none');
                }
            });

        });


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










    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>