<?php
include "top.php";

?>
<!DOCTYPE html>
<html>
<head>
	<?php include('include/head.php'); ?>
	<title>Company Master</title>
</head>
<body>
	<?php include('include/header.php'); ?>
	<?php include('include/sidebar.php'); ?>


	<?php 

$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
$cid = isset($_REQUEST['cid']) ? $_REQUEST['cid'] : (isset($_SESSION['company']['c_id']) ? $_SESSION['company']['c_id'] : 1);

// Verify and fallback to first company record if c_id is not found
$check_edit = f(q("SELECT * FROM tbl_company WHERE c_id='$cid'"));
if (!$check_edit || empty($check_edit['c_name'])) {
    $check_edit = f(q("SELECT * FROM tbl_company LIMIT 1"));
    if ($check_edit) {
        $cid = $check_edit['c_id'];
    }
}

if ($action == 'update') {
    if ($pupdate == 1) {
        $c_name = $_POST['c_name'];
        $c_shortname = $_POST['c_shortname'];
        $c_address = $_POST['c_address'];
        $c_email = $_POST['c_email'];
        $c_phone = $_POST['c_phone'];
        $c_mobile = $_POST['c_mobile'];
        $c_emailalt = $_POST['c_emailalt'];
        $isgst = $_POST['isgst'];
        
        $f = $_FILES['a_logo']['name'];
        if ($f != '') {
            $image = time() . $f;
            $folder = 'alogo/' . $image;
            $temp = $_FILES['a_logo']['tmp_name'];
            move_uploaded_file($temp, $folder);
        } else {
            $image = $_POST['imagestore1'];
        }

        $res = mysqli_query($con, "UPDATE tbl_company SET c_name='$c_name', c_shortname='$c_shortname', c_address='$c_address', c_email='$c_email', c_phone='$c_phone', c_mobile='$c_mobile', c_emailalt='$c_emailalt', isgst='$isgst', c_logo='$image' WHERE c_id='$cid'");
        echo "<script>alert('Company details updated successfully!');window.location.href='company-master.php?cid=" . $cid . "';</script>";
        exit;
    } else {
        echo "<script>alert('You are not Authorized to Update');</script>";
    }
}

// Load company data directly on page load
$edit = f(q("SELECT * FROM tbl_company WHERE c_id='$cid'"));



?>

	<div class="main-container">
		<div class="pd-ltr-20 customscroll customscroll-10-p height-100-p xs-pd-20-10">
			<div class="min-height-200px">
				<div class="page-header">
					<div class="row">
						<div class="col-md-6 col-sm-12">
							<div class="title">
								<h4>Company Master</h4>
							</div>
							<nav aria-label="breadcrumb" role="navigation">
								<ol class="breadcrumb">
									<li class="breadcrumb-item"><a href="index.php">Home</a></li>
									<li class="breadcrumb-item active" aria-current="page">Company Master</li>
								</ol>
							</nav>
						</div>
					</div>
				</div>

				<!-- Default Basic Forms Start -->
				<div class="pd-20 bg-white border-radius-4 box-shadow mb-30">
					<form action="<?=$_SERVER['PHP_SELF']?>?action=update&cid=<?php echo $cid; ?>" method="POST" enctype="multipart/form-data">
						<div class="clearfix" style="min-height: 30px;"></div>
						
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Company Name</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" type="text" name="c_name" placeholder="Company Name" value="<?=htmlspecialchars($edit['c_name'] ?? '')?>">
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Short Name</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" type="text" name="c_shortname" placeholder="Short Name" value="<?=htmlspecialchars($edit['c_shortname'] ?? '')?>">
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Address</label>
							<div class="col-sm-12 col-md-10">
								<textarea class="form-control" name="c_address"><?=htmlspecialchars($edit['c_address'] ?? '')?></textarea>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Email</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" name="c_email" type="email" value="<?=htmlspecialchars($edit['c_email'] ?? '')?>">
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">GST Number</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" name="c_emailalt" type="text" value="<?=htmlspecialchars($edit['c_emailalt'] ?? '')?>">
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Telephone</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" type="text" name="c_phone" value="<?=htmlspecialchars($edit['c_phone'] ?? '')?>">
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Mobile</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" type="text" name="c_mobile" value="<?=htmlspecialchars($edit['c_mobile'] ?? '')?>">
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Show GST?</label>
							<div class="col-sm-12 col-md-10">
								<select name="isgst" class="form-control">
									<option value="0" <?php if(($edit['isgst'] ?? '0') == '0'){?> selected <?php } ?>>No</option>
									<option value="1" <?php if(($edit['isgst'] ?? '0') == '1'){?> selected <?php } ?>>Yes</option>
								</select>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Payment QR Code</label>
							<div class="col-sm-12 col-md-10">
								<input type="file" class="form-control" name="a_logo" accept="image/*">
								<?php if(!empty($edit['c_logo'])){ ?>
									<div style="margin-top:8px;">
										<img src="alogo/<?=htmlspecialchars($edit['c_logo'])?>" style="max-width:200px; max-height:200px;" alt="QR Code">
									</div>
								<?php } ?>
								<input type="hidden" name="imagestore1" value="<?=htmlspecialchars($edit['c_logo'] ?? '')?>" />
							</div>
						</div> 

						<div class="clearfix">
							<div class="pull-right">
								<button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-save"></i> Save / Update</button>
							</div>
						</div>
					</form>
				</div>
			</div>
			<?php include('include/footer.php'); ?>
		</div>
	</div>
	<?php include('include/script.php'); ?>
</body>
</html>
