<?php

require __DIR__ . '/../../../vendor/autoload.php';
include '../../config/conn.php';

function encrypt($str)
{
    $kunci = '979a218e0632df2935317f98d47956c7';
    for ($i = 0; $i < strlen($str); $i++) {
        $karakter = substr($str, $i, 1);
        $kuncikarakter = substr($kunci, ($i % strlen($kunci)) - 1, 1);
        $karakter = chr(ord($karakter) + ord($kuncikarakter));
        @$hasil .= $karakter;
    }
    return urlencode(base64_encode($hasil));
}

function rupiah($rp)
{
    if ($rp != 0) {
        $hasil = number_format($rp, 0, '.', ',');
    } else {
        $hasil = 0;
    }
    return $hasil;
}

use Mike42\Escpos\EscposImage;
use Mike42\Escpos\Printer;
//jika menggunakan koneksi printer usb sahre
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
//jika menggunakan koneksi jaringan
use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
use Mike42\Escpos\CapabilityProfiles\StarCapabilityProfile;


$tux = EscposImage::load("ptma.png");

$query = mysqli_query($conn, "SELECT * FROM transaksi WHERE id_transaksi='$_GET[id_tran]' order by tanggal ASC ");



while ($row = mysqli_fetch_array($query)) {
    $keri = mysqli_query($conn, "SELECT * FROM santri_rt WHERE id_santri='$row[id_santri]'");
    $r = mysqli_fetch_array($keri);


    //nama admin
    $adm1 = mysqli_query($conn, "SELECT * FROM pegawai WHERE id_pegawai='$row[id_pegawai]'");
    $adm = mysqli_fetch_array($adm1);

    //no hp
    $hp1 = mysqli_query($conn, "SELECT * FROM pengaturan WHERE id_pengaturan=1");
    $hp = mysqli_fetch_array($hp1);

    try {

        //ini untuk lokal printer menggunakan USB
        //$connector = new WindowsPrintConnector("kasir2");


        //koneksi printer menggunakan ip jaringan LAN
        $print1 = mysqli_query($conn, "SELECT * FROM printer LIMIT 1");
        $pr = mysqli_fetch_array($print1);
        $connector = new WindowsPrintConnector("smb://$pr[nama_pc]/$pr[nama_print]");



        $printer = new Printer($connector);

        //printah untuk membuka laci / cash drawer
        $printer->pulse();



        /* mulai cetak */
        $printer->setJustification(Printer::JUSTIFY_CENTER);
        $printer->bitImage($tux);
        $printer->text("Pondok Tahfidz Modern AL-IMAM \n");
        $printer->text("Ds. Tirukidul, Kec. Gurah\n");
        $printer->text("Kabupaten Kediri\n");
        $printer->text("Telp. : " . $hp['telephone'] . "\n");
        $printer->text("_______________________________\n");
        $printer->text("ADMIN : " . $adm['nama'] . "| " . $row['tanggal'] . " \n");
        $printer->text("NO TRANSAKSI : $row[id_transaksi] \n");
        $printer->text("-------------------------------\n");

        /* buat perulangan data transaksi */


        /* bagian footer */
        $jns1 = mysqli_query($conn, "SELECT * FROM jenis_transaksi WHERE id_jenis='$row[id_jenis]'");
        $jns = mysqli_fetch_array($jns1);

        $printer->setJustification(Printer::JUSTIFY_LEFT);
        $printer->text("Jenis Pembayaran : $jns[nama_jenis]\n");
        $printer->text("Id Santri : $row[id_santri] \n");
        $printer->text("Nama Santri: $r[nama] \n");
        $printer->setJustification(Printer::JUSTIFY_CENTER);
        $printer->text("-------------------------------\n");
        $printer->setJustification(Printer::JUSTIFY_LEFT);
        $rp = rupiah($row['debit']);
        $printer->text("JUMLAH BAYAR : Rp.$rp \n");
        $printer->text("KETERANGAN : $row[keterangan]\n");
        $printer->setJustification(Printer::JUSTIFY_CENTER);
        $printer->text("-------------------------------\n");
        $printer->text("Jazakallah Khairan \n");
        $printer->text("tgl cetak struk " . date('d/m/y H:i') . " \n");
        $printer->text(" \n");
        $printer->text(" \n");
        $printer->text(" \n");
        $printer->text(" \n");
        $printer->text(" \n");


        //potong kertas
        $printer->cut();

        /* Close printer */
        $printer->close();
    } catch (Exception $e) {
        echo "TIDAK TERKONEKSI DENGAN PRINTER: " . $e->getMessage() . "\n";
    }
    $id_san = $row['id_santri'];

    //lokasi setelah print
    // if ($row['id_jenis'] == "5") {
    //     echo "<script>alert('Pastikan cetak 2 kali, untuk arsip!');history.go(-1);</script>";
    //     //echo "<script language='javascript'>document.location='../../?module=pembayaran-spp&aksi=tambah_spp&id=$id_san';</script>";

    // } elseif ($row['id_jenis'] == "14") {
    //     echo "<script>alert('Pastikan cetak 2 kali, untuk arsip!');history.go(-1);</script>";
    //     //echo "<script language='javascript'>document.location='../../?module=pembayaran-dam&aksi=tambah_dam&id=$id_san';</script>";     
    // }
    echo "<script>alert('Pastikan cetak 2 kali, untuk arsip!');history.go(-1);</script>";
}
