<?php



$id = $id_page;
$komponen_id = $id_page;
$form = new form;
$crud = new crudData;
$classsimakomRKAS = new simakom_rkas;
$komponen_id = $classsimakomRKAS->splitID($id);
$tb_komponen = $classsimakomRKAS->getTabelbyID($id, 'simakom_komponen');
$tb_kegiatan = $classsimakomRKAS->getTabelbyID($id, 'simakom_kegiatan');

$cekID = new cekID();
$cekID->cek($tb_komponen, 'id', $komponen_id);

$clasCrud = new crudData;
$kategori_kode = $clasCrud->getValue($tb_komponen, " where id='$komponen_id'", 'kategori');
$kegiatan_kode = $clasCrud->getValue($tb_komponen, " where id='$komponen_id'", 'kegiatan');
$komponen_kode = $clasCrud->getValue($tb_komponen, " where id='$komponen_id'", 'komponen');

$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, 'id', $komponen_id, '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-journal-text"></span> Tambah Akun</b>
                        </div>
                        <!-- /.card-header -->
                        <div class="card-body my-4 mx-4">
                            <form action="" method="post">
                                <?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>


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

                                    </dl>
                                </div>
                                <div class="div d-inline">
                                    <button type="submit" name="submitAkun" class="btn btn-primary btn-xs mb-2 btn-rounded small px-2"> <i class="bi bi-person-plus"></i> TAMBAHKAN AKUN KE KOMPONEN <?php echo $komponen_kode; ?></button>
                                </div>
                                <?php
                                $class = new simakom_search();
                                $showList = $class->search_input();
                                ?>

                                <table id="searchtable" class="table table-bordered table-striped table-sm mb-4 mt-2">
                                    <thead>
                                        <tr>

                                            <th width="50" align="center" class="text-center"><input type="checkbox" class="small check_all"></th>
                                            <th width="110">Kode Akun</th>
                                            <th width="150">Kode Akun Custome</th>
                                            <th width="">Uraian</th>
                                            <th width="250">Kategori</th>

                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $crud = new crudData();
                                        $showUser = $crud->showTableOrder("simakom_ref_akun where kategori='$kategori_kode'", 'akun asc');
                                        $no = 0;
                                        while ($data = $showUser->fetch(PDO::FETCH_OBJ)) {
                                            $link = new linkpage;
                                            if ($data->kategori == "A") {
                                                $kategori = $data->kategori . " - Pendapatan";
                                            } elseif ($data->kategori == "B") {
                                                $kategori = $data->kategori . " - Belanja";
                                            } else {
                                                $kategori = "";
                                            };
                                            $no++;
                                            echo "
                                                    <tr>
                                                        <td width='5px'  align='center'>
                                                            <input type='checkbox' class='d-inline small chk_boxes1' name='akun[]' value='$data->akun'> 
                                                        </td>
                                                      
                                                        <td>$data->akun</td>
                                                        <td>$data->akun_custome</td>
                                                        <td>$data->uraian</td>
                                                        <td>$kategori</td>
                                                        
                                                        </tr>";
                                        };
                                        ?>
                                    </tbody>
                                </table>

                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- /.content-wrapper -->
<script type="text/javascript">
    $(function() {
        $('.check_all').click(function() {
            $('.chk_boxes1').prop('checked', this.checked);
        });
    });
</script>

<?php

if (isset($_POST['submitAkun']) && isset($_POST['akun'])) {

    $kategori = htmlspecialchars($_POST['kategori']);
    $kegiatan = htmlspecialchars($_POST['kegiatan']);
    $komponen = htmlspecialchars($_POST['komponen']);
    $simakom_rkas = new simakom_rkas();
    $time = new time;
    $crud = new crudData();

    foreach ($_POST['akun'] as $a => $akuns) {

        $akun =  $_POST['akun'][$a];

        $kode_id = $kategori . $kegiatan . $komponen;
        $akun_id = $kategori . $kegiatan . $akun . $komponen;

        $id = substr(md5($akun), 6, 5) . substr(md5($kategori), 0, 5) . substr(md5($kegiatan), 3, 5) . substr(md5($komponen), 5,) . substr($time->getTime(), 2, 5);
        $uraian = $simakom_rkas->uraianRKAS('simakom_ref_akun', 'akun', $akun, 'uraian');
        $revisi = $simakom_rkas->maxRevisi($tb_revisi);


        $countData = $crud->countData($tb_akun, "where kategori='$kategori' && kegiatan='$kegiatan' && komponen='$komponen' && akun='$akun'");
        if ($countData > 0)
            continue;


        $array = [
            'kategori' => $kategori,
            'kegiatan' => $kegiatan,
            'komponen' => $komponen,
            'akun' => $akun,
            'uraian' => $uraian,
            'id' => $id,
            'kode_id' => $kode_id,
            'akun_id' => $akun_id,
            'revisi' => $revisi
        ];
        $save = $crud->insertData($tb_akun, $array);
    };


    $direct = new toast();
    echo  $direct->redirect('toast', 'save', 'rkas/index');
} else {
    echo "";
};
