<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
require("includes/config.php");
require("includes/function.php");

$formname  = 'Company Setup';
$maindb    = 'company';
$file      = 'company.php';
$keyfld    = 'code';
$keycond   = "";

$compcd   = $_SESSION["comp_"    . $dbname] ?? '';
$usertype = $_SESSION["usertype_" . $dbname];

// `company` is a root-level table — it lists the companies you pick from
// BEFORE a financial year is even chosen (see comp.php), so unlike every
// other master here it must stay on the root DB ($dbname), not the
// per-financial-year DB ($_SESSION["zdbyear_...]). dbconnect.php already
// selects the root DB by default; re-select it explicitly so this page
// doesn't depend on that not having changed.
mysqli_select_db($conn, $dbname);

// ── Auto-migration: `company` table already exists (used by comp.php/login
//    flow) but this is the first CRUD screen for it, so make sure the audit
//    columns every other master relies on are actually there before we try
//    to write to them.
foreach ([
    'pname'      => "VARCHAR(50) NOT NULL DEFAULT ''",
    'compchng'   => "VARCHAR(5) NOT NULL DEFAULT '0'",
    'bankname'   => "VARCHAR(100) NOT NULL DEFAULT ''",
    'branchname' => "VARCHAR(100) NOT NULL DEFAULT ''",
    'accountno'  => "VARCHAR(50) NOT NULL DEFAULT ''",
    'ifsccode'   => "VARCHAR(20) NOT NULL DEFAULT ''",
    'gstno'      => "VARCHAR(20) NOT NULL DEFAULT ''",
    'panno'      => "VARCHAR(20) NOT NULL DEFAULT ''",
    'address'    => "VARCHAR(250) NOT NULL DEFAULT ''",
    'city'       => "VARCHAR(60) NOT NULL DEFAULT ''",
    'pincode'    => "VARCHAR(12) NOT NULL DEFAULT ''",
    'country'    => "VARCHAR(60) NOT NULL DEFAULT ''",
    'telephone'  => "VARCHAR(30) NOT NULL DEFAULT ''",
    'adduser'    => "VARCHAR(50) NOT NULL DEFAULT ''",
    'adddate'    => "DATE NULL DEFAULT NULL",
    'addtime'    => "TIME NULL DEFAULT NULL",
    'edituser'   => "VARCHAR(50) NOT NULL DEFAULT ''",
    'editdate'   => "DATE NULL DEFAULT NULL",
    'edittime'   => "TIME NULL DEFAULT NULL",
    // ── GST invoice details (used on the Sale GST tax invoice PDF/print) ─────
    'tradename'  => "VARCHAR(150) NOT NULL DEFAULT ''",
    'state'      => "VARCHAR(60) NOT NULL DEFAULT ''",
    'statecode'  => "VARCHAR(5) NOT NULL DEFAULT ''",
    'cin'        => "VARCHAR(30) NOT NULL DEFAULT ''",
    'email'      => "VARCHAR(100) NOT NULL DEFAULT ''",
    // GST-specific Bank Details + Address — a separate set from the General
    // tab's, since the bank account / registered address used on a GST
    // invoice is often different from the company's general one. Falls back
    // to the General tab's fields wherever left blank (see salegst_pdf.php).
    'gst_bankname'   => "VARCHAR(100) NOT NULL DEFAULT ''",
    'gst_branchname' => "VARCHAR(100) NOT NULL DEFAULT ''",
    'gst_accountno'  => "VARCHAR(50) NOT NULL DEFAULT ''",
    'gst_ifsccode'   => "VARCHAR(20) NOT NULL DEFAULT ''",
    'gst_address'    => "VARCHAR(250) NOT NULL DEFAULT ''",
    'gst_city'       => "VARCHAR(60) NOT NULL DEFAULT ''",
    'gst_pincode'    => "VARCHAR(12) NOT NULL DEFAULT ''",
    'gst_country'    => "VARCHAR(60) NOT NULL DEFAULT ''",
    'gst_telephone'  => "VARCHAR(30) NOT NULL DEFAULT ''",
] as $col => $def) {
    $chkCol = mysqli_query($conn, "SHOW COLUMNS FROM `company` LIKE '$col'");
    if ($chkCol && mysqli_num_rows($chkCol) == 0) {
        mysqli_query($conn, "ALTER TABLE `company` ADD COLUMN `$col` $def");
    }
}

