<?php
include('config.php');

$user_id = $conn->real_escape_string(strtolower($_POST['user_id']));
$user_name = $conn->real_escape_string(strtolower($_POST['user_name']));
$cpass = enc($conn->real_escape_string($_POST['passwd']));
$outlet_id 		= $_POST['outlet_id'];



$act 			= $_POST['act'];
$autoid 		= $_POST['autoid'];
$order_flag 	= $_POST['order_flag'];
$inv_flag 		= $_POST['inv_flag'];
$md_flag 		= $_POST['md_flag'];
$rpt_flag 		= $_POST['rpt_flag'];
$content_flag 	= $_POST['content_flag'];
$setting_flag 	= $_POST['setting_flag'];
$prc_flag 		= $_POST['prc_flag'];
$acc_flag 		= $_POST['acc_flag'];
$hpp_flag 		= $_POST['hpp_flag'];

if($act=="add"){

	$sql = "INSERT INTO dl_useraccount 
			(id,username,npasswd,date_register,outlet_id) 
			VALUES 
			('$user_id','$user_name','$cpass',CURRENT_TIMESTAMP(),'$outlet_id')";
			$conn->query($sql) or die($sql);
	
	$sqlget = "SELECT * FROM dl_useraccount WHERE id = '$user_id' AND npasswd = '$cpass' LIMIT 0,1";
	$dcon = $conn->query($sqlget) or die($sqlget);
	$rs = $dcon->fetch_array();
	$autoid = $rs['user_autoid'];

	$sqlin = "INSERT INTO useraccess 
				(user_autoid,user_id,order_flag,inv_flag,md_flag,rpt_flag,content_flag,setting_flag,prc_flag,acc_flag,hpp_flag) 
				VALUES 
				('$autoid','$user_id','$order_flag','$inv_flag','$md_flag','$rpt_flag','$content_flag','$setting_flag','$prc_flag','$acc_flag','$hpp_flag')";		
			$conn->query($sqlin) or die($sqlin);
				
			header('Location:index');

}

if($act=="edit"){
	
	if($_POST['passwd']!=""){
		$sql = "UPDATE dl_useraccount SET username = '$user_name', npasswd = '$cpass' WHERE user_autoid = '$autoid'";
		}
	else{
		$sql = "UPDATE dl_useraccount SET username = '$user_name' WHERE user_autoid = '$autoid'";
		}
			$conn->query($sql) or die($sql);
	
	$conn->query("delete from useraccess WHERE user_autoid = '$autoid'");

	$sqlin = "INSERT INTO useraccess 
				(user_autoid,user_id,order_flag,inv_flag,md_flag,rpt_flag,content_flag,setting_flag,prc_flag) 
				VALUES 
				('$autoid','$user_id','$order_flag','$inv_flag','$md_flag','$rpt_flag','$content_flag','$setting_flag','$prc_flag')";		
			$conn->query($sqlin) or die($sqlin);
				
			header('Location:index');
	
}

if($act=="delete"){
	$sql = "DELETE FROM dl_useraccount WHERE user_autoid = '$autoid'";
			$conn->query($sql) or die($sql);
			header('Location:index');
	}

?>