Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
36152 aman 1
<section class="wrapper">
2
    <div class="row">
3
        <div class="col-lg-12">
4
            <h3 class="page-header">
5
                <i class="icon_document_alt"></i>
6
                #if($template) Edit Template: $template.getBrand() - $template.getState()
7
                #else New WOD Template #end
8
            </h3>
9
            <ol class="breadcrumb">
10
                <li><i class="fa fa-home"></i><a href="${rc.contextPath}/dashboard">Home</a></li>
11
                <li><a href="javascript:void(0);" class="wod-template-admin">WOD Template Admin</a></li>
12
                <li>#if($template) Edit #else New #end Template</li>
13
            </ol>
14
        </div>
15
    </div>
16
 
17
    <div class="row">
18
        <div class="col-lg-12">
19
            <div class="panel panel-default">
20
                <div class="panel-heading"><h4 style="margin:0;">Template Settings</h4></div>
21
                <div class="panel-body">
22
                    <input type="hidden" id="tplId" value="#if($template)$template.getId()#{else}0#end">
23
                    <div class="row">
24
                        <div class="col-md-3">
25
                            <label>Brand <span style="color:red;">*</span></label>
26
                            <input type="text" id="tplBrand" class="form-control"
27
                                   value="#if($template)$template.getBrand()#end" placeholder="e.g. Oppo, Vivo">
28
                        </div>
29
                        <div class="col-md-3">
30
                            <label>State/Region <span style="color:red;">*</span></label>
31
                            <input type="text" id="tplState" class="form-control"
32
                                   value="#if($template)$template.getState()#end" placeholder="e.g. UP East, Rajasthan">
33
                        </div>
34
                        <div class="col-md-4">
35
                            <label>Mail To (comma separated) <span style="color:red;">*</span></label>
36
                            <input type="text" id="tplMailTo" class="form-control"
37
                                   value="#if($template)$template.getMailTo()#end"
38
                                   placeholder="email1@domain.com,email2@domain.com">
39
                        </div>
40
                        <div class="col-md-2">
41
                            <label>Active</label>
42
                            <select id="tplActive" class="form-control">
43
                                <option value="true" #if(!$template || $template.isActive()) selected #end>Active
44
                                </option>
45
                                <option value="false" #if($template && !$template.isActive()) selected #end>Inactive
46
                                </option>
47
                            </select>
48
                        </div>
49
                        <div class="col-md-9" style="margin-top:10px;">
50
                            <label>Super Header Text (optional - shown above column headers in Excel/Email)</label>
51
                            <input type="text" id="tplSuperHeader" class="form-control"
52
                                   value="" placeholder="e.g. To be filled in / If Required for Open child Shop">
53
                        </div>
54
                        <div class="col-md-3" style="margin-top:10px;">
55
                            <label>Super Header BG Color</label>
56
                            <input type="color" id="tplSuperHeaderBg" value="#F0F0F0"
57
                                   style="width:60px;height:35px;padding:1px;vertical-align:middle;">
58
                            <span id="tplSuperHeaderBgLabel"
59
                                  style="margin-left:5px;font-size:12px;color:#666;">#F0F0F0</span>
60
                        </div>
61
                    </div>
62
                </div>
63
            </div>
64
        </div>
65
    </div>
66
 
67
    <div class="row">
68
        <div class="col-lg-12">
69
            <div class="panel panel-default">
70
                <div class="panel-heading">
71
                    <h4 style="margin:0;display:inline-block;">Column Configuration</h4>
72
                    <button class="btn btn-sm btn-success pull-right" id="addColumnBtn">
73
                        <i class="fa fa-plus"></i> Add Column
74
                    </button>
75
                </div>
76
                <div class="panel-body" style="overflow-x:auto;">
77
                    <table class="table table-bordered table-condensed" id="columnTable">
78
                        <thead>
79
                        <tr style="background:#f5f5f5;">
80
                            <th style="width:40px;">#</th>
81
                            <th style="width:120px;">Key</th>
82
                            <th style="width:150px;">Label</th>
83
                            <th style="width:90px;">Type</th>
84
                            <th style="width:200px;">Prefill Source</th>
85
                            <th style="width:120px;">Custom Default</th>
86
                            <th style="width:60px;">Required</th>
87
                            <th style="width:110px;">Condition</th>
88
                            <th style="width:50px;">Header BG</th>
89
                            <th style="width:50px;">Font Color</th>
90
                            <th style="width:80px;">Actions</th>
91
                        </tr>
92
                        </thead>
93
                        <tbody id="columnTableBody">
94
                        </tbody>
95
                    </table>
96
                </div>
97
            </div>
98
        </div>
99
    </div>
100
 
101
    <div class="row" style="margin-bottom:15px;">
102
        <div class="col-lg-12">
