Subversion Repositories SmartDukaan

Rev

Rev 36668 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
36657 ranu 1
<section class="wrapper">
2
    <div class="row">
3
        <div class="col-lg-12">
4
            <h3 class="page-header"><i class="icon_pin_alt"></i> Auth User Base Location</h3>
5
            <ol class="breadcrumb">
6
                <li><i class="fa fa-home"></i><a href="${rc.contextPath}/dashboard">Home</a></li>
7
                <li><i class="icon_group"></i> Partner Acquisition</li>
8
                <li><i class="icon_pin_alt"></i> Base Location</li>
9
            </ol>
10
        </div>
11
    </div>
12
 
13
    <div class="row" style="margin-bottom:10px;">
14
        <div class="col-lg-12" style="display:flex; gap:10px; align-items:center; flex-wrap:wrap;">
15
            <select id="blCategory" class="form-control input-sm" style="width:140px;">
16
                <option value="4">Sales</option>
17
                <option value="20">BGC</option>
18
            </select>
19
            <select id="blLevel" class="form-control input-sm" style="width:140px;">
20
                <option value="">Level</option>
21
                #foreach($e in $escalationTypes)
22
                    <option value="$e">$e</option>
23
                #end
24
            </select>
25
            <select id="blAuthUser" class="form-control input-sm" style="width:240px;">
26
                <option value="">Select User</option>
27
            </select>
28
            <span id="blCurrent" style="color:#777; font-size:12px; margin-left:8px;"></span>
29
        </div>
30
    </div>
31
 
32
    <div class="row">
33
        <div class="col-lg-12" style="margin-bottom:10px;">
34
            <input id="blSearch" type="text" class="form-control input-sm"
35
                   placeholder="Search a place..." style="width:60%; display:inline-block;"
36
                   autocomplete="off">
37
            <span style="font-size:11px; color:#888; margin-left:8px;">
38
                or click on the map to drop a pin
39
            </span>
40
        </div>
41
    </div>
42
 
43
    <div class="row">
44
        <div class="col-lg-9">
45
            <div id="blMap" style="width:100%; height:520px; border-radius:8px; background:#eee;"></div>
46
        </div>
47
        <div class="col-lg-3">
48
            <div style="padding:10px; background:#f8f9fa; border-radius:6px;">
49
                <div style="font-size:12px; color:#555;">Selected:</div>
50
                <div id="blPicked" style="font-weight:600; margin-top:4px;">-</div>
51
                <div id="blPickedCoords" style="font-size:11px; color:#888; margin-top:4px;"></div>
52
                <button id="blSave" class="btn btn-primary" style="width:100%; margin-top:14px;" disabled>
53
                    Save Base Location
54
                </button>
55
                <div id="blMsg" style="margin-top:10px; font-size:12px;"></div>
56
            </div>
57
        </div>
58
    </div>
59
</section>
60
 
61
<script>
62
    // Ensure Google Maps + Places is loaded (works whether page is loaded inline or full)
63
    function blEnsureMaps(cb) {
64
        if (typeof google !== 'undefined' && google.maps && google.maps.places) {
65
            cb();
66
            return;
67
        }
68
        var existing = document.getElementById('gmaps-api-loader');
69
        if (existing) {
70
            var t = setInterval(function () {
71
                if (typeof google !== 'undefined' && google.maps && google.maps.places) {
72
                    clearInterval(t);
73
                    cb();
74
                }
75
            }, 100);
76
            return;
77
        }
78
        var s = document.createElement('script');
79
        s.id = 'gmaps-api-loader';
80
        s.src = 'https://maps.googleapis.com/maps/api/js?key=AIzaSyAckO0y4Z6WhBOuMjNjioWLSYZDhGEvGBc&libraries=places&v=weekly';
81
        s.async = true;
82
        s.onload = function () {
83
            cb();
84
        };
85
        document.head.appendChild(s);
86
    }
87
 
