<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            Pendaftaran <label style="font-weight: normal; font-size:15px" id="timer"></label>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Sis-RS</a></li>
            <li class="active">Pendaftaran</li>
        </ol>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row" style="margin-bottom: 10px;">
            <div class="col-md-8">
                <i class="fa fa-fw fa-calendar"></i> Daftar Pasien


            </div>
            <div class="col-md-4" align="right">
               
            </div>
        </div>
        <div class="box">
            <div class="box-body">
                <?= $this->session->flashdata('message'); ?>
                <h4 style="font-weight: bold;" class="text-primary">IDENTITAS </h4>

                <form action="<?= base_url('Pendaftaran/simpanpasien'); ?>" method="post">

                    <div class="row">
                        <div class="col-md-6">
                            <table width="100%" class="table">
                                <tr>
                                    <td width="25%">
                                        No KTP / NIK
                                    </td>
                                    <td>                                        
                                        <div class="row">
                                            <div class="col-md-10">
                                                <input type="text" id="nikPeserta" name="NIK" class="form-control" required>
                                            </div>
                                            <div class="col-md-2">
                                                <button type="button" onClick="searchApiPeserta('NIK');" class="btn btn-sm btn-info bg-white-gradient"> <i class="fa fa-search fa-fw"></i></button>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Nama Depan
                                    </td>
                                    <td>
                                        <input type="text" id="FirstName" name="FirstName" class="form-control" required>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Tempat Lahir
                                    </td>
                                    <td>
                                        <input type="text"  name="DOBPlace" class="form-control" required>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Kebangsaan
                                    </td>
                                    <td>
                                        <input type="text" name="Nationality" class="form-control" value="INDONESIA" required>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Agama
                                    </td>
                                    <td>
                                        <select id="Religion" name="Religion" class="form-control" required>
                                            <option value=""></option>
                                            <?php
                                            foreach ($religi as $agama) :
                                            ?>
                                                <option value="<?= $agama['Religion']; ?>"><?= $agama['Religion']; ?>
                                                </option>
                                            <?php
                                            endforeach;
                                            ?>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Pendidikan
                                    </td>
                                    <td>
                                        <select name="Education" class="form-control" required>
                                            <option value=""></option>
                                            <?php
                                            foreach ($edu as $pend) :
                                            ?>
                                                <option value="<?= $pend['ID']; ?>">
                                                    <?= $pend['EducationName']; ?>
                                                </option>
                                            <?php
                                            endforeach;
                                            ?>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Pasien Tipe
                                    </td>
                                    <td>
                                        <select name="PatientType" class="form-control" required>
                                            <option value=""></option>
                                            <?php
                                            foreach ($tipe as $tip) :
                                            ?>
                                                <option value="<?= $tip['TypeID']; ?>">
                                                    <?= $tip['TypeName']; ?>
                                                </option>
                                            <?php
                                            endforeach;
                                            ?>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Bahasa</td>
                                    <td>
                                        <select id="Bahasa" name="Bahasa" class="form-control" required>
                                            <option value="">- PILIH BAHASA -</option>
                                            <?php
                                            foreach ($bahasa as $bhs) :
                                            ?>
                                                <option value="<?= $bhs['Bahasa']; ?>"><?= $bhs['Bahasa']; ?></option>
                                            <?php
                                            endforeach;
                                            ?>
                                        </select>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <table width="100%" class="table">
                                <tr>
                                    <td>
                                        Kartu BPJS / NIK Karyawan <br>
                                        <small style="color: red;">Asuransi, isi NIK</small>
                                    </td>
                                    <td>
                                        <div class="row">
                                            <div class="col-md-10">
                                                <input type="text" class="form-control" id="EmployeeID" name="EmployeeID">
                                            </div>
                                            <div class="col-md-2">
                                                <button type="button" onClick="searchApiPeserta('NOKA');" class="btn btn-sm btn-info bg-white-gradient"> <i class="fa fa-search fa-fw"></i></button>
                                            </div>
                                        </div>
                                        
                                    </td>
                                </tr>
                                <tr>
                                    <td width="25%">
                                        Jenis Kelamin
                                    </td>
                                    <td>
                                        <select name="Sex" id="Sex" class="form-control" required>
                                            <option value="" disabled></option>
                                            <option value="2">Laki-laki</option>
                                            <option value="1">Perempuan</option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Nama Belakang
                                    </td>
                                    <td>
                                        <input type="text" id="LastName" name="LastName" class="form-control" required>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Tanggal Lahir
                                    </td>
                                    <td>
                                        <input type="date" id="DOB" name="DOB" class="form-control" required>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Suku Bangsa
                                    </td>
                                    <td>
                                        <select name="SukuBangsa" class="form-control" required>
                                            <option value=""></option>
                                            <?php
                                            foreach ($suku as $dSuku) :
                                            ?>
                                                <option value="<?= $dSuku['SukuBangsa']; ?>"><?= $dSuku['SukuBangsa']; ?>
                                                </option>
                                            <?php
                                            endforeach;
                                            ?>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Status
                                    </td>
                                    <td>
                                        <select name="Status" class="form-control" required>
                                            <option value=""></option>
                                            <option value="SINGLE">SINGLE</option>
                                            <option value="MARRIED">MARRIED</option>
                                            <option value="WIDOW">WIDOW</option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Pekerjaan
                                    </td>
                                    <td>
                                        <select name="Profession" id="Profession" class="form-control" required>
                                            <option value=""></option>
                                            <?php
                                            foreach ($prof as $pek) :
                                            ?>
                                                <option value="<?= $pek['Deskripsi']; ?>">
                                                    <?= $pek['Deskripsi']; ?>
                                                </option>
                                            <?php
                                            endforeach;
                                            ?>
                                        </select>
                                    </td>
                                </tr>
                                
                            </table>
                    </div>
            </div>
            <div class="box">
            <div class="box-body">
                    <h4 style="font-weight: bold;" class="text-primary">DOMISILI </h4>
                    <table class="table table-striped">
                        <tr>
                            <td width="15%">Provinsi</td>
                            <td>
                                <select name="HomeProvince" id="HomeProvince" class="form-control">
                                    <option value=""></option>
                                    <?php
                                    foreach ($provinsi as $prov) :
                                    ?>
                                        <option value="<?= $prov['PROVINCE']; ?>"><?= $prov['PROVINCE']; ?></option>
                                    <?php
                                    endforeach;
                                    ?>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>Kota/Kabupaten</td>
                            <td>
                                <select name="HomeCity" id="HomeCity" class="form-control">

                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>Kecamatan</td>
                            <td>
                                <select name="Kecamatan" id="Kecamatan" class="form-control">

                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>Kelurahan</td>
                            <td>
                                <select name="Kelurahan" id="Kelurahan" class="form-control">

                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>Alamat Domisili</td>
                            <td>
                                <textarea name="HomeAddress1" class="form-control" required></textarea>
                            </td>
                        </tr>
                        <tr>
                            <td>Alamat KTP</td>
                            <td>
                                <textarea name="HomeAddress2" class="form-control" required></textarea>
                            </td>
                        </tr>
                    </table>
            </div></div>

            <div class="box">
            <div class="box-body">
            <h4 style="font-weight: bold;" class="text-primary">PENJAMIN </h4>
            <table class="table table-striped">
                        <tr>
                            <td width="15%">Asuransi / Penjamin</td>
                            <td>
                                <select name="CompanyCode" class="teanganData " data-live-search="true">
                                    <option value="">- Pilih -</option>
                                    <?php
                                    foreach ($asuransi as $penjamin) :
                                    ?>
                                        <option value="<?= $penjamin['CompanyCode']; ?>"><?= $penjamin['CompanyName']; ?></option>
                                    <?php
                                    endforeach;
                                    ?>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td width="15%">Perusahaan</td>
                            <td>
                                <select id="perusahaan" name="perusahaan" class="teanganData " data-live-search="true">
                                    <option value="">- Pilih -</option>
                                    <?php
                                    foreach ($perusahaan as $per) :
                                    ?>
                                        <option value="<?= $per['NAMA PERUSAHAAN']; ?>"><?= $per['NAMA PERUSAHAAN']; ?></option>
                                    <?php
                                    endforeach;
                                    ?>
                                </select>
                                <select name="kode" id="kode" hidden></select>
                            </td>
                        </tr>
                        <tr>
                            <td>No.HP / WhatsApp Aktif</td>
                            <td>
                                <input type="number" name="CellPhone" id="CellPhone" class="form-control" required>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td align="right">
                                <a href="" class="btn btn-sm btn-danger bg-red-gradient"><i class="fa fa-fw fa-undo"></i> Reset</a>
                                <button type="submit" class="btn btn-sm btn-success bg-green-gradient"> SIMPAN <i class="fa fa-save fa-fw"></i></button>
                            </td>
                        </tr>
                    </table>
                    </form> 
        </div></div>
        </div>

    </section>
