<script>
    defaultBackOnclick = null;

    try {
        currentData = @json($current_data);
    } catch (error) {
        currentData = null;
    }



    InitDataPageFuncOBJ();
    LoadDataPageFunc = {};
    LoadDataPageFunc.URLs = {};
    LoadDataPageFunc.URLs.EditUrl = `{{ route('admin.products.edit', [], false) }}`;
    LoadDataPageFunc.URLs.CategoryDatalist = `/Products/New/Category/Datalist`;
    LoadDataPageFunc.URLs.SubCategoryDatalist = `/Products/New/SubCategory/Datalist`;
    LoadDataPageFunc.URLs.PhotoUpload = `/File/Upload/Product`;

    LoadDataPageFunc.PageTitle = 'Edit Product';

    LoadDataPageFunc.pageName = 'EditProductAdmin';

    LoadDataPageFunc.ids = {};
    LoadDataPageFunc.ids.photodropzone = 'EditProductPhotos';
    LoadDataPageFunc.formclass = 'EditProductMarket';

    LoadDataPageFunc.UpdateSubCategoryDatalistasTyped = function () {

        document.getElementById("EditProductCategory").addEventListener("keyup", function (event) {
            const inputValue = event.target.value;
            const categoryinputvalue = document.getElementById("EditProductCategory").value;
            QueryandReplaceDatalist("EditProductSubCategoryDataList",
                LoadDataPageFunc.URLs.SubCategoryDatalist,
                'POST', { category: categoryinputvalue }, fromvarcache = true,
                function (response) {



                });
        });
    };

    LoadDataPageFunc.PopulateCategoryDatalist = function () {
        QueryandReplaceDatalist("EditProductCategoryDataList",
            LoadDataPageFunc.URLs.CategoryDatalist,
            'POST', { }, fromvarcache = true,
          );
    };

    FormBuilder.build({
        formClass: 'EditProductMarket',
        pageName: LoadDataPageFunc.pageName,
        submitUrl: '{{ route('admin.products.bystore.edit', [], false) }}',
        currentDataUrl: '{{ route('admin.products.bystore.view.data', [], false) }}',
        topbartitle: LoadDataPageFunc.PageTitle,
        defaultback: {
            page: 'ManageProductAdmin',
            data: currenttarget,
        },
        onSuccess: (response) => {
            ModalQuickDismiss('Success', 'Product Updated');
        },
        onError: (err) => {
            ModalQuickDismiss('Error', 'Failed to update product.<br>' + err.message);
        },
        unabletoLoadFunc: function (err) {
            
            ModalQuickDismiss('Error', 'Failed to update product.<br>' + err.message);
        },
        onFormGenerated: function () {
            LoadDataPageFunc.UpdateSubCategoryDatalistasTyped();
            LoadDataPageFunc.PopulateCategoryDatalist();
        },
        fields: [
            { type: 'textarea', id: 'EditProductDescription', label: 'Description', required: true, currentdatavar: 'description' },
            { type: 'text', id: 'EditProductCategory', label: 'Category', placeholder: 'Category', required: true, datalist: 'EditProductCategoryDataList', currentdatavar: 'category' },
            { type: 'text', id: 'EditProductSubCategory', label: 'Subcategory', placeholder: 'Subcategory', required: true, datalist: 'EditProductSubCategoryDataList', currentdatavar: 'subcategory' },
            { type: 'number', id: 'EditProductPrice', placeholder: '260', label: 'Price (PHP)', required: true, currentdatavar: 'price' },
            { type: 'text', id: 'EditProductUnitName', label: 'Unit', placeholder: 'ex 25kg', required: true, currentdatavar: 'unitname' },
            // { type: 'number', id: 'EditProductAvailable',placeholder:'ex. 999', label: 'Available Stock', required: true, currentdatavar: 'available' },
            { type: 'text', id: 'EditProductBarcode', label: 'Barcode', placeholder: '12-digit number', maxlength: 12, pattern: '[0-9]*', currentdatavar: 'barcode' },
            { type: 'dropzone', id: LoadDataPageFunc.ids.photodropzone, label: 'Photo', uploadUrl: LoadDataPageFunc.URLs.PhotoUpload, currentdatavar: 'photourlDropzone' },
            { type: 'button', label: 'Submit', onClick: 'FormBuilder.submit()' },
        ]
    });





</script>