<?php
include "./layout/class.php";

$id_page = $_GET['i4'];

$cekID = new cekID();
$cekID->cek($tb_detail, 'id', $id_page);

$class_crud = new crudData();
$edit = $class_crud->editData($tb_detail, 'id', $id_page);
$view = $edit->fetch(PDO::FETCH_OBJ);
$kategori_kode = $view->kategori;
$kegiatan_kode = $view->kegiatan;
$komponen_kode = $view->komponen;
$akun_kode = $view->akun;
$akun_pembiayaan = $view->pembiayaan_akun;
$pembiayaan_id = $view->pembiayaan_id;
$id = $view->id;

//$viewAKun = $class_crud->editData($tb_detail, 'detail_id', $pembiayaan_id);
//$viewAKun = $viewAKun->fetch(PDO::FETCH_OBJ);

$id_akun = $class_crud->getValue($tb_detail, "where detail_id='$pembiayaan_id'", 'id');
$kegiatan = $class_crud->getValue($tb_detail, "where detail_id='$pembiayaan_id'", 'kegiatan');
$komponen = $class_crud->getValue($tb_detail, "where detail_id='$pembiayaan_id'", 'komponen');
$akun = $class_crud->getValue($tb_detail, "where detail_id='$pembiayaan_id'", 'akun');
$detail = $class_crud->getValue($tb_detail, "where detail_id='$pembiayaan_id'", 'detail');

$class = new simakom_rkas();
$uraian_kategori = $class->uraianRKAS('simakom_ref_kategori', 'kategori', $kategori_kode, 'uraian');
$uraian_kegiatan = $class->uraianRKAS($tb_kegiatan, 'kegiatan', $kegiatan_kode, 'uraian');
$uraian_komponen = $class->uraianRKAS($tb_komponen, 'kode_id', $kategori_kode . $kegiatan_kode . $komponen_kode, 'uraian');
$uraian_akun = $class->uraianRKAS($tb_akun, 'akun', $akun_kode, 'uraian');

