<style type="text/css">
    .panel.panel-default.panel-body{
        margin-top: 20px;
    }


    .label-circle {


    }
    .table-background-odd{
        background-color: #f3e3e0;

    }
    .table-background-even{
        background-color: #f6f0ee  
    }
    .table-background-head{
        background-color: #e8eff4;  
    }




    .spinner{
        margin-top: 8px;
        margin-left: -5px;
        display: none;
    }
    .form_label{
        margin-top: 4px;
    }
    .td_success{
        background-color: green;
    }
    .td_danger{
        background-color: red;
    }
    .td_dots{
        background-color: blue;
    }
    .td_outside{
        background-color: #0099FF;
    }
    .td_remain{
        background-color: #5bc0de;
    }
    .permanent_patient_id{
        cursor: pointer;
    }

    .table-user-information tbody tr td{
        text-align: center;
        color: #269abc;

    }
    .table-user-information{
        margin-top: -12px;

    }


    .table-user-information > tbody > tr {
        border-top: 1px solid rgb(221, 221, 221);
    }

    .table-user-information > tbody > tr:first-child {
        border-top: 0;
    }


    .table-user-information > tbody > tr > td {
        border-top: 0;
    }



    #patient_dose_taken_phase1_table{
        border: 1px solid #857875;
    }
    #patient_dose_taken_phase1_table > thead > tr > th,
    #patient_dose_taken_phase1_table > tbody > tr > th,
    #patient_dose_taken_phase1_table > tfoot > tr > th,
    #patient_dose_taken_phase1_table > thead > tr > td,
    #patient_dose_taken_phase1_table > tbody > tr > td,
    #patient_dose_taken_phase1_table > tfoot > tr > td {
        border: 1px solid #857875;

    }
    #patient_dose_taken_phase1_table{
        display:inline-block;
        width: 100%;
        overflow-x: scroll;
    }

</style>



