Subversion Repositories SmartDukaan

Rev

Rev 36962 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36962 Rev 37120
Line 132... Line 132...
132
                                    ? ' <button class="btn btn-xs btn-success assign-visit-btn"'
132
                                    ? ' <button class="btn btn-xs btn-success assign-visit-btn"'
133
                                    + ' data-userid="' + r.authUserId + '" data-username="' + r.userName + '"'
133
                                    + ' data-userid="' + r.authUserId + '" data-username="' + r.userName + '"'
134
                                    + ' data-beatid="' + r.beatId + '"'
134
                                    + ' data-beatid="' + r.beatId + '"'
135
                                    + ' data-date="' + r.scheduleDate + '">Assign Visit</button>'
135
                                    + ' data-date="' + r.scheduleDate + '">Assign Visit</button>'
136
                                    : '')
136
                                    : '')
-
 
137
                            // Add Lead is allowed for today AND future runs — a lead is a dated stop.
-
 
138
                            + ' <button class="btn btn-xs add-lead-btn"'
-
 
139
                            + ' style="background:#0d9488;color:#fff;border:none;"'
-
 
140
                            + ' data-userid="' + r.authUserId + '" data-username="' + r.userName + '"'
-
 
141
                            + ' data-beatid="' + r.beatId + '"'
-
 
142
                            + ' data-date="' + r.scheduleDate + '">Add Lead</button>'
137
                            : '')
143
                            : '')
138
                    + '</td>'
144
                    + '</td>'
139
                    + '</tr>';
145
                    + '</tr>';
140
        });
146
        });
141
        $('#dayViewRows').html(html);
147
        $('#dayViewRows').html(html);
Line 416... Line 422...
416
                $('#avMsg').html('<span style="color:#c62828;">' + msg + '</span>');
422
                $('#avMsg').html('<span style="color:#c62828;">' + msg + '</span>');
417
                btn.prop('disabled', false).text('Save Assignments');
423
                btn.prop('disabled', false).text('Save Assignments');
418
            }
424
            }
419
        });
425
        });
420
    });
426
    });
-
 
427
 
-
 
428
    // ---------- ADD LEAD ----------
-
 
429
    // Routes the rep's assigned leads onto this scheduled beat/date. Writes an
-
 
430
    // APPROVED lead_route per pick via /beatPlan/leadsForBeat/submit.
-
 
431
    var alCtx = {authUserId: null, userName: '', beatId: null, date: '', allLeads: []};
-
 
432
 
-
 
433
    $(document).on('click', '.add-lead-btn', function () {
-
 
434
        alCtx.authUserId = $(this).data('userid');
-
 
435
        alCtx.userName = $(this).data('username') || '';
-
 
436
        alCtx.beatId = $(this).data('beatid');
-
 
437
        alCtx.date = $(this).data('date');
-
 
438
        alCtx.allLeads = [];
-
 
439
 
-
 
440
        $('#alTitle').text('Add Lead - ' + alCtx.userName + ' on ' + alCtx.date);
-
 
441
        $('#alSearch').val('');
-
 
442
        $('#alLeadList').html('<div style="padding:20px; color:#999; text-align:center;">Loading leads...</div>');
-
 
443
        $('#alMsg').text('');
-
 
444
        $('#alSelectedCount').text('0 selected');
-
 
445
        $('#alSubmit').prop('disabled', true);
-
 
446
        $('#addLeadModal').modal('show');
-
 
447
 
-
 
448
        $.get(context + '/beatPlan/assignedLeads', {
-
 
449
            authUserId: alCtx.authUserId,
-
 
450
            beatId: alCtx.beatId,
-
 
451
            date: alCtx.date
-
 
452
        }).done(function (r) {
-
 
453
            var data = r.response || r;
-
 
454
            alCtx.allLeads = data.rows || [];
-
 
455
            renderAlLeads(alCtx.allLeads);
-
 
456
        }).fail(function (xhr) {
-
 
457
            $('#alLeadList').html('<div style="padding:20px; color:#c62828; text-align:center;">' +
-
 
458
                    'Error loading leads: ' + (xhr.responseText || xhr.statusText) + '</div>');
-
 
459
        });
-
 
460
    });
