<?php
session_start();
$rootDb = __DIR__ . '/db.php';
require_once $rootDb;

$org = null;
$sessionOrgId = $_SESSION['org_id'] ?? null;
if ($sessionOrgId !== null && $sessionOrgId !== '') {
    $stmt = $conn->prepare("SELECT * FROM organizations WHERE id = ? OR org_id = ? ORDER BY id DESC LIMIT 1");
    if ($stmt) {
        $sessionOrgIdString = (string) $sessionOrgId;
        $sessionOrgIdInt = ctype_digit($sessionOrgIdString) ? (int) $sessionOrgIdString : 0;
        $stmt->bind_param('is', $sessionOrgIdInt, $sessionOrgIdString);
        $stmt->execute();
        $result = $stmt->get_result();
        $org = $result ? $result->fetch_assoc() : null;
        $stmt->close();
    }
}
if (!$org) {
    $result = $conn->query("SELECT * FROM organizations ORDER BY id DESC LIMIT 1");
    $org = $result ? $result->fetch_assoc() : null;
}

$errors = $_SESSION['org_errors'] ?? [];
$old = $_SESSION['org_old'] ?? [];
unset($_SESSION['org_errors'], $_SESSION['org_old']);
$show_errors = !empty($errors);
$user_company = $org['organisation_name'] ?? ($_SESSION['org_name'] ?? ($_SESSION['user_company'] ?? 'there'));
$organisationNameValue = $old['org_name'] ?? ($org['organisation_name'] ?? '');
$businessLocationValue = $old['location'] ?? ($org['business_location'] ?? 'India');
$industryValue = $old['industry'] ?? ($org['industry'] ?? 'Technology');
$address1Value = $old['address1'] ?? ($org['address_line1'] ?? '');
$address2Value = $old['address2'] ?? ($org['address_line2'] ?? '');
$stateValue = $old['state'] ?? ($org['state'] ?? '');
$cityValue = $old['city'] ?? ($org['city'] ?? '');
$pincodeValue = $old['pincode'] ?? ($org['pincode'] ?? '');
$payroll_value = $old['payroll'] ?? ($org['payroll_run'] ?? '');
$radio_invalid = isset($errors['payroll']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Organisation Setup</title>
<link rel="stylesheet" href="org.css">
<link rel="stylesheet" href="assets/font-manrope.css"></head>

<body class="org-page">
<main class="org-layout">
    <section class="org-left">
        <div class="brand-row">
            <img src="images/b2l_mobitech_pvt_ltd_logo.jpg" alt="B2L Mobitech Pvt Ltd" class="brand-logo">
        </div>

        <p class="greeting">Hi <?php echo htmlspecialchars($user_company); ?>! Let's get started</p>
        <h1>Tell us a bit about your organisation</h1>

        <div class="illustration-card">
            <img src="images/building-633c8f26cf12193d24be343b23b68517.svg" alt="Organisation illustration" class="illustration-img">
        </div>
    </section>

    <section class="org-right">
        <form action="save_org.php" method="POST" class="org-form<?php echo $show_errors ? ' show-errors' : ''; ?>" novalidate>
            <div class="form-row<?php echo isset($errors['org_name']) ? ' has-error' : ''; ?>">
                <label>Organisation Name<span class="req">*</span><span class="info-icon" title="Official registered name">i</span></label>
                <input type="text" name="org_name" placeholder="Organisation Name" value="<?php echo htmlspecialchars($organisationNameValue); ?>" required>
                <p class="field-error"><?php echo htmlspecialchars($errors['org_name'] ?? 'Please enter the organisation name.'); ?></p>
            </div>

            <div class="form-row two-col">
                <div class="<?php echo isset($errors['location']) ? 'has-error' : ''; ?>">
                    <label>Business Location<span class="req">*</span></label>
                    <input type="text" name="location" value="<?php echo htmlspecialchars($businessLocationValue); ?>" required>
                    <p class="field-error"><?php echo htmlspecialchars($errors['location'] ?? 'Please enter the business location.'); ?></p>
                </div>
                <div class="<?php echo isset($errors['industry']) ? 'has-error' : ''; ?>">
                    <label>Industry<span class="req">*</span></label>
                    <select name="industry" required>
                        <option value="" disabled <?php echo empty($industryValue) ? 'selected' : ''; ?>>Select an industry type</option>
                        <option <?php echo $industryValue === 'IT' ? 'selected' : ''; ?>>IT</option>
                        <option <?php echo $industryValue === 'Finance' ? 'selected' : ''; ?>>Finance</option>
                        <option <?php echo $industryValue === 'Manufacturing' ? 'selected' : ''; ?>>Manufacturing</option>
                        <option <?php echo $industryValue === 'Education' ? 'selected' : ''; ?>>Education</option>
                    </select>
                    <p class="field-error"><?php echo htmlspecialchars($errors['industry'] ?? 'Please select the type of industry.'); ?></p>
                </div>
            </div>

            <div class="form-row<?php echo isset($errors['address1']) ? ' has-error' : ''; ?>">
                <label>Organisation Address<span class="req">*</span></label>
                <p class="hint">This will be considered as the address of your primary work location.</p>
                <input type="text" name="address1" placeholder="Address Line 1" value="<?php echo htmlspecialchars($address1Value); ?>" required>
                <p class="field-error"><?php echo htmlspecialchars($errors['address1'] ?? 'Enter Address Line 1.'); ?></p>
            </div>

            <div class="form-row">
                <input type="text" name="address2" placeholder="Address Line 2" value="<?php echo htmlspecialchars($address2Value); ?>">
            </div>

            <div class="form-row three-col">
                <div class="<?php echo isset($errors['state']) ? 'has-error' : ''; ?>">
                    <label>State<span class="req">*</span></label>
                    <select name="state" class="state-select" required>
                        <option value="" disabled <?php echo empty($stateValue) ? 'selected' : ''; ?>>Select State</option>
                        <option value="Andhra Pradesh" <?php echo $stateValue === 'Andhra Pradesh' ? 'selected' : ''; ?>>Andhra Pradesh</option>
                        <option value="Arunachal Pradesh" <?php echo $stateValue === 'Arunachal Pradesh' ? 'selected' : ''; ?>>Arunachal Pradesh</option>
                        <option value="Assam" <?php echo $stateValue === 'Assam' ? 'selected' : ''; ?>>Assam</option>
                        <option value="Bihar" <?php echo $stateValue === 'Bihar' ? 'selected' : ''; ?>>Bihar</option>
                        <option value="Chhattisgarh" <?php echo $stateValue === 'Chhattisgarh' ? 'selected' : ''; ?>>Chhattisgarh</option>
                        <option value="Goa" <?php echo $stateValue === 'Goa' ? 'selected' : ''; ?>>Goa</option>
                        <option value="Gujarat" <?php echo $stateValue === 'Gujarat' ? 'selected' : ''; ?>>Gujarat</option>
                        <option value="Haryana" <?php echo $stateValue === 'Haryana' ? 'selected' : ''; ?>>Haryana</option>
                        <option value="Himachal Pradesh" <?php echo $stateValue === 'Himachal Pradesh' ? 'selected' : ''; ?>>Himachal Pradesh</option>
                        <option value="Jharkhand" <?php echo $stateValue === 'Jharkhand' ? 'selected' : ''; ?>>Jharkhand</option>
                        <option value="Karnataka" <?php echo $stateValue === 'Karnataka' ? 'selected' : ''; ?>>Karnataka</option>
                        <option value="Kerala" <?php echo $stateValue === 'Kerala' ? 'selected' : ''; ?>>Kerala</option>
                        <option value="Madhya Pradesh" <?php echo $stateValue === 'Madhya Pradesh' ? 'selected' : ''; ?>>Madhya Pradesh</option>
                        <option value="Maharashtra" <?php echo $stateValue === 'Maharashtra' ? 'selected' : ''; ?>>Maharashtra</option>
                        <option value="Manipur" <?php echo $stateValue === 'Manipur' ? 'selected' : ''; ?>>Manipur</option>
                        <option value="Meghalaya" <?php echo $stateValue === 'Meghalaya' ? 'selected' : ''; ?>>Meghalaya</option>
                        <option value="Mizoram" <?php echo $stateValue === 'Mizoram' ? 'selected' : ''; ?>>Mizoram</option>
                        <option value="Nagaland" <?php echo $stateValue === 'Nagaland' ? 'selected' : ''; ?>>Nagaland</option>
                        <option value="Odisha" <?php echo $stateValue === 'Odisha' ? 'selected' : ''; ?>>Odisha</option>
                        <option value="Punjab" <?php echo $stateValue === 'Punjab' ? 'selected' : ''; ?>>Punjab</option>
                        <option value="Rajasthan" <?php echo $stateValue === 'Rajasthan' ? 'selected' : ''; ?>>Rajasthan</option>
                        <option value="Sikkim" <?php echo $stateValue === 'Sikkim' ? 'selected' : ''; ?>>Sikkim</option>
                        <option value="Tamil Nadu" <?php echo $stateValue === 'Tamil Nadu' ? 'selected' : ''; ?>>Tamil Nadu</option>
                        <option value="Telangana" <?php echo $stateValue === 'Telangana' ? 'selected' : ''; ?>>Telangana</option>
                        <option value="Tripura" <?php echo $stateValue === 'Tripura' ? 'selected' : ''; ?>>Tripura</option>
                        <option value="Uttar Pradesh" <?php echo $stateValue === 'Uttar Pradesh' ? 'selected' : ''; ?>>Uttar Pradesh</option>
                        <option value="Uttarakhand" <?php echo $stateValue === 'Uttarakhand' ? 'selected' : ''; ?>>Uttarakhand</option>
                        <option value="West Bengal" <?php echo $stateValue === 'West Bengal' ? 'selected' : ''; ?>>West Bengal</option>
                        <option value="Delhi" <?php echo $stateValue === 'Delhi' ? 'selected' : ''; ?>>Delhi</option>
                        <option value="Jammu and Kashmir" <?php echo $stateValue === 'Jammu and Kashmir' ? 'selected' : ''; ?>>Jammu and Kashmir</option>
                        <option value="Ladakh" <?php echo $stateValue === 'Ladakh' ? 'selected' : ''; ?>>Ladakh</option>
                        <option value="Puducherry" <?php echo $stateValue === 'Puducherry' ? 'selected' : ''; ?>>Puducherry</option>
                        <option value="Chandigarh" <?php echo $stateValue === 'Chandigarh' ? 'selected' : ''; ?>>Chandigarh</option>
                        <option value="Andaman and Nicobar Islands" <?php echo $stateValue === 'Andaman and Nicobar Islands' ? 'selected' : ''; ?>>Andaman and Nicobar Islands</option>
                        <option value="Dadra and Nagar Haveli" <?php echo $stateValue === 'Dadra and Nagar Haveli' ? 'selected' : ''; ?>>Dadra and Nagar Haveli</option>
                        <option value="Daman and Diu" <?php echo $stateValue === 'Daman and Diu' ? 'selected' : ''; ?>>Daman and Diu</option>
                        <option value="Lakshadweep" <?php echo $stateValue === 'Lakshadweep' ? 'selected' : ''; ?>>Lakshadweep</option>
                    </select>
                    <p class="field-error"><?php echo htmlspecialchars($errors['state'] ?? 'Select the State in which you run your business.'); ?></p>
                </div>
                <div>
                    <label>City</label>
                    <input type="text" name="city" placeholder="City" value="<?php echo htmlspecialchars($cityValue); ?>">
                </div>
                <div>
                    <label>PIN Code</label>
                    <input type="text" name="pincode" placeholder="PIN Code" value="<?php echo htmlspecialchars($pincodeValue); ?>">
                </div>
            </div>

            <div class="form-row radio-group<?php echo $radio_invalid ? ' is-invalid has-error' : ''; ?>">
                <p class="radio-title">Have you run payroll earlier this financial year?<span class="req">*</span></p>
                <div class="radio-option">
                    <input id="payroll_yes" class="radio-input" type="radio" name="payroll" value="yes" <?php echo $payroll_value === 'yes' ? 'checked' : ''; ?> required>
                    <label class="radio-card" for="payroll_yes">Yes, we've already run payroll(s) for this financial year.</label>
                    <p class="radio-help<?php echo $payroll_value === 'yes' ? ' is-alert' : ''; ?>" id="payrollHelp">
                        You'll have to record all previous payments made to your employees before your first pay run in B2L Mobitech.
                        This ensures accurate YTD information while we prepare your forms for you.
                    </p>
                </div>
                <div class="radio-row">
                    <input id="payroll_no" class="radio-input" type="radio" name="payroll" value="no" <?php echo $payroll_value === 'no' ? 'checked' : ''; ?> required>
                    <label class="radio-card" for="payroll_no">No, we'll run this financial year's first payroll.</label>
                </div>
                <p class="field-error"><?php echo htmlspecialchars($errors['payroll'] ?? 'Mention if you have already run payrolls (using a different payroll provider) for this financial year.'); ?></p>
            </div>

            <button type="submit" class="primary-btn">Save & Continue</button>
        </form>
    </section>
</main>
</body>
</html>


