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

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

    .datepicker{
        margin-left:174px;
    }

</style>
<?php
$ashaName = 'Testing';
$level = $this->session->userdata('level');
$anmselected=$this->session->userdata('anmregionId');
$ashaselected=$this->session->userdata('asharegionId');
$start_date=$this->session->userdata('start_date');
$end_date=$this->session->userdata('end_date');

?>
<div class="panel"> 
    <div class="row">
        <div class="col col-xs-6 text-left">
            <h3 class="text-info"><strong>Pregnancy&nbsp;Running&nbsp;List</strong></h3>
        </div>
    </div>

    <div class="panel-body" style="margin-top: -16px">
        <table id="mira-pregnant-list-view" class="table table-bordred table-striped table-hover">
            <thead>
               <?php if($level==1){ ?>
                    <tr>
                        <th colspan="10">
                            <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 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>
                            </div>
                            
                           
                        </th>
                    </tr>
                    <?php } ?>
            <tr>
                <th class="text-center">#</th>
                <th class="text-center">Pregnant&nbsp;ID</th>
                <th class="text-center">House&nbsp;No.</th>
                <th class="text-center">Women&nbsp;Name</th>
                <th class="text-center">Week</th>
                <th class="text-center">LPM&nbsp;Date</th>
                <th class="text-center">Registered&nbsp;Date</th>

                <th class="text-center">ANC1</th>
                <th class="text-center">ANC2</th>
                <th class="text-center">ANC3</th>
                <th class="text-center">ANC4</th>
            </tr>
            </thead>
            <tbody>
                <?php
                if (isset($pregnantList[0]['pregnant_id']) and $pregnantList) {
                    for ($i = 0; $i < count($pregnantList); $i++) {
                        ?>
                        <tr id="<?= 'tr' . $pregnantList[$i]['pregnant_id'] ?>">
                            <td class="text-center"><?= ($i + 1) ?></td>
                            <td class="text-center"><?= "PR" . $pregnantList[$i]['pregnant_id'] ?></td>
                            <td class="text-center"><?= $pregnantList[$i]['house_number'] ?></td>
                            <td class="text-center"><?= str_replace(' ', '&nbsp;', $pregnantList[$i]['name']) ?></td>
                            <?php
                            if ($pregnantList[$i]['delivery_status'] == -999) {
                                $day = floor((time() - strtotime($pregnantList[$i]['pregnant_date'])) / (60 * 60 * 24));
                            } else {
                                $day = floor((strtotime($pregnantList[$i]['closing_date']) - strtotime($pregnantList[$i]['pregnant_date'])) / (60 * 60 * 24));
                            }


                            if ($day % 7 == 0) {
                                $num_week = (int) ($day / 7);
                            } else {
                                $num_week = (int) ($day / 7) + 1;
                            }
                            ?>

                            
                            <td class="text-center"><span id="<?= 'week' . $pregnantList[$i]['pregnant_id'] ?>"><?= $num_week ?></span></td>
                            <td class="text-center"><span id="<?= 'lpm' . $pregnantList[$i]['pregnant_id'] ?>"><?= date('d-m-Y', strtotime($pregnantList[$i]['pregnant_date'])); ?></span></td>
                            <td class="text-center"><?= date('d-m-Y', strtotime($pregnantList[$i]['registration_date'])); ?></td>





                            <?php if ($pregnantList[$i]['anc_data']['anc1'] == 0) { ?>
                                <td class="text-center" id="<?=$pregnantList[$i]['pregnant_id'].'1'?>"><p style="margin: 0" data-placement="top" data-toggle="tooltip" title="Missed ANC1 update"><button style="margin: 0" type="button" class="btn btn-danger btn-xs anc-update-data" data-title="ancUpdate" data-toggle="modal" data-target="#ancUpdate"  value="<?= $pregnantList[$i]['pregnant_id'] . ':1' ?>"><i class="fa fa-times"></i></button></p></td>
                            <?php } else if ($pregnantList[$i]['anc_data']['anc1'] == 1) { ?>
                                <td class="text-center"><span class="label label-success"><i class="fa fa-check"></i></span></td>
                            <?php } elseif ($pregnantList[$i]['anc_data']['anc1'] == 2) { ?>
                                <td class="text-center"><span class="label label-warning"><i class="fa fa-arrow-down"></i></span></td>
                            <?php } else if ($pregnantList[$i]['anc_data']['anc1'] == 3) { ?>
                                <td class="text-center"><span class="label label-info">--</span></td>
                            <?php }
                            ?>   


                            <?php if ($pregnantList[$i]['anc_data']['anc2'] == 0) { ?>
                                <td class="text-center" id="<?= $pregnantList[$i]['pregnant_id'] . '2' ?>"><p id="<?='p'.$pregnantList[$i]['pregnant_id'] . ':2'?>" style="margin: 0" data-placement="top" data-toggle="tooltip" title="Missed ANC2 update"><button style="margin: 0" type="button" class="btn btn-danger btn-xs anc-update-data" data-title="ancUpdate" data-toggle="modal" data-target="#ancUpdate"  value="<?= $pregnantList[$i]['pregnant_id'] . ':2' ?>"><i class="fa fa-times"></i></button></p></td>
                            <?php } else if ($pregnantList[$i]['anc_data']['anc2'] == 1) { ?>
                                <td class="text-center"><span class="label label-success"><i class="fa fa-check"></i></span></td>
                            <?php } elseif ($pregnantList[$i]['anc_data']['anc2'] == 2) { ?>
                                <td class="text-center"><span class="label label-warning"><i class="fa fa-arrow-down"></i></span></td>
                            <?php } else if ($pregnantList[$i]['anc_data']['anc2'] == 3) { ?>
                                <td class="text-center"><span class="label label-info">--</span></td>
                            <?php }
                            ?>


                            <?php if ($pregnantList[$i]['anc_data']['anc3'] == 0) { ?>
                                <td class="text-center" id="<?= $pregnantList[$i]['pregnant_id'] . '3' ?>"><p id="<?='p'.$pregnantList[$i]['pregnant_id'] . ':3'?>" style="margin: 0" data-placement="top" data-toggle="tooltip" title="Missed ANC3 update"><button style="margin: 0" type="button" class="btn btn-danger btn-xs anc-update-data" data-title="ancUpdate" data-toggle="modal" data-target="#ancUpdate"  value="<?= $pregnantList[$i]['pregnant_id'] . ':3' ?>"><i class="fa fa-times"></i></button></p></td>
                            <?php } else if ($pregnantList[$i]['anc_data']['anc3'] == 1) { ?>
                                <td class="text-center"><span class="label label-success"><i class="fa fa-check"></i></span></td>
                            <?php } elseif ($pregnantList[$i]['anc_data']['anc3'] == 2) { ?>
                                <td class="text-center"><span class="label label-warning"><i class="fa fa-arrow-down"></i></span></td>
                            <?php } else if ($pregnantList[$i]['anc_data']['anc3'] == 3) { ?>
                                <td class="text-center"><span class="label label-info">--</span></td>
                            <?php }
                            ?>



                            <?php if ($pregnantList[$i]['anc_data']['anc4'] == 0) { ?>
                                <td class="text-center" id="<?= $pregnantList[$i]['pregnant_id'] . '4' ?>"><p id="<?='p'.$pregnantList[$i]['pregnant_id'] . ':4'?>" style="margin: 0" data-placement="top" data-toggle="tooltip" title="Missed ANC4 update"><button style="margin: 0" type="button" class="btn btn-danger btn-xs anc-update-data" data-title="ancUpdate" data-toggle="modal" data-target="#ancUpdate"  value="<?= $pregnantList[$i]['pregnant_id'] . ':4' ?>"><i class="fa fa-times"></i></button></p></td>
                            <?php } else if ($pregnantList[$i]['anc_data']['anc4'] == 1) { ?>
                                <td class="text-center"><span class="label label-success"><i class="fa fa-check"></i></span></td>
                            <?php } elseif ($pregnantList[$i]['anc_data']['anc4'] == 2) { ?>
                                <td class="text-center"><span class="label label-warning"><i class="fa fa-arrow-down"></i></span></td>
                            <?php } else if ($pregnantList[$i]['anc_data']['anc4'] == 3) { ?>
                                <td class="text-center"><span class="label label-info">--</span></td>
                            <?php }
                            ?>


                            <td class="text-center"><p style="margin: 0" data-placement="top" data-toggle="tooltip" title="Edit"><button style="margin: 0" type="button" class="btn btn-info btn-xs pregnant-edit-data" data-title="editUser" data-toggle="modal" data-target="#pregnantEdit"  value="<?= $pregnantList[$i]['pregnant_id'] ?>"><span class="glyphicon glyphicon-pencil"></span></button></p></td>
                        </tr>
                        <?php
                    }
                }
                ?>

            </tbody>
        </table>

    </div>

