<?php

namespace PHPMaker2021\gudang_ubi;

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

    // Add fields
    var fields = ew.vars.tables.varietas.fields;
    fvarietasadd.addFields([
        ["var_nama", [fields.var_nama.required ? ew.Validators.required(fields.var_nama.caption) : null], fields.var_nama.isInvalid],
        ["var_foto", [fields.var_foto.required ? ew.Validators.fileRequired(fields.var_foto.caption) : null], fields.var_foto.isInvalid]
    ]);

    // Set invalid fields
    $(function() {
        var f = fvarietasadd,
            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
    fvarietasadd.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
    fvarietasadd.customValidate = function(fobj) { // DO NOT CHANGE THIS LINE!
        // Your custom validation code here, return false if invalid.
        return true;
    }

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

    // Dynamic selection lists
    loadjs.done("fvarietasadd");
});
</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="fvarietasadd" id="fvarietasadd" class="<?= $Page->FormClassName ?>" action="<?= CurrentPageUrl() ?>" 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="varietas">
<input type="hidden" name="action" id="action" value="insert">
<input type="hidden" name="modal" value="<?= (int)$Page->IsModal ?>">
<div class="ew-add-div"><!-- page* -->
<?php if ($Page->var_nama->Visible) { // var_nama ?>
    <div id="r_var_nama" class="form-group row">
        <label id="elh_varietas_var_nama" for="x_var_nama" class="<?= $Page->LeftColumnClass ?>"><?= $Page->var_nama->caption() ?><?= $Page->var_nama->Required ? $Language->phrase("FieldRequiredIndicator") : "" ?></label>
        <div class="<?= $Page->RightColumnClass ?>"><div <?= $Page->var_nama->cellAttributes() ?>>
<span id="el_varietas_var_nama">
<input type="<?= $Page->var_nama->getInputTextType() ?>" data-table="varietas" data-field="x_var_nama" name="x_var_nama" id="x_var_nama" size="30" maxlength="40" placeholder="<?= HtmlEncode($Page->var_nama->getPlaceHolder()) ?>" value="<?= $Page->var_nama->EditValue ?>"<?= $Page->var_nama->editAttributes() ?> aria-describedby="x_var_nama_help">
<?= $Page->var_nama->getCustomMessage() ?>
<div class="invalid-feedback"><?= $Page->var_nama->getErrorMessage() ?></div>
</span>
</div></div>
    </div>
<?php } ?>
<?php if ($Page->var_foto->Visible) { // var_foto ?>
    <div id="r_var_foto" class="form-group row">
        <label id="elh_varietas_var_foto" class="<?= $Page->LeftColumnClass ?>"><?= $Page->var_foto->caption() ?><?= $Page->var_foto->Required ? $Language->phrase("FieldRequiredIndicator") : "" ?></label>
        <div class="<?= $Page->RightColumnClass ?>"><div <?= $Page->var_foto->cellAttributes() ?>>
<span id="el_varietas_var_foto">
<div id="fd_x_var_foto">
<div class="input-group">
    <div class="custom-file">
        <input type="file" class="custom-file-input" title="<?= $Page->var_foto->title() ?>" data-table="varietas" data-field="x_var_foto" name="x_var_foto" id="x_var_foto" lang="<?= CurrentLanguageID() ?>"<?= $Page->var_foto->editAttributes() ?><?= ($Page->var_foto->ReadOnly || $Page->var_foto->Disabled) ? " disabled" : "" ?> aria-describedby="x_var_foto_help">
        <label class="custom-file-label ew-file-label" for="x_var_foto"><?= $Language->phrase("ChooseFile") ?></label>
    </div>
</div>
<?= $Page->var_foto->getCustomMessage() ?>
<div class="invalid-feedback"><?= $Page->var_foto->getErrorMessage() ?></div>
<input type="hidden" name="fn_x_var_foto" id= "fn_x_var_foto" value="<?= $Page->var_foto->Upload->FileName ?>">
<input type="hidden" name="fa_x_var_foto" id= "fa_x_var_foto" value="0">
<input type="hidden" name="fs_x_var_foto" id= "fs_x_var_foto" value="200">
<input type="hidden" name="fx_x_var_foto" id= "fx_x_var_foto" value="<?= $Page->var_foto->UploadAllowedFileExt ?>">
<input type="hidden" name="fm_x_var_foto" id= "fm_x_var_foto" value="<?= $Page->var_foto->UploadMaxFileSize ?>">
</div>
<table id="ft_x_var_foto" class="table table-sm float-left ew-upload-table"><tbody class="files"></tbody></table>
</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="<?= 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("varietas");
});
</script>
<script>
loadjs.ready("load", function () {
    // Write your table-specific startup script here, no need to add script tags.
});
</script>
