<style>
    #mmira-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;
    }
    

</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;Close&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">Pregnancy&nbsp;Status</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>
               
            </tbody>
        </table>

    </div>

</div>


<script type="text/javascript">
    
    $(document).ready(function () {
        $('#datetimepicker2').datepicker({
            format: 'dd-mm-yyyy'
            
        });
       

        $('#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_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);
                    console.log(pregnant_women_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]['delivery_status']) {
                                case '-999':
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-default">Running...</span></td>');
                                    break;
                                 case '0':
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-warning">Status not given</span></td>');
                                    break;
                                case '1':
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-success">Delivered</span></td>');
                                    break;
                                case '2':
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-primary">Aborted</span></td>');
                                    break;
                                case '3':
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-warning">Migrated</span></td>');
                                    break;
                                case '4':
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-danger">Died</span></td>');
                                    break;
                                case '5':
                                    $('#mira-pregnant-list-view tbody tr:last').append('<td class="text-center"><span class="label label-info">Other</span></td>');
                                    break;
                            }
                            
                                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;       
                                }


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

        });
        
        
    });

    

    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>
