<?php

$action = $_POST['action'] ?? 'none';
$confirm = $_POST['confirm'] ?? 'none';

if ($action == 'delete_bill' && $confirm == 'confirm') {
    \App\Models\BillHistory::where('bill_id', $bill_id)->delete();
    \App\Models\BillPort::where('bill_id', $bill_id)->delete();
    \App\Models\BillData::where('bill_id', $bill_id)->delete();
    \App\Models\BillPerm::where('bill_id', $bill_id)->delete();
    \App\Models\Bill::where('bill_id', $bill_id)->delete();

    echo '<div class=infobox>Bill Deleted. Redirecting to Bills list.</div>';

    echo "<meta http-equiv='Refresh' content=\"2; url='" . url('bills') . "'\">";
}

if ($action == 'reset_bill' && ($confirm == 'rrd' || $confirm == 'mysql')) {
    if ($confirm == 'mysql') {
        \App\Models\BillHistory::where('bill_id', $bill_id)->delete();
        \App\Models\BillData::where('bill_id', $bill_id)->delete();
    }

    if ($confirm == 'rrd') {
        // Stil todo
    }

    echo '<div class=infobox>Bill Resetting. Redirecting to Bills list.</div>';

    echo "<meta http-equiv='Refresh' content=\"2; url='" . url('bills') . "'\">";
}

if ($action == 'add_bill_port') {
    dbInsert(['bill_id' => $_POST['bill_id'], 'port_id' => $_POST['port_id']], 'bill_ports');
}

if ($action == 'delete_bill_port') {
    \App\Models\BillPort::where('bill_id', $bill_id)->where('port_id', $_POST['port_id'])->delete();
}

if ($action == 'update_bill') {
    if (isset($_POST['bill_quota']) or isset($_POST['bill_cdr'])) {
        if ($_POST['bill_type'] == 'quota') {
            if (isset($_POST['bill_quota_type'])) {
                if ($_POST['bill_quota_type'] == 'MB') {
                    $multiplier = (1 * \App\Facades\LibrenmsConfig::get('billing.base'));
                }

                if ($_POST['bill_quota_type'] == 'GB') {
                    $multiplier = (1 * \App\Facades\LibrenmsConfig::get('billing.base') * \App\Facades\LibrenmsConfig::get('billing.base'));
                }

                if ($_POST['bill_quota_type'] == 'TB') {
                    $multiplier = (1 * \App\Facades\LibrenmsConfig::get('billing.base') * \App\Facades\LibrenmsConfig::get('billing.base') * \App\Facades\LibrenmsConfig::get('billing.base'));
                }

                $bill_quota = (is_numeric($_POST['bill_quota']) ? $_POST['bill_quota'] * \App\Facades\LibrenmsConfig::get('billing.base') * $multiplier : 0);
                $bill_cdr = 0;
            }
        }

        if ($_POST['bill_type'] == 'cdr') {
            if (isset($_POST['bill_cdr_type'])) {
                if ($_POST['bill_cdr_type'] == 'Kbps') {
                    $multiplier = (1 * \App\Facades\LibrenmsConfig::get('billing.base'));
                }

                if ($_POST['bill_cdr_type'] == 'Mbps') {
                    $multiplier = (1 * \App\Facades\LibrenmsConfig::get('billing.base') * \App\Facades\LibrenmsConfig::get('billing.base'));
                }

                if ($_POST['bill_cdr_type'] == 'Gbps') {
                    $multiplier = (1 * \App\Facades\LibrenmsConfig::get('billing.base') * \App\Facades\LibrenmsConfig::get('billing.base') * \App\Facades\LibrenmsConfig::get('billing.base'));
                }

                $bill_cdr = (is_numeric($_POST['bill_cdr']) ? $_POST['bill_cdr'] * $multiplier : 0);
                $bill_quota = 0;
            }
        }
    }//end if

    // NOTE: casting to string for mysqli bug (fixed by mysqlnd)
    if (dbUpdate(
        [
            'bill_name' => $_POST['bill_name'],
            'bill_day' => $_POST['bill_day'],
            'bill_quota' => (string) $bill_quota,
            'bill_cdr' => (string) $bill_cdr,
            'bill_type' => $_POST['bill_type'],
            'dir_95th' => $_POST['dir_95th'],
            'bill_custid' => $_POST['bill_custid'],
            'bill_ref' => $_POST['bill_ref'],
            'bill_notes' => $_POST['bill_notes'],
        ],
        'bills',
        '`bill_id` = ?',
        [$bill_id]
    )) {
        print_message('Bill Properties Updated');
    }
}//end if
