<?php

namespace App\Controllers;

/**
 * Port dari Home::index() (CI3, storefront) -- halaman beranda. Slice
 * berikutnya menambahkan keranjang/checkout produk fisik & jasa
 * (keranjang/keranjangjasa/pembayaran/pembayaranjasa/invoice) -- lihat
 * Assync.php (keranjang CRUD) dan Cekout.php (proses bayar).
 */
class Home extends BaseController
{
    public function index()
    {
        $data = ['func' => $this->func, 'db' => $this->db];

        return view('customer/head', $data) . view('customer/home', $data) . view('customer/foot', $data);
    }

    /**
     * Keranjang PPOB -- port RINGKAS dari Home::keranjangppob() (CI3).
     * Versi asli menyatukan tampilan keranjang + form checkout penuh
     * (alamat/kurir/voucher/PIN, kebanyakan vestige dari cart produk
     * fisik & disembunyikan via display:none untuk produk digital) yang
     * submit ke Cekout::bayarpesananppob() -- sengaja BELUM diport
     * (fase pembayaran). Halaman ini hanya menampilkan ringkasan pesanan
     * + saldo, dengan tombol checkout yang jelas ditandai belum aktif.
     */
    public function keranjangppob()
    {
        $usrid = session()->get('usrid');
        if ($usrid === null) {
            return redirect()->to(site_url('home/signin'));
        }

        $items = $this->db->table('transaksiprodukdigital')
            ->where('usrid', $usrid)
            ->where('idtransaksi', 0)
            ->get()
            ->getResult();

        $total = 0;
        foreach ($items as $item) {
            $produk        = $this->func->getProdukDigital($item->idproduk, 'semua');
            $item->produk  = $produk;
            $item->harga   = $item->idproduk === 25 ? $item->harga / 1000 : $item->harga;
            $total        += $item->harga * $item->jumlah;

            // Detail produk (nama+nominal, brand) dari katalog Digiflazz lokal
            // (tuku_margin utk produk biasa, tuku_h2h utk produk h2h) -- idpo
            // di keranjang menyimpan buyer_sku_code, bukan nama yang enak dibaca.
            $item->skuInfo = null;
            if ($item->idpo !== '') {
                $skuTable = (int) $item->h2h === 1 ? 'h2h' : 'margin';
                $item->skuInfo = $this->db->table($skuTable)->where('buyer_sku_code', $item->idpo)->get()->getRow();
            }
        }

        $data = [
            'func'   => $this->func,
            'db'     => $this->db,
            'items'  => $items,
            'total'  => $total,
            'saldo'  => $this->func->getSaldo($usrid, 'saldo', 'usrid'),
        ];

        return view('customer/head', $data) . view('customer/main/keranjangppob', $data) . view('customer/foot', $data);
    }

    /** Port dari Home::keranjang() (CI3) -- keranjang produk fisik. */
    public function keranjang()
    {
        if ($this->func->cekLogin() === 0) {
            return redirect()->to(site_url('home/signin'));
        }

        $data = ['func' => $this->func, 'db' => $this->db, 'titel' => 'Keranjang'];

        return view('customer/head', $data) . view('customer/main/keranjang', $data) . view('customer/foot', $data);
    }

    /** Port dari Home::keranjangjasa() (CI3) -- keranjang jasa. */
    public function keranjangjasa()
    {
        if ($this->func->cekLogin() === 0) {
            return redirect()->to(site_url('home/signin'));
        }

        $data = ['func' => $this->func, 'db' => $this->db, 'titel' => 'Keranjang Jasa'];

        return view('customer/head', $data) . view('customer/main/keranjangjasa', $data) . view('customer/foot', $data);
    }

    /** Port dari Home::pembayaran() (CI3) -- form checkout produk fisik. */
    public function pembayaran()
    {
        $usrid = session()->get('usrid');
        if ($usrid === null) {
            return redirect()->to(site_url('home/signin'));
        }

        $data = [
            'func'  => $this->func,
            'db'    => $this->db,
            'titel' => 'Pembayaran Pesanan',
            'data'  => $this->db->table('transaksiproduk')->where('usrid', $usrid)->where('idtransaksi', 0)->where('idpo', 0)->where('jasa', 0)->get()->getResult(),
            'saldo' => $this->func->getSaldo($usrid, 'saldo', 'usrid'),
        ];

        return view('customer/head', $data) . view('customer/main/bayarpesanan', $data) . view('customer/foot', $data);
    }

    /** Port dari Home::pembayaranjasa() (CI3) -- form checkout jasa. */
    public function pembayaranjasa()
    {
        $usrid = session()->get('usrid');
        if ($usrid === null) {
            return redirect()->to(site_url('home/signin'));
        }

        $data = [
            'func'  => $this->func,
            'db'    => $this->db,
            'titel' => 'Pembayaran Pesanan',
            'data'  => $this->db->table('transaksiproduk')->where('usrid', $usrid)->where('idtransaksi', 0)->where('idpo', 0)->where('jasa', 1)->get()->getResult(),
            'saldo' => $this->func->getSaldo($usrid, 'saldo', 'usrid'),
        ];

        return view('customer/head', $data) . view('customer/main/bayarpesananjasa', $data) . view('customer/foot', $data);
    }

    /** Port dari Home::invoice() (CI3) -- halaman instruksi/status pembayaran. */
    public function invoice()
    {
        $usrid = session()->get('usrid');
        $inv   = $this->request->getGet('inv');
        if ($usrid === null) {
            return redirect()->to(site_url('home/signin'));
        }
        if ($inv === null) {
            throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        }

        $decoded = $this->func->arrDec($inv);
        $idbayar = $decoded['idbayar'] ?? null;
        $bayar   = $idbayar !== null ? $this->func->getBayar($idbayar, 'semua') : null;
        if ($bayar === null || (int) $bayar->usrid !== (int) $usrid) {
            throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        }

        if ($this->request->getGet('revoke') !== null) {
            $this->db->table('pembayaran')->where('id', $idbayar)->update(['invoice' => $bayar->invoice . date('Hi'), 'midtrans_id' => '']);
            $bayar = $this->func->getBayar($idbayar, 'semua');
        }

        $transaksi = $this->db->table('transaksi')->where('idbayar', $idbayar)->get()->getResult();

        $data = [
            'func'      => $this->func,
            'db'        => $this->db,
            'titel'     => 'Informasi Pembayaran Pesanan',
            'data'      => $bayar,
            'usrid'     => $this->func->getUser($bayar->usrid, 'semua'),
            'profil'    => $this->func->getProfil($bayar->usrid, 'semua', 'usrid'),
            'transaksi' => $transaksi,
            'alamat'    => count($transaksi) > 0 ? $this->func->getAlamat($transaksi[0]->alamat, 'semua') : null,
            'bank'      => $this->db->table('rekening')->select('rekening.*, rekeningbank.nama as namabank')->join('rekeningbank', 'rekeningbank.id = rekening.idbank')->where('rekening.usrid <=', 0)->get()->getResult(),
        ];

        return view('customer/head', $data) . view('customer/main/cekout', $data) . view('customer/foot', $data);
    }
}
