<?php

namespace PHPMaker2021\rdigital21;

// Page object
$TemaAdd = &$Page;
?>
<script>
var currentForm, currentPageID;
var ftemaadd;
loadjs.ready("head", function () {
    var $ = jQuery;
    // Form object
    currentPageID = ew.PAGE_ID = "add";
    ftemaadd = currentForm = new ew.Form("ftemaadd", "add");

    // Add fields
    var currentTable = <?= JsonEncode(GetClientVar("tables", "tema")) ?>,
        fields = currentTable.fields;
    if (!ew.vars.tables.tema)
        ew.vars.tables.tema = currentTable;
    ftemaadd.addFields([
        ["tema", [fields.tema.visible && fields.tema.required ? ew.Validators.required(fields.tema.caption) : null], fields.tema.isInvalid],
        ["dimensi", [fields.dimensi.visible && fields.dimensi.required ? ew.Validators.required(fields.dimensi.caption) : null], fields.dimensi.isInvalid]
    ]);

    // Set invalid fields
    $(function() {
        var f = ftemaadd,
            fobj = f.getForm(),
            $fobj = $(fobj),
            $k = $fobj.find("#" + f.formKeyCountName), // Get key_count
            rowcnt = ($k[0]) ? parseInt($k.val(), 10) : 1,
            startcnt = (rowcnt == 0) ? 0 : 1; // Check rowcnt == 0 => Inline-Add
        for (var i = startcnt; i <= rowcnt; i++) {
            var rowIndex = ($k[0]) ? String(i) : "";
            f.setInvalid(rowIndex);
        }
    });

    // Validate form
    ftemaadd.validate = function () {
        if (!this.validateRequired)
            return true; // Ignore validation
        var fobj = this.getForm(),
            $fobj = $(fobj);
        if ($fobj.find("#confirm").val() == "confirm")
            return true;
        var addcnt = 0,
            $k = $fobj.find("#" + this.formKeyCountName), // Get key_count
            rowcnt = ($k[0]) ? parseInt($k.val(), 10) : 1,
            startcnt = (rowcnt == 0) ? 0 : 1, // Check rowcnt == 0 => Inline-Add
            gridinsert = ["insert", "gridinsert"].includes($fobj.find("#action").val()) && $k[0];
        for (var i = startcnt; i <= rowcnt; i++) {
            var rowIndex = ($k[0]) ? String(i) : "";
            $fobj.data("rowindex", rowIndex);

            // Validate fields
            if (!this.validateFields(rowIndex))
                return false;

            // Call Form_CustomValidate event
            if (!this.customValidate(fobj)) {
                this.focus();
                return false;
            }
        }

        // Process detail forms
        var dfs = $fobj.find("input[name='detailpage']").get();
        for (var i = 0; i < dfs.length; i++) {
            var df = dfs[i],
                val = df.value,
                frm = ew.forms.get(val);
            if (val && frm && !frm.validate())
                return false;
        }
        return true;
    }

    // Form_CustomValidate
    ftemaadd.customValidate = function(fobj) { // DO NOT CHANGE THIS LINE!
        // Your custom validation code here, return false if invalid.
        return true;
    }

    // Use JavaScript validation or not
    ftemaadd.validateRequired = <?= Config("CLIENT_VALIDATE") ? "true" : "false" ?>;

    // Dynamic selection lists
    loadjs.done("ftemaadd");
});
</script>
<script>
loadjs.ready("head", function () {
    // Write your table-specific client script here, no need to add script tags.
});
</script>
<?php $Page->showPageHeader(); ?>
<?php
$Page->showMessage();
?>
<form name="ftemaadd" id="ftemaadd" class="<?= $Page->FormClassName ?>" action="<?= CurrentPageUrl(false) ?>" method="post">
<?php if (Config("CHECK_TOKEN")) { ?>
<input type="hidden" name="<?= $TokenNameKey ?>" value="<?= $TokenName ?>"><!-- CSRF token name -->
<input type="hidden" name="<?= $TokenValueKey ?>" value="<?= $TokenValue ?>"><!-- CSRF token value -->
<?php } ?>
<input type="hidden" name="t" value="tema">
<input type="hidden" name="action" id="action" value="insert">
<input type="hidden" name="modal" value="<?= (int)$Page->IsModal ?>">
<input type="hidden" name="<?= $Page->OldKeyName ?>" value="<?= $Page->OldKey ?>">
<div class="ew-add-div"><!-- page* -->
<?php if ($Page->tema->Visible) { // tema ?>
    <div id="r_tema" class="form-group row">
        <label id="elh_tema_tema" for="x_tema" class="<?= $Page->LeftColumnClass ?>"><?= $Page->tema->caption() ?><?= $Page->tema->Required ? $Language->phrase("FieldRequiredIndicator") : "" ?></label>
        <div class="<?= $Page->RightColumnClass ?>"><div <?= $Page->tema->cellAttributes() ?>>
<span id="el_tema_tema">
<input type="<?= $Page->tema->getInputTextType() ?>" data-table="tema" data-field="x_tema" name="x_tema" id="x_tema" size="30" maxlength="255" value="<?= $Page->tema->EditValue ?>"<?= $Page->tema->editAttributes() ?> aria-describedby="x_tema_help">
<?= $Page->tema->getCustomMessage() ?>
<div class="invalid-feedback"><?= $Page->tema->getErrorMessage() ?></div>
</span>
</div></div>
    </div>
<?php } ?>
<?php if ($Page->dimensi->Visible) { // dimensi ?>
    <div id="r_dimensi" class="form-group row">
        <label id="elh_tema_dimensi" for="x_dimensi" class="<?= $Page->LeftColumnClass ?>"><?= $Page->dimensi->caption() ?><?= $Page->dimensi->Required ? $Language->phrase("FieldRequiredIndicator") : "" ?></label>
        <div class="<?= $Page->RightColumnClass ?>"><div <?= $Page->dimensi->cellAttributes() ?>>
<span id="el_tema_dimensi">
<textarea data-table="tema" data-field="x_dimensi" name="x_dimensi" id="x_dimensi" cols="35" rows="4"<?= $Page->dimensi->editAttributes() ?> aria-describedby="x_dimensi_help"><?= $Page->dimensi->EditValue ?></textarea>
<?= $Page->dimensi->getCustomMessage() ?>
<div class="invalid-feedback"><?= $Page->dimensi->getErrorMessage() ?></div>
</span>
</div></div>
    </div>
<?php } ?>
</div><!-- /page* -->
<?php if (!$Page->IsModal) { ?>
<div class="form-group row"><!-- buttons .form-group -->
    <div class="<?= $Page->OffsetColumnClass ?>"><!-- buttons offset -->
<button class="btn btn-primary ew-btn" name="btn-action" id="btn-action" type="submit"><?= $Language->phrase("AddBtn") ?></button>
<button class="btn btn-default ew-btn" name="btn-cancel" id="btn-cancel" type="button" data-href="<?= HtmlEncode(GetUrl($Page->getReturnUrl())) ?>"><?= $Language->phrase("CancelBtn") ?></button>
    </div><!-- /buttons offset -->
</div><!-- /buttons .form-group -->
<?php } ?>
</form>
<?php
$Page->showPageFooter();
echo GetDebugMessage();
?>
<script>
// Field event handlers
loadjs.ready("head", function() {
    ew.addEventHandlers("tema");
});
</script>
<script>
loadjs.ready("load", function () {
    // Write your table-specific startup script here, no need to add script tags.
});
</script>
