<style>
    .table-header-background{
        background-color: #d9cdcd
    }
    #mira-women-list{
        border: 2px solid #dddddd;
    }
    #mira-women-list > thead > tr > th,
    #mira-women-list > tbody > tr > th,
    #mira-women-list > tfoot > tr > th,
    #mira-women-list > thead > tr > td,
    #mira-women-list > tbody > tr > td,
    #mira-women-list > tfoot > tr > td {
        border: 2px solid #dddddd;

    }
    #mira-women-list{
        width: 100%;
        overflow-x: scroll;
    }


</style>
<div class="panel"> 
    <div class="panel-heading panel-default">
        <h2 class="text-center">Deleted Women List</h2> 
    </div>
    <div class="panel-body" style="margin-top: -16px">
        <table id="mira-women-list" 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="women_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 class="table-header-background">
                <th class="text-center">#</th>
                <th class="text-center">Women&nbsp;ID</th>
                <th class="text-center">House&nbsp;No.</th>
                <th class="text-center">House&nbsp;Head</th>
                <th class="text-center">Women&nbsp;Name</th>
                <th class="text-center">Husband&nbsp;Name</th>
               
                <th class="text-center">Pregnant&nbsp;Status</th>
                <th class="text-center">Registered&nbsp;Date</th>
                <th class="text-center">Restore</th>
            </tr>
            </thead>
            <tbody>
                <?php
                if (isset($womenList) and $womenList) {
                    for ($i = 0; $i < count($womenList); $i++) {
                        ?>
                        <tr id="<?='w'.$womenList[$i]['women_id']?>">
                            <td class="text-center"><?= ($i + 1) ?></td>
                            <td class="text-center"><?= "W" . $womenList[$i]['women_id'] ?></td>
                            <td class="text-center"><?= $womenList[$i]['house_number'] ?></td>

                            <td class="text-center"><?= str_replace(' ', '&nbsp;', $womenList[$i]['house_head_name']) ?></td>
                            <td class="text-center"><?= str_replace(' ', '&nbsp;', $womenList[$i]['name']) ?></td>
                            <td class="text-center"><?= $womenList[$i]['husband'] ?></td>
                            

                            <?php if ($womenList[$i]['pregnant_status'] == 1) { ?>
                                <td class="text-center"><span class="label label-success">Pregnant</span></td>
                            <?php } else { ?>
                                <td class="text-center"><span class="label label-primary">Other&nbsp;Women</span></td>   
                            <?php } ?>

                            <td class="text-center"><?= date('d-m-Y', strtotime($womenList[$i]['create_date'])); ?></td>
                            <td class="text-center"><p style="margin: 0" data-placement="top" data-toggle="tooltip" title="Deleted women data restore"><button style="margin: 0" type="button" class="btn btn-info btn-xs women-restore-data" data-title="womenRestore" data-toggle="modal" value="<?= $womenList[$i]['women_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-12">
                <ul class="pagination pagination-large" id="pagination">
                    <?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 {

                                    if (isset($regionCode)) {
                                        ?>
                                        <li class="page"><a href =<?= base_url('get-restore-women-list/' . ($activePage - 1) . '/' . $regionCode) ?>><span><i class="fa fa-angle-double-left" aria-hidden="true"></i></span></a></li>
                                    <?php } else { ?>
                                        <li class="page" value="<?= ($activePage - 1) ?>"><a href="<?= base_url('get-restore-women-list/' . ($activePage - 1)) ?>"><span><i class="fa fa-angle-double-left" aria-hidden="true"></i></span></a></li>
                                        <?php
                                    }
                                }
                            }
                            if (($i + 1) == $activePage) {
                                ?>
                                <li class="active page"><span><?= ($i + 1) ?></span></li>  
                                <?php
                            } else {
                                if (isset($regionCode)) {
                                    ?>
                                    <li class="page"><a href =<?= base_url('get-restore-women-list/' . ($i + 1) . '/' . $regionCode) ?>><span><?=($i+1)?></span></a></li>
                                <?php } else { ?>
                                    <li class="page"><a href="<?= base_url('get-restore-women-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 {
                                    if (isset($regionCode)) {
                                        ?>
                                        <li class="page"><a href =<?= base_url('get-restore-women-list/' . ($activePage + 1) . '/' . $regionCode) ?>><span>Next</span></a></li>
                                        <?php } else { ?>
                                        <li class="page"><a href="<?= base_url('get-restore-women-list/' . ($activePage + 1)) ?>"><span>Next</span></a></li>
                                            <?php
                                        }
                                    }
                                }
                            }
                        }
                        ?>
                </ul>

            </div>

        </div>

    </div>
