product.js 7.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
document.addEventListener("DOMContentLoaded", function () {
    // плус/минус копче кај количина на продукт
    document.getElementById("name").focus();

    document.getElementById("minus-btn").addEventListener("click", function () {
        var input = document.getElementById("stock_quantity");
        var value = parseInt(input.value);
        if (!isNaN(value) && value > 0) {
            input.value = value - 1;
        }
Vasko Mitevski's avatar
Vasko Mitevski committed
11
        updateColorCheckboxValidity();
12
        updateCheckboxValidity();
13
14
15
16
17
18
19
20
    });

    document.getElementById("plus-btn").addEventListener("click", function () {
        var input = document.getElementById("stock_quantity");
        var value = parseInt(input.value);
        if (!isNaN(value)) {
            input.value = value + 1;
        }
Vasko Mitevski's avatar
Vasko Mitevski committed
21
        updateColorCheckboxValidity();
22
        updateCheckboxValidity();
23
24
    });

25
    // чек боксовите за боја
26
27
28
29
30
31
32
33
34
    const colorCheckboxes = document.querySelectorAll(".color-checkbox-input");
    colorCheckboxes.forEach(function (checkbox) {
        checkbox.addEventListener("change", function () {
            const label = this.nextElementSibling;
            if (this.checked) {
                label.classList.add("checked");
            } else {
                label.classList.remove("checked");
            }
Vasko Mitevski's avatar
Vasko Mitevski committed
35
            updateColorCheckboxValidity();
36
37
38
39
40
41
42
43
            updateCheckboxValidity();
        });
    });

    const sizeCheckboxes = document.querySelectorAll(".size-checkbox-input");
    sizeCheckboxes.forEach(function (checkbox) {
        checkbox.addEventListener("change", function () {
            updateCheckboxValidity();
44
45
46
        });
    });

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    function updateCheckboxValidity() {
        const stockQuantity = parseInt(
            document.getElementById("stock_quantity").value
        );

        // Проверка за боите
        const checkedColorCheckboxes = document.querySelectorAll(
            ".color-checkbox-input:checked"
        );
        if (checkedColorCheckboxes.length > stockQuantity) {
            checkedColorCheckboxes.forEach(function (checkbox) {
                checkbox.checked = false;
                checkbox.nextElementSibling.classList.remove("checked");
            });
        }

        // Проверка за величините
        const checkedSizeCheckboxes = document.querySelectorAll(
            ".size-checkbox-input:checked"
        );
        if (checkedSizeCheckboxes.length > stockQuantity) {
            checkedSizeCheckboxes.forEach(function (checkbox) {
                checkbox.checked = false;
                checkbox.nextElementSibling.classList.remove("checked");
            });
        }

        const uncheckedSizeCheckboxes = document.querySelectorAll(
            ".size-checkbox-input:not(:checked)"
        );

        uncheckedSizeCheckboxes.forEach(function (checkbox) {
            checkbox.disabled = checkedSizeCheckboxes.length >= stockQuantity;
            if (checkbox.disabled) {
                const messageElement = document.querySelector(
                    ".stock_quantity_exceeded_for_sizes"
                );
                messageElement.innerText = `Максимален број на различни величини е достигнат. (Количина ${
                    document.getElementById("stock_quantity").value
                })`;
            } else {
                checkbox.nextElementSibling.style.backgroundColor = "";
                document.querySelector(
                    ".stock_quantity_exceeded_for_sizes"
                ).innerText = "";
            }
        });
    }

Vasko Mitevski's avatar
Vasko Mitevski committed
96
97
98
99
    function updateColorCheckboxValidity() {
        const stockQuantity = parseInt(
            document.getElementById("stock_quantity").value
        );
100
101
102
103
        const checkedColorCheckboxes = document.querySelectorAll(
            ".color-checkbox-input:checked"
        );

Vasko Mitevski's avatar
Vasko Mitevski committed
104
105
106
107
108
109
110
        if (checkedColorCheckboxes.length > stockQuantity) {
            const excessCheckedCheckboxes = Array.from(
                checkedColorCheckboxes
            ).slice(stockQuantity);
            excessCheckedCheckboxes.forEach(function (checkbox) {
                checkbox.checked = false;
                checkbox.nextElementSibling.classList.remove("checked");
111
112
            });
        }
Vasko Mitevski's avatar
Vasko Mitevski committed
113
114
115
116
117
118

        const uncheckedColorCheckboxes = document.querySelectorAll(
            ".color-checkbox-input:not(:checked)"
        );
        uncheckedColorCheckboxes.forEach(function (checkbox) {
            checkbox.disabled = checkedColorCheckboxes.length >= stockQuantity;
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
            if (checkbox.disabled) {
                checkbox.nextElementSibling.style.backgroundColor = "white";
                const messageElement = document.querySelector(
                    ".stock_quantity_exceeded_for_colors"
                );
                messageElement.innerText =`Максимален број на различни бои е достигнат. (Количина ${
                    document.getElementById("stock_quantity").value
                })`;
            } else {
                checkbox.nextElementSibling.style.backgroundColor = "";
                // Remove the message when the checkbox is enabled
                document.querySelector(
                    ".stock_quantity_exceeded_for_colors"
                ).innerText = "";
            }
Vasko Mitevski's avatar
Vasko Mitevski committed
134
        });
135
136
137
    }

    // делот со 4-те слики
138
139
140
    document
        .querySelectorAll('.image-upload-box input[type="file"]')
        .forEach(function (input) {
Vasko Mitevski's avatar
Vasko Mitevski committed
141
142
143
144
145
            input.addEventListener("change", function () {
                const reader = new FileReader();
                const imageContainer = this.parentNode.querySelector(
                    ".image-container img"
                );
146

Vasko Mitevski's avatar
Vasko Mitevski committed
147
148
149
                reader.onload = function (e) {
                    imageContainer.src = e.target.result;
                };
150
151
152
153
                input.parentElement.querySelector(
                    ".image-container + i"
                ).style.display = "none";

Vasko Mitevski's avatar
Vasko Mitevski committed
154
155
                reader.readAsDataURL(this.files[0]);
            });
156
157
        });

158
159
    //копчето -откажи- на крајо од формата
    document.getElementById("cancel").addEventListener("click", function () {
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
        const form = document.getElementById("create_product");

        form.querySelectorAll(
            'input[type="text"], input[type="number"], input[type="checkbox"], textarea'
        ).forEach(function (input) {
            if (input.type === "checkbox") {
                input.checked = false;
            } else {
                input.value = "";
            }
        });

        document.getElementById("stock_quantity").value = 1;

        const colorCheckboxes = form.querySelectorAll(".color-checkbox-input");
175
176
177
178
        colorCheckboxes.forEach(function (checkbox) {
            const label = checkbox.nextElementSibling;
            label.classList.remove("checked");
        });
179
180
181
182
183
184
185
186
187
188
189
190
191

        form.querySelectorAll(".text-danger").forEach(function (error) {
            error.textContent = "";
        });

        form.querySelectorAll('input[type="file"]').forEach(function (input) {
            input.value = "";
            const imageContainer = input.parentNode.querySelector(
                ".image-container img"
            );
            imageContainer.src = "";
        });

192
193
194
195
        window.scrollTo({ top: 0, behavior: "smooth" });
        document.getElementById("name").focus();
    });
});