/**
* PDA v1.1.2
* (c) 2024 Product Designer App
* @license GPLv3
* Printing
* Free version
* Unminify code for Dev
**/
const { createApp: createApp } = Vue;
var pdappVueApp = createApp({
    data: () => ({
        loadingPage: !0,
        uploadUrl: pdappUploadUrl,
        pluginUrl: pdappPluginUrl,
        modal: { show: !1 },
        toast: { show: !1, type: "success", text: "" },
        pageAdd: { show: !1, saving: !1, active: 0, data: {} },
        pageList: { show: !0, data: [] },
        langs: {},
        listLang: { default: pdappLangDefault, list: [] },
        printCheckbox: !1,
        printCheckboxs: [],
        listAttrPrints: [],
    }),
    created() {
        jQuery("#productdesignerapp-app").show(), this.getPrintSetting();
    },
    computed: {
        breadcrumbs() {
            let t = { title: "", pages: [] };
            return (
                void 0 === this.langs.menu ||
                    (this.pageAdd.show
                        ? ((t.title =
                              void 0 !== this.pageAdd.data.id && "" != this.pageAdd.data.id
                                  ? this.langs.menu.edit_print
                                  : this.langs.menu.add_print),
                          (t.pages = [
                              { to: "printings", title: this.langs.menu.printings },
                              { to: "", title: t.title },
                          ]))
                        : ((t.title = this.langs.menu.printings), (t.pages = [{ to: "", title: t.title }]))),
                t
            );
        },
        listPrints() {
            return this.pageList.data;
        },
        disabledSave() {
            let t = !0;
            return (
                1 != this.pageAdd.saving &&
                    "" != this.pageAdd.data.title &&
                    0 != this.pageAdd.data.prints.length &&
                    (t = !1),
                t
            );
        },
    },
    methods: {
        goToPage(t) {
            "printings" == t && this.showListPrints();
        },
        async getRefreshToken() {
            let t = this,
                a = pdappAdminUrl + "admin-ajax.php?action=pdapp-refresh-token&admin=1";
            try {
                let t = await pdappAPI.tool.getData(a);
                pdappNonce = t.data.token;
            } catch (t) {}
            setTimeout(() => {
                t.getRefreshToken();
            }, 6e4);
        },
        async getPrintSetting() {
            let t = this,
                a =
                    pdappAdminUrl +
                    "admin-ajax.php?action=pdapp-load-setting&listLangs=1&langs=1&listPrints=1&admin=1&pdapp-nonce=" +
                    pdappNonce;
            try {
                let e = await pdappAPI.tool.getData(a);
                (t.langs = e.data.langs), (t.listLang.list = e.data.listLangs), (t.listAttrPrints = e.data.listPrints);
                let s = 0 == pdappEditPage ? "" : { id: pdappEditPage };
                "" === pdappEditPage ? t.showListPrints() : (t.editPrint(s), t.showListPrints(!0));
            } catch (t) {}
            setTimeout(() => {
                t.getRefreshToken();
            }, 6e4);
        },
        async showListPrints(t) {
            let a = this;
            if (((a.pageAdd.show = !1), (a.pageList.show = !0), (a.loadingPage = !0), 0 == a.pageList.data.length)) {
                let t = pdappAdminUrl + "admin-ajax.php?action=pdapp-prints&admin=1&pdapp-nonce=" + pdappNonce;
                try {
                    let e = await pdappAPI.tool.getData(t);
                    a.pageList.data = e.data;
                } catch (t) {
                    (a.toast = { show: !0, type: "warning", text: t.msg }),
                        setTimeout(() => {
                            a.toast.show = !1;
                        }, 5e3);
                }
            }
            void 0 === t && pdappAPI.tool.updateURL("printings"), (a.loadingPage = !1);
        },
        checkAllPrint() {
            if (((this.printCheckboxs = []), this.printCheckbox))
                for (let t = 0; t < this.listPrints.length; t++) this.printCheckboxs.push(this.listPrints[t].id);
        },
        convertDate: (t) => pdappAPI.tool.toDate(t),
        confirmRemove(t, a) {
            this.modal = {
                show: !0,
                header: !1,
                title: "",
                size: "md modal-dialog-centered",
                type: "message",
                data: {
                    id: t,
                    pid: a,
                    title: this.langs.msg.confirm_title,
                    text: this.langs.msg.confirm_data,
                    type: "warning",
                    btn: this.langs.delete,
                    confirm: "print",
                },
            };
        },
        async removePrint(t, a) {
            let e = this,
                s = [];
            if ("" === t && "" === a) {
                if (0 == e.printCheckboxs.length) return !1;
                for (let t = 0; t < e.listPrints.length; t++)
                    e.printCheckboxs.includes(e.listPrints[t].id) &&
                        s.push({ id: e.listPrints[t].id, pid: e.listPrints[t].pid });
            } else s.push({ id: t, pid: a });
            e.loadingPage = !0;
            let i = pdappAdminUrl + "admin-ajax.php?action=pdapp-remove-print&admin=1&pdapp-nonce=" + pdappNonce;
            try {
                let t = await pdappAPI.tool.postData(i, { data: s });
                for (let a = e.pageList.data.length - 1; a >= 0; a--)
                    t.data.includes(parseInt(e.pageList.data[a].id)) && e.pageList.data.splice(a, 1);
                (e.printCheckbox = !1),
                    (e.printCheckboxs = []),
                    (e.toast = { show: !0, type: "success", text: t.msg }),
                    setTimeout(() => {
                        e.toast.show = !1;
                    }, 5e3);
            } catch (t) {
                (e.toast = { show: !0, type: "warning", text: t.msg }),
                    setTimeout(() => {
                        e.toast.show = !1;
                    }, 5e3);
            }
            e.loadingPage = !1;
        },
        confirmEvt(t) {
            "print" == t && this.removePrint(this.modal.data.id, this.modal.data.pid), (this.modal.show = !1);
        },
        showImg(t) {
            return void 0 === t || "" == t
                ? this.pluginUrl + "assets/images/image.png"
                : ("" != t && -1 == t.indexOf("base64,") && (t = this.uploadUrl + "/prints/print-" + t), t);
        },
        async editPrint(t) {
            let a = this;
            if (((a.loadingPage = !0), "" == t))
                (a.pageAdd.data = { id: "", pid: "", title: "", thumb: "", prints: [] }),
                    pdappAPI.tool.updateURL("printings&edit=0");
            else if (void 0 !== t.prints) (a.pageAdd.data = t), pdappAPI.tool.updateURL("printings&edit=" + t.id);
            else {
                let e =
                    pdappAdminUrl +
                    "admin-ajax.php?action=pdapp-print&id=" +
                    t.id +
                    "&admin=1&pdapp-nonce=" +
                    pdappNonce;
                try {
                    let s = await pdappAPI.tool.getData(e);
                    a.pageAdd.data = s.data;
                    for (let e = 0; e < a.pageList.data.length; e++)
                        a.pageList.data[e].id == t.id && (a.pageList.data[e] = s.data);
                    pdappAPI.tool.updateURL("printings&edit=" + a.pageAdd.data.id);
                } catch (t) {
                    (a.toast = { show: !0, type: "warning", text: t.msg }),
                        setTimeout(() => {
                            a.toast.show = !1;
                        }, 5e3);
                }
            }
            void 0 !== a.pageAdd.data.id && ((a.pageAdd.show = !0), (a.pageList.show = !1)), (a.loadingPage = !1);
        },
        changeThumbFile(t) {
            if (void 0 === t.target.files[0]) return !1;
            const a = t.target.files[0];
            let e = this;
            pdappAPI.tool.changeThumb(a, this.langs, function (t) {
                e.pageAdd.data.thumb = t;
            });
        },
        showCustomView() {
            this.modal = {
                show: !0,
                header: !0,
                size: "",
                title: this.langs.print.custom_side,
                type: "side",
                data: { side: 14 },
            };
        },
        setCustomView(t) {
            let a = this;
            "modal" == t && (t = a.modal.data.side), (a.modal.show = !1), (t = parseInt(t));
            for (let e = 0; e < t; e++) {
                let t = {
                    title: 0 == e ? "1 " + a.langs.print.side : e + 1 + " " + a.langs.print.sides,
                    attrs: {},
                    qtys: [1, 10],
                    values: {},
                };
                (t.attrs[a.listAttrPrints[0].value[0].type] = a.listAttrPrints[0].value[0].title),
                    (t.values[a.listAttrPrints[0].value[0].type] = [0, 0]),
                    a.pageAdd.data.prints.push(t);
            }
        },
        addNewMoreView() {
            let t = this,
                a = {
                    title: t.pageAdd.data.prints.length + 1 + " " + t.langs.print.sides,
                    attrs: JSON.parse(JSON.stringify(t.pageAdd.data.prints[0].attrs)),
                    qtys: JSON.parse(JSON.stringify(t.pageAdd.data.prints[0].qtys)),
                    values: JSON.parse(JSON.stringify(t.pageAdd.data.prints[0].values)),
                };
            t.pageAdd.data.prints.push(a);
        },
        addAttribute() {
            let t = [];
            if (void 0 !== this.pageAdd.data.prints[0]) for (let a in this.pageAdd.data.prints[0].attrs) t.push(a);
            this.modal = {
                show: !0,
                header: !0,
                size: "full-width modal-dialog-scrollable",
                title: this.langs.print.add_attribute,
                type: "attr",
                data: { attrs: this.listAttrPrints, addeds: t },
            };
        },
        addAttrModal(t) {
            if (this.modal.data.addeds.includes(t))
                for (let a = 0; a < this.modal.data.addeds.length; a++)
                    this.modal.data.addeds[a] == t && this.modal.data.addeds.splice(a, 1);
            else this.modal.data.addeds.push(t);
        },
        addQuantity() {
            this.modal = {
                show: !0,
                header: !0,
                size: "",
                title: this.langs.print.add_qty,
                type: "qty",
                data: { qty: 10 },
            };
        },
        deleteAttr(t, a) {
            for (let e = 0; e < this.pageAdd.data.prints.length; e++) {
                "attr" == t &&
                    void 0 !== this.pageAdd.data.prints[e].attrs[a] &&
                    delete this.pageAdd.data.prints[e].attrs[a];
                for (let t in this.pageAdd.data.prints[e].values)
                    void 0 === this.pageAdd.data.prints[e].attrs[t] && delete this.pageAdd.data.prints[e].values[t];
                if ("qty" == t && void 0 !== this.pageAdd.data.prints[e].qtys[a]) {
                    this.pageAdd.data.prints[e].qtys.splice(a, 1);
                    for (let t in this.pageAdd.data.prints[e].values)
                        for (
                            let a = this.pageAdd.data.prints[e].values[t].length - 1;
                            a >= this.pageAdd.data.prints[e].qtys.length;
                            a--
                        )
                            this.pageAdd.data.prints[e].values[t].splice(a, 1);
                }
            }
        },
        getTypeDesc(t) {
            let a = "";
            for (let e = 0; e < this.listAttrPrints.length; e++)
                for (let s = 0; s < this.listAttrPrints[e].value.length; s++)
                    t == this.listAttrPrints[e].value[s].type && (a = this.listAttrPrints[e].value[s].desc);
            return a;
        },
        addMoreColor() {
            for (let t = 0; t < this.pageAdd.data.prints.length; t++)
                for (let a = 0; a < this.pageAdd.data.prints[t].values.per_colors.length; a++)
                    this.pageAdd.data.prints[t].values.per_colors[a].push(0);
        },
        removeAttrColor(t) {
            for (let a = 0; a < this.pageAdd.data.prints.length; a++)
                for (let e = this.pageAdd.data.prints[a].values.per_colors.length - 1; e >= 0; e--)
                    this.pageAdd.data.prints[a].values.per_colors[e].splice(t, 1);
        },
        savePrintValues() {
            for (let t = 0; t < this.pageAdd.data.prints.length; t++)
                if ("attr" == this.modal.type) {
                    let a = {};
                    for (let e = 0; e < this.modal.data.addeds.length; e++) {
                        let s = this.modal.data.addeds[e];
                        if (void 0 === this.pageAdd.data.prints[t].attrs[s]) {
                            for (let e = 0; e < this.listAttrPrints.length; e++)
                                for (let i = 0; i < this.listAttrPrints[e].value.length; i++)
                                    if (this.listAttrPrints[e].value[i].type == s) {
                                        a[s] = this.listAttrPrints[e].value[i].title;
                                        let d = [];
                                        for (let a = 0; a < this.pageAdd.data.prints[t].qtys.length; a++)
                                            "per_colors" == s ? d.push([0]) : d.push(0);
                                        this.pageAdd.data.prints[t].values[s] = d;
                                    }
                        } else a[s] = this.pageAdd.data.prints[t].attrs[s];
                    }
                    this.pageAdd.data.prints[t].attrs = a;
                    for (let e in this.pageAdd.data.prints[t].values)
                        void 0 === a[e] && delete this.pageAdd.data.prints[t].values[e];
                } else {
                    this.pageAdd.data.prints[t].qtys.push(this.modal.data.qty);
                    for (let a in this.pageAdd.data.prints[t].values) {
                        let e = 0;
                        if (
                            void 0 !== this.pageAdd.data.prints[t].values[a][0] &&
                            Array.isArray(this.pageAdd.data.prints[t].values[a][0])
                        ) {
                            e = [];
                            for (let s = 0; s < this.pageAdd.data.prints[t].values[a][0].length; s++) e.push(0);
                        }
                        if (this.pageAdd.data.prints[t].values[a].length > this.pageAdd.data.prints[t].qtys.length)
                            for (let e = 0; e < this.pageAdd.data.prints[t].values[a].length; e++)
                                delete this.pageAdd.data.prints[t].values[a][e];
                        else if (this.pageAdd.data.prints[t].values[a].length < this.pageAdd.data.prints[t].qtys.length)
                            for (let s = 0; s < this.pageAdd.data.prints[t].qtys.length; s++)
                                void 0 === this.pageAdd.data.prints[t].values[a][s] &&
                                    this.pageAdd.data.prints[t].values[a].push(e);
                    }
                }
            this.modal.show = "";
        },
        async savePrint() {
            let t = this;
            t.pageAdd.saving = !0;
            let a = JSON.parse(JSON.stringify(t.pageAdd.data));
            a.prints = JSON.stringify(a.prints);
            let e = pdappAdminUrl + "admin-ajax.php?action=pdapp-save-print&admin=1&pdapp-nonce=" + pdappNonce;
            try {
                let s = new Blob([JSON.stringify(a)], { type: "application/json" }),
                    i = new FormData();
                i.append("print", s, "print.json");
                let d = await pdappAPI.tool.uploadData(e, i);
                if (((t.pageAdd.show = !1), (t.pageList.show = !0), "" == t.pageAdd.data.id))
                    t.pageList.data.push(d.data);
                else
                    for (let a = 0; a < t.pageList.data.length; a++)
                        t.pageList.data[a].id == t.pageAdd.data.id && (t.pageList.data[a] = d.data);
                (t.toast = { show: !0, type: "success", text: d.msg }),
                    setTimeout(() => {
                        t.toast.show = !1;
                    }, 5e3),
                    pdappAPI.tool.updateURL("printings");
            } catch (a) {
                (t.toast = { show: !0, type: "warning", text: a.msg }),
                    setTimeout(() => {
                        t.toast.show = !1;
                    }, 5e3);
            }
            t.pageAdd.saving = !1;
        },
    },
}).mount("#productdesignerapp-app");