<?php

require('../../asset/mod/autoLoad.php');
$id_billing = $_POST['id_billing'];
$id_siswa = $_POST['id_siswa'];
$status = $_POST['status'];
$tahun = $_POST['tahun'];
$tahun_tabel = $_POST['tahun_tabel'];

$payment_list = "payment_list" . $tahun_tabel;
$tb_transaksi = "db_transaksi" . $tahun_tabel;
$crud = new crudData();
$cekpost = new cekissetPost;
$keterangan = $cekpost->cek('keterangan');

if (isset($_POST['submitKonfirmasi']) && isset($_POST['id_billing'])) {
    $db = database::getInstance();

    //wa



    $time = new time();
    $waktu = $time->getTime();
    $browser = $_SERVER['HTTP_USER_AGENT'];


    $dataarray = [
        'status' => $status,
        'keterangan' => $keterangan
    ];
    $update = $crud->updateData($payment_list, $dataarray, ['payment_id', ' = ', $id_billing]);

    $countID = $crud->countData($payment_list, "where payment_id='$id_billing'");

    if ($status == "PAID" && $countID == 1) {


        $show = $crud->showTable("payment_list_detail where payment_id='$id_billing'");
        while ($data = $show->fetch(PDO::FETCH_OBJ)) {
            $random = new random;
            $randomx = strtoupper(substr(md5($waktu), 2, 5) . $random->create(5) . substr(md5($random->create(20)), 2, 2));
            $id = strtoupper(substr(md5($randomx), 4, 8) . $random->create(7) . substr(md5($random->create(20)), 5, 5));

            $tanggal = $data->tanggal;
            $nis = $data->nis;
            $nama = $data->nama;
            $kelamin = $data->kelamin;
            $jenjang = $data->jenjang;
            $paralel = $data->paralel;
            $jenis = $data->jenis;
            $tahap = $data->tahap;
            $jumlah = $data->jumlah;

            $dataarray = [
                'tanggal' => $tanggal,
                'nis' => $nis,
                'nama' => $nama,
                'kelamin' => $kelamin,
                'jenjang' => $jenjang,
                'paralel' => $paralel,
                'jenis' => $jenis,
                'tahap' => $tahap,
                'jumlah' => $jumlah,
                'random' => $randomx,
                'penerima' => 'Transfer',
                'setor' => '',
                'id_setor' => '',
                'metode' => 'Bank',
                'catatan' => 'Transfer',
                'time' => $waktu,
                'ip' => '',
                'browser' => $browser,
                'id_tr' => $id_billing,
                'id' => $id
            ];
            $save = $crud->insertData($tb_transaksi, $dataarray);
        }


        //Kirim WA & Email
        $whatsapp = new whatsapp;
        $mailer = new mailer;

        $aktif_wa = $crud->getValue('whatsapp', "where no='1'", 'aktif');
        $aktif_mailer = $crud->getValue('mailer', "where no='1'", 'aktif');

        $nama = $crud->getValue($payment_list, "where payment_id='$id_billing'", 'nama');
        $nis = $crud->getValue($payment_list, "where payment_id='$id_billing'", 'nis');
        $jenjang = $crud->getValue($payment_list, "where payment_id='$id_billing'", 'jenjang');
        $paralel = $crud->getValue($payment_list, "where payment_id='$id_billing'", 'paralel');
        $hp = $crud->getValue($payment_list, "where payment_id='$id_billing'", 'hp');
        $siswa_email = $crud->getValue($payment_list, "where payment_id='$id_billing'", 'hp');
        $nama = $crud->getValue($payment_list, "where payment_id='$id_billing'", 'nama');
        $jmlKuitansi = $crud->getValue($payment_list, "where payment_id='$id_billing'", 'jumlah');

        if (strlen($hp >= 10 && $aktif_wa == 1)) {
            echo $whatsapp->notifikasiTransaksi($nama, $nis, $jenjang . "-" . $paralel, $hp, '', $jmlKuitansi, $id_billing, 'Transfer', $id_billing, $tahun);
        } else {
            echo "";
        };
        if (strlen($siswa_email >= 7 && $aktif_mailer == 1)) {
            $mailer->sendMail($siswa_email, $nama, $nis, $jenjang . "-" . $paralel, $hp, $jmlKuitansi, $id_billing, 'Transfer', '');
        } else {
            echo "";
        };

        $delete = $crud->deleteData('payment_list_detail', 'payment_id', $id_billing);
    } else {
        echo "";
    };

    $direct = new toast();
    echo  $direct->redirectID('toast', 'konfirmasi', $id_siswa, 'transfer/index');
};
