/**
* PDA v1.1.2
* (c) 2024 Product Designer App
* @license GPLv3
* Product field
* Free version
* Unminify code for Dev
**/
var pdappProduct = {
    toast: function (p = "", d = "success", t = !1) {
        jQuery("#pdapp-toast-bar .pdapp-text-content").text(p);
        let e = "task_alt";
        "warning" == d && (e = "warning"),
            "error" == d && (e = "error"),
            jQuery("#pdapp-toast-bar .material-icons").text(e),
            t
                ? jQuery("#pdapp-toast-bar .material-icons").addClass("pdapp-show")
                : jQuery("#pdapp-toast-bar .material-icons").removeClass("pdapp-show"),
            jQuery("#pdapp-toast-bar").attr("class", "pdapp-show pdapp-toast-" + d),
            setTimeout(() => {
                jQuery("#pdapp-toast-bar").attr("class", "");
            }, 3e3);
    },
    getData: function (p) {
        return new Promise(function (d, t) {
            let e = {
                method: "GET",
                url: p,
                dataType: "json",
                success: function (p) {
                    0 == p
                        ? t({ msg: "403 Forbidden", error: 1 })
                        : (void 0 === p.error && (p.error = 1),
                          0 === p.error ? d({ msg: p.msg, data: p.data }) : t({ msg: p.msg, error: p.error }));
                },
                error: function (p, d, e) {
                    t({ msg: e, error: 1 });
                },
            };
            jQuery.ajax(e);
        });
    },
    postData: function (p, d) {
        return new Promise(function (t, e) {
            let a = {
                method: "POST",
                url: p,
                dataType: "json",
                data: d,
                success: function (p) {
                    0 == p
                        ? e({ msg: "403 Forbidden", error: 1 })
                        : (void 0 === p.error && (p.error = 1),
                          0 === p.error ? t({ msg: p.msg, data: p.data }) : e({ msg: p.msg, error: p.error }));
                },
                error: function (p, d, t) {
                    e({ msg: t, error: 1 });
                },
            };
            jQuery.ajax(a);
        });
    },
    products: [],
    product: {},
    getProduct: async function () {
        let p = this;
        if ((p.openModal("product"), p.loadingModal(".pdapp_product_options_topbox", !0), 0 == p.products.length)) {
            let d = pdappAdminUrl + "admin-ajax.php?action=pdapp-products&admin=1&pdapp-nonce=" + pdappNonce;
            try {
                let t = await p.getData(d);
                p.products = t.data;
            } catch (p) {}
        }
        p.genListProducts(p.products), p.loadingModal(".pdapp_product_options_topbox", !1);
    },
    searchProducts: function () {
        let p = this,
            d = [],
            t = jQuery("#pdapp-search-products").val();
        if ("" != t)
            for (let e = 0; e < p.products.length; e++)
                -1 != p.products[e].title.toLowerCase().indexOf(t.toLowerCase()) && d.push(p.products[e]);
        else d = p.products;
        p.genListProducts(d);
    },
    genListProducts: function (p) {
        let d = "";
        for (let t = 0; t < p.length; t++) {
            let e =
                "" != p[t].thumb
                    ? pdappUploadUrl + "products/product-" + p[t].id + "/" + p[t].thumb
                    : pdappPluginUrl + "assets/images/image.png";
            (d +=
                '<div class="pdapp-col-6 pdapp-col-md-4 pdapp-col-lg-3 pdapp-col-xl-2 pdapp-product-card-' +
                p[t].id +
                '" onclick="pdappProduct.selectProduct(' +
                p[t].id +
                ", 'modal')\">"),
                (d += '<div class="pdapp-card">'),
                (d += '<img class="pdapp-card-img-top pdapp-img-fluid" src="' + e + '" alt="' + p[t].title + '">'),
                (d += '<h5 class="pdapp-card-title">' + p[t].title + " (#" + p[t].id + ")</h5>"),
                (d += '<div class="pdapp-loading-box pdapp-none">'),
                (d += '<div class="pdapp-loading-mask">'),
                (d +=
                    '<div class="pdapp-spinner"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>'),
                (d += "</div>"),
                (d += "</div>"),
                (d += "</div>"),
                (d += "</div>");
        }
        jQuery("#pdapp-list-products .pdapp-products").html(d);
    },
    clearProduct: function () {
        jQuery(".pdapp-btn-clearproduct").addClass("pdapp-none"),
            jQuery(".pdapp_product_options_box").addClass("pdapp-none"),
            jQuery(".pdapp_product_options_box .pdapp_product_info").html(""),
            jQuery(".pdapp_product_options_box .pdapp_product_template").html(""),
            jQuery("#pdapp_product_design_id").val(""),
            jQuery("#pdapp_product_value_id").val(""),
            jQuery("#pdapp_product_design_title").val(""),
            jQuery("#pdapp_product_design_thumb").val(""),
            jQuery("#pdapp_product_design_value").val("[]");
    },
    selectProduct: async function (p, d) {
        let t = this,
            e = {};
        if ("modal" == d) for (let d = 0; d < t.products.length; d++) t.products[d].id == p && (e = t.products[d]);
        else "modal" != d && void 0 !== t.product && (e.id = p);
        if (void 0 === e.id || "" == e.id) return !1;
        if (
            ("modal" == d && t.loadingProduct(!0, p),
            "modal" != d && jQuery(".pdapp_product_options_topbox .pdapp-btn-spinner").removeClass("pdapp-none"),
            void 0 === e.views)
        ) {
            let a =
                pdappAdminUrl + "admin-ajax.php?action=pdapp-product&id=" + e.id + "&admin=1&pdapp-nonce=" + pdappNonce;
            try {
                (e = (await t.getData(a)).data), "modal" == d && (t.products[p] = e);
            } catch (e) {
                return (
                    t.showToast("warning", e.msg),
                    "modal" == d
                        ? t.loadingProduct(!1, p)
                        : jQuery(".pdapp_product_options_topbox .pdapp-btn-spinner").addClass("pdapp-none"),
                    !1
                );
            }
        }
        t.product = e;
        let a = '<div class="pdapp-row">';
        (a += '<div class="pdapp-col-6 pdapp-col-md-4 pdapp-col-lg-3 pdapp-col-xl-2">'),
            (a += '<div class="pdapp-card">'),
            (a +=
                '<img class="pdapp-card-img-top pdapp-img-fluid" src="' +
                ("" != e.thumb
                    ? pdappUploadUrl + "products/product-" + e.id + "/" + e.thumb
                    : pdappPluginUrl + "assets/images/image.png") +
                '" alt="' +
                e.title +
                '">'),
            (a += '<h5 class="pdapp-card-title">' + e.title + "(#" + e.id + ")</h5>"),
            (a += "</div>"),
            (a += "</div></div>");
        let o = "",
            l = "pdapp-none";
        for (let p = 0; p < e.views.length; p++)
            o +=
                '<button type="button" class="pdapp-btn pdapp-btn-light pdapp-temp-view pdapp-temp-view-' +
                p +
                '" onclick="pdappProduct.activeView(' +
                p +
                ')">' +
                e.views[p].title +
                "</button>";
        e.views.length > 1 && (l = "");
        let i = '<div class="pdapp_temp_view ' + l + '">' + o + "</div>";
        (i += '<div class="pdapp_temp_viewbox"></div>'),
            (i += '<div class="pdapp_temp_thumb pdapp-row"></div>'),
            jQuery(".pdapp_product_options_box").removeClass("pdapp-none"),
            jQuery(".pdapp_product_options_box .pdapp_product_info").html(a),
            jQuery(".pdapp_product_options_box .pdapp_product_template").html(i),
            "modal" == d &&
                (jQuery("#pdapp_product_design_id").val(e.id),
                jQuery("#pdapp_product_value_id").val(e.vid),
                jQuery("#pdapp_product_design_title").val(JSON.stringify(e.title)),
                jQuery("#pdapp_product_design_thumb").val(e.thumb),
                jQuery("#pdapp_product_design_value").val("[]")),
            jQuery("#pdapp-list-templates .pdapp-list-temps").html(""),
            jQuery(".pdapp_product_options_box").removeClass("pdapp-none"),
            "modal" == d && t.loadingProduct(!1, p),
            "modal" == d && t.closeModal(),
            jQuery(".pdapp-btn-clearproduct").removeClass("pdapp-none"),
            "modal" != d && jQuery(".pdapp_product_options_topbox .pdapp-btn-spinner").addClass("pdapp-none"),
            t.activeView(0);
    },
    getTitle: function (p) {
        let d = "Untitled";
        if (void 0 !== p[pdappLangDefault] && "" != p[pdappLangDefault]) d = p[pdappLangDefault];
        else for (let t in p) "" != p[t] && (d = p[t]);
        return d;
    },
    loadingProduct: function (p, d) {
        p
            ? jQuery(".pdapp-product-card-" + d)
                  .find(".pdapp-loading-box")
                  .removeClass("pdapp-none")
            : jQuery(".pdapp-product-card-" + d)
                  .find(".pdapp-loading-box")
                  .addClass("pdapp-none");
    },
    showToast: function (p, d) {
        jQuery(".pdapp-toast").removeClass("pdapp-bg-success"),
            jQuery(".pdapp-toast").removeClass("pdapp-bg-warning"),
            jQuery(".pdapp-toast").addClass("pdapp-bg-" + p),
            jQuery(".pdapp-toast").removeClass("pdapp-none"),
            jQuery(".pdapp-toast .pdapp-toast-body").html(d),
            setTimeout(() => {
                jQuery(".pdapp-toast").addClass("pdapp-none");
            }, 5e3);
    },
    openModal: function (p) {
        "product" == p &&
            (jQuery(".pdapp-list-products").removeClass("pdapp-none"),
            jQuery(".pdapp-list-templates").addClass("pdapp-none")),
            "template" == p &&
                (jQuery(".pdapp-list-templates").removeClass("pdapp-none"),
                jQuery(".pdapp-list-products").addClass("pdapp-none")),
            jQuery("#pdapp-product-modal").removeClass("pdapp-none"),
            jQuery("#pdapp-product-modal").addClass("pdapp-block"),
            jQuery("#pdapp_product_options_box .pdapp-modal-backdrop").addClass("pdapp-block");
    },
    loadingModal: function (p, d) {
        d
            ? (jQuery("#pdapp-product-modal .pdapp-modal-body").addClass("pdapp-none"),
              jQuery("#pdapp-product-modal .pdapp-loading-box").removeClass("pdapp-none"),
              jQuery(p + " .pdapp-btn-spinner").removeClass("pdapp-none"))
            : (jQuery("#pdapp-product-modal .pdapp-modal-body").removeClass("pdapp-none"),
              jQuery("#pdapp-product-modal .pdapp-loading-box").addClass("pdapp-none"),
              jQuery(p + " .pdapp-btn-spinner").addClass("pdapp-none"));
    },
    closeModal: function () {
        jQuery("#pdapp-product-modal").addClass("pdapp-none"),
            jQuery("#pdapp-product-modal").removeClass("pdapp-block"),
            jQuery("#pdapp_product_options_box .pdapp-modal-backdrop").removeClass("pdapp-block");
    },
    activeView: function (p) {
        let d = this;
        jQuery(".pdapp-temp-view.pdapp-btn-primary").addClass("pdapp-btn-light"),
            jQuery(".pdapp-temp-view").removeClass("pdapp-btn-primary"),
            jQuery(".pdapp-temp-view-" + p).removeClass("pdapp-btn-light"),
            jQuery(".pdapp-temp-view-" + p).addClass("pdapp-btn-primary");
        let t = "";
        if (void 0 !== d.product.views[p])
            for (let e = 0; e < d.product.views[p].layers.length; e++)
                if (void 0 !== d.product.views[p].layers[e]) {
                    t +=
                        '<button type="button" class="pdapp-btn pdapp-btn-sm pdapp-btn-primary pdapp-temp-viewbox pdapp-temp-viewbox-' +
                        p +
                        "_" +
                        e +
                        '" onclick="pdappProduct.loadTemplate(' +
                        p +
                        ", " +
                        e +
                        ')"><span class="pdapp-btn-spinner pdapp-none" role="status" aria-hidden="true""></span> ' +
                        (d.product.views[p].layers[e].title +
                            "<small>" +
                            d.product.views[p].layers[e].width +
                            "x" +
                            d.product.views[p].layers[e].height +
                            d.product.unit.type +
                            "</small>") +
                        "</button>";
                }
        jQuery(".pdapp_temp_viewbox").html(t), (d.tempLoad.vid = p), d.genDesignThumbs();
    },
    templates: {},
    tempFolders: {},
    tempFolder: 0,
    tempLoad: { vid: 0, lid: 0 },
    loadTemplate: async function (p, d) {
        let t = this;
        if (
            (t.openModal("template"),
            t.loadingModal(".pdapp-temp-viewbox-" + p + "_" + d, !0),
            0 == Object.keys(t.tempFolders).length)
        ) {
            let p =
                pdappAdminUrl +
                "admin-ajax.php?action=pdapp-load-setting&tempFolder=1&admin=1&pdapp-nonce=" +
                pdappNonce;
            try {
                let d = await t.getData(p);
                t.tempFolders = d.data.folders;
            } catch (p) {}
        }
        if (void 0 === t.templates[t.tempFolder]) {
            let p =
                pdappAdminUrl +
                "admin-ajax.php?action=pdapp-temps&folder=" +
                t.tempFolder +
                "&admin=1&pdapp-nonce=" +
                pdappNonce;
            try {
                let d = await t.getData(p);
                t.templates[t.tempFolder] = d.data;
            } catch (p) {}
        }
        (t.tempLoad = { vid: p, lid: d }),
            t.openFolder(t.tempFolder),
            setTimeout(() => {
                t.loadingModal(".pdapp-temp-viewbox-" + p + "_" + d, !1);
            }, 100);
    },
    openFolder: async function (p) {
        let d = this;
        d.tempFolder = p;
        let t = void 0 !== d.tempFolders[d.tempFolder] ? d.tempFolders[d.tempFolder] : [];
        d.genFolders(t);
        let e = [],
            a = { id: 0, pid: 0, title: jQuery(".pdapp-templates-text").text() };
        if (
            (d.folder.getFolderBreads(d.tempFolders, a, d.tempFolder, function (p) {
                for (let t = 0; t < p.length; t++) {
                    let a = p[t].id == d.tempFolder ? "" : p[t].id;
                    e.push({ to: a, title: p[t].title });
                }
                let t = "";
                for (let p = 0; p < e.length; p++)
                    "" === e[p].to
                        ? (t += '<li class="pdapp-breadcrumb-item">' + e[p].title + "</li>")
                        : (t +=
                              '<li class="pdapp-breadcrumb-item"><a href="javascript: void(0);" onclick="pdappProduct.openFolder(' +
                              e[p].to +
                              ')">' +
                              e[p].title +
                              "</a></li>");
                jQuery("#pdapp-list-templates .pdapp-breadcrumb").html(t);
            }),
            void 0 === d.templates[d.tempFolder])
        ) {
            let p =
                pdappAdminUrl +
                "admin-ajax.php?action=pdapp-temps&folder=" +
                d.tempFolder +
                "&admin=1&pdapp-nonce=" +
                pdappNonce;
            try {
                let t = await d.getData(p);
                d.templates[d.tempFolder] = t.data;
            } catch (p) {}
        }
        let o = void 0 !== d.templates[d.tempFolder] ? d.templates[d.tempFolder] : [];
        o.length
            ? (d.genListTemplates(o), jQuery("#pdapp-list-templates .pdapp-temps").removeClass("pdapp-none"))
            : jQuery("#pdapp-list-templates .pdapp-temps").addClass("pdapp-none");
    },
    genFolders: function (p) {
        let d = "";
        for (let t = 0; t < p.length; t++) {
            let e =
                "" != p[t].thumb
                    ? pdappUploadUrl + "thumbnails/templates/template-folder-" + p[t].thumb
                    : pdappPluginUrl + "assets/images/image.png";
            (d +=
                '<div class="pdapp-col-4 pdapp-col-md-3 pdapp-col-lg-2 pdapp-col-xl-1" onclick="pdappProduct.openFolder(' +
                p[t].id +
                ')">'),
                (d += '<div class="pdapp-card">'),
                (d +=
                    '<img class="pdapp-card-img-top pdapp-img-fluid" src="' +
                    e +
                    '" alt="' +
                    this.getTitle(p[t].title) +
                    '">'),
                (d += '<h5 class="pdapp-card-title">' + this.getTitle(p[t].title) + "</h5>"),
                (d += "</div>"),
                (d += "</div>");
        }
        jQuery("#pdapp-list-templates .pdapp-list-folder").html(d);
    },
    searchTemplates: function () {
        let p = this,
            d = void 0 !== p.templates[p.tempFolder] ? p.templates[p.tempFolder] : [],
            t = [],
            e = jQuery("#pdapp-search-templates").val();
        if ("" != e)
            for (let a = 0; a < d.length; a++)
                -1 != p.getTitle(d[a].title).toLowerCase().indexOf(e.toLowerCase()) && t.push(d[a]);
        else t = d;
        p.genListTemplates(t);
    },
    genListTemplates: function (p) {
        let d = "";
        for (let t = 0; t < p.length; t++) {
            (d +=
                '<div class="pdapp-col-6 pdapp-col-md-4 pdapp-col-lg-3 pdapp-col-xl-2" onclick="pdappProduct.selectTemplate(' +
                t +
                ')">'),
                (d += '<div class="pdapp-card pdapp-card-template pdapp-card-template-' + t + '">'),
                (d +=
                    '<img class="pdapp-card-img-top pdapp-img-fluid" src="' +
                    ("" != p[t].thumb
                        ? pdappUploadUrl + "thumbnails/templates/template-" + p[t].thumb
                        : pdappPluginUrl + "assets/images/image.png") +
                    '" alt="' +
                    this.getTitle(p[t].title) +
                    '">'),
                (d += '<h5 class="pdapp-card-title">' + this.getTitle(p[t].title) + "(#" + p[t].id + ")</h5>"),
                (d +=
                    '<small class="pdapp-card-title">' +
                    p[t].size.width +
                    "x" +
                    p[t].size.height +
                    p[t].size.unit +
                    "</small>"),
                (d += '<div class="pdapp-loading-box pdapp-none">'),
                (d += '<div class="pdapp-loading-mask">'),
                (d +=
                    '<div class="pdapp-spinner"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>'),
                (d += "</div>"),
                (d += "</div>"),
                (d += "</div>"),
                (d += "</div>");
        }
        jQuery("#pdapp-list-templates .pdapp-list-temps").html(d);
    },
    selectTemplate: function (p) {
        let d = this,
            t = {};
        if (
            (void 0 !== d.templates[d.tempFolder] &&
                void 0 !== d.templates[d.tempFolder][p] &&
                (t = d.templates[d.tempFolder][p]),
            void 0 === t.id)
        )
            return !1;
        d.loadingTemplate(!0, p);
        let e = jQuery("#pdapp_product_design_value").val(),
            a = JSON.parse(e),
            o = [];
        for (let p = 0; p < d.product.views.length; p++) {
            let e = [];
            for (let o = 0; o < d.product.views[p].layers.length; o++) {
                let l = { id: 0, title: "", thumb: "", size: {} };
                void 0 !== a[p] && void 0 !== a[p][o] && (l = a[p][o]),
                    d.tempLoad.vid == p &&
                        d.tempLoad.lid == o &&
                        (l = {
                            id: t.id,
                            title: d.getTitle(t.title),
                            thumb: t.thumb,
                            size: { width: t.size.width, height: t.size.height, unit: t.size.unit },
                        }),
                    e.push(l);
            }
            o.push(e);
        }
        jQuery("#pdapp_product_design_value").val(JSON.stringify(o)),
            d.genDesignThumbs(),
            d.loadingTemplate(!1, p),
            d.closeModal();
    },
    loadingTemplate: function (p, d) {
        p
            ? jQuery(".pdapp-card-template-" + d)
                  .find(".pdapp-loading-box")
                  .removeClass("pdapp-none")
            : jQuery(".pdapp-card-template-" + d)
                  .find(".pdapp-loading-box")
                  .addClass("pdapp-none");
    },
    genDesignThumbs: function () {
        let p = this,
            d = jQuery("#pdapp_product_design_value").val(),
            t = JSON.parse(d),
            e = "";
        if (t.length)
            for (let d = 0; d < t.length; d++)
                if (p.tempLoad.vid == d)
                    for (let p = 0; p < t[d].length; p++)
                        if (0 != t[d][p].id) {
                            (e += '<div class="pdapp-col-6 pdapp-col-md-4 pdapp-col-lg-3 pdapp-col-xl-2">'),
                                (e += '<div class="pdapp-card">'),
                                (e +=
                                    '<img class="pdapp-card-img-top pdapp-img-fluid" src="' +
                                    ("" != t[d][p].thumb
                                        ? pdappUploadUrl + "thumbnails/templates/template-" + t[d][p].thumb
                                        : pdappPluginUrl + "assets/images/image.png") +
                                    '" alt="' +
                                    t[d][p].title +
                                    '">'),
                                (e += '<h5 class="pdapp-card-title">' + t[d][p].title + "</h5>"),
                                (e +=
                                    '<small class="pdapp-card-title">' +
                                    t[d][p].size.width +
                                    "x" +
                                    t[d][p].size.height +
                                    t[d][p].size.unit +
                                    "</small>"),
                                (e +=
                                    '<span class="pdapp-remove-card-btn" onclick="pdappProduct.clearTemplate(' +
                                    d +
                                    ", " +
                                    p +
                                    ')"></span>'),
                                (e += "</div>"),
                                (e += "</div>");
                        }
        jQuery(".pdapp_product_template .pdapp_temp_thumb").html(e);
    },
    clearTemplate: function (p, d) {
        let t = jQuery("#pdapp_product_design_value").val(),
            e = JSON.parse(t);
        void 0 !== e[p] && void 0 !== e[p][d] && (e[p][d] = { id: 0, title: "", thumb: "", size: {} }),
            jQuery("#pdapp_product_design_value").val(JSON.stringify(e)),
            this.genDesignThumbs();
    },
    folder: {
        getFolderBreads: function (p, d, t, e) {
            let a = [];
            this.getFolderBread(p, d, t, function (p) {
                a.push(p), 0 == p.id && (a.reverse(), "function" == typeof e && e(a));
            });
        },
        getFolderBread: function (p, d, t, e) {
            if (0 != t) {
                let a = 0;
                for (let d in p)
                    if (d != t)
                        for (let o = 0; o < p[d].length; o++)
                            p[d][o].id == t &&
                                ((a = p[d][o].pid),
                                "function" == typeof e &&
                                    e({
                                        id: p[d][o].id,
                                        pid: p[d][o].pid,
                                        title: pdappProduct.getTitle(p[d][o].title),
                                    }));
                0 == a
                    ? "function" == typeof e && e(d)
                    : this.getFolderBread(p, d, a, function (p) {
                          "function" == typeof e && e(p);
                      });
            } else "function" == typeof e && e(d);
        },
    },
    genProductDesign: function () {
        let p = jQuery("#pdapp_product_design_id").val();
        if ("" === p) return !1;
        this.selectProduct(p, "load");
    },
};
setTimeout(() => {
    pdappProduct.genProductDesign(), jQuery("#pdapp_productdata_tab a").trigger("click");
}, 500);