<?php
$page_title = 'Tambah Pelanggan';
require_once('includes/load.php');
// Level pengguna yang diizinkan untuk melihat halaman ini
page_require_level(5);

// Ambil semua akun COA dengan type_akun 'Piutang'
$coas = find_by_sql("SELECT kode, akun FROM coa WHERE type_akun = 'Piutang' ORDER BY kode ASC");
$default_coa_kode = '115.01'; // Default value based on the COA table provided

if (isset($_POST['add_customer'])) {
    $req_fields = array('customer-name', 'customer-phone');
    validate_fields($req_fields);

    if (empty($errors)) {
        $c_name     = remove_junk($db->escape($_POST['customer-name']));
        $c_address  = remove_junk($db->escape($_POST['customer-address']));
        $c_phone    = remove_junk($db->escape($_POST['customer-phone']));
        $c_email    = remove_junk($db->escape($_POST['customer-email']));
        $c_contact  = remove_junk($db->escape($_POST['customer-contact']));
        $c_coa      = remove_junk($db->escape($_POST['customer-coa-kode']));

        $query = "INSERT INTO customers (name, address, phone, email, contact_person, coa_kode) VALUES (
            '{$c_name}', 
            '{$c_address}', 
            '{$c_phone}', 
            '{$c_email}', 
            '{$c_contact}', 
            '{$c_coa}'
        )";

        if ($db->query($query)) {
            $session->msg('s', "Pelanggan berhasil ditambahkan.");
            redirect('customers.php', false);
        } else {
            $session->msg('d', 'Gagal menambahkan pelanggan. ' . $db->error);
            redirect('add_customer.php', false);
        }
    } else {
        $session->msg("d", $errors);
        redirect('add_customer.php', false);
    }
}
?>

<?php include_once('layouts/header.php'); ?>

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row">
    <div class="col-md-8">
        <div class="panel panel-default">
            <div class="panel-heading">
                <strong>
                    <span class="glyphicon glyphicon-th"></span>
                    <span>Form Tambah Pelanggan</span>
                </strong>
            </div>
            <div class="panel-body">
                <form method="post" action="add_customer.php" class="clearfix">
                    <div class="form-group">
                        <label for="customer-name">Nama Pelanggan</label>
                        <input type="text" class="form-control" name="customer-name" placeholder="Nama perusahaan atau perorangan" required>
                    </div>
                    <div class="form-group">
                        <label for="customer-address">Alamat</label>
                        <textarea class="form-control" name="customer-address" rows="3" placeholder="Alamat lengkap"></textarea>
                    </div>
                    <div class="form-group">
                        <label for="customer-phone">No. Telepon</label>
                        <input type="text" class="form-control" name="customer-phone" placeholder="No. telepon pelanggan" required>
                    </div>
                    <div class="form-group">
                        <label for="customer-email">Email</label>
                        <input type="email" class="form-control" name="customer-email" placeholder="Email pelanggan">
                    </div>
                    <div class="form-group">
                        <label for="customer-contact">Contact Person</label>
                        <input type="text" class="form-control" name="customer-contact" placeholder="Nama PIC atau penanggung jawab">
                    </div>
                    <div class="form-group">
                        <label for="customer-coa-kode">Kode COA (Piutang)</label>
                        <select class="form-control" name="customer-coa-kode">
                            <option value="">Pilih Kode COA</option>
                            <?php foreach ($coas as $coa): ?>
                                <option value="<?php echo htmlspecialchars($coa['kode']); ?>" <?php if ($coa['kode'] == $default_coa_kode) echo 'selected'; ?>>
                                    <?php echo htmlspecialchars($coa['kode']); ?> - <?php echo htmlspecialchars($coa['akun']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <button type="submit" name="add_customer" class="btn btn-primary">Tambah Pelanggan</button>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>