</div>

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/jquery-migrate-1.4.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        // PROVINSI
        $('#HomeProvince').change(function() {
            var _provinsi = document.getElementById("HomeProvince").value;
            // alert(_provinsi);
            $.ajax({
                url: "<?= base_url('Pendaftaran/homecity'); ?>",
                method: "POST",
                data: {
                    nprovinsi: _provinsi
                },
                async: true,
                dataType: 'json',
                success: function(data) {

                    var html = '';
                    var i;
                    html += '<option value=""></option>';
                    for (i = 0; i < data.length; i++) {
                        html += '<option value="' + data[i].City + '">' + data[i].City +
                            '</option>';
                    }
                    $('#HomeCity').html(html);
                }
            });
            return false;
        });

        // KABUPATEN - KECAMATAN
        $('#HomeCity').change(function() {
            var _kabupaten = document.getElementById("HomeCity").value;
            // alert(_kabupaten);
            $.ajax({
                url: "<?= base_url('Pendaftaran/kecamatan'); ?>",
                method: "POST",
                data: {
                    nkabupaten: _kabupaten
                },
                async: true,
                dataType: 'json',
                success: function(data) {

                    var html = '';
                    var i;
                    html += '<option value=""></option>';
                    for (i = 0; i < data.length; i++) {
                        html += '<option value="' + data[i].Kecamatan + '">' + data[i].Kecamatan +
                            '</option>';
                    }
                    $('#Kecamatan').html(html);

                }
            });

            return false;
        });

        $('#Kecamatan').change(function() {
            var _kecamatan = document.getElementById("Kecamatan").value;
            var _kabupaten = document.getElementById("HomeCity").value;
            // alert(_kabupaten + ' | ' + _kecamatan);
            $.ajax({
                url: "<?= base_url('Pendaftaran/kelurahan'); ?>",
                method: "POST",
                data: {
                    nkecamatan: _kecamatan,
                    nkabupaten: _kabupaten
                },
                async: true,
                dataType: 'json',
                success: function(data) {

                    var html = '';
                    var i;
                    html += '<option value=""></option>';
                    for (i = 0; i < data.length; i++) {
                        html += '<option value="' + data[i].Kelurahan + '">' + data[i].Kelurahan +
                            '</option>';
                    }
                    $('#Kelurahan').html(html);

                }
            });

            return false;
        });

        $('#Kelurahan').change(function() {
            $("#domisili1").show();
            $("#domisili2").show();
        });

    });

    document.getElementById("Bahasa").value = "INDONESIA";
    document.getElementById("Religion").value = "ISLAM";

    function searchApiPeserta($type){
        var _nik = document.getElementById("nikPeserta").value;
        var _noka = document.getElementById("EmployeeID").value;
        $.ajax({
                url: "<?= base_url('Pendaftaran/getApiPeserta'); ?>",
                method: "POST",
                data: {
                    nik: _nik,
                    noka: _noka,
                },
                async: true,
                dataType: 'json',
                success: function(data) {

                    if(data == null){
                        alert('Data Tidak Ditemukan'); return false;
                    }

                    document.getElementById("FirstName").value = data.nama;
                    //if(data.nama.split(" ").length > 1){
                       // document.getElementById("LastName").value = data.nama.split(" ")[1];
                   // }   

                    var jk = 2 ;
                    if(data.sex != "L"){
                        jk = 1;
                    }
                    
                    document.getElementById("Sex").value = jk;
                    document.getElementById("CellPhone").value = data.noHP;
                    document.getElementById("EmployeeID").value = data.noKartu;
                    document.getElementById("nikPeserta").value = data.noKTP;
                    
                    document.getElementById("Profession").value = data.jnsPeserta.nama;
                    //alert(data.tglLahir);
                    var tglLahir = new Date(data.tglLahir);
                    //alert(tglLahir);
                    document.getElementById("DOB").value = tglLahir.toISOString().substring(0,10);
                }
            });
    }


</script>