<?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'];

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

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

$currency = mysqli_fetch_assoc($get);

/* DON'T DELETE DEFAULT CURRENCY */
if($currency['is_default']==1){

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

}

/* DELETE */

$delete=mysqli_query($conn,"
DELETE FROM currencies
WHERE id='$id'
");

if($delete){

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

}else{

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

}
?>