<?php
$configCongTy = include 'configcongty.php';
$configBanLe = include 'configbanle.php';

function formatVND($n){
    return number_format($n, floor($n)==$n?0:3, ',', '.');
}

// map key -> tên hiển thị
$labels = [
    'hanoi_30' => 'Hà Nội (30L)',
    'hanoi_50' => 'Hà Nội (50L)',
    'new_30' => 'New (30L)',
    'new_50' => 'New (50L)',
    'tai' => 'Tải',
    'keg' => 'Keg',
    'chai' => 'Chai'
];

// AJAX
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $type = $_POST['type'];
    $file = $type === 'banle' ? 'configbanle.php' : 'configcongty.php';
    $config = include $file;

    $tax = $config['tax'];

    if ($_POST['tax_decimal'] !== '') {
        $tax = (float)str_replace(',', '.', $_POST['tax_decimal']);
    } elseif ($_POST['tax_percent'] !== '') {
        $tax = ((float)str_replace('%','',$_POST['tax_percent'])) / 100;
    }

    $price = $config['price'];
    foreach(array_keys($price) as $f){
        if(isset($_POST[$f]) && $_POST[$f]!==''){
            $price[$f] = (float)$_POST[$f];
        }
    }

    file_put_contents($file, '<?php return '.var_export([
        'price'=>$price,
        'tax'=>$tax
    ], true).';');

    echo json_encode(["status"=>"success","msg"=>"Đã lưu $type"]);
    exit;
}
?>

<style>
body {font-family: Arial; background:#f5f5f5; padding:20px;}
.container {max-width:600px; margin:auto; background:#fff; padding:20px; border-radius:10px;}
.tabs {display:flex; margin-bottom:15px;}
.tabs button {flex:1; padding:10px; border:none; cursor:pointer; border-radius:5px; margin-right:5px;}
.tabs button.active {background:#4CAF50; color:#fff;}
.panel {display:none;}
.panel.active {display:block;}
.row {display:flex; align-items:center; margin-bottom:10px;}
.row label {flex:1;}
.row .old {color:red; margin-left:10px; font-weight:bold;}
.row input {flex:1; padding:6px; margin-left:10px;}
.save {width:100%; padding:10px; background:#2196F3; color:#fff; border:none; border-radius:5px;}
.msg {margin:10px 0;}
</style>

<div class="container">

<h2>⚙️ ADMIN</h2>

<div class="tabs">
    <button onclick="showTab('congty')" id="btn-congty" class="active">🏢 Công ty</button>
    <button onclick="showTab('banle')" id="btn-banle">🛒 Bán lẻ</button>
</div>

<div id="msg" class="msg"></div>

<!-- CÔNG TY -->
<div id="congty" class="panel active">
<form onsubmit="save(event,'congty')">

<?php foreach($configCongTy['price'] as $k=>$v): ?>
<div class="row">
    <label><?= $labels[$k] ?></label>
    <span class="old">(<?= formatVND($v) ?>)</span>
    <input name="<?= $k ?>" placeholder="<?= $v ?>">
</div>
<?php endforeach; ?>

<div class="row">
    <label>Thuế (<?= $configCongTy['tax']*100 ?>%)</label>
    <input name="tax_decimal" placeholder="Ví dụ: 0.1 = 10%">
    <input name="tax_percent" placeholder="Ví dụ: 10 = 10%">
</div>

<button class="save">💾 Lưu Công Ty</button>
</form>
</div>

<!-- BÁN LẺ -->
<div id="banle" class="panel">
<form onsubmit="save(event,'banle')">

<?php foreach($configBanLe['price'] as $k=>$v): ?>
<div class="row">
    <label><?= $labels[$k] ?></label>
    <span class="old">(<?= formatVND($v) ?>)</span>
    <input name="<?= $k ?>" placeholder="<?= $v ?>">
</div>
<?php endforeach; ?>

<div class="row">
    <label>Thuế (<?= $configBanLe['tax']*100 ?>%)</label>
    <input name="tax_decimal" placeholder="Ví dụ: 0.1 = 10%">
    <input name="tax_percent" placeholder="Ví dụ: 10 = 10%">
</div>

<button class="save">💾 Lưu Bán Lẻ</button>
</form>
</div>

</div>

<script>
function showTab(tab){
    document.querySelectorAll('.panel').forEach(e=>e.classList.remove('active'));
    document.querySelectorAll('.tabs button').forEach(e=>e.classList.remove('active'));
    document.getElementById(tab).classList.add('active');
    document.getElementById('btn-'+tab).classList.add('active');
}

function save(e,type){
    e.preventDefault();
    const data = new FormData(e.target);
    data.append('type',type);
    fetch('',{method:'POST',body:data})
        .then(r=>r.json())
        .then(d=>document.getElementById('msg').innerHTML=
            `<span style="color:${d.status=='success'?'green':'red'}">${d.msg}</span>`);
}
</script>