-
 
461
 
-
 
462
    function renderAlLeads(list) {
-
 
463
        if (!list || list.length === 0) {
-
 
464
            $('#alLeadList').html('<div style="padding:20px; color:#999; text-align:center;">No active leads assigned to this user.</div>');
-
 
465
            return;
-
 
466
        }
-
 
467
        var html = '<table class="table table-condensed table-hover" style="margin-bottom:0; font-size:13px;">';
-
 
468
        html += '<thead><tr>'
-
 
469
                + '<th style="width:30px;"><input type="checkbox" id="alSelectAll" title="Select all"></th>'
-
 
470
                + '<th>Lead</th><th>Outlet</th><th>City</th><th>Stage</th><th style="width:80px;"></th>'
-
 
471
                + '</tr></thead><tbody>';
-
 
472
        list.forEach(function (p) {
-
 
473
            var onBeat = !!p.alreadyOnBeat;
-
 
474
            html += '<tr' + (onBeat ? ' style="opacity:.55;"' : '') + '>'
-
 
475
                    + '<td><input type="checkbox" class="al-lead-chk" data-lead="' + p.leadId + '"'
-
 
476
                    + (onBeat ? ' checked disabled' : '') + '></td>'
-
 
477
                    + '<td>' + (p.leadName || '') + (p.mobile ? ' <span style="color:#999;">· ' + p.mobile + '</span>' : '') + '</td>'
-
 
478
                    + '<td>' + (p.outletName || '') + '</td>'
-
 
479
                    + '<td>' + (p.city || '') + '</td>'
-
 
480
                    + '<td>' + (p.stage || '') + (p.hasGeo ? '' : ' <span class="label label-warning" title="No approved location">no geo</span>') + '</td>'
-
 
481
                    + '<td>' + (onBeat ? '<span class="label label-success">On beat</span>' : '') + '</td>'
-
 
482
                    + '</tr>';
-
 
483
        });
-
 
484
        html += '</tbody></table>';
-
 
485
        $('#alLeadList').html(html);
-
 
486
        updateAlSelectedCount();
-
 
487
    }
-
 
488
 
-
 
489
    $(document).on('input', '#alSearch', function () {
-
 
490
        var q = $(this).val().toLowerCase().trim();
-
 
491
        if (!q) {
-
 
492
            renderAlLeads(alCtx.allLeads);
-
 
493
            return;
-
 
494
        }
-
 
495
        var filtered = alCtx.allLeads.filter(function (p) {
-
 
496
            return (p.leadName || '').toLowerCase().indexOf(q) !== -1
-
 
497
                    || (p.outletName || '').toLowerCase().indexOf(q) !== -1
-
 
498
                    || (p.city || '').toLowerCase().indexOf(q) !== -1
-
 
499
                    || (p.mobile || '').toLowerCase().indexOf(q) !== -1;
-
 
500
        });
-
 
501
        renderAlLeads(filtered);
-
 
502
    });
-
 
503
 
-
 
504
    $(document).on('change', '#alSelectAll', function () {
-
 
505
        $('.al-lead-chk:not(:disabled)').prop('checked', this.checked);
-
 
506
        updateAlSelectedCount();
-
 
507
    });
-
 
508
 
-
 
509
    $(document).on('change', '.al-lead-chk', updateAlSelectedCount);
-
 
510
 
-
 
511
    function updateAlSelectedCount() {
-
 
512
        var n = $('.al-lead-chk:checked:not(:disabled)').length;
-
 
513
        $('#alSelectedCount').text(n + ' selected');
-
 
514
        $('#alSubmit').prop('disabled', n === 0);
-
 
515
    }
-
 
516
 
-
 