103
            <div class="panel panel-info">
104
                <div class="panel-heading"><h4 style="margin:0;">Header Preview</h4></div>
105
                <div class="panel-body" style="overflow-x:auto;">
106
                    <div id="previewSuperHeader"
107
                         style="font-weight:bold;font-size:14px;margin-bottom:8px;display:none;text-align:center;background:#f0f0f0;padding:6px;border:1px solid #ccc;"></div>
108
                    <table style="border-collapse:collapse;width:100%;" id="headerPreview">
109
                        <tr id="previewRow"></tr>
110
                    </table>
111
                </div>
112
            </div>
113
        </div>
114
    </div>
115
 
116
    <div class="row">
117
        <div class="col-lg-12">
118
            <button class="btn btn-success btn-lg" id="saveTemplateBtn">
119
                <i class="fa fa-save"></i> Save Template
120
            </button>
121
            <a href="javascript:void(0);" class="btn btn-default btn-lg wod-template-admin" style="margin-left:10px;">
122
                Cancel
123
            </a>
124
        </div>
125
    </div>
126
</section>
127
 
128
<script type="text/javascript">
129
    var prefillOptions = [
130
        {value: '', label: '(No Prefill)'},
131
        {value: 'CUSTOM', label: 'Custom Default Value'},
132
        {value: 'PartnerOnBoardingPanel.outLetName', label: 'Onboarding - Outlet Name'},
133
        {value: 'PartnerOnBoardingPanel.phoneNumber', label: 'Onboarding - Phone'},
134
        {value: 'PartnerOnBoardingPanel.address', label: 'Onboarding - Address'},
135
        {value: 'PartnerOnBoardingPanel.city', label: 'Onboarding - City'},
136
        {value: 'FofoStore.code', label: 'Store - SD Code'},
137
        {value: 'FofoStore.gstNumber', label: 'Store - GST Number'},
138
        {value: 'FofoStore.pan', label: 'Store - PAN'},
139
        {value: 'FofoKyc.email', label: 'KYC - Email'},
140
        {value: 'FofoKyc.state', label: 'KYC - State'},
141
        {value: 'FofoKyc.pincode', label: 'KYC - Pincode'},
142
        {value: 'FofoKyc.firstName', label: 'KYC - First Name'},
143
        {value: 'FofoKyc.lastName', label: 'KYC - Last Name'},
144
        {value: 'FofoKyc.mobile', label: 'KYC - Mobile'},
145
        {value: 'LoiForm.firstName', label: 'LOI - First Name'},
146
        {value: 'LoiForm.lastName', label: 'LOI - Last Name'},
147
        {value: 'LoiForm.mobile', label: 'LOI - Mobile'},
148
        {value: 'LoiForm.email', label: 'LOI - Email'},
149
        {value: 'LoiForm.panNo', label: 'LOI - PAN No'},
150
        {value: 'LoiForm.adharNo', label: 'LOI - Aadhaar No'},
151
        {value: 'LoiForm.gstNo', label: 'LOI - GST No'},
152
        {value: 'LoiForm.companyName', label: 'LOI - Company Name'},
153
        {value: 'LoiForm.gstPin', label: 'LOI - GST Pincode'},
154
        {value: 'LoiForm.gstState', label: 'LOI - GST State'},
155
        {value: 'LoiForm.gstDistrict', label: 'LOI - GST District'},
156
        {value: 'LoiForm.gstCity', label: 'LOI - GST City'},
157
        {value: 'LoiForm.storeArea', label: 'LOI - Store Area'},
158
        {value: 'PartnerDealerMapping.brandCode', label: 'Dealer - DMS/WOD Code'}
159
    ];
160
 
161
    var columnCounter = 0;
162
 
163
    function buildPrefillSelect(selected) {
164
        var html = '';
165
        prefillOptions.forEach(function (opt) {
166
            var sel = (opt.value === (selected || '')) ? 'selected' : '';
167
            html += '<option value="' + opt.value + '" ' + sel + '>' + opt.label + '</option>';
168
        });
169
        return html;
170
    }
171
 
