<?php

$cekid = new cekID();
$cekid->Cek($tb_siswa, 'id', $id_page);

$random = new random;
$time = new time;

$tanggal = $time->getDateInput();
$time = $time->getTime();
$crud = new crudData();
$editUser = $crud->editData($tb_siswa, 'id', $id_page);
$data = $editUser->fetch(PDO::FETCH_OBJ);
$nis = $data->nis;

if ($data->startAt == "") {
    $startAt = 1;
} else {
    $startAt = $data->startAt;
};

$id_tr = strtoupper(substr(md5($random->create(25)), 1, 2) . substr(md5($random->create(25)), 3, 4) . substr(md5($nis), 4, 4) . substr(md5($time), 6, 4));

//Hapus Data Draf Payment Gateway
$payment_id = $crud->getValue($payment_list, " where nis='$nis' && status='DRAF'", 'payment_id');
$delete = $crud->deleteDatabyKondisi('payment_list_detail', " where payment_id='$payment_id'");

$delete = $crud->deleteDatabyKondisi($payment_list, " where nis='$nis' && status='DRAF'");

?>
<style>
    .fontInput {
        font-size: 11px;
        font-family: arial;
    }

    .fontJudul {
        font-size: 12px;
        font-family: arial;
        color: dodgerblue;
        background: #000;

    }
</style>
<script>
    function formatNumber(num) {
        return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1.')
    }
