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

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

if(!isset($_GET['id']) || !is_numeric($_GET['id'])){
    header("Location:/beauty_pos/currencies/index.php");
    exit;
}

$id=(int)$_GET['id'];

$get=mysqli_query($conn,"
SELECT *
FROM currencies
WHERE id='$id'
");

if(mysqli_num_rows($get)==0){
    header("Location:/beauty_pos/currencies/index.php");
    exit;
}

$row=mysqli_fetch_assoc($get);

$error="";

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

    $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;

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

    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");
        }

        $update=mysqli_query($conn,"
        UPDATE currencies SET
        currency_name='$currency_name',
        currency_code='$currency_code',
        currency_symbol='$currency_symbol',
        status='$status',
        is_default='$is_default'
        WHERE id='$id'
        ");

        if($update){

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

        }else{

            $error="Update failed.";

        }

    }

}

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>Edit 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" value="<?php echo htmlspecialchars($row['currency_name']); ?>" required>
</div>

<div class="col-md-3">
<label>Currency Code</label>
<input type="text" name="currency_code" class="form-control" value="<?php echo htmlspecialchars($row['currency_code']); ?>" required>
</div>

<div class="col-md-3">
<label>Currency Symbol</label>
<input type="text" name="currency_symbol" class="form-control" value="<?php echo htmlspecialchars($row['currency_symbol']); ?>" required>
</div>

<div class="col-md-6">
<label>Status</label>
<select name="status" class="form-control">
<option value="1" <?php if($row['status']==1) echo "selected"; ?>>Active</option>
<option value="0" <?php if($row['status']==0) echo "selected"; ?>>Inactive</option>
</select>
</div>

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

<div class="form-check">

<input
type="checkbox"
class="form-check-input"
name="is_default"
id="is_default"
<?php if($row['is_default']==1) echo "checked"; ?>>

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

Set as Default Currency

</label>

</div>

</div>

<div class="col-12">

<button
type="submit"
name="update"
class="btn btn-success">

Update Currency

</button>

</div>

</div>

</form>

</div>

</div>

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

</div>