</div>


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


        });
        
        
        
        
        
        
        
         $('.women-delete-data').click(function () {
            $('#women_id').val($(this).val());
           
           var posY = $(this).position().top;
            $('#womenDelete').css({'margin-top' :300});
            $('#womenDelete').css({'margin-left' :530});
            

        });
        
         //***********************************************//
        $('#yes_women').click(function () {
            var womenId = $('#women_id').val();

            $.ajax({
                url: '<?= base_url('women-delete') ?>',
                data: {
                    women_id: womenId
                },
                type: "POST",
                success: function (data, textStatus, jqXHR) {
                    var women_update_data = JSON.parse(data);
                    if (women_update_data['status']) {
                        $('#w' + women_update_data['women_id']).remove();
                        $('#womenDelete').modal('hide');
                        
                    }
                }
            });
        });
        $('#region_code').change(function () {
            var region_code = parseInt($('#region_code').val());
            
            $('#women_list_update_spinner').css('display', 'block');
            $('#mira-women-list tbody tr').remove();
            $('#pagination li').remove();
            $.ajax({
                url: '<?= base_url('women_list_view_jquery') ?>',
                data: {
                    region_code: region_code
                },
                type: "POST",
                success: function (data, textStatus, jqXHR) {
                    $('#women_list_update_spinner').css('display', 'none');
                    var women_data = JSON.parse(data);
                    if (women_data['womenList']) {
                        for (var i = 0; i < women_data['womenList'].length; i++) {
                            $("#mira-women-list tbody").append('<tr></tr>');
                            var date_string = getDateFormat(women_data['womenList'][i]['create_date']);

                            $('#mira-women-list tbody tr:last').append('<td class="text-center">' + (parseInt(i) + 1) + '</td>');
                            $('#mira-women-list tbody tr:last').append('<td class="text-center">' + "W" + women_data['womenList'][i]['women_id'] + '</td>');
                            $('#mira-women-list tbody tr:last').append('<td class="text-center">' + women_data['womenList'][i]['house_number'] + '</td>');
                            $('#mira-women-list tbody tr:last').append('<td class="text-center">' + women_data['womenList'][i]['house_head_name'] + '</td>');
                            $('#mira-women-list tbody tr:last').append('<td class="text-center">' + women_data['womenList'][i]['name'] + '</td>');
                            $('#mira-women-list tbody tr:last').append('<td class="text-center">' + women_data['womenList'][i]['husband'] + '</td>');
                            if (women_data['womenList'][i]['dob'] == null) {
                                $('#mira-women-list tbody tr:last').append('<td class="text-center">' + "NA" + '</td>');
                            } else {
                                var dob = getDateFormat(women_data['womenList'][i]['dob']);
                                $('#mira-women-list tbody tr:last').append('<td class="text-center">' + dob + '</td>');
                            }
                            if (parseInt(women_data['womenList'][i]['pregnant_status']) === 1) {
                                $('#mira-women-list tbody tr:last').append('<td class="text-center"><span class="label label-success">' + "Pregnant" + '</span></td>');
                            } else {
                                $('#mira-women-list tbody tr:last').append('<td class="text-center"><span class="label label-success">' + "Unpregnant" + '</span></td>');
                            }
                            $('#mira-women-list tbody tr:last').append('<td class="text-center">' + date_string + '</td>');
                        }
                    }
                    // This block is working for pagination.
                    if (women_data['totalPage'] && parseInt(women_data['totalPage']) > 1) {
                        var current_page = parseInt(women_data['activePage']);
                        var total_page = parseInt(women_data['totalPage']);
                        var region_code = parseInt(women_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('women_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('women_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('women_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('women_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>