if ($akun_pembiayaan == "") {
    $uraian_pembiayaan = "";
} else {
    $uraian_pembiayaan = $class->uraianRKAS($tb_detail, 'id', $id_akun, 'uraian');
};
?>
<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-3">
                        <div class="timeline-header bg-primary py-2 pl-2">
                            <b class="text-light judul"><span class="bi bi-pensil"></span> Edit Detail Akun </b>
                        </div>
                        <form action='' method='post' role='form'>
                            <?php $csrf->token();
                            ?>
                            <input type="text" name="kategori" value="<?php echo $kategori_kode;
                                                                        ?>" hidden>
                            <input type="text" name="kegiatan" value="<?php echo $kegiatan_kode;
                                                                        ?>" hidden>
                            <input type="text" name="komponen" value="<?php echo $komponen_kode;
                                                                        ?>" hidden>
                            <input type="text" name="akun" value="<?php echo $akun_kode;
                                                                    ?>" hidden>
                            <div class="card-body">
                                <div class="col-md-12 ml-2 pl-3">
                                    <input type="text" name="kegiatan" value="<?php echo $kegiatan_kode;
                                                                                ?>" hidden>
                                    <div class="info-box-content">
                                        <dl class="row">
                                            <dt class="col-sm-3 pt-0">KATEGORI</dt>
                                            <dd class="col-sm-9  pt-0"><b>: <?php echo $kategori_kode . ' - ' . $uraian_kategori; ?></b></dd>
                                            <dt class="col-sm-3">KEGIATAN</dt>
                                            <dd class="col-sm-9"><b>: <?php echo $kegiatan_kode . ' - ' . $uraian_kegiatan; ?></b></dd>
                                            <dt class="col-sm-3">KOMPONEN</dt>
                                            <dd class="col-sm-9"><b>: <?php echo $komponen_kode . ' - ' . $uraian_komponen; ?></b></dd>
                                            <dt class="col-sm-3">AKUN</dt>
                                            <dd class="col-sm-9"><b>: <?php echo $akun_kode . ' - ' . $uraian_akun; ?></b></dd>

                                        </dl>
                                    </div>
                                </div>
                                <div class="row col-12">
                                    <div class="card-body col-md-4">
                                        <div class="row pt-3">
                                            <label class="col-sm-12 col-form-label">Kode Detail</label>
                                            <div class="row col-md-12">
                                                <div class="col-md-4">
                                                    <input type="text" name="" class="form-control form-control-sm fontInput" value="<?php echo $kategori_kode . '.' . $kegiatan_kode . '.' . $komponen_kode . '.' . $akun_kode . '.'; ?>" disabled>
                                                </div>
                                                <div class="col-md-8">
                                                    <input type="text" name="detail" class="form-control form-control-sm fontInput" value="<?php echo $view->detail; ?>" placeholder="Kode Detail" required>
                                                </div>
                                            </div>
                                            <p class="footnote pl-2 mt-2 small"> <i class="footnote">Kode Detail digunakan untuk Kodifikasi Pembeda antara satu Detail dengan Detail lainnya dalam satu akun. Dapat berupa urut angka 1 - 999.</i></p>

                                        </div>
                                        <div class="row pt-0">
                                            <label class="col-sm-12 col-form-label">Uraian Detail </label>
                                            <div class="col-md-12">
                                                <input type="text" name="uraian" class="form-control form-control-sm fontInput" value="<?php echo $view->uraian;
                                                                                                                                        ?>" placeholder="Uraian Detail" required>
                                            </div>
                                        </div>
                                        <div class="row mt-2">
                                            <label class="col-sm-12 col-form-label">Sumber Pembiayaan</label>
                                            <div class="col-md-12">
                                                <select name="pembiayaan" class="select2 form-control form-control-sm fontInput small" <?php if ($kategori_kode == "A") {
                                                                                                                                            echo "disabled";
                                                                                                                                        } else {
                                                                                                                                            echo "required";
                                                                                                                                        }; ?>>
                                                    <option value="<?php echo $id_akun; ?>"><?php if ($kategori_kode == "A") {
                                                                                                echo "-- Khusus untuk Akun Belanja--";
                                                                                            } else {
                                                                                                echo 'A.' . $kegiatan . '.' . $komponen . '.' . $akun . '-' . $detail . ' : ' . $uraian_pembiayaan;
                                                                                            }; ?></option>
                                                    <?php
                                                    $opt = new form();
                                                    $opval = $opt->optSelect($tb_detail, "where kategori='A' order by akun asc");
                                                    while ($data_opt = $opval->fetch(PDO::FETCH_OBJ)) {
                                                        echo "<option value='$data_opt->id'>$data_opt->kategori.$data_opt->kegiatan.$data_opt->komponen.$data_opt->akun-$data_opt->detail.$data_opt->kode : $data_opt->uraian</option>";
                                                    };
                                                    ?>
                                                </select>
                                            </div>
                                            <p class="footnote pl-2 mt-2 small"> <i class="footnote">Sumber Pembiayaan adalah Akun Pendapatan yang akan digunakan untuk membiayai Kegiatan ini. <b>Khusus untuk Rencana Belanja</b></i></p>

                                        </div>


                                    </div>
                                    <div class="card-body col-md-8">
                                        <div class="row mt-3">
                                            <label class="col-sm-12 col-form-label"><i class="text-primary">Perhitungan [Optional]</i></label>

                                            <div class="col-md-1">
                                                <input type="text" name="komponen_1" id="komp_1" onkeyup="sum1();" <?php echo $format->onlyNumber(); ?> class="form-control form-control-sm fontInput bg-secondary text-default" style="text-align:center" value="<?php echo $view->komponen_1;
                                                                                                                                                                                                                                                                    ?>" placeholder="Vol">
                                            </div>
                                            <div class="col-md-2">
                                                <input type="text" name="unit_1" class="form-control form-control-sm fontInput  bg-secondary text-default" value="<?php echo $view->unit_1;
                                                                                                                                                                    ?>" placeholder="Unit">
                                            </div>
                                            <div class="col-md-1">
                                                <input type="text" name="komponen_2" id="komp_2" onkeyup="sum2();" <?php echo $format->onlyNumber(); ?> class="form-control form-control-sm fontInput  bg-secondary text-default" style="text-align:center" value="<?php echo $view->komponen_2;
                                                                                                                                                                                                                                                                    ?>" placeholder="Vol">
                                            </div>
                                            <div class="col-md-2">
                                                <input type="text" name="unit_2" class="form-control form-control-sm fontInput  bg-secondary text-default" value="<?php echo $view->unit_2;
                                                                                                                                                                    ?>" placeholder="Unit">
                                            </div>
                                            <div class="col-md-1">
                                                <input type="text" name="komponen_3" id="komp_3" onkeyup="sum3();" <?php echo $format->onlyNumber(); ?> class="form-control form-control-sm fontInput  bg-secondary text-default" style="text-align:center" value="<?php echo $view->komponen_3;
                                                                                                                                                                                                                                                                    ?>" placeholder="Vol">
                                            </div>
                                            <div class="col-md-2">
                                                <input type="text" name="unit_3" class="form-control form-control-sm fontInput  bg-secondary text-default" value="<?php echo $view->unit_3;
                                                                                                                                                                    ?>" placeholder="Unit">
                                            </div>
                                            <div class="col-md-1">
                                                <input type="text" name="komponen_4" id="komp_4" onkeyup="sum4();" <?php echo $format->onlyNumber(); ?> class="form-control form-control-sm fontInput  bg-secondary text-default" style="text-align:center" value="<?php echo $view->komponen_4;
                                                                                                                                                                                                                                                                    ?>" placeholder="Vol">
                                            </div>
                                            <div class="col-md-2">
                                                <input type="text" name="unit_4" class="form-control form-control-sm fontInput  bg-secondary text-default" value="<?php echo $view->unit_4;
                                                                                                                                                                    ?>" placeholder="Unit">
                                            </div>
                                            <p class="footnote pl-2 mt-2 small"> <i class="footnote">Kolom Perhitungan hanya optional. Bisa langsung diisi di kolom Jumlah Anggaran di bawah. Gunakan tombol <b>TAB</b> untuk kalkulasi otomatis</i></p>

                                        </div>
                                        <div class="row ">
                                            <label class="col-sm-12 col-form-label">Jumlah Anggaran</label>
                                            <div class="col-md-2">
                                                <input type="text" name="volume" value="<?php echo $view->volume;
                                                                                        ?>" id="total_volume" onkeyup="nilai_total();" <?php echo $format->onlyNumber(); ?> class="form-control form-control-sm fontInput" style="text-align:center" placeholder="Total Vol" required>
                                            </div>
                                            <div class="col-md-2">
                                                <input type="text" name="unit" value="<?php echo $view->unit;
                                                                                        ?>" class="form-control form-control-sm fontInput" value="" placeholder="Satuan" required>
                                            </div>
                                            <div class="col-md-4">
                                                <input type="text" name="harga" value="<?php echo $view->satuan;
                                                                                        ?>" onkeyup="nilai_total();" <?php echo $format->onlyNumber(); ?> id="nilai_satuan" class="form-control form-control-sm fontInput" style="text-align:right" placeholder="Harga Satuan" required>
                                            </div>
                                            <div class="col-md-4">
                                                <input type="text" name="total" value="<?php echo $view->total;
                                                                                        ?>" onkeyup="nilai_total();" <?php echo $format->onlyNumber(); ?> id="total" class="form-control form-control-sm  fontInput" style="text-align:right" placeholder="Total" required>
                                            </div>
                                        </div>
                                        <div class="row mt-5 float-right">
                                            <div class="col-auto ml-4">
                                                <div class='float-right'>
                                                    <a <?php echo $link->page('rkas/index'); ?> class="btn btn-primary btn-xs float-right mx-2"><i class="bi-arrow-left-circle"></i> BATAL</a>
                                                    <button type='submit' class='btn btn-primary btn-xs' name='submitDetail'><i class='bi-save'></i> SIMPAN</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>


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

<?php

$class = new simakom_rkas();

if (isset($_POST['submitDetail'])) {
    $csrf = $csrf->cekToken();
    $class = new simakom_rkas();

    $save = $class->updateRKASDetail($tb_akun, $tb_detail, $tb_revisi, $id_page);
} else {
    echo "";
};
