Subversion Repositories SmartDukaan

Rev

Rev 33973 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
34070 vikas.jang 1
 
2
function initializeSelect(selector, type='catalogs') {
3
    if (type === 'catalogs') {
4
        $(selector).multiselect({
5
            includeSelectAllOption: false,
6
            multiple: false,
7
            maxHeight: 200,
8
            buttonWidth: '100%',
9
            numberDisplayed: 1,
10
            nonSelectedText: '-- Select One --',
11
            nSelectedText: ' - Selected',
12
            enableFiltering: true,
13
            enableCaseInsensitiveFiltering: true
14
        });
15
    } else {
16
        $(selector).select2({
17
            placeholder: "Select or add new",
18
            tags: true,
19
            createTag: params => ({ id: params.term, text: params.term, newOption: true }),
20
            templateResult: data => data.newOption ? $('<span class="new-option">Add: ' + data.text + '</span>') : data.text,
21
        });
22
    }
23
}
24
 
25
function validateRequiredFields(fields) {
26
    let isValid = true;
27
    fields.each(function () {
28
        if (!$(this).val()) {
29
            isValid = false;
30
            $(this).addClass('is-invalid');
31
        } else {
32
            $(this).removeClass('is-invalid');
33
        }
34
    });
35
    return isValid;
36
}
37
 
38
function loadCatalogOptions(brandId, targetElement, type = 'catalogs') {
39
    $.get(`/super-catalog/brand/${brandId}`, function (response) {
40
        let options = `<option value="">-- Select --</option>`;
41
        if (type === 'catalogs'){
42
            response.catalogs.forEach(row => { options += `<option value="${row.id}">${row.description}</option>`; });
43
        } else {
44
            response.superCatalogs.forEach(row => { options += `<option value="${row.id}">${row.superCatalogName}</option>`; });
45
        }
46
        $(targetElement).html(options);
47
        initializeSelect(targetElement, type);
48
    }).fail(() => {
49
        alert("Error: Unable to load catalogs for the selected brand.");
50
    });
51
}
52
 
33973 tejus.loha 53
$(document).on('click', ".super-catalog-list", function () {
34070 vikas.jang 54
 
55
    const freshTr = `
56
            <tr>
57
                <td>
58
                    <div class="form-group">
59
                        <select id="catalog_id" class="form-control"></select>
60
                    </div>
61
                </td>
62
                <td>
63
                    <div class="form-group">
64
                        <input id="variant_name" class="form-control" placeholder="Variant Name">
65
                        <input id="mapping_id" type="hidden" value="0">
66
                    </div>
67
                </td>
68
                <td>
69
                    <button class="btn btn-danger removeExisting"><i class="fa fa-minus"></i></button>
70
                </td>
71
            </tr>`;
72
 
33973 tejus.loha 73
    doGetAjaxRequestHandler(context + "/super-catalog",
74
        (response) => {
34070 vikas.jang 75
            $('#main-content').html(response);
76
 
77
            // Initialize multiSelect
78
            initializeSelect('#brand_id');
79
 
80
            // Add new catalog mapping row
81
            $('#manageSuperCatalogModal').on('click', '.addNew', function () {
82
                const brandId = $('#brand_id').val();
83
                if (brandId) {
84
                    $('#catalogMappingTable tbody').append(freshTr);
85
                    const lastRow = $('#catalogMappingTable tbody tr:last');
86
                    loadCatalogOptions(brandId, lastRow.find('#catalog_id'));
87
                } else {
88
                    alert("Please select a brand first.");
89
                }
90
            });
91
 
92
            // Remove catalog mapping row
93
            $('#manageSuperCatalogModal').on('click', '.removeExisting', function () {
94
                if (confirm("Are you sure you want to remove this row?")) {
95
                    $(this).closest('tr').remove();
96
                }
97
            });
98
 
99
            // Save super catalog
100
            $('.save_super_model').on('click', function () {
101
                const requiredFields = $('#brand_id, #super_catalog_name');
102
                if (!validateRequiredFields(requiredFields)) {
103
                    alert('Please fill all required fields.');
104
                    return;
105
                }
106
                alert('Super Catalog saved successfully!');
107
            });
108
 
109
            $('#super-catalog').on('click', '.view-super-catalog', function (ev) {
110
                ev.preventDefault();
111
                let superCatalogId = $(this).parent().parent('tr').data('id');
112
                doGetAjaxRequestHandler(context + "/super-catalog/"+superCatalogId,(response) => {
113
                    $('#manageSuperCatalogModalBody').empty();
114
                    $('#manageSuperCatalogModalBody').html(response);
115
                    initializeSelect('.selectOrCreate', 'superCatalog');
116
                    initializeSelect('#catalog_id, #brand_id');
117
                    $('#manageSuperCatalogModal').modal('show');
118
                });
119
            });
120
 
121
            // Initialize DataTable
122
            $('#super-catalog').DataTable();
123
 
33973 tejus.loha 124
        });
125
});
126
 
127
$(document).on('click', ".save_super_model", function () {
128
    let mappingArr = [];
129
    $('#catalogMapping').find('tr').each(function () {
130
        const catalogId = $(this).find('select#catalog_id').val();
131
        const variantName = $(this).find('input#variant_name').val();
132
        const mappingId = $(this).find('input#mapping_id').val();
133
        if (catalogId > 0) {
134
            mappingArr.push({
135
                catalogId: catalogId,
136
                variantName: variantName,
34070 vikas.jang 137
                superCatalogId: parseInt($('#super_catalog_name').val()) || null,
33973 tejus.loha 138
                id: mappingId
139
            })
140
        }
141
    });
142
    const params = {
143
        id: mappingArr.length ? mappingArr[0].id : 0,
144
        brandId: parseInt($('#brand_id').val()),
145
        mappingArr: mappingArr,
146
        superCatalogName: parseInt($('#super_catalog_name').val()) || $('#super_catalog_name').val(),
147
    };
148
    console.log('params',params);
149
    if (!params.brandId || mappingArr.length === 0 || !params.superCatalogName){
150
        alert("Please fill all details"); return;
151
    }
152
    doAjaxRequestWithJsonHandler(context + "/super-catalog", "POST", JSON.stringify(params), (response) => {
153
        if (response) {
154
            $('.super-catalog-list').trigger('click');
155
            $('#manageSuperCatalogModal').modal('hide');
156
            alert("Saved successfully");
157
        } else {
158
            alert("Something went wrong!");
159
        }
160
    });
161
});
162
 
163
$(document).on('click', ".delete-super-catalog-mapping", function() {
164
    const id = parseInt($(this).closest('td').data('id')) || 0;
165
    console.log("id",id);
166
    if (confirm("Are you sure you want to delete this record")) {
167
        if (id > 0) {
168
            doDeleteAjaxRequestHandler(context + "/super-catalog/" + id, (response) => {
169
                if (response) {
170
                    $(this).parent('td').closest('tr').remove();
171
                    $('.super-catalog-list').trigger('click');
172
                    alert("Deleted successfully");
173
                } else {
174
                    alert("Something went wrong!");
175
                }
176
            });
177
        } else {
178
            $(this).parent('td').closest('tr').remove();
179
        }
180
    }
34070 vikas.jang 181
});
182
 
183
 
184
$(document).on('change', "#brand_id", function() {
185
    loadCatalogOptions($(this).val(), 'select#super_catalog_name', 'superCatalogs');
186
});