172
    function addColumnRow(col) {
173
        columnCounter++;
174
        var c = col || {};
175
        var html = '<tr class="col-row" data-order="' + columnCounter + '">' +
176
                '<td class="col-order-num">' + columnCounter + '</td>' +
177
                '<td><input type="text" class="form-control input-sm col-key" value="' + (c.key || '') + '" placeholder="field_key"></td>' +
178
                '<td><input type="text" class="form-control input-sm col-label" value="' + (c.label || '') + '" placeholder="Display Label"></td>' +
179
                '<td><select class="form-control input-sm col-type">' +
180
                '<option value="text"' + (c.type === 'text' ? ' selected' : '') + '>Text</option>' +
181
                '<option value="textarea"' + (c.type === 'textarea' ? ' selected' : '') + '>Textarea</option>' +
182
                '<option value="file"' + (c.type === 'file' ? ' selected' : '') + '>File</option>' +
183
                '<option value="date"' + (c.type === 'date' ? ' selected' : '') + '>Date</option>' +
184
                '<option value="number"' + (c.type === 'number' ? ' selected' : '') + '>Number</option>' +
185
                '</select></td>' +
186
                '<td><select class="form-control input-sm col-prefill">' + buildPrefillSelect(c.default_value ? 'CUSTOM' : c.prefill_source) + '</select></td>' +
187
                '<td><input type="text" class="form-control input-sm col-custom-default" value="' + (c.default_value || '') + '" placeholder="Default value" style="' + (c.default_value ? '' : 'display:none;') + '"></td>' +
188
                '<td style="text-align:center;"><input type="checkbox" class="col-required"' + (c.required ? ' checked' : '') + '></td>' +
189
                '<td><select class="form-control input-sm col-condition">' +
190
                '<option value="always"' + ((c.condition || 'always') === 'always' ? ' selected' : '') + '>Always</option>' +
191
                '<option value="distributor"' + (c.condition === 'distributor' ? ' selected' : '') + '>Distributor Only</option>' +
192
                '<option value="non_distributor"' + (c.condition === 'non_distributor' ? ' selected' : '') + '>Non-Distributor Only</option>' +
193
                '</select></td>' +
194
                '<td><input type="color" class="col-header-bg" value="' + (c.header_bg || '#4472C4') + '" style="width:35px;height:30px;padding:1px;"></td>' +
195
                '<td><input type="color" class="col-header-font" value="' + (c.header_font_color || '#FFFFFF') + '" style="width:35px;height:30px;padding:1px;"></td>' +
196
                '<td>' +
197
                '<button class="btn btn-xs btn-default col-move-up" title="Move Up"><i class="fa fa-arrow-up"></i></button> ' +
198
                '<button class="btn btn-xs btn-default col-move-down" title="Move Down"><i class="fa fa-arrow-down"></i></button> ' +
199
                '<button class="btn btn-xs btn-danger col-remove" title="Remove"><i class="fa fa-times"></i></button>' +
200
                '</td>' +
201
                '</tr>';
202
        $('#columnTableBody').append(html);
203
        updatePreview();
204
    }
205
 
206
    function reorderNumbers() {
207
        var idx = 1;
208
        $('#columnTableBody .col-row').each(function () {
209
            $(this).find('.col-order-num').text(idx);
210
            $(this).attr('data-order', idx);
211
            idx++;
212
        });
213
    }
214
 
215
    function updatePreview() {
216
        // Super header
217
        var superText = $('#tplSuperHeader').val();
218
        var superBg = $('#tplSuperHeaderBg').val() || '#F0F0F0';
219
        if (superText && superText.trim()) {
220
            $('#previewSuperHeader').text(superText).css('background-color', superBg).show();
221
        } else {
222
            $('#previewSuperHeader').hide();
223
        }
224
 
225
        // Column headers
226
        var html = '';
227
        $('#columnTableBody .col-row').each(function () {
228
            var label = $(this).find('.col-label').val() || '(empty)';
229
            var bg = $(this).find('.col-header-bg').val();
230
            var fc = $(this).find('.col-header-font').val();
231
            var type = $(this).find('.col-type').val();
232
            if (type === 'file') return;
233
            html += '<td style="border:1px solid #ccc;padding:6px 10px;background-color:' + bg +
234
                    ';color:' + fc + ';font-weight:bold;font-size:12px;white-space:nowrap;text-align:center;">' + label + '</td>';
235
        });
236
        $('#previewRow').html(html);
237
    }
238
 
239
    function collectColumnsJson() {
240
        var columns = [];
241
        var order = 1;
242
        $('#columnTableBody .col-row').each(function () {
243
            var prefillVal = $(this).find('.col-prefill').val();
244
            var col = {
245
                key: $(this).find('.col-key').val().trim(),
246
                label: $(this).find('.col-label').val().trim(),
247
                type: $(this).find('.col-type').val(),
248
                prefill_source: (prefillVal === 'CUSTOM' ? null : prefillVal) || null,
249
                default_value: (prefillVal === 'CUSTOM') ? $(this).find('.col-custom-default').val().trim() : null,
250
                required: $(this).find('.col-required').is(':checked'),
251
                order: order++,
252
                condition: $(this).find('.col-condition').val(),
253
                header_bg: $(this).find('.col-header-bg').val(),
254
                header_font_color: $(this).find('.col-header-font').val()
255
            };
256
            if (col.key && col.label) {
257
                columns.push(col);
258
            }
259
        });
260
        return JSON.stringify(columns);
261
    }
