Administrator
2022-12-15 9ce4bb2b6bd1378ba7b0bd6681d22cb37a5e1452
提交 | 用户 | 时间
58d006 1 /**
A 2 * date: 
3 * author: neeler 
4 */
5
6 //test data start
7 // var data_getCompanyList = Mock.mock('/company/getList.htm', {code: 0, 'list|5-20': [{id: 'id', companyNo: 'companyNo', note: 'note', name: 'name'}], pages: 12})
8 // var data_getList = Mock.mock('/user/getList.htm', {code: 0, 'list|5-20': [{companyName: 'companyName', id: 'id',name: 'name',pinyin: 'pinyin',headpinyin: 'headpinyin',sex: '男',phone: 'phone',state: false,username: 'username',companyNo: 'companyNo',}], pages: 12})
9 // var data_getCompanyList = Mock.mock('/basicconfig/materielType/getList.htm', {code: 0, 'list|5': [{id: 'id', name: 'name', note: 'note', pinyin: 'pinyin', jianpin: 'jianpin', helpcode: 'helpcode'}], pages: 12})
10 // var data_doDel = Mock.mock('/user/delItem.htm', {code: 0, errmsg: '不能删除!'})
11 // var data_doSave = Mock.mock('/user/saveItem.htm', {code: 0, errmsg: '不能删除!'})
12 // var data_doValid = Mock.mock('/user/saveState.htm', {code: 0, item: true})
13 //test data end
14
15 var TYPE = ['男' ,'女' ]
16 var UTYPE = [{key: '普通用户', value: 'worker'}, {key: '管理员', value: 'admin'}]
17
18 window.I = {
19     PZ: 30,
20     keyword: $('#keyword'),
21     searchBtn: $('#searchBtn'),
22     pageValue: !!$('#p').val() ? $('#p').val() - 1 : 0,
23     keywordValue: $('#k').val(),
24     newObj: $('#newObj'),
25     modal: $('#modal'),
26     save: $('#save')
27 }
28
29 var Obj = {
30     id: 0,
31     name: '',
32     sex: '',
33     phone: '',
34     state: true,
35     username: '',
36     password: '',
37     companyNo: '',
38     utype: ''
39 }
40
41 var ViewModel = function() {
42     var self = this;
43     self.tList = ko.observableArray();
44     self.mtid = ko.observable();
45     self.o = ko.observable(ko.mapping.fromJS(Obj));
46
47     self.keyword = ko.observable(I.keywordValue);
48     self.list = ko.observableArray();
49
50     self.edit = function(item) {
51         doEdit(item);
52     }
53     self.del = function(item) {
54         delConfirm(item);
55     }
56
57     self.change = function() {
58         doOpen();
59     }
60     self.mtvalid=function(item){
61         doValid(item);
62     }   
63
64     self.tyList = ko.observableArray(TYPE);
65     self.utypeList = ko.observableArray(UTYPE);
66 }
67
68 $(function () {
69     window.vm = new ViewModel();
70     ko.applyBindings(vm);
71     doSearch();
72     autoHeight();
73     getCompanyList();
74     pageInit();
75 });
76
77 function pageInit() {
78     $('.table-responsive').scroll(function() {
79         $('.th').css('top', $(this).scrollTop());
80     })
81     I.save.click(function(event) {
82         if (valid()) {
83             doSave();
84         }
85     });
86     I.newObj.click(function(event) {
87         vm.o(ko.mapping.fromJS(Obj));
88         // getNewNo();
89         I.modal.modal('show');
90     });
91     I.searchBtn.click(function(event) {
92         doOpen();
93     });
94     I.modal.on('hidden.bs.modal', function (e) {
95         clearError();
96     });
97     I.keyword.on(ISIE ? 'keydown' : 'keyup', function (event) {
98         if (event.keyCode == 13) {
99             doOpen();
100         }
101     });
102 }
103
104 function doOpen() {
105     doSearch();
106 }
107
108 function doSearch() {
109     getList(vm.keyword(), vm.mtid(), I.pageValue, I.PZ, 1);
110 }
111
112 function getList(keyword, companyNo, page, pagesize, pages) {
113     I._page = page;
114     $.post('/user/getList.htm', {
115         searchword: keyword,
116         companyNo: companyNo,
117         page: page,
118         pagesize: pagesize,
119         pages: pages
120     }, function(data, textStatus, xhr) {
121         vm.list.removeAll();
122         if (data.code >= 1) {
123             //parent.showErrmsg(data.errmsg);
124             swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
125             return;
126         }
127         if (data.code == 0) {
128             if (isList(data.list)) {
129                 $.each(data.list, function(index, val) {
130                     val.state=ko.observable(val.state);
131                     val.password = '';
132                     vm.list.push(val);
133                 });
134                 $('tbody tr').click(function (e) { 
135                     $('tbody tr').removeClass('info');
136                     $(this).addClass('info');
137                 });
138             }
139             if (!!pages) {
140                 pages = data.pages;
141                 $('#pagdiv').unbind('page').empty();
142                 if (pages > 1) {
143                     $('#pagdiv').bootpag({total: pages, maxVisible: 10, page: page + 1}).on('page',function(event,num){
144                         getList(keyword, companyNo, num - 1, pagesize, 0);
145                     });
146                 }
147             }
148             return;
149         }
150     }, 'json');
151 }
152
153 function valid(){
154     var isValid = false;
155     var iF = $('#form');
156     iF.validate({
157         rules: {
158             'name': {
159                 required: true
160             },
161             'phone': {
162                 required: true,
163                 isPhone: true
164             }
165         },
166         messages: {
167             'name': {
168                 required: '不能为空!'
169             },
170             'phone': {
171                 required: '不能为空!',
172                 isPhone: '手机号不正确!'
173             }
174         },
175         errorPlacement: function (err, element) {
176             var p = element.parents('.form-group').eq(0);
177             var d = p.find('.text-error').eq(0);
178             d.css('color', '#D9534F');
179             err.appendTo(d);
180         }
181     })
182     isValid = iF.valid();
183     return isValid;
184 }
185
186 function doEdit(item) {
187     if(isFunction(item.valid)){
188         item.valid=item.valid();
189     }
190     vm.o(ko.mapping.fromJS(item));
191     I.modal.modal('show');
192 }
193
194 function delConfirm(item) {
195     $("#modalContent").html('确定要删除【' + item.name + '】吗?');
196     $("#dialog-confirm").removeClass('hide').dialog({
197         resizable: false,
198         width: '320',
199         modal: true,
200         title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-exclamation-triangle red'></i>确认信息</h4></div>",
201         title_html: true,
202         position: { my: "center", at: "center", of: window },
203         buttons: [
204             {
205                 html: "<i class='ace-icon fa fa-trash-o bigger-110'></i>&nbsp; 确定",
206                 "class" : "btn btn-danger btn-minier",
207                 click: function() {
208                     $( this ).dialog( "close" );
209                     doDel(item);
210                 }
211             }
212             ,
213             {
214                 html: "<i class='ace-icon fa fa-times bigger-110'></i>&nbsp; 取消",
215                 "class" : "btn btn-minier",
216                 click: function() {
217                     $( this ).dialog( "close" );
218                 }
219             }
220         ]
221     });
222 }
223
224 function doDel(item) {
225     if (!!I.AjaxDelItem) return;
226     I.AjaxDelItem = true;
227     $.post('/user/delItem.htm', {id: item.id}, function(data, textStatus, xhr) {
228         I.AjaxDelItem = false;
229         if (data.code >= 1) {
230             //parent.showErrmsg(data.errmsg);
231             swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
232             return;
233         }
234         if (data.code == 0) {
235             vm.list.remove(item);
236             parent.showErrmsg('删除成功!');
237             I.modal.modal('hide');
238             return;
239         }
240     }, 'json');
241 }
242
243 function doSave() {
244     if (!I.AjaxSaveItem &&valid()) {
245         I.AjaxSaveItem = true;
246         I.save.prop('disabled', true);
247         var o = ko.mapping.toJS(vm.o());
248         o.state = $('#state').prop('checked');
249         $.post('/user/saveItem.htm', o, function(data, textStatus, xhr) {
250             I.AjaxSaveItem = false;
251             I.save.prop('disabled', false);
252             if (data.code >= 1) {
253                 //parent.showErrmsg(data.errmsg);
254                 swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
255                 return;
256             }
257             if (data.code == 0) {
258                 parent.showErrmsg('保存成功!');
259                 I.modal.modal('hide');
260                 getList(vm.keyword(), vm.mtid(), I._page, I.PZ, 1);
261                 return;
262             }
263         }, 'json');
264     }
265 }
266
267 function clearError() {
268     $('#form input').removeClass('error');
269     $('#form .text-error').html('');
270 }
271
272 function getCompanyList() {
273     $.post('/company/getList.htm', null, function(data, textStatus, xhr) {
274         vm.tList.removeAll();
275         if (data.code >= 1) {
276             //parent.showErrmsg(data.errmsg);
277             swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
278             return;
279         }
280         if (data.code == 0) {
281             if (isList(data.list)) {
282                 $.each(data.list, function(index, val) {
283                     vm.tList.push(val);
284                 });
285             }
286         }
287     }, 'json');
288 }
289
290
291 function doValid(item){
292     var state=true;
293     if(isFunction(item.state)){
294         state=item.state();
295     }else{
296         state=item.state;
297     }
298     jQuery.post('/user/saveState.htm', {id: item.id, state: !state}, function(data, textStatus, xhr) {
299       //optional stuff to do after success
300       if(data.code==0){
301         item.state(data.item);
302       }else{
303         //parent.showErrmsg(data.errmsg);
304         swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
305       }
306     },'json');
307 }
308
309 function getNewNo() {
310     $.post("/basicconfig/prefix/getNewNo.htm", {type: 'materiel'},
311         function (data, textStatus, jqXHR) {
312             if (data.code >= 1) {
313                 //parent.showErrmsg(data.errmsg);
314                 swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
315                 return;
316             }
317             if (data.code == 0) {
318                 // vm.o().visitNo(data.number);
319                 $('#materielNo').val(data.number);
320                 return;
321             }
322         },
323         "json"
324     );
325 }
326
327 function autoHeight() {
328     $('.page-content').css('min-height', $(parent.window).height() - 117);
329     $('.table-responsive').css('height', $(parent.window).height() - 217);
330 }