</div>



<script type="text/javascript">

    $(document).ready(function () {
        ////////////////////////////////////////////////////////////////////////
        $('#datetimepicker3').datepicker({
            format: 'dd-mm-yyyy',
            autoclose: true
        });
        //////////***************************************//////////////////////
        $('#datetimepicker2').datepicker({
            format: 'dd-mm-yyyy'

        });


        $('#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');

            $.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');
                }
            });

        });
       

        $('#region_code').change(function () {

            var region_code = parseInt($('#region_code').val());
            $('#pregnant_list_update_spinner').css('display', 'block');
            $('#mira-pregnant-list-view tbody tr').remove();
            $('#pagination li').remove();
            $.ajax({
                url: '<?= base_url('pregnant_Running_list_view_jquery') ?>',
                data: {
                    region_code: region_code
                },
                type: "POST",
                success: function (data, textStatus, jqXHR) {
                    $('#pregnant_list_update_spinner').css('display', 'none');
                    var pregnant_women_data = JSON.parse(data);
                    if (pregnant_women_data['pregnantList']) {
                        for (var i = 0; i < pregnant_women_data['pregnantList'].length; i++) {
                            $("#mira-pregnant-list-view tbody").append('<tr></tr>');
                            var pregnant_date = getDateFormat(pregnant_women_data['pregnantList'][i]['pregnant_date']);
                            var register_date = getDateFormat(pregnant_women_data['pregnantList'][i]['registration_date']);

                            $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center">' + (i + 1) + '</td>');
                            $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center">' + "PR" + pregnant_women_data['pregnantList'][i]['pregnant_id'] + '</td>');
                            $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center">' + pregnant_women_data['pregnantList'][i]['house_number'] + '</td>');
                            $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center">' + pregnant_women_data['pregnantList'][i]['name'] + '</td>');
                            $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center">' + pregnant_women_data['pregnantList'][i]['current_week'] + '</td>');
                            $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center">' + pregnant_date + '</td>');
                            $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center">' + register_date + '</td>');
                            switch (pregnant_women_data['pregnantList'][i]['anc1']) {
                                case '0':
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-danger">No</span></td>');
                                    break;
                                 case '1':
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-success">Yes</span></td>');
                                    break;
                                 default:
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-danger">No</span></td>');
                                    break;       
                                }
                                switch (pregnant_women_data['pregnantList'][i]['anc2']) {
                                case '0':
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-danger">No</span></td>');
                                    break;
                                 case '1':
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-success">Yes</span></td>');
                                    break;
                                 default:
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-danger">No</span></td>');
                                    break;       
                                }
                                switch (pregnant_women_data['pregnantList'][i]['anc3']) {
                                case '0':
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-danger">No</span></td>');
                                    break;
                                 case '1':
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-success">Yes</span></td>');
                                    break;
                                 default:
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-danger">No</span></td>');
                                    break;    
                                }
                                switch (pregnant_women_data['pregnantList'][i]['anc4']) {
                                case '0':
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-danger">No</span></td>');
                                    break;
                                 case '1':
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-success">Yes</span></td>');
                                    break;
                                    
                                 default:
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-danger">No</span></td>');
                                    break;       
                                }   

                        }
                    }
                    // This block is working for pagination.
                    if (pregnant_women_data['totalPage'] && parseInt(pregnant_women_data['totalPage']) > 1) {
                        var current_page = parseInt(pregnant_women_data['activePage']);
                        var total_page = parseInt(pregnant_women_data['totalPage']);
                        var region_code = parseInt(pregnant_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 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>
