<?php
include 'db.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $branch_name = $_POST['branch_name'];
    $branch_code = $_POST['branch_code'];
    $branch_address = $_POST['branch_address'];
    $branch_phone = $_POST['branch_phone'];
    $province = $_POST['province'];
    $ntn = $_POST['ntn'];
    $strn = $_POST['strn'];
    $branch_type = $_POST['branch_type'];
    $is_default = isset($_POST['is_default']) ? 1 : 0;
    
    // If setting as default, remove default from other branches
    if ($is_default) {
        $conn->query("UPDATE branch_info SET is_default = 0");
    }
    
    $stmt = $conn->prepare("
        INSERT INTO branch_info (branch_name, branch_code, branch_address, branch_phone, province, ntn, strn, branch_type, is_default) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
    ");
    $stmt->bind_param("ssssssssi", $branch_name, $branch_code, $branch_address, $branch_phone, $province, $ntn, $strn, $branch_type, $is_default);
    
    if ($stmt->execute()) {
        header("Location: branch_list.php?success=1");
    } else {
        header("Location: branch_list.php?error=1");
    }
    $stmt->close();
    exit;
}

header("Location: branch_list.php");
exit;
?>
