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

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


</style>
<div class="panel"> 
    <div class="panel-heading panel-default">

        <div class="row">
            <div class="col col-xs-6 text-left">
                <h3 class="text-info"><strong>Deleted&nbsp;House&nbsp;List</strong></h3>
            </div>
        </div>
    </div>
    <div class="panel-body" style="margin-top: -16px">
        <table id="mira-house-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="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">House&nbsp;ID</th>
                <th class="text-center">House&nbsp;No.</th>
                <th class="text-center">Registered&nbsp;Dates</th>
                <th class="text-center">House&nbsp;Head</th>
                <th class="text-center">Total&nbsp;Member</th>
                <th class="text-center">Married&nbsp;Women</th>
                <th class="text-center">Unmarried&nbsp;Women</th>
                <th class="text-center">Adolence&nbsp;Girl</th>
                <th class="text-center">Child</th>
                <th class="text-center">Restore</th>
            </tr>
            </thead>
            <tbody>
                <?php
                if (isset($houseList) and $houseList) {
                    for ($i = 0; $i < count($houseList); $i++) {
                        ?>
                        <tr id="<?= 'h' . $houseList[$i]['auto_id'] ?>">
                            <td class="text-center"><?= ($i + 1) ?></td>
                            <td class="text-center"><?= "MR" . $houseList[$i]['auto_id'] ?></td>
                            <td class="text-center"><?= $houseList[$i]['house_number'] ?></td>
                            <td class="text-center"><?= date('d-m-Y', strtotime($houseList[$i]['create_date'])); ?></td>
                            <td class="text-center"><?= str_replace(' ', '&nbsp;', $houseList[$i]['house_head_name']) ?></td>
                            <td class="text-center"><?= $houseList[$i]['total_family_member'] ?></td>
                            <td class="text-center"><?= $houseList[$i]['married_women'] ?></td>
                            <td class="text-center"><?= $houseList[$i]['unmarried_women'] ?></td>
                            <td class="text-center"><?= $houseList[$i]['total_adolenc_girl'] ?></td>
                            <td class="text-center"><?= $houseList[$i]['total_child'] ?></td>
                            <td class="text-center"><p style="margin: 0" data-placement="top" data-toggle="tooltip" title="Restore this data"><button style="margin: 0" type="button" class="btn btn-info btn-xs house-restore-data" value="<?= $houseList[$i]['auto_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-house-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-house-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-house-list/' . ($i + 1) . '/' . $regionCode) ?>><span><?= ($i + 1) ?></span></a></li>
                                <?php } else { ?>
                                    <li class="page"><a href="<?= base_url('get-restore-house-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-house-list/' . ($activePage + 1) . '/' . $regionCode) ?>><span>Next</span></a></li>
                                    <?php } else { ?>
                                        <li class="page"><a href="<?= base_url('get-restore-house-list/' . ($activePage + 1)) ?>"><span>Next</span></a></li>
                                        <?php
                                    }
                                }
                            }
                        }
                    }
                    ?>
                </ul>

            </div>
            <!--            <div class="col-lg-6">
                            <p class="text-center" style="margin-top: 30px">
                                <span class="label label-info">Not Attempt</span>
                                <span class="label label-success ">1:Success</span>
                                <span class="label label-danger ">2:Danger</span>
                                <span class="label" style="background-color: orange">5:Refer To Asha</span>
                                <span class="label" style="background-color: black">3:Refer To PHC(Doctor)</span>
                                <span class="label" style="background-color: blue">4:Refer to SubCenter</span>
                            </p>
                        </div>-->
        </div>
    </div>
</div>
<div class="modal fade" id="houseDelete" 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 House</h4>
            </div>
            <div class="modal-body">
                <input id="auto_id_house" type="hidden" name="auto_id_house" 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_house" type="button" class="btn btn-success" ><span class="glyphicon glyphicon-ok-sign"></span> Yes</button>
                <button id="no_house" 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">
    bootstrap_alert = function () {
    }
    bootstrap_alert.warning = function (message) {
        $('#alert_placeholder').html('<div class="alert"><a class="close" data-dismiss="alert">×</a><span>' + message + '</span></div>')
    }





    $(document).ready(function () {

        //**********************************************//
        $('.house-restore-data').click(function () {
            var house_id=$(this).val();
            $.ajax({
                url: '<?= base_url('update-restore-house')?>',
                data: {
                    auto_id: house_id
                },
                type: "POST",
                success: function (data, textStatus, jqXHR) {
                    var house_update_data = JSON.parse(data);
                    if (house_update_data['status']) {
                         $('#h' +house_id ).remove();
                    }
                }
            });
           


        });
        //***********************************************//
        $('#yes_house').click(function () {
            var houseId = $('#auto_id_house').val();

            $.ajax({
                url: '<?= base_url('house-delete') ?>',
                data: {
                    auto_id: houseId
                },
                type: "POST",
                success: function (data, textStatus, jqXHR) {
                    var house_update_data = JSON.parse(data);
                    if (house_update_data['status']) {
                        $('#h' + house_update_data['house_id']).remove();
                        $('#houseDelete').modal('hide');
                        bootstrap_alert.warning('Your text goes here');
                    }
                }
            });
        });

        //*************************************************//
        $('#region_code').change(function () {
            var region_code = parseInt($('#region_code').val());
            $('#house_list_update_spinner').css('display', 'block');
            $('#mira-house-list tbody tr').remove();
            $('#pagination li').remove();
            $.ajax({
                url: '<?= base_url('house_list_view_jquery') ?>',
                data: {
                    region_code: region_code
                },
                type: "POST",
                success: function (data, textStatus, jqXHR) {
                    $('#house_list_update_spinner').css('display', 'none');
                    var house_data = JSON.parse(data);
                    if (house_data['houseList']) {
                        for (var i = 0; i < house_data['houseList'].length; i++) {
                            $("#mira-house-list tbody").append('<tr></tr>');
                            var date_string = getDateFormat(house_data['houseList'][i]['create_date']);
                            $('#mira-house-list tbody tr:last').append('<td class="text-center">' + (parseInt(i) + 1) + '</td>');
                            $('#mira-house-list tbody tr:last').append('<td class="text-center">' + "MR" + house_data['houseList'][i]['auto_id'] + '</td>');
                            $('#mira-house-list tbody tr:last').append('<td class="text-center">' + house_data['houseList'][i]['house_number'] + '</td>');
                            $('#mira-house-list tbody tr:last').append('<td class="text-center">' + date_string + '</td>');
                            $('#mira-house-list tbody tr:last').append('<td class="text-center">' + house_data['houseList'][i]['house_head_name'] + '</td>');
                            $('#mira-house-list tbody tr:last').append('<td class="text-center">' + house_data['houseList'][i]['total_family_member'] + '</td>');
                            $('#mira-house-list tbody tr:last').append('<td class="text-center">' + house_data['houseList'][i]['married_women'] + '</td>');
                            $('#mira-house-list tbody tr:last').append('<td class="text-center">' + house_data['houseList'][i]['unmarried_women'] + '</td>');
                            $('#mira-house-list tbody tr:last').append('<td class="text-center">' + house_data['houseList'][i]['total_adolenc_girl'] + '</td>');
                            $('#mira-house-list tbody tr:last').append('<td class="text-center">' + house_data['houseList'][i]['total_child'] + '</td>');
                        }
                    }
                    // This block is working for pagination.
                    if (house_data['totalPage'] && parseInt(house_data['totalPage']) > 1) {
                        var current_page = parseInt(house_data['activePage']);
                        var total_page = parseInt(house_data['totalPage']);
                        var region_code = parseInt(house_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>

