<?php
/**
 * ============================================================
 * ace_stem_form.php
 * ------------------------------------------------------------
 * HTML form for ACE — Stem Tester
 * This file expects variables to already exist:
 *   $lang, $form, $tense, $r0, $r1, $r2,
 *   $infl1, $infl2, $pronIdx,
 *   $conjSel, $partSel, $objSel, $declSel
 *
 * And also expects these arrays to be loaded:
 *   $conjunctions, $particle40, $OBJ_PRO, $DECL_TIT
 * ============================================================
 */
?>

<form method="get" action="ace_stem_tester.php" class="ace-form">

    <h2>ACE — Stem Tester</h2>

    <!-- ============================
         Language
         ============================ -->
    <div class="ace-form-row">
        <label>Language:</label>
        <select name="lang">
            <option value="English" <?= $lang==='English'?'selected':''; ?>>English</option>
            <option value="Urdu"    <?= $lang==='Urdu'   ?'selected':''; ?>>Urdu</option>
        </select>
    </div>

    <!-- ============================
         Root letters
         ============================ -->
    <div class="ace-form-row">
        <label>Root:</label>
        <input type="text" name="r0" size="1" value="<?= htmlspecialchars($r0) ?>">
        <input type="text" name="r1" size="1" value="<?= htmlspecialchars($r1) ?>">
        <input type="text" name="r2" size="1" value="<?= htmlspecialchars($r2) ?>">
    </div>

    <!-- ============================
         Form & Tense
         ============================ -->
    <div class="ace-form-row">
        <label>Form:</label>
        <select name="form">
            <?php for ($i=1; $i<=10; $i++): ?>
                <option value="<?= $i ?>" <?= $form===$i?'selected':''; ?>>
                    Form <?= $i ?>
                </option>
            <?php endfor; ?>
        </select>

        <label>Tense:</label>
        <select name="tense">
            <?php for ($i=1; $i<=14; $i++): ?>
                <option value="<?= $i ?>" <?= $tense===$i?'selected':''; ?>>
                    <?= $i ?>
                </option>
            <?php endfor; ?>
        </select>
    </div>

    <!-- ============================
         V1 / V2
         ============================ -->
    <div class="ace-form-row">
        <label>V1 / V2:</label>

        <select name="infl1">
            <option value=""  <?= $infl1===''?'selected':''; ?>>&nbsp;</option>
            <option value="a" <?= $infl1==='a'?'selected':''; ?>>a (fatḥa)</option>
            <option value="i" <?= $infl1==='i'?'selected':''; ?>>i (kasra)</option>
            <option value="u" <?= $infl1==='u'?'selected':''; ?>>u (ḍamma)</option>
        </select>

        <select name="infl2">
            <option value=""  <?= $infl2===''?'selected':''; ?>>&nbsp;</option>
            <option value="a" <?= $infl2==='a'?'selected':''; ?>>a (fatḥa)</option>
            <option value="i" <?= $infl2==='i'?'selected':''; ?>>i (kasra)</option>
            <option value="u" <?= $infl2==='u'?'selected':''; ?>>u (ḍamma)</option>
        </select>
    </div>

    <!-- ============================
         Pronoun
         ============================ -->
    <div class="ace-form-row">
        <label>Pronoun #:</label>
        <select name="pronIdx">
            <?php for ($i=1; $i<=14; $i++): ?>
                <option value="<?= $i ?>" <?= $pronIdx===$i?'selected':''; ?>>
                    <?= $i ?>
                </option>
            <?php endfor; ?>
        </select>
    </div>

    <!-- ============================
         Conjunction with titles
         ============================ -->
    <div class="ace-form-row">
        <label>Conjunction:</label>
        <select name="conj">
            <option value="">(none)</option>

            <?php if (!empty($conjunctions)): ?>
                <?php foreach ($conjunctions as $idx => $c): ?>
                    <?php if (!is_array($c) || empty($c['latin'])) continue; ?>

                    <?php
                        $value = $c['latin'];
                        $label = $idx.'. '.$c['arabic'].' | '.$c['latin'].' — '.$c['english'].' / '.$c['urdu'];
                        $sel   = ($conjSel===$value) ? 'selected' : '';
                    ?>

                    <option value="<?= htmlspecialchars($value) ?>" <?= $sel ?>>
                        <?= htmlspecialchars($label) ?>
                    </option>
                <?php endforeach; ?>
            <?php endif; ?>

        </select>
    </div>

    <!-- ============================
         Particle with titles
         ============================ -->
    <div class="ace-form-row">
        <label>Particle:</label>
        <select name="part">
            <option value="">(none)</option>

            <?php if (!empty($particle40)): ?>
                <?php foreach ($particle40 as $idx => $p): ?>
                    <?php if (!is_array($p) || empty($p['latin'])) continue; ?>

                    <?php
                        $value = $p['latin'];
                        $label = $idx.'. '.$p['arabic'].' | '.$p['latin'].' — '.$p['english'].' / '.$p['urdu'];
                        $sel   = ($partSel===$value) ? 'selected' : '';
                    ?>

                    <option value="<?= htmlspecialchars($value) ?>" <?= $sel ?>>
                        <?= htmlspecialchars($label) ?>
                    </option>

                <?php endforeach; ?>
            <?php endif; ?>

        </select>
    </div>

    <!-- ============================
         Object Pronoun (optional)
         ============================ -->
    <div class="ace-form-row">
        <label>Object Pronoun:</label>
        <select name="objpro">
            <option value="">(none)</option>

            <?php if (!empty($OBJ_PRO)): ?>
                <?php foreach ($OBJ_PRO as $key => $o): ?>
                    <?php if (!is_array($o)) continue; ?>

                    <?php
                        $value = $o['latin'] ?? '';
                        $label = $o['arabic'].' | '.$o['latin'].' — '.$o['english'].' / '.$o['urdu'];
                        $sel   = ($objSel===$value) ? 'selected' : '';
                    ?>

                    <option value="<?= htmlspecialchars($value) ?>" <?= $sel ?>>
                        <?= htmlspecialchars($label) ?>
                    </option>

                <?php endforeach; ?>
            <?php endif; ?>

        </select>
    </div>

    <!-- ============================
         Declension (optional)
         ============================ -->
    <div class="ace-form-row">
        <label>Declension:</label>
        <select name="decl">
            <option value="">(none)</option>

            <?php if (!empty($DECL_TIT)): ?>
                <?php foreach ($DECL_TIT as $key => $label): ?>
                    <?php if (!$key || !is_string($label)) continue; ?>

                    <?php
                        $sel = ($declSel===(string)$key) ? 'selected' : '';
                    ?>

                    <option value="<?= $key ?>" <?= $sel ?>>
                        <?= htmlspecialchars($label) ?>
                    </option>

                <?php endforeach; ?>
            <?php endif; ?>

        </select>
    </div>

    <!-- ============================
         Submit
         ============================ -->
    <div class="ace-form-row">
        <button type="submit">Analyze</button>
    </div>

</form>