262
 
263
    // Init: load existing data if editing
264
    $(document).ready(function () {
265
        #if($template)
266
            var existingConfig = $template.getColumnsConfig();
267
            if (existingConfig && existingConfig.length > 0) {
268
                existingConfig.forEach(function (col) {
269
                    addColumnRow(col);
270
                });
271
            }
272
            #if($template.getSuperHeader())
273
                $('#tplSuperHeader').val("$template.getSuperHeader().replace('"', '\"').replace("'", "\\'")");
274
            #end
275
            #if($template.getSuperHeaderBg())
276
                $('#tplSuperHeaderBg').val('$template.getSuperHeaderBg()');
277
                $('#tplSuperHeaderBgLabel').text('$template.getSuperHeaderBg()');
278
            #end
279
        #end
280
    });
281
 
282
    // Unbind previous handlers to prevent stacking on AJAX reload
283
    $(document).off('click.wodtpl');
284
    $(document).off('change.wodtpl input.wodtpl');
285
 
286
    // Add column
287
    $(document).on('click.wodtpl', '#addColumnBtn', function () {
288
        addColumnRow();
289
    });
290
 
291
    // Remove column
292
    $(document).on('click.wodtpl', '.col-remove', function () {
293
        $(this).closest('tr').remove();
294
        reorderNumbers();
295
        updatePreview();
296
    });
297
 
298
    // Move up
299
    $(document).on('click.wodtpl', '.col-move-up', function () {
300
        var row = $(this).closest('tr');
301
        var prev = row.prev('.col-row');
302
        if (prev.length) {
303
            row.insertBefore(prev);
304
            reorderNumbers();
305
            updatePreview();
306
        }
307
    });
308
 
309
    // Move down
310
    $(document).on('click.wodtpl', '.col-move-down', function () {
311
        var row = $(this).closest('tr');
312
        var next = row.next('.col-row');
313
        if (next.length) {
314
            row.insertAfter(next);
315
            reorderNumbers();
316
            updatePreview();
317
        }
318
    });
319
 
320
    // Show/hide custom default
321
    $(document).on('change.wodtpl', '.col-prefill', function () {
322
        var row = $(this).closest('tr');
323
        if ($(this).val() === 'CUSTOM') {
324
            row.find('.col-custom-default').show();
325
        } else {
326
            row.find('.col-custom-default').hide().val('');
327
        }
328
    });
329
 
330
    // Live preview on any change
331
    $(document).on('change.wodtpl input.wodtpl', '.col-label, .col-header-bg, .col-header-font, .col-type, #tplSuperHeader, #tplSuperHeaderBg', function () {
332
        updatePreview();
333
        $('#tplSuperHeaderBgLabel').text($('#tplSuperHeaderBg').val());
334
    });
335
 
336
    // Save
337
    $(document).on('click.wodtpl', '#saveTemplateBtn', function () {
338
        var brand = $('#tplBrand').val().trim();
339
        var state = $('#tplState').val().trim();
340
        var mailTo = $('#tplMailTo').val().trim();
341
 
342
        if (!brand || !state || !mailTo) {
343
            alert('Brand, State and Mail To are required.');
344
            return;
345
        }
346
 
347
        var columnsJson = collectColumnsJson();
348
        var parsed = JSON.parse(columnsJson);
349
        if (parsed.length === 0) {
350
            alert('Please add at least one column.');
351
            return;
352
        }
353
 
354
        $(this).prop('disabled', true).text('Saving...');
355
 
356
        $.ajax({
357
            url: context + '/wod-template/save',
358
            type: 'POST',
359
            data: {
360
                id: $('#tplId').val(),
361
                brand: brand,
362
                state: state,
363
                mailTo: mailTo,
364
                columnsConfig: columnsJson,
365
                superHeader: $('#tplSuperHeader').val().trim(),
366
                superHeaderBg: $('#tplSuperHeaderBg').val(),
367
                active: $('#tplActive').val()
368
            },
369
            success: function (resp) {
370
                var result = resp;
371
                try {
372
                    if (typeof resp === 'string') result = JSON.parse(resp);
373
                } catch (e) {
374
                }
375
                if (result && result.success) {
376
                    alert('Template saved successfully!');
377
                    doGetAjaxRequestHandler(context + '/wod-template-admin', function (response) {
378
                        $('#main-content').html(response);
379
                    });
380
                } else {
381
                    alert('Save failed.');
382
                    $('#saveTemplateBtn').prop('disabled', false).html('<i class="fa fa-save"></i> Save Template');
383
                }
384
            },
385
            error: function () {
386
                alert('Save failed. Server error.');
387
                $('#saveTemplateBtn').prop('disabled', false).html('<i class="fa fa-save"></i> Save Template');
388
            }
389
        });
390
    });
391
</script>