<?php
session_start();
include_once "../config/database.php";

if(!isset($_SESSION['user_id'])){
    header("Location:/beauty_pos/login.php");
    exit;
}

$error="";

if(isset($_POST['submit'])){

    $currency_name=mysqli_real_escape_string($conn,trim($_POST['currency_name']));
    $currency_code=strtoupper(mysqli_real_escape_string($conn,trim($_POST['currency_code'])));
    $currency_symbol=mysqli_real_escape_string($conn,trim($_POST['currency_symbol']));
    $status=(int)$_POST['status'];
    $is_default=isset($_POST['is_default']) ? 1 : 0;

    if($currency_name=="" || $currency_code=="" || $currency_symbol==""){

        $error="All fields are required.";

    }else{

        $check=mysqli_query($conn,"
        SELECT id
        FROM currencies
        WHERE currency_code='$currency_code'
        ");

        if(mysqli_num_rows($check)>0){

            $error="Currency Code already exists.";

        }else{

            if($is_default==1){

                mysqli_query($conn,"UPDATE currencies SET is_default=0");

            }

            $insert=mysqli_query($conn,"
            INSERT INTO currencies
            (
                currency_name,
                currency_code,
                currency_symbol,
                status,
                is_default
            )
            VALUES
            (
                '$currency_name',
                '$currency_code',
                '$currency_symbol',
                '$status',
                '$is_default'
            )
            ");

            if($insert){

                header("Location:/beauty_pos/currencies/index.php?success=added");
                exit;

            }else{

                $error="Unable to save currency.";

            }

        }

    }

}

include_once "../includes/header.php";
include_once "../includes/sidebar.php";
?>

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

<div class="main-content">

<?php include_once "../includes/topbar.php"; ?>

<div class="container-fluid">

<div class="card-box rounded-4 shadow p-4">

<div class="d-flex justify-content-between align-items-center mb-4">

<h4>Add Currency</h4>

<a href="/beauty_pos/currencies/index.php" class="btn btn-secondary">
Back
</a>

</div>

<?php if($error!=""){ ?>

<div class="alert alert-danger">
<?php echo $error; ?>
</div>

<?php } ?>

<form method="POST">

<div class="row g-3">

<div class="col-md-6">

<label>Currency Name</label>

<input
type="text"
name="currency_name"
class="form-control"
required>

</div>

<div class="col-md-3">

<label>Currency Code</label>

<input
type="text"
name="currency_code"
class="form-control"
maxlength="10"
required>

</div>

<div class="col-md-3">

<label>Currency Symbol</label>

<input
type="text"
name="currency_symbol"
class="form-control"
required>

</div>

<div class="col-md-6">

<label>Status</label>

<select name="status" class="form-control">

<option value="1">Active</option>

<option value="0">Inactive</option>

</select>

</div>

<div class="col-md-6 d-flex align-items-end">

<div class="form-check">

<input
class="form-check-input"
type="checkbox"
name="is_default"
id="is_default">

<label class="form-check-label" for="is_default">

Set as Default Currency

</label>

</div>

</div>

<div class="col-12">

<button
type="submit"
name="submit"
class="btn btn-primary">

Save Currency

</button>

</div>

</div>

</form>

</div>

</div>

<?php include_once "../includes/footer.php"; ?>

</div>