<div class="panel">

        <div class="row">
        <div class="col col-xs-6 text-left">
            <h3 class="text-info"><strong>Pregnant Women View Tracker</strong></h3>
        </div>
    </div>
  

    <div class="panel-body" style="margin-top: -16px">
        <table class="table tatable-bordered table-hover tableborder" id="patient_dose_taken_phase1_table">
            <thead>
               
                    <tr>
                        <th colspan="10">
                            <div class="row">
                                <div class="col-md-6">
                                    <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-6">
                                    <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>
                       
                <tr class="table-background-head">
                    <th class="text-center fix" rowspan="2">H.&nbsp;No.</th> 
                    <th class="text-center" rowspan="2">Women&nbsp;Name</th>
                    <th class="text-center" rowspan="2">Start&nbsp;Date&nbsp;&nbsp;</th> 
                    <th class="text-center" rowspan="2">Week&nbsp;Day</th>
                    <th class="text-center" colspan="4">ANC Status</th>
                    <?php for ($i = 0; $i < 40; $i++) { ?>
                    <th class="text-center" colspan="5">Week&nbsp;No.<?= ($i + 1) ?></th>
                    <?php } ?>


                </tr>

                <tr class="table-background-head">
                    <th class="text-center">ANC1</th>
                    <th class="text-center">ANC2</th>
                    <th class="text-center">ANC3</th>
                    <th class="text-center">ANC4</th>
                    <?php for ($i = 0; $i < 40; $i++) { ?>
                        <th class="text-center">Q1</th>
                        <th class="text-center">Q2</th>
                        <th class="text-center">Q3</th>
                        <th class="text-center">Q4</th>
                        <th class="text-center">Q5</th>
                    <?php } ?>


                </tr>
            </thead>
            <tbody>
                <?php
                if (isset($list) and $list) {
                    for ($i = 0; $i < count($list); $i++) {
                        ?>


                        <tr>
                            <td class="text-center table-background-odd fix"><?= $list[$i]['house_number'] ?></td>
                            <td class="text-center table-background-odd"><?= str_replace(' ', '&nbsp;', $list[$i]['name']) ?></td>
                            <td class="text-center table-background-odd"><?= date('d-m-Y', strtotime($list[$i]['pregnant_date'])) ?></td>




                            <?php if ($list[$i]['current_week'] > 40) { ?>
                                <td class="text-center table-background-odd"><?= "W40+&nbsp;&nbsp;D" . ($list[$i]['day']+1) ?></td>
                            <?php } else { ?>
                                <td class="text-center table-background-odd"><?= "W" . $list[$i]['current_week'] . "&nbsp;&nbsp;D" . ($list[$i]['day']+1) ?></td>
                            <?php }
                            ?>
                                
                                
                                
                            <?php if ($list[$i]['anc_data']['anc1'] == 0) { ?>
                                <td class="text-center"><span class="label label-danger"><i class="fa fa-times"></i></span></td>
                            <?php } else if ($list[$i]['anc_data']['anc1'] == 1) { ?>
                                <td class="text-center"><span class="label label-success"><i class="fa fa-check"></i></span></td>
                            <?php } elseif ($list[$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 ($list[$i]['anc_data']['anc1'] == 3) { ?>
                                <td class="text-center"><span class="label label-info">--</span></td>
                            <?php }
                            ?>   


                            <?php if ($list[$i]['anc_data']['anc2'] == 0) { ?>
                                <td class="text-center"><span class="label label-danger"><i class="fa fa-times"></i></span></td>
                            <?php } else if ($list[$i]['anc_data']['anc2'] == 1) { ?>
                                <td class="text-center"><span class="label label-success"><i class="fa fa-check"></i></span></td>
                            <?php } elseif ($list[$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 ($list[$i]['anc_data']['anc2'] == 3) { ?>
                                <td class="text-center"><span class="label label-info">--</span></td>
                            <?php }
                            ?>


                            <?php if ($list[$i]['anc_data']['anc3'] == 0) { ?>
                                <td class="text-center"><span class="label label-danger"><i class="fa fa-times"></i></span></td>
                            <?php } else if ($list[$i]['anc_data']['anc3'] == 1) { ?>
                                <td class="text-center"><span class="label label-success"><i class="fa fa-check"></i></span></td>
                            <?php } elseif ($list[$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 ($list[$i]['anc_data']['anc3'] == 3) { ?>
                                <td class="text-center"><span class="label label-info">--</span></td>
                            <?php }
                            ?>



                            <?php if ($list[$i]['anc_data']['anc4'] == 0) { ?>
                                <td class="text-center"><span class="label label-danger"><i class="fa fa-times"></i></span></td>
                            <?php } else if ($list[$i]['anc_data']['anc4'] == 1) { ?>
                                <td class="text-center"><span class="label label-success"><i class="fa fa-check"></i></span></td>
                            <?php } elseif ($list[$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 ($list[$i]['anc_data']['anc4'] == 3) { ?>
                                <td class="text-center"><span class="label label-info">--</span></td>
                            <?php }
                            ?>





                            <?php
                            $flag12 = TRUE;
                            for ($j = 0; $j < 40; $j++) {
                                $odd_even_css = NULL;
                                if ($j % 2 == 0) {
                                    $odd_even_css = 'table-background-even';
                                } else {
                                    $odd_even_css = 'table-background-odd';
                                }

                                $flag = TRUE;
                                $flag1 = TRUE;

                                if ($flag12 and $list[$i]['out_side_week_complete']) {
                                    $flag1 = FALSE;
                                    $flag12 = FALSE;
                                    if ($list[$i]['out_side_week_complete'] > 40) {
                                        $counter = 40;
                                    } else {
                                        $counter = $list[$i]['out_side_week_complete'];
                                    }
                                    for ($l = 0; $l < $counter; $l++) {
                                        if ($l % 2 == 0) {
                                            $odd_even_css = 'table-background-even';
                                        } else {
                                            $odd_even_css = 'table-background-odd';
                                        }
                                        ?>
                                        <td class=<?= $odd_even_css ?>><span class="label label-info label-circle"><strong>0</strong></span></td>
                                        <td class=<?= $odd_even_css ?>><span class="label label-info label-circle"><strong>0</strong></span></td>
                                        <td class=<?= $odd_even_css ?>><span class="label label-info label-circle"><strong>0</strong></span></td>
                                        <td class=<?= $odd_even_css ?>><span class="label label-info label-circle"><strong>0</strong></span></td>
                                        <td class=<?= $odd_even_css ?>><span class="label label-info label-circle"><strong>0</strong></span></td>
                                        <?php
                                    }
                                    $j = ($l - 1);
                                }


                                if ($flag1) {
                                    for ($k = 0; $k < count($list[$i]['ans']); $k++) {
                                        if ($list[$i]['ans'][$k]['week_id'] == ($j + 1)) {
                                            $flag = FALSE;
                                            $_span_css = NULL;
                                            foreach ($list[$i]['ans'][$k]['ans'] as $key => $value) {
                                                if (!$list[$i]['ans'][$k]['alert'] or ! $value['anm_answer']) {
                                                    //Mira question answer update.
                                                    if ($value['asha_answer'] == 1) {
                                                        ?>

                                                        <td class=<?= $odd_even_css ?>><span class="label label-success label-circle"><?= $value['asha_answer'] ?></span></td>
                                                    <?php } else { ?>
                                                        <td class=<?= $odd_even_css ?>><span class="label label-danger label-circle"><?= $value['asha_answer'] ?></span></td> 
                                                        <?php
                                                    }
                                                } else {
                                                    //ANM Rrefre to doctor,phi etc.
                                                    if ($value['anm_answer'] == 1) {
                                                        ?>
                                                        <td class=<?= $odd_even_css ?>><span class="label label-success label-circle"><?= $value['asha_answer'] ?></span></td>
                                                        <?php
                                                    } elseif ($value['anm_answer'] == 3) {
                                                        ?>
                                                        <td class=<?= $odd_even_css ?>><span class="label label-primary label-circle"><?= $value['anm_answer'] ?></span></td>
                                                        <?php
                                                    } elseif ($value['anm_answer'] == 4) {
                                                        ?>
                                                        <td class=<?= $odd_even_css ?>><span class="label label-default label-circle"><?= $value['anm_answer'] ?></span></td> 
                                                        <?php
                                                    } elseif ($value['anm_answer'] == 5) {
                                                        ?>
                                                        <td class=<?= $odd_even_css ?>><span class="label label-default label-circle"><?= $value['anm_answer'] ?></span></td> 
                                                        <?php
                                                    }
                                                }
                                            }
                                            break;
                                        }
                                    }
                                }

                                if ($flag and $flag1) {
                                    if ($list[$i]['delivery_status'] == -999) {
                                        if (($j) < ($list[$i]['current_week'] - 1)) {
                                            ?>
                                            <td class=<?= $odd_even_css ?>><span class="label label-warning label-circle">M</span></td>
                                            <td class=<?= $odd_even_css ?>><span class="label label-warning label-circle">M</span></td>
                                            <td class=<?= $odd_even_css ?>><span class="label label-warning label-circle">M</span></td>
                                            <td class=<?= $odd_even_css ?>><span class="label label-warning label-circle">M</span></td>
                                            <td class=<?= $odd_even_css ?>><span class="label label-warning label-circle">M</span></td>

                                            <?php
                                        } else {
                                            ?>
                                            <td class=<?= $odd_even_css ?>></td>
                                            <td class=<?= $odd_even_css ?>></td>
                                            <td class=<?= $odd_even_css ?>></td>
                                            <td class=<?= $odd_even_css ?>></td>
                                            <td class=<?= $odd_even_css ?>></td>
                                            <?php
                                        }
                                    } else {
                                        if (($j) < ($list[$i]['current_week'])) {
                                            ?>
                                            <td class=<?= $odd_even_css ?>><span class="label label-warning label-circle">M</span></td>
                                            <td class=<?= $odd_even_css ?>><span class="label label-warning label-circle">M</span></td>
                                            <td class=<?= $odd_even_css ?>><span class="label label-warning label-circle">M</span></td>
                                            <td class=<?= $odd_even_css ?>><span class="label label-warning label-circle">M</span></td>
                                            <td class=<?= $odd_even_css ?>><span class="label label-warning label-circle">M</span></td>
                                            <?php
                                        } else {
                                            ?>
                                            <td class=<?= $odd_even_css ?>></td>
                                            <td class=<?= $odd_even_css ?>></td>
                                            <td class=<?= $odd_even_css ?>></td>
                                            <td class=<?= $odd_even_css ?>></td>
                                            <td class=<?= $odd_even_css ?>></td>
                                            <?php
                                        }
                                    }
                                }
                            }
                            ?>
                        </tr>
                        <?php
                    }
                }
                ?>


            </tbody>
        </table>
    </div>
  
</div>


    
  <script type="text/javascript">
    
    $(document).ready(function () {

    
        $('#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_immunization_get') ?>',
                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 () {
         alert('Welcome');
            var region_code = parseInt($('#anm_region_code').val());
            $('#region_code').children('option:not(:first)').remove();
            $('#region_code').css('display', 'block');


            $.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>

 