88
    blEnsureMaps(function () {
89
        var blMap, blMarker, blAutocomplete;
90
        var picked = {lat: null, lng: null, name: '', address: ''};
91
 
92
        function setPicked(lat, lng, name, address) {
93
            picked.lat = lat;
94
            picked.lng = lng;
95
            picked.name = name || '';
96
            picked.address = address || name || '';
97
            $('#blPicked').text(name || '(unnamed pin)');
98
            $('#blPickedCoords').text(lat.toFixed(6) + ', ' + lng.toFixed(6));
99
            $('#blSave').prop('disabled', !$('#blAuthUser').val());
100
        }
101
 
102
        function placeMarker(lat, lng) {
103
            var pos = new google.maps.LatLng(lat, lng);
104
            if (!blMarker) {
105
                blMarker = new google.maps.Marker({map: blMap, position: pos, draggable: true});
106
                blMarker.addListener('dragend', function (e) {
107
                    setPicked(e.latLng.lat(), e.latLng.lng(), picked.name, picked.address);
108
                });
109
            } else {
110
                blMarker.setPosition(pos);
111
            }
112
            blMap.panTo(pos);
113
            if (blMap.getZoom() < 12) blMap.setZoom(14);
114
        }
115
 
116
        // Init map (centered on India)
117
        blMap = new google.maps.Map(document.getElementById('blMap'), {
118
            center: {lat: 22.0, lng: 79.0}, zoom: 5
119
        });
120
        blMap.addListener('click', function (e) {
121
            placeMarker(e.latLng.lat(), e.latLng.lng());
122
            setPicked(e.latLng.lat(), e.latLng.lng(), 'Picked location', '');
123
        });
124
 
125
        // Places autocomplete
126
        blAutocomplete = new google.maps.places.Autocomplete(
127
                document.getElementById('blSearch'),
128
                {componentRestrictions: {country: 'in'}}
129
        );
130
        blAutocomplete.addListener('place_changed', function () {
131
            var p = blAutocomplete.getPlace();
132
            if (!p.geometry) return;
133
            var lat = p.geometry.location.lat(), lng = p.geometry.location.lng();
134
            placeMarker(lat, lng);
135
            setPicked(lat, lng, p.name || p.formatted_address || '', p.formatted_address || '');
136
        });
137
 
138
        // Level change -> load users (direct binding with namespace so re-loading
139
        // the page doesn't stack duplicate handlers from previous loads)
140
        $('#blLevel').off('change.bl').on('change.bl', function () {
141
            var level = $(this).val();
142
            $('#blAuthUser').html('<option value="">Select User</option>');
143
            $('#blCurrent').text('');
144
            $('#blSave').prop('disabled', true);
145
            if (!level) return;
146
            console.log('[BL] loading users level=' + level + ' cat=' + $('#blCategory').val());
147
            $.ajax({
148
                url: context + '/beatPlan/getAuthUsers',
149
                type: 'GET',
150
                dataType: 'json',
151
                data: {categoryId: $('#blCategory').val(), escalationType: level},
152
                success: function (r) {
153
                    var data = r.response || r;
154
                    console.log('[BL] users loaded:', data && data.length);
155
                    var h = '<option value="">Select User</option>';
156
                    if (data && data.length) {
157
                        data.forEach(function (u) {
158
                            h += '<option value="' + u.id + '">' + u.name + '</option>';
159
                        });
160
                    } else {
161
                        h = '<option value="">No users at this level</option>';
162
                    }
163
                    $('#blAuthUser').html(h);
164
                },
165
                error: function (xhr) {
166
                    console.error('[BL] getAuthUsers failed', xhr.status, xhr.responseText);
167
                    $('#blAuthUser').html('<option value="">Error loading users</option>');
168
                }
169
            });
170
        });
171
 
172
        // Category change resets level + user
173
        $('#blCategory').off('change.bl').on('change.bl', function () {
174
            $('#blLevel').val('').trigger('change.bl');
175
        });
176
 
177
        // User change -> load current base location (if any)
178
        $('#blAuthUser').off('change.bl').on('change.bl', function () {
179
            var uid = $(this).val();
180
            $('#blCurrent').text('');
181
            $('#blSave').prop('disabled', true);
182
            if (!uid) return;
183
            $.ajax({
184
                url: context + '/beatPlan/getBaseLocation',
185
                type: 'GET', dataType: 'json',
186
                data: {authUserId: uid},
187
                success: function (r) {
188
                    var data = r.response || r;
189
                    if (data && data.locationName) {
190
                        $('#blCurrent').html('Current: <strong>' + data.locationName + '</strong> ('
191
                                + data.latitude + ', ' + data.longitude + ')');
192
                        placeMarker(parseFloat(data.latitude), parseFloat(data.longitude));
193
                        setPicked(parseFloat(data.latitude), parseFloat(data.longitude),
194
                                data.locationName, data.address || '');
195
                    } else {
196
                        $('#blCurrent').html('<em>No base location set yet</em>');
197
                        $('#blSave').prop('disabled', picked.lat == null);
198
                    }
199
                }
200
            });
201
        });
202
 
203
        // Save
204
        $('#blSave').off('click.bl').on('click.bl', function () {
205
            var uid = $('#blAuthUser').val();
206
            if (!uid) {
207
                alert('Pick a user');
208
                return;
209
            }
210
            if (picked.lat == null) {
211
                alert('Pick a location on the map');
212
                return;
213
            }
214
            var btn = $(this);
215
            btn.prop('disabled', true).text('Saving...');
216
            $('#blMsg').text('');
217
 
218
            $.ajax({
219
                url: context + '/beatPlan/updateBaseLocation',
220
                type: 'POST',
221
                data: {
222
                    authUserId: uid,
223
                    locationName: picked.name || ('Location ' + picked.lat.toFixed(4) + ',' + picked.lng.toFixed(4)),
224
                    latitude: String(picked.lat),
225
                    longitude: String(picked.lng),
226
                    address: picked.address
227
                },
228
                success: function (r) {
229
                    var d = r.response || r;
230
                    $('#blMsg').html('<span style="color:#2e7d32;">Saved successfully.</span>');
231
                    btn.prop('disabled', false).text('Save Base Location');
232
                    // Refresh "Current" line
233
                    $('#blAuthUser').trigger('change');
234
                },
235
                error: function (xhr) {
236
                    var msg = 'Save failed';
237
                    try {
238
                        var err = JSON.parse(xhr.responseText);
239
                        msg = (err.response && err.response.message) || msg;
240
                    } catch (e) {
241
                    }
242
                    $('#blMsg').html('<span style="color:#c62828;">' + msg + '</span>');
243
                    btn.prop('disabled', false).text('Save Base Location');
244
                }
245
            });
246
        });
247
    });
248
</script>