include("includes/vouchno_generate.php");

// ── POST: Insert / Update ────────────────────────────────────────────────────
if (isset($_POST["submit"])) {
    $name       = mysqli_real_escape_string($conn, trim($_POST["name"]));
    $pname      = mysqli_real_escape_string($conn, trim($_POST["pname"]      ?? ''));
    $compchng   = isset($_POST["compchng"]) && $_POST["compchng"] == '1' ? '1' : '0';
    $bankname   = mysqli_real_escape_string($conn, trim($_POST["bankname"]   ?? ''));
    $branchname = mysqli_real_escape_string($conn, trim($_POST["branchname"] ?? ''));
    $accountno  = mysqli_real_escape_string($conn, trim($_POST["accountno"]  ?? ''));
    $ifsccode   = mysqli_real_escape_string($conn, trim($_POST["ifsccode"]   ?? ''));
    $gstno      = mysqli_real_escape_string($conn, trim($_POST["gstno"]      ?? ''));
    $panno      = mysqli_real_escape_string($conn, trim($_POST["panno"]      ?? ''));
    $address    = mysqli_real_escape_string($conn, trim($_POST["address"]    ?? ''));
    $city       = mysqli_real_escape_string($conn, trim($_POST["city"]       ?? ''));
    $pincode    = mysqli_real_escape_string($conn, trim($_POST["pincode"]    ?? ''));
    $country    = mysqli_real_escape_string($conn, trim($_POST["country"]    ?? ''));
    $telephone  = mysqli_real_escape_string($conn, trim($_POST["telephone"]  ?? ''));
    $tradename  = mysqli_real_escape_string($conn, trim($_POST["tradename"]  ?? ''));
    $state      = mysqli_real_escape_string($conn, trim($_POST["state"]      ?? ''));
    $statecode  = mysqli_real_escape_string($conn, trim($_POST["statecode"]  ?? ''));
    $cin        = mysqli_real_escape_string($conn, trim($_POST["cin"]        ?? ''));
    $email      = mysqli_real_escape_string($conn, trim($_POST["email"]      ?? ''));

    $gst_bankname   = mysqli_real_escape_string($conn, trim($_POST["gst_bankname"]   ?? ''));
    $gst_branchname = mysqli_real_escape_string($conn, trim($_POST["gst_branchname"] ?? ''));
    $gst_accountno  = mysqli_real_escape_string($conn, trim($_POST["gst_accountno"]  ?? ''));
    $gst_ifsccode   = mysqli_real_escape_string($conn, trim($_POST["gst_ifsccode"]   ?? ''));
    $gst_address    = mysqli_real_escape_string($conn, trim($_POST["gst_address"]    ?? ''));
    $gst_city       = mysqli_real_escape_string($conn, trim($_POST["gst_city"]       ?? ''));
    $gst_pincode    = mysqli_real_escape_string($conn, trim($_POST["gst_pincode"]    ?? ''));
    $gst_country    = mysqli_real_escape_string($conn, trim($_POST["gst_country"]    ?? ''));
    $gst_telephone  = mysqli_real_escape_string($conn, trim($_POST["gst_telephone"]  ?? ''));

    $a = 1;

    if ($name == '') {
        $a = 0;
        browse("Name Cannot Be Empty");
        header("refresh:1; url=$file?mode=last");
    }

    if ($a == 1) {
        $chkdup = mysqli_query($conn, "SELECT `code` FROM `$maindb` WHERE `name`='$name' AND `code`!='$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkdup) > 0) {
            $a = 0;
            browse("Company With This Name Already Exists");
            header("refresh:1; url=$file?mode=last");
        }
    }

    if ($a == 1) {
        $audituser = mysqli_real_escape_string($conn, audituser());

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`
                (`code`,`name`,`pname`,`compchng`,`bankname`,`branchname`,`accountno`,`ifsccode`,`gstno`,`panno`,
                 `address`,`city`,`pincode`,`country`,`telephone`,
                 `tradename`,`state`,`statecode`,`cin`,`email`,
                 `gst_bankname`,`gst_branchname`,`gst_accountno`,`gst_ifsccode`,
                 `gst_address`,`gst_city`,`gst_pincode`,`gst_country`,`gst_telephone`,
                 `adduser`,`adddate`,`addtime`)
                VALUES
                ('$qvouch','$name','$pname','$compchng','$bankname','$branchname','$accountno','$ifsccode','$gstno','$panno',
                 '$address','$city','$pincode','$country','$telephone',
                 '$tradename','$state','$statecode','$cin','$email',
                 '$gst_bankname','$gst_branchname','$gst_accountno','$gst_ifsccode',
                 '$gst_address','$gst_city','$gst_pincode','$gst_country','$gst_telephone',
                 '$audituser',CURDATE(),CURTIME())")
            or die(mysqli_error($conn));
            insmess();
            header("refresh:1; url=$file?$keyfld=$qvouch&mode=edit");
        } else {
            mysqli_query($conn, "UPDATE `$maindb` SET
                `name`='$name', `pname`='$pname', `compchng`='$compchng',
                `bankname`='$bankname', `branchname`='$branchname', `accountno`='$accountno',
                `ifsccode`='$ifsccode', `gstno`='$gstno', `panno`='$panno',
                `address`='$address', `city`='$city', `pincode`='$pincode', `country`='$country', `telephone`='$telephone',
                `tradename`='$tradename', `state`='$state', `statecode`='$statecode', `cin`='$cin', `email`='$email',
                `gst_bankname`='$gst_bankname', `gst_branchname`='$gst_branchname',
                `gst_accountno`='$gst_accountno', `gst_ifsccode`='$gst_ifsccode',
                `gst_address`='$gst_address', `gst_city`='$gst_city', `gst_pincode`='$gst_pincode', `gst_country`='$gst_country',
                `gst_telephone`='$gst_telephone',
                `edituser`='$audituser', `editdate`=CURDATE(), `edittime`=CURTIME()
                WHERE `code`='$qvouch'")
            or die(mysqli_error($conn));
            updmess();
            header("refresh:1; url=$file?$keyfld=$qvouch&mode=edit");
        }
    }
}

// ── Fetch record ─────────────────────────────────────────────────────────────
$get = [];
if ($addflag != 1 && $qvouch > 0) {
    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `$keyfld`='$qvouch'") or die(mysqli_error($conn));
    $get    = mysqli_fetch_array($result);
    if (!$get) $get = [];
}

// ── DELETE ───────────────────────────────────────────────────────────────────
if (isset($_GET["delete"])) {
    mysqli_query($conn, "DELETE FROM `$maindb` WHERE `$keyfld`='$qvouch'") or die(mysqli_error($conn));
    delmess();
    header("refresh:1; url=$file?mode=last");
    exit;
}

// Helper: get value safely
function fv($get, $key, $default = '') {
    return isset($get[$key]) ? htmlspecialchars($get[$key]) : $default;
}

include("includes/linkshead.php");
?>
<style>
    body { background:#f0f2f5; margin:0; padding:10px 14px; }

    .sim-card {
        background:#fff;
        border-radius:10px;
        box-shadow:0 1px 3px rgba(0,0,0,.08), 0 4px 16px rgba(0,0,0,.06);
        overflow:hidden;
    }

    .sim-card-header {
        background:#fff;
        border-bottom:2px solid #f1f5f9;
        padding:12px 20px;
        display:flex;
        align-items:center;
        justify-content:space-between;
    }
    .sim-card-header h2 {
        font-size:15px;
        font-weight:700;
        color:#1e293b;
        margin:0;
        letter-spacing:.01em;
    }
    .sim-badge {
        font-size:11px;
        background:#f1f5f9;
        color:#64748b;
        border-radius:20px;
        padding:3px 10px;
        font-weight:600;
    }

    /* ── In-page tabs (General / GST Details) — same single record, just
       split across two views so the form isn't one long scroll. ────────── */
    .cfg-tabs {
        display:flex;
        gap:4px;
        padding:10px 20px 0;
        background:#fff;
        border-bottom:1px solid #f1f5f9;
    }
    .cfg-tab-btn {
        padding:9px 18px;
        border:none;
        background:transparent;
        color:#64748b;
        font-size:12.5px;
        font-weight:700;
        cursor:pointer;
        border-radius:8px 8px 0 0;
        display:inline-flex;
        align-items:center;
        gap:6px;
        transition:background .15s, color .15s;
    }
    .cfg-tab-btn:hover { background:#f8fafc; }
    .cfg-tab-btn.active { background:#eff6ff; color:#2563eb; }
    .cfg-pane { display:none; }
    .cfg-pane.active { display:block; }

    .sim-body { padding:20px; }

    /* Form rows */
    .fr { display:flex; align-items:center; margin-bottom:9px; gap:0; }

    .fl { /* field wrapper */
        display:flex;
        align-items:center;
        flex:1;
    }

    .lbl {
        font-size:12px;
        font-weight:500;
        color:#64748b;
        white-space:nowrap;
        text-align:right;
        padding-right:10px;
        min-width:130px;
        flex-shrink:0;
    }

    .finp {
        flex:1;
        padding:7px 11px;
        background:#f8fafc;
        border:1px solid #e2e8f0;
        border-radius:6px;
        font-size:12.5px;
        color:#1e293b;
        outline:none;
        transition:border-color .15s, background .15s, box-shadow .15s;
        width:100%;
        box-sizing:border-box;
        font-family:inherit;
    }
    .finp:focus {
        background:#fff;
        border-color:#3b82f6;
        box-shadow:0 0 0 3px rgba(59,130,246,.12);
    }
    .finp:disabled {
        background:#f1f5f9;
        color:#94a3b8;
        cursor:not-allowed;
        border-color:#e2e8f0;
    }
    .finp.required-star { border-left:3px solid #3b82f6; }

    .fld-hint { font-size:10.5px; color:#94a3b8; margin-top:4px; margin-left:140px; margin-bottom:9px; }

    textarea.finp { resize:vertical; min-height:64px; font-family:inherit; line-height:1.4; }

    .fr-half { display:flex; align-items:center; gap:16px; margin-bottom:9px; }
    .fr-half .fl { flex:1; }
    .fr-half .lbl { min-width:50px; }

    /* Boxed section (fieldset-style), e.g. Address */
    fieldset.section-box {
        border:1px solid #e2e8f0;
        border-radius:8px;
        margin:18px 0 9px;
        padding:16px 16px 6px;
    }
    fieldset.section-box legend {
        font-size:12px;
        font-weight:700;
        color:#374151;
        padding:0 8px;
        margin-left:4px;
    }

    /* Button bar */
    .btn-bar {
        display:flex;
        justify-content:center;
        align-items:center;
        gap:8px;
        padding:16px 20px;
        border-top:1px solid #f1f5f9;
        background:#fafafa;
    }
    .abtn {
        width:46px;
        height:46px;
        border-radius:10px;
        border:none;
        cursor:pointer;
        font-size:15px;
        color:#fff;
        display:inline-flex;
        align-items:center;
        justify-content:center;
        text-decoration:none;
        transition:transform .12s, box-shadow .12s, filter .12s;
        position:relative;
    }
    .abtn:hover { transform:translateY(-2px); box-shadow:0 5px 14px rgba(0,0,0,.22); filter:brightness(1.06); }
    .abtn:active { transform:translateY(0); box-shadow:none; filter:brightness(.95); }
    .abtn[disabled] { opacity:.4; pointer-events:none; }

    .abtn-prev   { background:#2563eb; }
    .abtn-next   { background:#38bdf8; }
    .abtn-search { background:#db2777; }
    .abtn-add    { background:#16a34a; }
    .abtn-edit   { background:#ea580c; }
    .abtn-del    { background:#dc2626; }
    .abtn-exit   { background:#e2e8f0; color:#64748b; }

    /* tooltip on hover */
    .abtn::after {
        content: attr(data-tip);
        position:absolute;
        bottom:calc(100% + 6px);
        left:50%;
        transform:translateX(-50%);
        background:#1e293b;
        color:#fff;
        font-size:10px;
        font-weight:600;
        padding:3px 8px;
        border-radius:4px;
        white-space:nowrap;
        opacity:0;
        pointer-events:none;
        transition:opacity .15s;
    }
    .abtn:hover::after { opacity:1; }

    /* Save/Cancel extended buttons */
    .abtn-save   { width:auto; padding:0 18px; background:#16a34a; gap:6px; font-size:12.5px; font-weight:600; }
    .abtn-cancel { width:auto; padding:0 16px; background:#64748b; gap:6px; font-size:12.5px; font-weight:600; }

    /* Audit info footer */
    .audit-info {
        margin-top:14px;
        padding-top:10px;
        border-top:1px dashed #e2e8f0;
        display:flex;
        flex-wrap:wrap;
        gap:16px;
        font-size:11px;
        color:#94a3b8;
    }
    .audit-info i { margin-right:4px; }
    .audit-info b { color:#64748b; }
</style>

<body>
<form action="" method="POST">
<div class="sim-card">

    <!-- Header -->
    <div class="sim-card-header">
        <h2><i class="fa fa-building" style="color:#3b82f6;margin-right:8px;font-size:14px;"></i><?php echo $formname; ?></h2>
        <span class="sim-badge">Code: <?php echo $qvouch > 0 ? $qvouch : 'New'; ?></span>
    </div>

    <!-- Tabs: same record, just split into two views -->
    <div class="cfg-tabs">
        <button type="button" class="cfg-tab-btn active" data-pane="pane-general">
            <i class="fa fa-building"></i> General
        </button>
        <button type="button" class="cfg-tab-btn" data-pane="pane-gst">
            <i class="fa fa-file-invoice"></i> GST Details
        </button>
    </div>

    <div class="sim-body">

        <!-- ══ Tab: General ══════════════════════════════════════════════ -->
        <div id="pane-general" class="cfg-pane active">

            <!-- Name -->
            <div class="fr">
                <div class="fl">
                    <span class="lbl">Company Name <span style="color:#ef4444;">*</span></span>
                    <input type="text" name="name" class="finp form focusfield required-star" required
                        value="<?php echo fv($get,'name'); ?>">
                </div>
            </div>

            <!-- Short Name -->
            <div class="fr">
                <div class="fl">
                    <span class="lbl">Short Name</span>
                    <input type="text" name="pname" class="finp form" value="<?php echo fv($get,'pname'); ?>">
                </div>
            </div>

            <!-- Bank Details section -->
            <fieldset class="section-box">
                <legend>Bank Details</legend>

                <div class="fr">
                    <div class="fl">
                        <span class="lbl">Bank Name</span>
                        <input type="text" name="bankname" class="finp form" value="<?php echo fv($get,'bankname'); ?>">
                    </div>
                </div>

                <div class="fr">
                    <div class="fl">
                        <span class="lbl">Branch Name</span>
                        <input type="text" name="branchname" class="finp form" value="<?php echo fv($get,'branchname'); ?>">
                    </div>
                </div>

                <div class="fr-half">
                    <div class="fl">
                        <span class="lbl">Account No.</span>
                        <input type="text" name="accountno" class="finp form" value="<?php echo fv($get,'accountno'); ?>">
                    </div>
                    <div class="fl">
                        <span class="lbl">IFSC Code</span>
                        <input type="text" name="ifsccode" class="finp form" value="<?php echo fv($get,'ifsccode'); ?>">
                    </div>
                </div>
            </fieldset>

            <!-- Address section -->
            <fieldset class="section-box">
                <legend>Address</legend>

                <div class="fr">
                    <div class="fl">
                        <span class="lbl">Address</span>
                        <textarea name="address" class="finp form" rows="3"><?php echo fv($get,'address'); ?></textarea>
                    </div>
                </div>

                <div class="fr-half">
                    <div class="fl">
                        <span class="lbl">City</span>
                        <input type="text" name="city" class="finp form" value="<?php echo fv($get,'city'); ?>">
                    </div>
                    <div class="fl">
                        <span class="lbl">Pin</span>
                        <input type="text" name="pincode" class="finp form" value="<?php echo fv($get,'pincode'); ?>">
                    </div>
                </div>

                <div class="fr">
                    <div class="fl">
                        <span class="lbl">Country</span>
                        <input type="text" name="country" class="finp form" value="<?php echo fv($get,'country'); ?>">
                    </div>
                </div>

                <div class="fr">
                    <div class="fl">
                        <span class="lbl">Telephone</span>
                        <input type="text" name="telephone" class="finp form" value="<?php echo fv($get,'telephone'); ?>">
                    </div>
                </div>
            </fieldset>

        </div><!-- /pane-general -->

        <!-- ══ Tab: GST Details ══════════════════════════════════════════ -->
        <div id="pane-gst" class="cfg-pane">

            <!-- Trade Name -->
            <div class="fr">
                <div class="fl">
                    <span class="lbl">Trade Name</span>
                    <input type="text" name="tradename" class="finp form" value="<?php echo fv($get,'tradename'); ?>">
                </div>
            </div>
            <div class="fld-hint">Shown as the invoice header if different from the legal Company Name.</div>

            <div class="fr-half">
                <div class="fl">
                    <span class="lbl">GST No.</span>
                    <input type="text" name="gstno" class="finp form" value="<?php echo fv($get,'gstno'); ?>">
                </div>
                <div class="fl">
                    <span class="lbl">PAN No.</span>
                    <input type="text" name="panno" class="finp form" value="<?php echo fv($get,'panno'); ?>">
                </div>
            </div>

            <div class="fr-half">
                <div class="fl">
                    <span class="lbl">State</span>
                    <input type="text" name="state" class="finp form" value="<?php echo fv($get,'state'); ?>">
                </div>
                <div class="fl">
                    <span class="lbl">State Code</span>
                    <input type="text" name="statecode" class="finp form" maxlength="5" value="<?php echo fv($get,'statecode'); ?>">
                </div>
            </div>
            <div class="fld-hint">Determines CGST+SGST (same state) vs. IGST (different state) on GST invoices. Leave blank to fall back to the first 2 digits of the GSTIN.</div>

            <div class="fr-half">
                <div class="fl">
                    <span class="lbl">CIN</span>
                    <input type="text" name="cin" class="finp form" value="<?php echo fv($get,'cin'); ?>">
                </div>
                <div class="fl">
                    <span class="lbl">Email</span>
                    <input type="email" name="email" class="finp form" value="<?php echo fv($get,'email'); ?>">
                </div>
            </div>

            <div class="fr-half">
                <div class="fl">
                    <span class="lbl">Telephone</span>
                    <input type="text" name="gst_telephone" class="finp form" value="<?php echo fv($get,'gst_telephone'); ?>">
                </div>
                <div class="fl"></div>
            </div>
            <div class="fld-hint">Leave blank to use the General tab's Telephone on the GST invoice.</div>

            <!-- GST Bank Details section -->
            <fieldset class="section-box">
                <legend>Bank Details (GST Invoice)</legend>

                <div class="fr">
                    <div class="fl">
                        <span class="lbl">Bank Name</span>
                        <input type="text" name="gst_bankname" class="finp form" value="<?php echo fv($get,'gst_bankname'); ?>">
                    </div>
                </div>

                <div class="fr">
                    <div class="fl">
                        <span class="lbl">Branch Name</span>
                        <input type="text" name="gst_branchname" class="finp form" value="<?php echo fv($get,'gst_branchname'); ?>">
                    </div>
                </div>

                <div class="fr-half">
                    <div class="fl">
                        <span class="lbl">Account No.</span>
                        <input type="text" name="gst_accountno" class="finp form" value="<?php echo fv($get,'gst_accountno'); ?>">
                    </div>
                    <div class="fl">
                        <span class="lbl">IFSC Code</span>
                        <input type="text" name="gst_ifsccode" class="finp form" value="<?php echo fv($get,'gst_ifsccode'); ?>">
                    </div>
                </div>
            </fieldset>
            <div class="fld-hint">Leave blank to use the General tab's Bank Details on the GST invoice.</div>

            <!-- GST Address section -->
            <fieldset class="section-box">
                <legend>Address (GST Invoice)</legend>

                <div class="fr">
                    <div class="fl">
                        <span class="lbl">Address</span>
                        <textarea name="gst_address" class="finp form" rows="3"><?php echo fv($get,'gst_address'); ?></textarea>
                    </div>
                </div>

                <div class="fr-half">
                    <div class="fl">
                        <span class="lbl">City</span>
                        <input type="text" name="gst_city" class="finp form" value="<?php echo fv($get,'gst_city'); ?>">
                    </div>
                    <div class="fl">
                        <span class="lbl">Pin</span>
                        <input type="text" name="gst_pincode" class="finp form" value="<?php echo fv($get,'gst_pincode'); ?>">
                    </div>
                </div>

                <div class="fr">
                    <div class="fl">
                        <span class="lbl">Country</span>
                        <input type="text" name="gst_country" class="finp form" value="<?php echo fv($get,'gst_country'); ?>">
                    </div>
                </div>
            </fieldset>
            <div class="fld-hint">Leave blank to use the General tab's Address on the GST invoice.</div>

        </div><!-- /pane-gst -->

        <!-- Audit Info -->
        <?php if ($qvouch > 0 && (fv($get,'adduser') != '' || fv($get,'edituser') != '')): ?>
        <div class="audit-info">
            <?php if (fv($get,'adduser') != ''): ?>
            <span><i class="fa fa-plus-circle"></i> Added by <b><?php echo fv($get,'adduser'); ?></b> on <?php echo fv($get,'adddate'); ?> <?php echo fv($get,'addtime'); ?></span>
            <?php endif; ?>
            <?php if (fv($get,'edituser') != ''): ?>
            <span><i class="fa fa-pen"></i> Last edited by <b><?php echo fv($get,'edituser'); ?></b> on <?php echo fv($get,'editdate'); ?> <?php echo fv($get,'edittime'); ?></span>
            <?php endif; ?>
        </div>
        <?php endif; ?>

    </div><!-- /sim-body -->

    <!-- Action Button Bar -->
    <div class="btn-bar">

        <!-- Browse buttons (hidden in add mode) -->
        <button id="hedit" type="button" class="abtn abtn-edit buttonform"
            style="display:<?php echo $editdisabled; ?>;"
            data-tip="Edit">
            <i class="fa fa-pencil"></i>
        </button>

        <button id="hexit" type="button" class="abtn abtn-exit buttonform"
            style="display:<?php echo $exitdisabled; ?>;"
            data-tip="Exit"
            onclick="window.location.href='default.php'">
            <i class="fa fa-power-off"></i>
        </button>

        <!-- Save / Cancel (hidden in browse mode) -->
        <button id="ssubmit" type="submit" name="submit"
            value="<?php echo ($addflag == 1) ? 'Save' : 'Update'; ?>"
            class="abtn abtn-save buttonform"
            data-tip="<?php echo ($addflag == 1) ? 'Save' : 'Update'; ?>"
            style="display:<?php echo $savedisabled; ?>;">
            <i class="fa fa-check"></i>
            <?php echo ($addflag == 1) ? 'Save' : 'Update'; ?>
        </button>

        <button id="scancel" type="button"
            class="abtn abtn-cancel buttonform"
            data-tip="Cancel"
            style="display:<?php echo $canceldisabled; ?>;"
            onclick="window.location.href='<?php echo $file . '?mode=last'; ?>'">
            <i class="fa fa-times"></i> Cancel
        </button>

    </div>

</div><!-- /sim-card -->
</form>

<script>
    // Tab switch is purely visual — both panes belong to the same <form>, so
    // Save/Update always submits every field regardless of which tab is showing.
    document.querySelectorAll('.cfg-tab-btn').forEach(function (btn) {
        btn.addEventListener('click', function () {
            document.querySelectorAll('.cfg-tab-btn').forEach(function (b) { b.classList.remove('active'); });
            document.querySelectorAll('.cfg-pane').forEach(function (p) { p.classList.remove('active'); });
            this.classList.add('active');
            document.getElementById(this.getAttribute('data-pane')).classList.add('active');
        });
    });
</script>

<?php include("includes/linksfoot.php"); ?>
</body>
</html>
