Administrator
2022-12-15 9ce4bb2b6bd1378ba7b0bd6681d22cb37a5e1452
提交 | 用户 | 时间
58d006 1 /**
A 2 * date: 
3 * author: neeler 
4 */
5
6 //test data start
7 // var data_getList = Mock.mock('/business/getItem.htm', { "item": { "td_shrq": "YYYY-MM-DD", "td_bmbh": "0601", "td_bmmc": null, "td_djrq": "20170626", "td_khmc": "巩义市贝思特贸易有限公司", "td_bz": "发上海 不开票 不带清单", "td_ywrq": "20170626", "td_zdr": "王雅", "td_tdbh": "TD-17060001", "td_tdls": "43" }, "code": 0, "list|3-50": [{"tdmx_wlbh": "D8F001","tdmx_wlmc": "8系热轧箔","tdmx_tdfl": "0000000001","tdmx_tdls": "43","tdmx_hjzt": null,"tdmx_dwmx": "吨","tdmx_pch": "GFC1706N80075-1","tdmx_bhse": 15372.56,"tdmx_mz": 0.764,"tdmx_pz": 0,"tdmx_jz": 0.679,"tdmx_shdw": "巩义市贝思特贸易有限公司","tdmx_fhdw": "河南眀泰","tdmx_tdlx": "欠款提货","tdmx_sl": 17,"tdmx_ggxh": "0.04*500*C","tdmx_sfzc": "0.0","tdmx_cph": "豫C555555", "tdmx_bz": "tdmx_bz", "tdmx_hw": "tdmx_hw"}]})
8 // var data_getVehicleList = Mock.mock('/business/getVehicleList.htm', {code: 0, 'list|5-30': [{vehicleId: 'vehicleId', feature: 'feature', note: 'note', fdnum: 'fdnum', fdweight: '4.551', vehicleweight: 'vehicleweight', loadweight: '51.111', driver: 'driver', phone: 'phone'}]})
9 // var data_validVehicle = Mock.mock('/business/validVehicle.htm', {code: 0, errmsg: '车牌号不能使用!'})
10 // var data_doUnbind = Mock.mock('/business/doUnbind.htm', {code: 0, })
11 // var data_doBind = Mock.mock('/business/doBind.htm', {code: 0, })
12 // var data_getallList = Mock.mock('/vehicle/getallList.htm', {"rowCount":0,"pagesize":0,"pages":0,"page":0,"items":null,"list":[{"note":null,"companyNo":"009","driver":"九阳神功","loadweight":15.0,"phone":"13007607721","companyName":"运输部","rownumber":1,"id|+1": 1,"state":1,"vehicleId":"豫A11111","vehicleweight":10.0}],"hint":null,"errmsg":null,"code":0})
13 // var data_saveItem = Mock.mock('/business/saveItem.htm', {code: 0, })
14 // var data_doDel = Mock.mock('/transport/yscompany/del.htm', {code: 0, errmsg: 'errmsg'})
15 // var data_doSave = Mock.mock('/transport/yscompany/save.htm', {code: 0, errmsg: 'errmsg'})
16 //test data end
17
18 window.I = {
19     _lastIndex: 0,
20     nowVehicle: null,
21     no: $('#no').val(),
22     allNumBtn: $('#allNumBtn'),
23     bindNumBtn: $('#bindNumBtn'),
24     unbindNumBtn: $('#unbindNumBtn'),
25     vehicleKeyword: $('#vehicleKeyword'),
26     searchVehicleBtn: $('#searchVehicleBtn'),
27     refreshVehicleBtn: $('#refreshVehicleBtn'),
28     tempVehicleModal: $('#tempVehicleModal'),
29     addTempVehicle: $('#addTempVehicle'),
30     tempVehicleSaveBtn: $('#tempVehicleSaveBtn'),
31     selectAllBtn: $('#selectAllBtn'),
32     selectNoBtn: $('#selectNoBtn'),
33     clearSelectBtn: $('#clearSelectBtn'),
34     bindBtn: $('#bindBtn'),
35     unbindBtn: $('#unbindBtn')
36 }
37
38 var Obj = {
39     vehicleId: '豫A',
40     driver: '',
41     phone: '',
42     IDNum: '',
43     vehicleweight: '',
44     loadweight: '',
45     note: '',
46     feature: ''
47 }
48
49 var TD = {
50     td_bmbh: '',
51     td_bmmc: '',
52     td_bz: '',
53     td_djrq: '',
54     td_khmc: '',
55     td_tdbh: '',
56     td_tdls: '',
57     td_ywrq: '',
58     td_zdr: '',
59     td_shrq: '',
60     td_shsj: ''
61 }
62
63 var ViewModel = function() {
64     var self = this;
65     self.o = ko.observable(ko.mapping.fromJS(Obj));
66     self.td = ko.observable(TD);
67     self.vList = ko.observableArray();
68     self.list = ko.observableArray();
69
70     self.isAdmin = ko.observable(typeof(parent.G) != 'undefined' ? parent.G.isAdmin : false);
71
72     self.isSelectAll = ko.observable(false);
73     self.allNum = ko.observable(0);
74     self.bindNum = ko.observable(0);
75     self.unbindNum = ko.observable(0);
76
77     self.selectedNum = ko.observable(0);
78     self.selectedMZ = ko.observable(0);
79     self.selectedJZ = ko.observable(0);
80
81     self.select = function (item) {
82         I.nowVehicle = {};
83         for(attr in item) {
84             I.nowVehicle[attr] = item[attr];
85         }
86         $('#_vhicleId').text(item.vehicleId);
87         $('#_lastweight').text(item.lastweight);
88         $('#_fdweight').text(item.fdweight);
89         $('#_fdnum').text(item.fdnum);
90         $('#_loadweight').text(item.loadweight);
91         $('#vehicleMessage').show();
92     }
93     self.check = function (item) {
94         self.list.removeAll();
95         $.each(I._MDB, function (index, value) { 
96             if (value.tdmx_cph() === item.vehicleId) {
97                 self.list.push(value);
98             }
99         });
100         setMaterialTableEvent();
101         computedSelect();
102     }
103     self.vehicleTasks = function (item) {
104         gotoVehicleDetails(item);
105     }
106 }
107
108 $(function () {
109     window.vm = new ViewModel();
110     ko.applyBindings(vm);
111     autoHeight();
112     getVehicleList();
113     if (!!I.no) {
114         getList(I.no);
115     }
116     pageInit();
117 });
118
119 function pageInit() {
120     I.unbindBtn.click(function (e) { 
121         e.preventDefault();
122         if ($('.mitem.info').length) {
123             validMessage('确定要取消配车吗?', doUnbind)
124         } else {
125             parent.showErrmsg('请选择一个提单物资!');
126         }
127     });
128     I.bindBtn.click(function (e) { 
129         e.preventDefault();
130         if ($('.mitem.info').length) {
131             doBind();
132         } else {
133             parent.showErrmsg('请选择一个提单物资!');
134         }
135     });
136     I.tempVehicleSaveBtn.click(function (e) { 
137         e.preventDefault();
138         if (valid()) {
139             validVehicle();
140         }
141     });
142     I.addTempVehicle.click(function (e) { 
143         e.preventDefault();
144         vm.o(ko.mapping.fromJS(Obj));
145         I.tempVehicleModal.modal('show');
146     });
147     I.tempVehicleModal.on('hidden.bs.modal', function (e) {
148         clearError();
149     });
150     I.vehicleKeyword.keyup(function (e) { 
151         updateVList();
152     });
153     I.searchVehicleBtn.click(function (e) { 
154         e.preventDefault();
155         updateVList();
156     });
157     I.refreshVehicleBtn.click(function (e) { 
158         e.preventDefault();
159         getVehicleList();
160     });
161     I.allNumBtn.click(function (e) { 
162         e.preventDefault();
163         $('.mStatus').removeClass('btn-info');
164         $(this).addClass('btn-info');
165         vm.list.removeAll();
166         $.each(I._MDB, function (index, value) { 
167              vm.list.push(value);
168         });
169         setMaterialTableEvent();
170         computedSelect();
171     });
172     I.bindNumBtn.click(function (e) { 
173         e.preventDefault();
174         $('.mStatus').removeClass('btn-info');
175         $(this).addClass('btn-info');
176         vm.list.removeAll();
177         $.each(I._MDB, function (index, value) { 
178             if (value.tdmx_cph() !== '') {
179                 vm.list.push(value);
180             }
181         });
182         setMaterialTableEvent();
183         computedSelect();
184     });
185     I.unbindNumBtn.click(function (e) { 
186         e.preventDefault();
187         $('.mStatus').removeClass('btn-info');
188         $(this).addClass('btn-info');
189         vm.list.removeAll();
190         $.each(I._MDB, function (index, value) { 
191             if (value.tdmx_cph() == '') {
192                 vm.list.push(value);
193             }
194         });
195         setMaterialTableEvent();
196         computedSelect();
197     });
198     I.selectAllBtn.click(function (e) { 
199         e.preventDefault();
200         $('.mitem').addClass('info');
201         computedSelect();
202     });
203     I.selectNoBtn.click(function (e) { 
204         e.preventDefault();
205         $('.mitem').removeClass('info');
206         computedSelect();
207     });
208     I.clearSelectBtn.click(function (e) { 
209         e.preventDefault();
210         $('.mitem').removeClass('info');
211         computedSelect();
212     });
213 }
214
215 function doBind() {
216     if (!!I.AjaxdoBind) {
217         return;
218     }
219     var list = [];
220     var vehicle = I.nowVehicle;
221     if (!vehicle) {
222         parent.showErrmsg('请选择一个车辆!');
223         return;
224     }
225     var result = true;
226     $.each($('.mitem.info'), function (index, value) { 
227         var indx = $(this).attr('data-indx');
228         $.each(vm.list(), function (index, value) { 
229             if (indx === value.indx) {
230                 if (value.tdmx_cph() != '') {
231                     swal({title:'',text:'选择的配车物资' + value.tdmx_wlmc + '已经配过车!',type:'error',confirmButtonText:'确定'}); 
232                     result = false;
233                     return false;
234                 } else {
235                     list.push({
236                         tdls: value.tdmx_tdls,
237                         tdfl: value.tdmx_tdfl
238                     })
239                 }
240             }
241         });
242         if (!result) {
243             return false;
244         }
245     });
246     if (!result) {
247         return;
248     }
249     I.AjaxdoBind = true;
250     I.bindBtn.prop('disabled', true);
251     $.post("/business/doBind.htm", {list: $.toJSON(list), vehicle: $.toJSON(vehicle), tdbh: I.no},
252         function (data, textStatus, jqXHR) {
253             I.AjaxdoBind = false;
254             I.bindBtn.prop('disabled', false);
255             if (data.code >= 1) {
256                 swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
257                 return;
258             }
259             if (data.code == 0) {
260                 showErrmsg('配车成功!');
261                 $.each($('.mitem.info'), function (index, value) { 
262                     var indx = $(this).attr('data-indx');
263                     $.each(I._MDB, function (index, value) { 
264                         if (indx === value.indx) {
265                             value.tdmx_cph(I.nowVehicle.vehicleId);
266                         }
267                     });
268                     $.each(vm.list(), function (index, value) { 
269                         if (indx === value.indx) {
270                             value.tdmx_cph(I.nowVehicle.vehicleId);
271                         }
272                     });
273                 });
274                 updateNum();
275                 getVehicleList();
276                 return;
277             }
278         },
279         "json"
280     );
281 }
282
283 function doUnbind() {
284     if (!!I.AjaxdoUnbind) {
285         return;
286     }
287     var list = [];
288     var arrIndx = [];
289     $.each($('.mitem.info'), function (index, value) { 
290         var indx = $(this).attr('data-indx');
291         $.each(vm.list(), function (index, value) { 
292             if (indx === value.indx) {
293                 if (value.tdmx_cph() === '') {
294                     return true;
295                 } else {
296                     list.push({
297                         tdls: value.tdmx_tdls,
298                         tdfl: value.tdmx_tdfl,
299                         vehicleId: value.tdmx_cph()
300                     })
301                     arrIndx.push(value.indx);
302                 }
303             }
304         });
305     });
306     if (!list.length) {
307         return;
308     }
309     I.AjaxdoUnbind = true;
310     I.unbindBtn.prop('disabled', true);
311     $.post("/business/doUnbind.htm", {list: $.toJSON(list), tdbh: I.no},
312         function (data, textStatus, jqXHR) {
313             I.AjaxdoUnbind = false;
314             I.unbindBtn.prop('disabled', false);
315             if (data.code >= 1) {
316                 swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
317                 return;
318             }
319             if (data.code == 0) {
320                 showErrmsg('取消配车成功!');
321                 $.each(arrIndx, function (index, iv) { 
322                     $.each(I._MDB, function (index, value) { 
323                         if (iv === value.indx) {
324                             value.tdmx_cph('');
325                         }
326                     });
327                     $.each(vm.list(), function (index, value) { 
328                         if (iv === value.indx) {
329                             value.tdmx_cph('');
330                         }
331                     });
332                 });
333                 updateNum();
334                 getVehicleList();
335                 return;
336             }
337         },
338         "json"
339     );
340 }
341
342 function getList(keyword) {
343     $.post('/business/getItem.htm', {
344         keyword: keyword
345     }, function(data, textStatus, xhr) {
346         vm.list.removeAll();
347         if (data.code >= 1) {
348             //parent.showErrmsg(data.errmsg);
349             swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
350             return;
351         }
352         if (data.code == 0) {
353             I._MDB = [];
354             data.item.td_shsj = data.item.td_shsj.slice(0, 19);
355             vm.td(data.item);
356             if (isList(data.list)) {
357                 $.each(data.list, function (index, value) { 
358                     value.tdmx_cph = ko.observable(value.tdmx_cph);
359                     value.indx = index + '';
360                     vm.list.push(value);
361                     I._MDB.push(value);
362                 });
363             }
364             setMaterialTableEvent();
365             updateNum();
366             return;
367         }
368     }, 'json');
369 }
370
371 function updateNum() {
372     var allNum = I._MDB.length;
373     var unbindNum = 0;
374     $.each(I._MDB, function (index, value) { 
375         value.tdmx_cph() === '' && unbindNum++;
376     });
377     vm.allNum(allNum);
378     vm.bindNum(allNum - unbindNum);
379     vm.unbindNum(unbindNum);
380 }
381
382 function setMaterialTableEvent() {
383     $('.mitem').unbind('click').click(function (e) { 
384         var index = $('.mitem').index($(this));
385         e.preventDefault();
386         if (e.shiftKey) {
387             setMaterialTrSelected(Math.min(index, I._lastIndex), Math.max(index, I._lastIndex))
388         } else {
389             I._lastIndex = index;
390             $(this).toggleClass('info');
391         }
392         computedSelect();
393     });
394 }
395
396 function setMaterialTrSelected(start, end) {
397     $.each($('.mitem'), function (index, value) { 
398          if (index >= start && index <= end) {
399              $(this).addClass('info');
400          }
401     });
402 }
403
404 function computedSelect() {
405     var selectedNum = $('.mitem.info').length;
406     var selectedMZ = 0;
407     var selectedJZ = 0;
408     $.each($('.mitem.info'), function (index, value) { 
409          var indx = $(this).attr('data-indx');
410          $.each(I._MDB, function (j, jv) { 
411               if (jv.indx === indx) {
412                 selectedMZ += jv.tdmx_mz;
413                 selectedJZ += jv.tdmx_jz;
414               }
415          });
416     });
417     vm.selectedNum(selectedNum);
418     vm.selectedMZ(selectedMZ.toFixed(3));
419     vm.selectedJZ(selectedJZ.toFixed(3));
420     vm.isSelectAll($('.mitem').length === $('.mitem.info').length);
421 }
422
423 function getVehicleList() {
424     $.post("/business/getVehicleList.htm", {companyNo: vm.isAdmin() ? '' : (typeof(parent.G) != 'undefined' ? parent.G.loginCompanyNo : 'ABCDEFG')},
425         function (data, textStatus, jqXHR) {
426             I.VDB = [];            
427             if (data.code >= 1) {
428                 swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
429                 return;
430             }
431             if (data.code == 0) {
432                 if (isList(data.list)) {
433                     $.each(data.list, function (index, value) { 
434                         value.indx = index + '';
435                         value.lastweight = '';
436                         if ($.isNumeric(value.loadweight) && $.isNumeric(value.fdweight)) {
437                             value.lastweight = (parseFloat(value.loadweight) - parseFloat(value.fdweight)).toFixed(3);
438                         }
439                         I.VDB.push(value);
440                     });
441                 }
442                 updateVList();
443                 return;
444             }
445         },
446         "json"
447     );
448 }
449
450 function updateVList() {
451     var str = I.vehicleKeyword.val();
452     vm.vList.removeAll();
453     if (!!str) {
454         var reg = new RegExp(str);
455         $.each(I.VDB, function (index, value) { 
456              if (reg.test(value.vehicleId)) {
457                  vm.vList.push(value);
458              }
459         });
460     } else {
461         $.each(I.VDB, function (index, value) { 
462              vm.vList.push(value);
463         });
464     }
465     // $('.vitem').click(function (e) { 
466     //     $('.vitem').removeClass('info');
467     //     $(this).addClass('info');
468     // });
469 }
470
471 function valid(){
472     var isValid = false;
473     var iF = $('#form');
474     iF.validate({
475         rules: {
476             'vehicleId': {
477                 required: true,
478                 isPlateNumber: true
479             },
480             'driver': {
481                 required: true,
482             },
483             'phone': {
484                 required: true,
485                 isPhone: true
486             },
487             'IDNum': {
488                 required: true,
489             }
490         },
491         messages: {
492             'vehicleId': {
493                 required: '不能为空!',
494                 isPlateNumber: '车牌号不正确!'
495             },
496             'driver': {
497                 required: '不能为空!',
498             },
499             'phone': {
500                 required: '不能为空!',
501                 isPhone: '手机号不正确!'
502             },
503             'IDNum': {
504                 required: '不能为空!',
505             }
506         },
507         errorPlacement: function (err, element) {
508             var p = element.parents('.form-group').eq(0);
509             var d = p.find('.text-error').eq(0);
510             d.css('color', '#D9534F');
511             err.appendTo(d);
512         }
513     })
514     isValid = iF.valid();
515     return isValid;
516 }
517
518 function validVehicle() {
519     $.post("/business/validVehicle.htm", {vehicleId: ko.mapping.toJS(vm.o()).vehicleId.toUpperCase()},
520         function (data, textStatus, jqXHR) {
521             if (data.code >= 1) {
522                 swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
523                 return;
524             }
525             if (data.code == 0) {
526                 I.nowVehicle = {};
527                 var obj = ko.mapping.toJS(vm.o());
528                 for(attr in obj) {
529                     I.nowVehicle[attr] = obj[attr];
530                 }
531                 $('#_vhicleId').text(I.nowVehicle.vehicleId);
532                 $('#_lastweight').text(I.nowVehicle.lastweight);
533                 $('#_fdweight').text(I.nowVehicle.fdweight);
534                 $('#_fdnum').text(I.nowVehicle.fdnum);
535                 $('#_loadweight').text(I.nowVehicle.loadweight);
536                 $('#vehicleMessage').show();
537                 I.tempVehicleModal.modal('hide');
538                 return;
539             }
540         },
541         "json"
542     );
543 }
544
545 function clearError() {
546     $('#form input').removeClass('error');
547     $('#form .text-error').html('');
548 }
549
550 function autoHeight() {
551     $('.page-content').css('min-height', $(parent.window).height() - 117);
552     $('.table-responsive').css('height', $(parent.window).height() - 275);
553     $('.split').css('left', $('.table-vehicle').width() + 12);
554 }
555
556 function validMessage(message, cb, cbparams) {
557     $("#modalContent").html(message);
558     $("#dialog-confirm").removeClass('hide').dialog({
559         resizable: false,
560         width: '320',
561         modal: true,
562         title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-exclamation-triangle red'></i>确认信息</h4></div>",
563         title_html: true,
564         position: { my: "center", at: "center", of: window },
565         buttons: [
566             {
567                 html: "<i class='ace-icon fa fa-trash-o bigger-110'></i>&nbsp; 确定",
568                 "class" : "btn btn-danger btn-minier",
569                 click: function() {
570                     $( this ).dialog( "close" );
571                     cb(cbparams);
572                 }
573             }
574             ,
575             {
576                 html: "<i class='ace-icon fa fa-times bigger-110'></i>&nbsp; 取消",
577                 "class" : "btn btn-minier",
578                 click: function() {
579                     $( this ).dialog( "close" );
580                 }
581             }
582         ]
583     });
584 }
585
586 function gotoVehicleDetails(item) {
587     parent.closableTab.addTab({
588         'id': new Date().getTime(),
589         'name': item.vehicleId + '的配车物资',
590         'url': '/basic/vehicleBindMaterials.jsf?numberplates=' + encodeURI(encodeURI(item.vehicleId)),
591         'closable': true
592     });
593 }