<?php


session_start();
if($_SESSION['dbName']==""){
    header("location:../index.php");
}

include("config.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

$selectedCustomerType = $_POST['customertype'];
$selectedCustomerTypedesc = $_POST['customertypedesc'];
$mainmode = $_POST['mainmode'];
$sdefault = $_POST['sdefault'];


if($sdefault == 'remove'){
    $customertypequery = "update clientscustomertype set clientscustomertype_default = 0";
    mysql_query($customertypequery, $connection) or die(mysql_error());
    header('Location: Form_CustomerType.php?&message=Default customer type has been removed');
}
else {
    if ($mainmode == '') {
        header("Location: Form_CustomerType.php");
    } else if ($mainmode == 'edit') {
        $customertypequery = "update clientscustomertype set clientscustomertype_description = '$selectedCustomerTypedesc'
        where clientscustomertype_code = '$selectedCustomerType'";
        mysql_query($customertypequery, $connection) or die(mysql_error());
        if ($sdefault == 'true') {
            $customertypequery = "update clientscustomertype set clientscustomertype_default = 0";
            mysql_query($customertypequery, $connection) or die(mysql_error());
            $customertypequery = "update clientscustomertype set clientscustomertype_default = 1 where clientscustomertype_code = '$selectedCustomerType'";
            mysql_query($customertypequery, $connection) or die(mysql_error());
            header('Location: Form_CustomerType.php?selectedcustomertype=' . $selectedCustomerType . '&selectedcustomertypedesc=' . $selectedCustomerTypeper . "&message=Default customer type has changed successfully");
        } else
            header('Location: Form_CustomerType.php?selectedcustomertype=' . $selectedCustomerType . '&selectedcustomertypedesc=' . $selectedCustomerTypedesc . '&mainmode=edit');
    } else if ($mainmode == 'add') {
        $customertypequery = "select * from clientscustomertype where clientscustomertype_code = '$selectedCustomerType'";
        $result = mysql_query($customertypequery, $connection);
        if (mysql_num_rows($result) > 0) {
            while ($row = mysql_fetch_row($result)) {
                $selectedCustomerTypedesc = $row[2];
            }
            header('Location: Form_CustomerType.php?message=Customer type already available, please edit&selectedcustomertype=' . $selectedCustomerType . '&selectedcustomertypedesc=' . $selectedCustomerTypedesc . '&mainmode=edit');
        } else {
            $customertypequery = "insert into clientscustomertype (clientscustomertype_code,clientscustomertype_description) VALUES ('$selectedCustomerType','$selectedCustomerTypedesc')";
            mysql_query($customertypequery, $connection) or die(mysql_error());
            if ($sdefault == 'true') {
                $customertypequery = "update clientscustomertype set clientscustomertype_default = 0";
                mysql_query($customertypequery, $connection) or die(mysql_error());
                $customertypequery = "update clientscustomertype set clientscustomertype_default = 1 where clientscustomertype_code = '$selectedCustomerType'";
                mysql_query($customertypequery, $connection) or die(mysql_error());
            }
            header('Location: Form_CustomerType.php?selectedcustomertype=' . $selectedCustomerType . '&selectedcustomertypedesc=' . $selectedCustomerTypedesc . '&mainmode=edit');
        }
    } else if ($mainmode == 'delete') {
        $customertypequery = "select Client_Rating from clients where Client_Rating = '$selectedCustomerType'";
        $result = mysql_query($customertypequery, $connection);
        if (mysql_num_rows($result) > 0) {
            header('Location: Form_CustomerType.php?message=Cannot delete record.');
        } else {
            $customertypequery = "delete from clientscustomertype where clientscustomertype_code = '$selectedCustomerType'";
            mysql_query($customertypequery, $connection) or die(mysql_error());
            header('Location: Form_CustomerType.php');
        }
    }
}

//clientscustomertype_id, clientscustomertype_code, clientscustomertype_description, clientscustomertype_default