2f28fe13881d2c75bbae5cb537722356538d897c
[namibia] / public / js / app / portal-workspace / auction.js
1 ;
2 (function () {
3
4         window._w.auction = {
5
6                 ti: null,
7                 rawData: {},
8                 gridData: {},
9                 dfrom: '',
10                 dto: '',
11                 basketOnly: false,
12                 gridStatus: null,
13                 refresher: false,
14
15
16                 onPageDestruct: function ()
17                 {
18                         App.Event.removeListener('auctionGridSuFilter', 'SuChange');
19                         //_.Api.websocket.close();
20                         //_w.websocketRunning = false;
21                         if (false !== _w.auction.refresher)
22                         {
23                                 clearInterval(_w.auction.refresher);
24                                 _w.auction.refresher = false;
25                         }
26                 },
27
28                 onPushConnect: function()
29                 {
30                         console.log('onPushConnect');
31                         /*if (false !== _w.auction.refresher)
32                         {
33                                 clearInterval(_w.auction.refresher);
34                                 _w.auction.refresher = false;
35                         }*/
36                 },
37
38                 onPushDisconnect: function()
39                 {
40                         if (!_w.websocketRunning)
41                         {
42                                 return;
43                         }
44                         console.log('onPushDisconnect');
45                         /*if (false === _w.auction.refresher)
46                         {
47                                 _w.auction.refresher = setInterval(
48                                         function ()
49                                         {
50                                                 console.log('refreshing auction grid');
51                                                 _w.auction.loadAuctionGrid(null, {}, {});
52                                         },
53                                         120000
54                                 );
55                         }*/
56                 },
57
58                 onPushMessage: function (data)
59                 {
60                         if (_w.auction.gridData && _w.auction.gridData['i' + data.id])
61                         {
62                                 var aid = 'a' + data.id;
63                                 if (0 < _w.auction.rawData.DataSet[aid].numAllBids)
64                                 {
65                                         _w.auction.rawData.DataSet[aid].currentBidCompanyId = data.current_bid.company;
66                                 }
67                                 _w.auction.rawData.DataSet[aid].currentBidPrice = data.current_bid.amount;
68                                 _w.auction.rawData.DataSet[aid].numBids = data.number_of_bids;
69                                 _w.auction.rawData.DataSet[aid].numAllBids = parseInt(_w.auction.rawData.DataSet[aid].numAllBids) + 1;
70                                 _w.auction.rawData.DataSet[aid].numendDateBids = data.expire_datetime;
71                                 if (data.basket.company == App.companyData.id)
72                                 {
73                                         _w.auction.rawData.DataSet[aid].baskets = [[{
74                                                 id: data.basket.id,
75                                                 archived: data.basket.archived
76                                         }]];
77                                 }
78                                 _w.auction._buildGrid();
79                         }
80                         if (_w.bidview && _w.bidview.ti && _w.bidview.ti.published && _w.bidview.itemId == data.id)
81                         {
82                                 console.log();
83                                 App.API.getTask(
84                                         'viewAuction:' + _w.bidview.itemId, 'Auction', 'Auction.View', _w.bidview.itemId,
85                                         {}, $.proxy(_w.bidview._onStockViewDataReceived, _w.bidview), _w.taskContractError
86                                 );
87                         }
88                         if (data.winner != App.companyData.id && data.looser == App.companyData.id)
89                         {
90                                 if (!_w.bidview || !_w.bidview.ti || !_w.bidview.ti.published || _w.bidview.itemId != data.id)
91                                 {
92                                         alert('You were just outbid on the following vehicle: ' + data.vehicle);
93                                 }
94                         }
95                 },
96
97                 onAuctionPageTemplateReady: function (template, event, eventData)
98                 {
99                         _w.auction.ti = _t[template];
100                         if (!_w.websocketRunning || undefined == _w.websocketRunning)
101                         {
102                                 _w.websocketRunning = true;
103                                 _.Api.websocket._initialize();
104                                 _.Api.websocket._messageCallback = _w.auction.onPushMessage;
105                                 _.Api.websocket._connectionOpenedCallback = _w.auction.onPushConnect;
106                                 _.Api.websocket._connectionClosedCallback = _w.auction.onPushDisconnect;
107
108                 console.log('_w.websocketRunning',_w.websocketRunning);
109
110                         }
111                         App.DataStore.setData('auctionstatuses',
112                                 [
113                                         {"value": "Active", "label": "Active"},
114                                         {"value": "Sold", "label": "Purchased"}
115                                 ]
116                         );
117                         App.DataStore.loadSelectListData('filtermakes', true, 'Stock', 'Make.SelectList');
118                         App.DataStore.loadSelectListData('makesmobi', true, 'Stock', 'Make.SelectList');
119                         App.DataStore.loadSelectListData('regionsmobi', true, 'Location', 'Region.SelectList');
120                         App.DataStore.loadSelectListData('regions', true, 'Location', 'Region.SelectList');
121                         _w.auction.ti.hydrate({
122                                 'gridTitle': 'Auction',
123                                 'gridTitleSubtext': 'Please use the filters below to narrow down the view',
124                                 'gridTitleButtons': {
125                                         constructor: 'Button',
126                                         items: {
127                                                 Export: {
128                                                         preset: 'GridExport',
129                                                         id: 'exportAuctionGrid',
130                                                         handler: _w.auction.exportAuctionGrid,
131                                                         permission: App.permissions.stockGridExport
132                                                 },
133                                                 New: {
134                                                         preset: 'SellVehicle',
135                                                         id: 'SellVehicle ',
136                                                         style: 'MakeMobi hidden-tablet hidden-phone',
137                                                         handler: _w.stock.SellVehicle,
138                                                         permission: App.permissions.stockRouteAuction
139                                                                     && App.permissions.stockPage
140                                                                     && App.permissions.stockCreateAuction
141                                                 }
142
143                                         }
144
145                                 },
146                                 // hide all the elements exept for the ones to use on mobi / create duplicate ones for mobi
147                                 'gridContextFilter': {
148                                         constructor: 'GridContextFilter',
149                                         items: {
150
151                                                 RefNum: {
152                                                         title: 'Search here:',
153                                                         style: 'filter-input-wrapper hidden-tablet hidden-phone',
154                                                         id: 'gridAuctionContext_RefNo',
155                                                         placeholder: 'Ref number',
156                                                         context: 'gridAuctionContext'
157                                                 },
158                                                 RegNum: {
159                                                         style: 'filter-input-wrapper hidden-tablet hidden-phone',
160                                                         id: 'gridAuctionContext_RegNo',
161                                                         placeholder: 'Registration number',
162                                                         context: 'gridAuctionContext'
163                                                 },
164                                                 DateFrom: {
165                                                         style: 'input-append date filter-input-wrapper-date mobile-width12p dfrom hidden-tablet hidden-phone',
166                                                         title: 'Search by date here',
167                                                         id: 'gridAuctionContext_DateFrom',
168                                                         placeholder: 'From',
169                                                         context: 'gridAuctionContext',
170                                                         append: '<i class="icon-calendar"></i>',
171                                                         readonly: true,
172                                                         bind: function () {
173                                                                 $('#gridAuctionContext_DateFrom').change(function () {
174                                                                         _w.auction.dFrom = $(this).val();
175                                                                 });
176                                                         }
177                                                 },
178                                                 DateTo: {
179                                                         style: 'input-append date filter-input-wrapper-date mobile-width12p dto hidden-tablet hidden-phone',
180                                                         id: 'gridAuctionContext_DateTo',
181                                                         placeholder: 'To',
182                                                         context: 'gridAuctionContext',
183                                                         append: '<i class="icon-calendar"></i>',
184                                                         readonly: true,
185                                                         bind: function () {
186                                                                 $('#gridAuctionContext_DateTo').change(function () {
187                                                                         _w.auction.dTo = $(this).val();
188                                                                 });
189                                                         }
190                                                 },
191                                                 MakeMobi: {
192                                                         style: 'filter-input-wrapper filter-input-select',
193                                                         placeholder: 'Make Mobi',
194                                                         id: 'gridAuctionFilter_MakeMobi',
195                                                         context: 'gridAuctionContext',
196                                                         dataStoreId: 'filtermakes',
197                                                         selectEmpty: 'All Makes',
198                                                         style: 'MakeMobi hidden-desktop hidden-tablet visible-phone',
199                                                         bind: function (meta) {
200                                                                 $('#gridAuctionFilter_MakeMobi').change(function () {
201                                                                         $('#gridAuctionFilter_Make').selectpicker('val', $(this).val());
202                                                                 });
203                                                         }
204                                                 },
205                                                 RegionMobi: {
206                                                         style: 'filter-input-wrapper filter-input-select ',
207                                                         placeholder: 'Region Mobi',
208                                                         id: 'gridAuctionFilter_RegionMobi',
209                                                         context: 'gridAuctionContext',
210                                                         dataStoreId: 'regionsmobi',
211                                                         selectEmpty: 'Regions',
212                                                         style: 'RegionMobi hidden-desktop hidden-tablet visible-phone',
213                                                         bind: function (meta) {
214                                                                 $('#gridAuctionFilter_RegionMobi').change(function () {
215                                                                         $('#gridAuctionFilter_Region').selectpicker('val', $(this).val());
216                                                                 });
217                                                         }
218                                                 },
219                                                 RegNumMobi: {
220                                                         style: 'filter-input-wrapper hidden-desktop',
221                                                         id: 'gridAuctionContext_RegNoMobi',
222                                                         placeholder: 'Registration number',
223                                                         context: 'gridAuctionContext'
224                                                 },
225                                                 Buttons: {
226                                                         actions: true,
227                                                         searchId: 'btnSearch',
228                                                         bindSearch: _w.auction.searchAuctionGrid,
229                                                         clearId: 'btnClearSearch',
230                                                         bindClear: _w.auction.clearAuctionGridContext
231
232                                                 }
233                                         }
234                                 },
235                                 'gridColumnHeaders': {
236                                         constructor: 'GridColumnHeader',
237                                         items: {
238                                                 Seller: {
239                                                         id: 'gridAuctionColumn_Seller',
240                                                         title: 'SELLER',
241                                                         orderAsc: {
242                                                                 id: 'gridAuctionOrder_SellerAsc',
243                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.auctionCompanyName', 'ASC')
244                                                         },
245                                                         orderDesc: {
246                                                                 id: 'gridAuctionOrder_SellerDesc',
247                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.auctionCompanyName', 'DESC')
248                                                         },
249                                                         style: 'hidden-phone'
250                                                 },
251                                                 Make: {
252                                                         id: 'gridAuctionColumn_Make',
253                                                         title: 'MAKE',
254                                                         orderAsc: {
255                                                                 id: 'gridAuctionOrder_MakeAsc',
256                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.makeId', 'ASC')
257                                                         },
258                                                         orderDesc: {
259                                                                 id: 'gridAuctionOrder_MakeDesc',
260                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.makeId', 'DESC')
261                                                         },
262                                                         style: 'hidden-phone'
263                                                 },
264                                                 Model: {
265                                                         id: 'gridAuctionColumn_Model',
266                                                         title: 'MODEL',
267                                                         orderAsc: {
268                                                                 id: 'gridAuctionOrder_ModelAsc',
269                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.modelName', 'ASC')
270                                                         },
271                                                         orderDesc: {
272                                                                 id: 'gridAuctionOrder_ModelDesc',
273                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.modelName', 'DESC')
274                                                         },
275                                                         style: 'hidden-phone'
276                                                 },
277                                                 Type: {
278                                                         id: 'gridAuctionColumn_Type',
279                                                         title: 'TYPE',
280                                                         orderAsc: {
281                                                                 id: 'gridAuctionOrder_TypeAsc',
282                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.typeName', 'ASC')
283                                                         },
284                                                         orderDesc: {
285                                                                 id: 'gridAuctionOrder_TypeDesc',
286                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.typeName', 'DESC')
287                                                         }
288
289                                                 },
290                                                 Year: {
291                                                         id: 'gridAuctionColumn_Year',
292                                                         title: 'YEAR',
293                                                         orderAsc: {
294                                                                 id: 'gridAuctionOrder_YearAsc',
295                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.yearName', 'ASC')
296                                                         },
297                                                         orderDesc: {
298                                                                 id: 'gridAuctionOrder_YearDesc',
299                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.yearName', 'DESC')
300                                                         },
301                                                         style: 'year'
302                                                 },
303                                                 KM: {
304                                                         id: 'gridAuctionColumn_KM',
305                                                         title: 'KM&#39;S',
306                                                         orderAsc: {
307                                                                 id: 'gridAuctionOrder_KMAsc',
308                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.stockKm', 'ASC')
309                                                         },
310                                                         orderDesc: {
311                                                                 id: 'gridAuctionOrder_KMDesc',
312                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.stockKm', 'DESC')
313                                                         },
314                                                         style: 'kms hidden-phone'
315                                                 },
316                                                 Region: {
317                                                         id: 'gridAuctionColumn_Retail',
318                                                         title: 'REGION',
319                                                         orderAsc: {
320                                                                 id: 'gridAuctionOrder_RegionAsc',
321                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.regionName', 'ASC')
322                                                         },
323                                                         orderDesc: {
324                                                                 id: 'gridAuctionOrder_RegionDesc',
325                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.regionName', 'DESC')
326                                                         },
327                                                         style: 'hidden-phone'
328                                                 },
329                                                 Bid: {
330                                                         id: 'gridAuctionColumn_Bid',
331                                                         title: 'AMOUNT',
332                                                         orderAsc: {
333                                                                 id: 'gridAuctionOrder_BidAsc',
334                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.currentBidPrice', 'ASC')
335                                                         },
336                                                         orderDesc: {
337                                                                 id: 'gridAuctionOrder_BidDesc',
338                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.currentBidPrice', 'DESC')
339                                                         },
340                                                         style: 'amount'
341                                                 },
342                                                 Bids: {
343                                                         id: 'gridAuctionColumn_Bids',
344                                                         title: 'BIDS',
345                                                         orderAsc: {
346                                                                 id: 'gridAuctionOrder_BidsAsc',
347                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.numberOfBids', 'ASC')
348                                                         },
349                                                         orderDesc: {
350                                                                 id: 'gridAuctionOrder_BidsDesc',
351                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.numberOfBids', 'DESC')
352                                                         },
353                                                         style: 'bids hidden-phone'
354                                                 },
355                                                 Time: {
356                                                         id: 'gridAuctionColumn_Time',
357                                                         title: 'TIME LEFT',
358                                                         orderAsc: {
359                                                                 id: 'gridAuctionOrder_TimeAsc',
360                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.endDate', 'ASC')
361                                                         },
362                                                         orderDesc: {
363                                                                 id: 'gridAuctionOrder_TimeDesc',
364                                                                 bind: $.proxy(_w.auction.orderAuctionGrid, this, 'auction.endDate', 'DESC')
365                                                         },
366                                                         style: 'time '
367
368                                                 },
369                                                 BidView: {
370                                                         id: 'gridAuctionColumn_BidView',
371                                                         title: 'BID/VIEW',
372                                                         style: 'hidden-phone'
373                                                 },
374                                                 Basket: {
375                                                         id: 'gridAuctionColumn_Basket',
376                                                         title: 'MY BASKET',
377                                                         style: 'hidden-phone'
378                                                 },
379                                         }
380                                 },
381
382                                 'gridColumnFilters': {
383                                         constructor: 'GridColumnFilter',
384                                         items: {
385                                                 Seller: {
386                                                         id: 'gridAuctionFilter_Seller',
387                                                         context: 'gridAuctionFilter'
388                                                 },
389                                                 Make: {
390                                                         id: 'gridAuctionFilter_Make',
391                                                         context: 'gridAuctionFilter',
392                                                         dataStoreId: 'filtermakes',
393                                                         selectEmpty: 'All',
394                                                         bind: function (meta) {
395                                                                 _w.auction.changes = 0;
396                                                                 $('#' + meta.id).change(function () {
397                                                                         _w.auction.changes++;
398                                                                         if (1 == _w.auction.changes || 'Active' != _w.auction.gridStatus)
399                                                                         {
400                                                                                 return;
401                                                                         }
402                                                                         _w.auction.searchAuctionGrid();
403                                                                 });
404                                                         }
405                                                 },
406                                                 Model: {
407                                                         id: 'gridAuctionFilter_Model',
408                                                         context: 'gridAuctionFilter'
409                                                 },
410                                                 Type: {
411                                                         id: 'gridAuctionFilter_Type',
412                                                         context: 'gridAuctionFilter'
413                                                 },
414                                                 Year: {
415                                                         id: 'gridAuctionFilter_Year',
416                                                         context: 'gridAuctionFilter'
417                                                 },
418                                                 KM: {
419                                                         id: 'gridAuctionFilter_KM',
420                                                         context: 'gridAuctionFilter'
421                                                 },
422                                                 Region: {
423                                                         id: 'gridAuctionFilter_Region',
424                                                         context: 'gridAuctionFilter',
425                                                         dataStoreId: 'regions',
426                                                         selectEmpty: 'All',
427                                                         bind: function (meta) {
428                                                                 _w.auction.changes = 0;
429                                                                 $('#' + meta.id).change(function () {
430                                                                         _w.auction.changes++;
431                                                                         if (1 == _w.auction.changes || 'Active' != _w.auction.gridStatus)
432                                                                         {
433                                                                                 return;
434                                                                         }
435                                                                         _w.auction.searchAuctionGrid();
436                                                                 });
437                                                         }
438                                                 },
439
440                                                 Bid: {
441                                                         id: 'gridAuctionFilter_Bid',
442                                                         context: 'gridAuctionFilter'
443                                                 },
444                                                 Bids: {
445                                                         id: 'gridAuctionFilter_Bids',
446                                                         context: 'gridAuctionFilter'
447                                                 },
448                                                 Time: {id: false},
449                                                 BidView: {},
450                                                 Basket: {},
451                                         }
452                                 },
453                                 'gridRowRepeater': {},
454                                 'gridPager': {}
455                         });
456
457                         // Retrieve initial grid data.
458                         if (null == App.API.taskContract('gridAuction'))
459                         {
460                                 App.API.getTask(
461                                         'gridAuction', 'Auction', 'Auction.Grid', null,
462                                         {}, function () {
463                                                 request = {
464                                                         "Grid": {
465                                                                 "Page": 1,
466                                                                 "Filter": {
467                                                                         'auction.jobState': 'Active'
468                                                                 }
469                                                         }
470                                                 };
471                                                 _w.auction.loadAuctionGrid(null, request, {});
472                                         }, _w.taskContractError
473                                 );
474                         }
475                         else
476                         {
477                                 _w.auction.loadAuctionGrid(null, {}, {});
478                         }
479                 },
480
481                 onAuctionGridPublished: function ()
482                 {
483
484                         //mobi menu
485                         var isMobile = 'none' == $('#mobiCheck').css('display');
486                         if (isMobile)
487                         {
488                                 // show the mobi menu && hide old menu 
489                                 $("#MobiAuctionPriceGuideMenu").show();
490                                 $("#MobiPortalMenu").hide();
491
492                                 // Check active state
493                                 $("#mobiAuctionBtn").addClass("active");
494                                 $("#mobiPriceGuideBtn").removeClass("active");
495                                 $("#mobiValuationsBtn").removeClass("active");
496                         }
497
498                         $("#mobiPriceGuideBtn").click(function () {
499                                 window.location.hash = '#/priceguide';
500                         });
501
502                         $("#mobiValuationsBtn").click(function () {
503                                 window.location.hash = '#/valuations';
504                         });
505
506
507                         $(".gridAuctionFilter").keyup(function (evt) {
508                                 var charCode = evt.charCode || evt.keyCode;
509                                 if (charCode == 13)
510                                 { //Enter key's keycode
511                                         $('#btnSearch').click();
512                                 }
513
514                         });
515                         $(".gridAuctionContext").keyup(function (evt) {
516                                 var charCode = evt.charCode || evt.keyCode;
517                                 if (charCode == 13)
518                                 { //Enter key's keycode
519                                         $('#btnSearch').click();
520                                 }
521
522                         });
523
524                         $('#btnBasket, #btnBasketMobi').click(function () {
525                                 if (_w.auction.basketOnly)
526                                 {
527                                         _w.auction.basketOnly = false;
528                                         $('#btnBasket').html('<i class="icon-shopping-cart icon-white"></i> My basket');
529                                 }
530                                 else
531                                 {
532                                         _w.auction.basketOnly = true;
533                                         $('#btnBasket').html('Show All');
534                                 }
535                                 $('#gridAuctionFilter_basket').val(_w.auction.basketOnly ? '1' : '');
536                                 _w.auction.searchAuctionGrid();
537                         });
538
539                         var setTableFormWidth = function ()
540                         {
541                                 $('.table-form').css({
542                                         width: $(window).width() * 0.98
543                                 });
544                         };
545
546                         $(window).on('resize', setTableFormWidth);
547                         setTableFormWidth();
548                         App.Event.listen('auctionGridSuFilter', 'SuChange', $.proxy(_w.auction.loadAuctionGrid, this), 'Recurring');
549                         $('#exportAuctionGrid').prop('disabled', !App.permissions.auctionGridExport);
550                         $('.dfrom').datepicker({format: "yyyy-mm-dd"});
551                         $('.dto').datepicker({format: "yyyy-mm-dd"});
552                         $('#gridAuctionContext_DateFrom').val(_w.auction.dFrom);
553                         $('#gridAuctionContext_DateTo').val(_w.auction.dTo);
554                         if ('open' != _.Api.websocket._state)
555                         {
556                                 _w.auction.onPushDisconnect();
557                         }
558                 },
559
560                 pageAuctionGrid: function (page)
561                 {
562                         _w.auction.loadAuctionGrid({}, {"Grid": {"Page": page}});
563                 },
564
565                 loadAuctionGrid: function (contract, data, options)
566                 {
567                         App.API.execTask(
568                                 'gridAuction', data, options,
569                                 _w.auction._onAuctionGridDataReceived, _w.taskExecError
570                         );
571                 },
572
573                 _buildGrid: function ()
574                 {
575                         _w.auction.gridData = {};
576                         for (var i in _w.auction.rawData.DataSet)
577                         {
578                                 var row = _w.auction.rawData.DataSet[i];
579
580                                 var checkBox = '';
581                                 if (row.auctionCompanyId != App.companyData.id && row.stockCompanyId != App.companyData.id)
582                                 {
583                                         var checked = row.baskets[0] && row.baskets[0][0] && false == row.baskets[0][0]['archived']
584                                                 ? ' checked'
585                                                 : '';
586                                         checkBox += '<input type="checkbox" id="' + row.id + 'Check" ';
587                                         checkBox += 'value="" class="checkbox checkbox-line" ' + checked + '>';
588                                 }
589                                 if (row.auctionCompanyId == App.companyData.id
590                                     || (0 < row.numAllBids && row.currentBidCompanyId == App.companyData.id)
591                                     || (row.stockCompanyId == App.companyData.id))
592                                 {
593                                         buttonVal = 'View';
594                                         buttonClass = row.auctionCompanyId == App.companyData.id || row.stockCompanyId == App.companyData.id
595                                                 ? ''
596                                                 : 'btn-primary';
597                                 }
598                                 else
599                                 {
600                                         buttonVal = 'Bid/View';
601                                         buttonClass = 0 < row.numBids
602                                                 ? 'btn-success'
603                                                 : '';
604                                 }
605
606                                 var basketId = row.baskets[0] && row.baskets[0][0] && row.baskets[0][0].id
607                                         ? row.baskets[0][0].id
608                                         : false;
609
610                                 _w.auction.gridData['i' + row.id] = {
611                                         surl: 'bidview',
612                                         prepend: {
613                                                 Trade: 'R ',
614                                                 Retail: 'R ',
615                                                 Bid: 'R '
616                                         },
617                                         rowId: row.id,
618                                         basketId: basketId,
619                                         bind: function (meta) {
620                                                 App.Util.updateCheckboxStyle($('#' + meta.rowId + 'Check'));
621                                                 $('#row' + meta.rowId).click(function (e) {
622                                                         setTimeout(function () {
623                                                                 if (!_w.checkBoxChanged)
624                                                                 {
625                                                                         window.location = '#/' + meta.surl + '?id=' + meta.rowId + '&basketId=' + meta.basketId;
626                                                                 }
627                                                                 _w.checkBoxChanged = false;
628                                                         }, 100);
629                                                 });
630                                                 $('#' + meta.rowId + 'Check').prop('data-auction-id', meta.rowId);
631                                                 $('#' + meta.rowId + 'Check').prop('data-basket-id', meta.basketId);
632                                                 $('#' + meta.rowId + 'Check').click(function (e) {
633                                                         _w.checkBoxChanged = true;
634                                                         e.preventDefault();
635                                                         e.stopPropagation();
636                                                         var archived = $('#' + meta.rowId + 'Check').prop('checked')
637                                                                 ? false
638                                                                 : true;
639                                                         var aucId = $('#' + meta.rowId + 'Check').prop('data-auction-id');
640                                                         var basId = $('#' + meta.rowId + 'Check').prop('data-basket-id');
641                                                         if (!basId)
642                                                         {
643                                                                 App.API.getTask(
644                                                                         'createBasket:' + aucId, 'Auction', 'Basket.Create',
645                                                                         aucId, {}, function () {
646                                                                                 App.API.execTask(
647                                                                                         'createBasket:' + aucId, {Basket: {auction: aucId}}, {},
648                                                                                         function () {
649                                                                                                 _w.auction.loadAuctionGrid(null, {}, {});
650                                                                                         }, _w.taskExecError
651                                                                                 );
652                                                                         }, _w.taskContractError
653                                                                 );
654                                                         }
655                                                         else
656                                                         {
657                                                                 App.API.getTask(
658                                                                         'updateBasket:' + aucId, 'Auction', 'Basket.SetStatus',
659                                                                         aucId, {id: basId}, function () {
660                                                                                 App.API.execTask(
661                                                                                         'updateBasket:' + aucId, {Basket: {archived: archived}}, {},
662                                                                                         function () {
663                                                                                                 _w.auction.loadAuctionGrid(null, {}, {});
664                                                                                         }, _w.taskExecError
665                                                                                 );
666                                                                         }, _w.taskContractError
667                                                                 );
668                                                         }
669                                                 });
670                                         },
671                                         style: {
672                                                 Seller: 'hidden-phone',
673                                                 Make: 'make hidden-phone',
674                                                 Model: 'hidden-phone',
675                                                 Type: 'type',
676                                                 Year: 'year',
677                                                 KM: 'hidden-phone',
678                                                 Region: 'hidden-phone',
679                                                 Bid: 'amount',
680                                                 Bids: 'hidden-phone',
681                                                 Time: 'Time',
682                                                 BidView: 'hidden-phone',
683                                                 Basket: 'hidden-phone'
684
685                                         },
686                                         lineClass: buttonClass == 'btn-primary' && buttonVal == 'View'
687                                                 ? 'line-primary'
688                                                 : '',
689                                         items: {
690                                                 'id': {id: row.id, basketId: basketId},
691                                                 'Seller': row.auctionCompanyName,
692                                                 'Make': row.makeName,
693                                                 'Model': row.modelName,
694                                                 'Type': row.typeName,
695                                                 'Year': row.yearName,
696                                                 'KM': row.stockKm,
697                                                 'Region': row.regionName,
698                                                 'Bid': row.currentBidPrice,
699                                                 'Bids': row.numAllBids,
700                                                 'Time': App.Util.calculateTimeLeft(row.created, null, row.endDate),
701                                                 'BidView': '<button class="btn ' + buttonClass + '">' + buttonVal + '</button>',
702                                                 'Basket': checkBox
703                                         }
704                                 };
705                         }
706                         _w.checkBoxChanged = false;
707                         _w.auction.ti.hydrateParam('gridRowRepeater', {
708                                 constructor: 'GridDataRow',
709                                 items: _w.auction.gridData
710                         });
711                 },
712
713                 _onAuctionGridDataReceived: function (response)
714                 {
715                         _w.auction.gridStatus = 'Active';
716                         _w.auction.ti.hydrateParam('gridRowRepeater', {});
717                         _w.auction.basketOnly = undefined != response.Data.Meta.Filters['basket.archived'];
718                         _w.populateFilters(response.Data.Meta.Filters, {
719                                 'auction.referenceNumber': 'gridAuctionContext_RefNo',
720                                 'basket.archived': 'gridAuctionFilter_basket',
721                                 'auction.auctionCompanyName': 'gridAuctionFilter_Seller',
722                                 'auction.modelName': 'gridAuctionFilter_Model',
723                                 'auction.typeName': 'gridAuctionFilter_Type',
724                                 'auction.yearName': 'gridAuctionFilter_Year',
725                                 'auction.stockKm': 'gridAuctionFilter_KM',
726                                 'auction.regionId': 'gridAuctionFilter_Region',
727                                 'auction.makeId': 'gridAuctionFilter_Make',
728                                 'auction.currentBidPrice': 'gridAuctionFilter_Bid',
729                                 'numBids': 'gridAuctionFilter_Bids'
730                         });
731                         $('select.gridAuctionFilter').each(function (i, elem) {
732                                 $(elem).selectpicker('refresh');
733                         });
734                         if (response.Data.Meta.Filters['basket.archived'])
735                         {
736                                 _w.auction.basketOnly = true;
737                                 $('#btnBasket').html('Show All');
738                         }
739                         else
740                         {
741                                 ;
742                                 _w.auction.basketOnly = false;
743                                 $('#btnBasket').html('<i class="icon-shopping-cart icon-white"></i> My basket');
744                         }
745                         $('select.gridAuctionFilter').each(function (i, elem) {
746                                 $(elem).selectpicker('refresh');
747                         });
748                         _w.auction.rawData = {
749                                 DataSet: {},
750                                 Meta: response.Data.Meta
751                         };
752                         for (var i = 0; i < response.Data.DataSet.length; i++)
753                         {
754                                 _w.auction.rawData.DataSet['a' + response.Data.DataSet[i]['id']] = response.Data.DataSet[i];
755                         }
756                         _w.auction._buildGrid();
757                         _w.auction.rawData.Meta.itemName = 'stockPager';
758                         _w.auction.rawData.Meta.bind = _w.auction.pageAuctionGrid;
759                         _w.auction.ti.hydrateParam('gridPager', {
760                                 constructor: 'GridPager',
761                                 items: [_w.auction.rawData.Meta]
762                         });
763                         if (false === _w.auction.refresher)
764                         {
765                                 _w.auction.refresher = setInterval(
766                                         function ()
767                                         {
768                                                 console.log('refreshing auction grid');
769                                                 _w.auction.loadAuctionGrid(null, {}, {});
770                                         },
771                                         120000
772                                 );
773                         }
774                 },
775
776                 exportAuctionGrid: function ()
777                 {
778                         App.API.execTask(
779                                 'gridAuction', {}, {'ExportToExcel': true},
780                                 _w.auction._onExportReceived, _w.taskExecError,
781                                 'EXPORT', true
782                         );
783                 },
784
785                 _onExportReceived: function (response)
786                 {
787                         var win = window.open();
788                         win.document.write(response);
789                 },
790
791                 clearAuctionGridContext: function ()
792                 {
793                         _w.auction.gridStatus = 'Inactive';
794                         try
795                         {
796                                 $('select.gridAuctionFilter').each(function (i, elem) {
797                                         $(elem).selectpicker('val', '');
798                                         $(elem).selectpicker('refresh');
799                                 });
800                         }
801                         catch (err)
802                         {
803                                 console.log(err);
804                         }
805                         $('.gridAuctionContext').val('');
806                         $('.gridAuctionFilter').val('');
807                         _w.auction.basketOnly = false;
808                         $('#btnBasket').html('<i class="icon-shopping-cart icon-white"></i> My basket');
809                         _w.auction.searchAuctionGrid(true);
810                 },
811
812                 searchAuctionGrid: function (resetOrder)
813                 {
814                         _w.auction.gridStatus = 'Inactive';
815                         var filter = {
816                                 count: 0,
817                                 filters: {}
818                         };
819
820                         filter = _w.filterIfnotEmpty(filter, 'gridAuctionContext_DateFrom', 'auction.endDate', '>=');
821                         filter = _w.filterIfnotEmpty(filter, 'gridAuctionContext_DateTo', 'auction.endDate', '<=', ' 23:59:59');
822                         filter = _w.filterIfnotEmpty(filter, 'gridAuctionContext_RegNo', 'auction.registrationNumber');
823                         filter = _w.filterIfnotEmpty(filter, 'gridAuctionContext_RefNo', 'auction.referenceNumber');
824                         filter = _w.filterIfnotEmpty(filter, 'gridAuctionFilter_basket', 'basket.archived', '!=');
825                         filter = _w.filterIfnotEmpty(filter, 'gridAuctionFilter_Make', 'auction.makeId');
826                         filter = _w.filterIfnotEmpty(filter, 'gridAuctionFilter_Seller', 'auction.auctionCompanyName');
827                         filter = _w.filterIfnotEmpty(filter, 'gridAuctionFilter_Model', 'auction.modelName');
828                         filter = _w.filterIfnotEmpty(filter, 'gridAuctionFilter_Type', 'auction.typeName');
829                         filter = _w.filterIfnotEmpty(filter, 'gridAuctionFilter_Year', 'auction.yearName');
830                         filter = _w.filterIfnotEmpty(filter, 'gridAuctionFilter_KM', 'auction.stockKm');
831                         filter = _w.filterIfnotEmpty(filter, 'gridAuctionFilter_Region', 'auction.regionId');
832                         filter = _w.filterIfnotEmpty(filter, 'gridAuctionFilter_Bid', 'auction.currentBidPrice');
833                         filter = _w.filterIfnotEmpty(filter, 'gridAuctionFilter_Bids', 'numberOfBids');
834                         filter.filters['auction.jobState'] = 'Active';
835                         var request = {
836                                 "Grid": {
837                                         "Page": 1,
838                                         "Filter": filter.filters
839                                 }
840                         };
841
842             console.log('resetOrder',resetOrder);
843
844                         //if (resetOrder)
845                         {
846                                 request.Grid.OrderBy = {
847                                         'auction.endDate': 'ASC',
848                                         'auction.auctionCompanyName': 'ASC',
849                     'auction.makeName': 'ASC',
850                     'auction.modelName': 'ASC',
851                     'auction.typeName': 'ASC',
852                     'auction.yearName': 'ASC'
853                                 };
854                         }
855
856                         _w.auction.loadAuctionGrid(null, request, {});
857                 },
858
859                 orderAuctionGrid: function (field, direction)
860                 {
861             console.log('field',field);
862             console.log('direction',direction);
863
864                         var order = {};
865                         order[field] = direction;
866                         _w.auction.loadAuctionGrid({}, {"Grid": {"Page": 1, "OrderBy": order}});
867                 }
868
869         };
870
871 })();