517
    $(document).on('click', '#alSubmit', function () {
-
 
518
        var ids = [];
-
 
519
        $('.al-lead-chk:checked:not(:disabled)').each(function () {
-
 
520
            ids.push(parseInt($(this).data('lead')));
-
 
521
        });
-
 
522
        if (ids.length === 0) return;
-
 
523
        var btn = $(this);
-
 
524
        btn.prop('disabled', true).text('Adding...');
-
 
525
        $('#alMsg').text('');
-
 
526
        $.ajax({
-
 
527
            url: context + '/beatPlan/leadsForBeat/submit',
-
 
528
            type: 'POST',
-
 
529
            contentType: 'application/json',
-
 
530
            data: JSON.stringify({
-
 
531
                authUserId: alCtx.authUserId,
-
 
532
                beatId: alCtx.beatId,
-
 
533
                date: alCtx.date,
-
 
534
                leadIds: ids
-
 
535
            }),
-
 
536
            success: function (r) {
-
 
537
                var d = r.response || r;
-
 
538
                $('#alMsg').html('<span style="color:#2e7d32;">' + (d.message || 'Saved') + '</span>');
-
 
539
                btn.prop('disabled', false).text('Add Leads');
-
 
540
                setTimeout(function () {
-
 
541
                    $('#addLeadModal').modal('hide');
-
 
542
                    loadDayView();
-
 
543
                }, 1200);
-
 
544
            },
-
 
545
            error: function (xhr) {
-
 
546
                var msg = 'Failed';
-
 
547
                try {
-
 
548
                    msg = (JSON.parse(xhr.responseText).response.message) || msg;
-
 
549
                } catch (e) {
-
 
550
                }
-
 
551
                $('#alMsg').html('<span style="color:#c62828;">' + msg + '</span>');
-
 
552
                btn.prop('disabled', false).text('Add Leads');
-
 
553
            }
-
 
554
        });
-
 
555
    });
421
</script>
556
</script>
422
 
557
 
-
 
558
<!-- Add Lead Modal -->
-
 
559
<div class="modal fade" id="addLeadModal" tabindex="-1" style="overflow-y:auto;">
-
 
560
    <div class="modal-dialog" style="width:90%; max-width:820px; margin-top:30px;">
-
 
561
        <div class="modal-content">
-
 
562
            <div class="modal-header">
-
 
563
                <button type="button" class="close" data-dismiss="modal">&times;</button>
-
 
564
                <h4 class="modal-title" id="alTitle">Add Lead</h4>
-
 
565
            </div>
-
 
566
            <div class="modal-body">
-
 
567
                <div style="display:flex; gap:10px; align-items:center; margin-bottom:10px;">
-
 
568
                    <input type="text" id="alSearch" class="form-control input-sm"
-
 
569
                           placeholder="Search by name, outlet, city or mobile..." style="flex:1;">
-
 
570
                    <span id="alSelectedCount" style="font-size:12px; color:#666;">0 selected</span>
-
 
571
                </div>
-
 
572
                <div id="alLeadList"
-
 
573
                     style="max-height:50vh; overflow-y:auto; border:1px solid #e5e5e5; border-radius:4px;"></div>
-
 
574
                <div id="alMsg" style="margin-top:10px; font-size:12px;"></div>
-
 
575
            </div>
-
 
576
            <div class="modal-footer">
-
 
577
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
-
 
578
                <button type="button" class="btn btn-success" id="alSubmit" disabled>Add Leads</button>
-
 
579
            </div>
-
 
580
        </div>
-
 
581
    </div>
-
 
582
</div>
-
 
583
 
423
<!-- Assign Visit Modal -->
584
<!-- Assign Visit Modal -->
424
<div class="modal fade" id="assignVisitModal" tabindex="-1" style="overflow-y:auto;">
585
<div class="modal fade" id="assignVisitModal" tabindex="-1" style="overflow-y:auto;">
425
    <div class="modal-dialog" style="width:90%; max-width:900px; margin-top:30px;">
586
    <div class="modal-dialog" style="width:90%; max-width:900px; margin-top:30px;">
426
        <div class="modal-content">
587
        <div class="modal-content">
427
            <div class="modal-header">
588
            <div class="modal-header">