</script>

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">

    <?php require('./layout/headerpage.php'); ?>

    <div class="container col-12 mb-4 pb-5">
        <div class="row">
            <div class="col-12">
                <div class="col-12">
                    <div class="card  mx-0">
                        <div class="timeline-header bg-primary py-2 pl-2">
                            <b class="text-light judul"><span class="bi bi-pencil"></span> Tambah Transaksi</b>
                        </div>
                        <div class="card-body mb-1">
                            <form action="" method="post">
                                <input type="text" name="id" value="<?php echo $data->id; ?>" hidden>
                                <input type="text" name="tahun" value="<?php echo $tahun; ?>" hidden>
                                <input type="text" name="nis" value="<?php echo $data->nis; ?>" hidden>
                                <input type="text" name="nama" value="<?php echo $data->nama; ?>" hidden>
                                <input type="text" name="kelamin" value="<?php echo $data->kelamin; ?>" hidden>
                                <input type="text" name="jenjang" value="<?php echo $data->jenjang; ?>" hidden>
                                <input type="text" name="paralel" value="<?php echo $data->paralel; ?>" hidden>
                                <input type="text" name="kategori" value="<?php echo $data->kategori; ?>" hidden>
                                <input type="text" name="id_tr" value="<?php echo $id_tr; ?>" hidden>
                                <input type="text" name="startAt" value="<?php echo $startAt; ?>" hidden>
                                <div class="row mx-3 mt-0">
                                    <div class="col-md-6">
                                        <div class="card-body mt-0 px-0 mx-0">
                                            <div class="row">
                                                <label for="exampleEmail" class="col-sm-4 col-form-label text-primary">Nomor Induk </label>
                                                <div class="col-sm-8">
                                                    <input type="text" name="" value="<?php echo $data->nis; ?>" class="form-control form-control-sm fontInput text-secondary" style='text-align:left;font-size:14px;font-weight:bold' placeholder="NIS Siswa" disabled>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <label for="exampleEmail" class="col-sm-4 col-form-label text-primary">Nama Siswa </label>
                                                <div class="col-sm-8">
                                                    <input type="text" name="" value="<?php echo $data->nama; ?>" class="form-control form-control-sm fontInput" placeholder="Nama Siswa" disabled>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <label class="col-sm-4 col-form-label text-primary">Jenis Kelamin</label>
                                                <div class="col-md-8">
                                                    <select name="" class="form-control form-control-sm fontInput small" disabled>
                                                        <option value="<?php echo $data->kelamin; ?>">
                                                            <?php if ($data->kelamin == 'L') {
                                                                echo "Laki-laki";
                                                            } else {
                                                                echo "Perempuan";
                                                            }; ?></option>
                                                        <option value="L">Laki-laki</option>
                                                        <option value="P">Perempuan</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <label class="col-sm-4 col-form-label text-primary">Kelas</label>
                                                <div class="col-md-2">
                                                    <select name="" class="form-control form-control-sm fontInput small" disabled>
                                                        <option><?php echo $data->jenjang; ?></option>
                                                        <?php

                                                        $sekolah = $crud->editData('sekolah', 'no', 1);
                                                        $dataSekolah = $sekolah->fetch(PDO::FETCH_OBJ);
                                                        $jenjang = strtolower($dataSekolah->jenjang);

                                                        $opt = new form();
                                                        $opval = $opt->optSelect('tb_tingkat', "where {$jenjang}<>'' order by no asc");
                                                        while ($dataopt = $opval->fetch(PDO::FETCH_OBJ)) {
                                                            $optJenjang = $dataopt->{$jenjang};
                                                            echo "<option>" . $optJenjang . " </option>";
                                                        };

                                                        ?>
                                                    </select>
                                                </div>

                                                <div class="col-md-6">
                                                    <select name="" class="form-control form-control-sm fontInput small" disabled>
                                                        <option><?php echo $data->paralel; ?></option>
                                                        <?php
                                                        $opval = $opt->optSelect('tb_paralel', ' order by no asc');
                                                        while ($datapar = $opval->fetch(PDO::FETCH_OBJ)) {
                                                            echo "<option>$datapar->paralel</option>";
                                                        };

                                                        ?>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="card-body mt-0 px-0 mx-0">
                                            <div class="row">
                                                <label for="nis" class="col-sm-4 col-form-label text-primary">No. Telpon/WA</label>
                                                <div class="col-sm-8">
                                                    <input type="text" class="form-control form-control-sm fontInput" value="<?php echo $data->hp; ?>" placeholder="No Telpon User" name="hp" disabled>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <label for="nis" class="col-sm-4 col-form-label text-primary">Email</label>
                                                <div class="col-sm-8">
                                                    <input type="email" class="form-control form-control-sm fontInput" value="<?php echo $data->email; ?>" placeholder="email@mail.com" name="email" disabled>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <label for="nis" class="col-sm-4 col-form-label text-primary">Orang Tua/Wali</label>
                                                <div class="col-sm-8">
                                                    <input type="text" class="form-control form-control-sm fontInput" value="<?php echo $data->ortu; ?>" placeholder="Nama Orang Tua/Wali" name="ortu" disabled>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <label for="nis" class="col-sm-4 col-form-label text-primary">Alamat</label>
                                                <div class="col-sm-8">
                                                    <input type="text" class="form-control form-control-sm fontInput" value="<?php echo $data->alamat; ?>" placeholder="Alamat" name="alamat" disabled>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="row mx-3 mt-0">

                                    <div class="col-md-12">

                                        <div class="card-body mt-0 px-0 mx-0">
                                            <label class="text-secondary">ID-TR : <?php echo $id_tr; ?></label>
                                            <?php
                                            echo "
                                                <div class='row'>
                                                    <input type='text' class='form-control form-control-sm fontJudul col-lg-4 col-sm-12 mx-0' style='text-align:left;font-size:14px;font-weight:bold'  value='Jenis / Nama Pembayaran' disabled >
                                                    <input type='text' class='form-control form-control-sm fontJudul col-lg-1 col-sm-12 mx-0' style='text-align:left;font-size:14px;font-weight:bold'  value='Bulanan' disabled >
                                                    <input type='text' class='form-control form-control-sm fontJudul col-lg-1 col-sm-12 mx-0' style='text-align:right;font-size:14px;font-weight:bold'   value='Tanggungan' disabled>
                                                    <input type='text' class='form-control form-control-sm fontJudul col-lg-1 col-sm-12 mx-0' style='text-align:right;font-size:14px;font-weight:bold'   value='Terbayar' disabled>
                                                    <input type='text' class='form-control form-control-sm fontJudul col-lg-1 col-sm-12 mx-0' style='text-align:right;font-size:14px;font-weight:bold'   value='Kurang' disabled>
                                                    <input type='text' class='form-control form-control-sm fontJudul col-lg-1 col-sm-12 mx-0' style='text-align:right;font-size:14px;font-weight:bold'   value='Qty' disabled >
                                                    <input type='text' class='form-control form-control-sm fontJudul col-lg-1 col-sm-12 mx-0' style='text-align:right;font-size:14px;font-weight:bold'   value='Bayar' disabled >
                                                    <input type='text' class='form-control form-control-sm fontJudul col-lg-1 col-sm-12 mx-0' style='text-align:right;font-size:14px;font-weight:bold'  value='Total' disabled >
                                                    <input type='text' class='form-control form-control-sm fontJudul col-lg-1 col-sm-12 mx-0' style='text-align:left;font-size:14px;font-weight:bold'   value='Catatan' disabled >
                                                </div>
                                                ";

                                            $dt_jenis = new namaBayar();
                                            $showJenis = $dt_jenis->NamaTransaksi('tb_nama_bayar', 'tp', $tahun, $data->jenjang, 'kode');
                                            $count = $showJenis->rowCount();
                                            while ($datajenis = $showJenis->fetch(PDO::FETCH_OBJ)) {
                                                $field_tang = 'tang_' . $datajenis->kode;

                                                if ($data->{$field_tang} == "" || !is_numeric($data->{$field_tang})) {
                                                    $val_tang = 0;
                                                } else {
                                                    $val_tang = $data->{$field_tang};
                                                }; //Cek jika jumlah tanggungan kosong / bukan angka

                                                $setbulanan = $datajenis->bulanan; //Cek Apakah Bulanan

                                                $rupiah_tang = @(number_format($val_tang, 0, ".", "."));
                                                $jenis = $datajenis->kode;

                                                //Terbayar & Kurang
                                                $classTrans = new transaksi();
                                                $infoTerbayar = $classTrans->infoterbayar($tb_transaksi, $nis, $jenis);
                                                $terbayar = @(number_format($infoTerbayar, 0, ".", "."));
                                                $kurang = @(number_format(($val_tang - $infoTerbayar), 0, ".", "."));

                                                if ($setbulanan == "Ya" || $setbulanan == "ya") {
                                                    $bulanan = @(number_format(($val_tang / (12 - ($startAt - 1))), 0, ".", "."));
                                                    $disabledQty = "";
                                                } else {
                                                    $bulanan = "--";
                                                    $disabledQty = "disabled";
                                                };
                                                //$format = new format;
                                                $onlyNumber = $format->onlyNumber();

                                                echo "
                                                    <div class='row'>
                                                            <input type='text' class='col-lg-5 col-sm-12 form-control  form-control-sm fontInput' style='text-align:left' name='jenis[]' value='$jenis' hidden >
                                                            <input type='text' class='col-lg-4 col-sm-12 form-control  form-control-sm fontInput' style='text-align:left' name='' value='$datajenis->kode : $datajenis->nama' disabled >
                                                            <input type='text' class='col-lg-1 col-sm-12 form-control  form-control-sm fontInput text-primary' style='text-align:right' name='' value='$bulanan' disabled >
                                                            <input type='text' class='form-control form-control-sm fontInput col-lg-1 col-sm-12 mx-0' style='text-align:right' name='' value='$rupiah_tang' disabled >
                                                            <input type='text' class='form-control form-control-sm fontInput col-lg-1 col-sm-12 mx-0' style='text-align:right' name='' value='$terbayar' disabled>
                                                            <input type='text' class='form-control form-control-sm fontInput col-lg-1 col-sm-12 mx-0' style='text-align:right' name='' value='$kurang' disabled>
                                                            <input type='text' class='form-control form-control-sm fontInput col-lg-1 col-sm-12 mx-0' style='text-align:right' id='qty$jenis' name='quantity[]' value='1' min='1' max='12' $onlyNumber>
                                                            <input type='text' class='form-control-sm fontInput col-lg-1 col-sm-12 mx-0' style='text-align:right' id='jumlah$jenis' name='jumlah[]' value='' $onlyNumber  autofocus >
                                                            <input type='text' class='form-control form-control-sm fontInput col-lg-1 col-sm-12 mx-0 text-secondary' style='text-align:right;font-weight:bold' name='subtotal' id='sTotal$jenis' disabled >
                                                            <input type='text' class='form-control-sm fontInput col-lg-1 col-sm-12 mx-0' style='text-align:left'  name='catatan[]'>
                                                    </div>
                                                  ";

                                                echo "
                                                <script>
                                                    $('input').keyup(function() {
                                                        var getQty = Number($('#qty$jenis').val());
                                                        var getJumlah = Number($('#jumlah$jenis').val());
                                                        var subTotal = Math.floor((getQty * getJumlah));
                                                        $('#subTotalx$jenis').val(subTotal);
                                                        var subtt$jenis=formatNumber($('#subTotalx$jenis').val());
                                                        $('#sTotal$jenis').val(subtt$jenis);
                                                        var subtl$jenis= Math.floor((getQty * getJumlah));";

                                                echo "var totale=";
                                                $jns[] = $datajenis->kode;
                                                foreach ($jns as $j => $jenises) {
                                                    $j_jenis = $jenises;
                                                    $kd = "+Number($('#subTotalx" . $j_jenis++ . "').val())";
                                                    echo $kd;
                                                };
                                                echo ";";

                                                echo " $('#Total').val(totale);
                                                         var tot=formatNumber($('#Total').val());
                                                        $('#TotalAkh').val(tot);
                                                    })
                                                </script>";

                                                echo "<input type='text' class='col-sm-4 form-control  form-control-sm fontJudul' style='text-align:left;font-size:14px;font-weight:bold' name='subtHiden' id='subTotalx$jenis'  hidden >
                                                ";
                                            }

                                            //Jumlah
                                            $clsTanggungan = new listTanggungan();
                                            $jumlah = $clsTanggungan->tanggunganSiswa($tb_siswa, 'id', $id_page);
                                            $total_tang = number_format($jumlah, 0, ".", ".");

                                            //Terbayar & Kurang Total
                                            $classTrans = new transaksi();
                                            $infoTerbayartotal = $classTrans->infoterbayartotal($tb_transaksi, $nis);
                                            $terbayarTotal = @(number_format($infoTerbayartotal, 0, ".", "."));
                                            $kurangTotal = @(number_format(($jumlah - $infoTerbayartotal), 0, ".", "."));

                                            echo "
                                                    <div class='row '>
                                                        <input type='text' class='col-lg-5 col-sm-12 form-control  form-control-sm fontJudul' style='text-align:left;font-size:14px;font-weight:bold' name='' value='JUMLAH' disabled >
                                                        <input type='text' class='form-control form-control-sm fontInput text-primary col-lg-1 col-sm-12 mx-0' style='text-align:right;font-size:14px;font-weight:bold' value='$total_tang' disabled>
                                                        <input type='text' class='form-control form-control-sm fontInput text-primary col-lg-1 col-sm-12 mx-0' style='text-align:right;font-size:14px;font-weight:bold' value='$terbayarTotal' disabled>
                                                        <input type='text' class='form-control form-control-sm fontInput text-primary col-lg-1 col-sm-12 mx-0' style='text-align:right;font-size:14px;font-weight:bold' value='$kurangTotal' disabled>
                                                        <input type='text' class='form-control form-control-sm fontInput text-primary col-lg-2 col-sm-12 mx-0' style='text-align:right;font-size:14px;font-weight:bold' value='' disabled>
                                                        <input type='text' class='form-control form-control-sm fontInput text-danger col-lg-1 col-sm-12 mx-0' style='text-align:right;font-size:14px;font-weight:bold' id='TotalAkh' name='' disabled>
                                                        <input type='text' class='form-control form-control-sm fontInput text-danger col-lg-1 col-sm-12 mx-0' style='text-align:right;font-size:14px;font-weight:bold'  disabled>
                                                    </div>
                                                ";

                                            echo " <input type='text' class='col-sm-4 form-control  form-control-sm fontJudul' style='text-align:left;font-size:14px;font-weight:bold' id='Total' hidden >";

                                            ?>

                                        </div>

                                        <div class='row '>
                                            <div class="col-12 my-0 pl-0 py-0 mx-0">
                                                <div class="row">
                                                    <label for="tanggal" class="col-sm-1 col-form-label text-primary">Tanggal</label>
                                                    <div class="col-sm-2">
                                                        <input type="text" class="form-control form-control-sm col-sm-8 fontInput" style="text-align:center" value="<?php echo $tanggal; ?>" placeholder="dd-mm-yyyy" name="tanggal" id="tanggal">
                                                    </div>
                                                </div>
                                                <div class="row float-right">
                                                    <strong class="text-primary col-lg-4 col-sm-12">Metode Pembayaran : </strong>
                                                    <label class="radio-inline">
                                                        <input type="radio" name="metode" class="fontInput" id="optionsRadiosInline1" value="Tunai" checked> <i class="text-primary">Langsung [Tunai]</i> </label>
                                                    <label class="radio-inline">&nbsp;
                                                        <input type="radio" name="metode" class="fontInput" id="optionsRadiosInline2" value="Bank"> <i class="text-primary">Rekening [Transfer Bank]</i></label>

                                                    <div class="col-auto">
                                                        <button type="submit" class="btn btn-primary btn-xs px-2" name="save_transaksi"><i class="bi-save"></i> Simpan Transaksi</button>
                                                        <a <?php echo $link->page('transaksi/siswa_read'); ?> class="btn btn-primary btn-xs float-right mx-1 px-2"><i class="bi-arrow-left-circle"></i> Cancel</a>
                                                    </div>

                                                </div>

                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>

                    <div class="card  mx-0">
                        <div class="timeline-header bg-primary py-2 pl-2">
                            <b class="judul"><span class="bi bi-pencil"></span> Rincian Transaksi Pembayaran</b>
                        </div>
                        <div class="card-body mb-1">

                            <div class="row mx-3 mt-0">

                                <div class="col-md-12">


                                    <table id="example51" class="table table-bordered table-striped table-sm mb-4 text-secondary">
                                        <thead>
                                            <tr>
                                                <th>No</th>
                                                <th>Tanggal</th>
                                                <th>Kode : Jenis Pembayaran</th>
                                                <th>Periode</th>
                                                <th>Jumlah</th>
                                                <th>Metode</th>
                                                <th>Penerima</th>
                                                <th>Keterangan</th>

                                            </tr>
                                        </thead>
                                        <tbody>

                                            <?php
                                            $crud = new crudData;
                                            $class = new transaksi();

                                            $showList = $crud->showTable($tb_transaksi . " where nis='$nis' order by tanggal desc, jenis asc");
                                            $no = 0;
                                            while ($dataTr = $showList->fetch(PDO::FETCH_OBJ)) {
                                                if ($dataTr->jumlah == "" || $dataTr->jumlah == 0) {
                                                    $jumlah = number_format(0, 0, ",", ".");
                                                } else {
                                                    $jumlah = number_format($dataTr->jumlah, 0, ",", ".");
                                                };

                                                $tahap = $dataTr->tahap;

                                                //startAt
                                                $startAt = $class->startAt($tb_siswa, $nis);
                                                $cekbulan = $class->cekPeriode($startAt, $tahap, $tahun1, $tahun2);

                                                //nama Bayar
                                                $classNB = new namaBayar();
                                                $ceknamaBayar = $classNB->ceknamaBayar($tahun, $dataTr->jenis, $dataTr->jenjang);

                                                $showJenis = $classNB->shownamaBayar($tahun, $dataTr->jenis, $dataTr->jenjang);
                                                $datajenis = $showJenis->fetch(PDO::FETCH_OBJ);
                                                $field_tang = 'tang_' . $datajenis->kode;
                                                $setbulanan = $datajenis->bulanan; //Cek Apakah Bulanan

                                                if ($setbulanan == "Tidak") {
                                                    $periode = "Tahap " . $tahap;
                                                } else {
                                                    $periode = $cekbulan;
                                                };
                                                $no++;

                                                //$penerima = $crud->getValue('user', "where id='$dataTr->penerima'", 'nama');
                                                $penerima = $class->namaPenerimaByID($dataTr->penerima);
                                                echo "
                                                    <tr>
                                                        <td width='5px'>$no</td>
                                                        <td align=left>" . $format->dateToIndo($dataTr->tanggal) . "</td>
                                                       <td align=left>$dataTr->jenis : $ceknamaBayar</td>
                                                        <td align=left>$periode</td>
                                                        <td align=left>" . $jumlah . "</td>
                                                        <td align=left>$dataTr->metode</td>
                                                        <td align=left>$penerima</td>
                                                        <td align=left>$dataTr->catatan</td>
                                                       
                                                    </tr>";
                                            }

                                            ?>
                                        </tbody>
                                    </table>




                                </div>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php
$class = new transaksi();
$save = $class->save($tahun);
