Administrator
2023-04-17 63fbfddabe08e353ad75e495c2ac8dc5203da88c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
* date: 
* author: neeler
*/
 
// test data start
// var data_getList = Mock.mock('/area/getList.htm', {"rowCount":0,"pagesize":0,"pages":0,"page":0,"items":null,
//     "list":[
//     {"name":"河南省","bianhao":"01","xsbianhao":"01","mx":false,'jishu':1},
//         {"name":"河南省郑州市","bianhao":"0101","xsbianhao":"01","mx":true,'jishu':2},
//         {"name":"河南省洛阳市","bianhao":"0102","xsbianhao":"02","mx":false,'jishu':2},
//         {"name":"河南省洛阳市偃师市","bianhao":"010201","xsbianhao":"01","mx":true,'jishu':3},
//     {"name":"山东省","bianhao":"02","xsbianhao":"02","mx":false,'jishu':1}
//     ],"hint":null,"errmsg":null,"code":0}) 
// var data_gecmtList = Mock.mock('/area/getallCMlist.htm', {"rowCount":0,"pagesize":0,"pages":0,"page":0,"items":null,
//     "list":[
//     {"name":"郑州鑫玉升运输有限公司","companyNo":"001"},
//     {"name":"河南天海物流有限公司","companyNo":"002"},
//     {"name":"洛阳市汇宇汽车运输有限公司","companyNo":"003"},
//     {"name":"河南龙威货运有限公司","companyNo":"004"},
//     {"name":"王学寨","companyNo":"005"}
//     ],"hint":null,"errmsg":null,"code":0})
// var date_getsecmlist = Mock.mock('/area/getcmList.htm', {code: 0, item: '001,003' })
// var data_delItem = Mock.mock('/area/delItem.htm', {code: 0, errmsg: '删除失败!'})
// var data_saveItem = Mock.mock('/area/saveItem.htm', {code: 0, errmsg: '保存成功!'})
 
window.I = {
    PZ: 0,
    addSibling: $('#addSibling'),
    addChild: $('#addChild'),
    search: $('#search'),
    save: $('#save'),
    DB_ep: {},
    GLXJ:$('#GLXJ')
}
 
function EnterpriseObj() {
    return {
        name: '',
        bianhao: '',//编号
        jishu:'',//级数
        xsbianhao: '',//显示的编号
        pbianhao:'',//父节点
        isadd:false//是否是新增
    }
}
 
var ViewModel = function() {
    var self = this;
    //待编辑对象
    self.o = ko.observable(ko.mapping.fromJS(EnterpriseObj()));
    //是否可以删除
    self.canDelete = ko.observable(false);
    //是否可以添加子节点
    self.canAddChild = ko.observable(false);
    //是否修改下级
    self.canGLXJ=ko.observable(false);
    //删除操作
    self.del = function() {
        delConfirm();
    }
    //已选择的物流公司
    self.selectedList=ko.observableArray();
    //全部的物流公司
    self.allCMlist=ko.observableArray();
}
 
$(function () {
    window.vm = new ViewModel();
    ko.applyBindings(vm);
    getList();
    pageInit();
    getallCMlist();
});
 
function pageInit() {
    I.save.click(function(event) {
        saveItem();
    });
    I.addSibling.click(function(event) {
        var o = EnterpriseObj();
        o.jishu = I.DB_ep[I.selectedId] && I.DB_ep[I.selectedId].jishu;
        o.pbianhao=I.DB_ep[I.selectedId] && I.DB_ep[I.selectedId].bianhao.substring(0,I.DB_ep[I.selectedId].bianhao.length-2);
        vm.o(ko.mapping.fromJS(o));
        vm.selectedList([]);
        $('#dp').val('').trigger("chosen:updated");
        I.GLXJ.attr("checked",false);
        vm.canGLXJ(false);
    });
    I.addChild.click(function(event) {
        var o = EnterpriseObj();
         o.jishu = I.DB_ep[I.selectedId] && I.DB_ep[I.selectedId].jishu;
         o.jishu+=1;
         o.pbianhao=I.DB_ep[I.selectedId] && I.DB_ep[I.selectedId].bianhao;
        vm.o(ko.mapping.fromJS(o));
        vm.selectedList([]);
        $('#dp').val('').trigger("chosen:updated");
        I.GLXJ.attr("checked",false);
        vm.canGLXJ(false);
    });
    I.search.keyup(function(event) {
        var to = false;
        if(to) {
            clearTimeout(to);
        }
        to = setTimeout(function () {
            var v = I.search.val();
            $('#jstree_Enterprise').jstree(true).search(v);
        }, 250);
    });
    I.GLXJ.change(function(event){
        if(I.GLXJ.prop("checked")){
            I.GLXJ.attr("checked",true);
        }else{
            I.GLXJ.attr("checked",false);
        }
    });
}
 
function getList(keyword, page, pagesize, pages) {
    $.post('/area/getList.htm', {
        search: keyword,
        page: page,
        pagesize: pagesize,
        pages: pages
    }, function(data, textStatus, xhr) {
        if (data.code >= 1) {
            swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
            return;
        }
        if (data.code == 0) {
            if (!!$('#jstree_Enterprise').jstree(true)) {
                $('#jstree_Enterprise').jstree(true).refresh();
            }
            if (isList(data.list)) {
                $.jstree.destroy();
                vm.canAddChild(true);
                var Arr = [];
                var level = 0;
                $.each(data.list, function(index, val) {
                    I.DB_ep[val.bianhao] = val;
                    //顶级
                    if (val.jishu==1) {
                        Arr.push({
                            text: val.xsbianhao + ' ' + val.name,
                            id: val.bianhao,
                            children: [],
                            state: index === 0 ? {selected: true} : ''
                        });
                    } else {
                        var j = 2;
                        var children = Arr[Arr.length - 1].children;
                       while (j < val.jishu) {
                            children = children[children.length - 1].children;
                            j++;
                        }
                        children.push({
                            text: val.xsbianhao + ' ' + val.name,
                            id: val.bianhao,
                            children: []
                        })
                    }
                });
                $('#jstree_Enterprise')
                .jstree({
                    core: {
                        check_callback : true,
                        data: Arr,
                        themes: {
                            //  stripes: true 
                        }
                    },
                    types: {
                        "default" : {
                            "icon" : false  // 删除默认图标
                        },
                    },
                    plugins : [
                      'search',
                      'types',
                      'themes'
                    ]
                })
                .on("changed.jstree", function (e, data) {
                    if(data.selected.length) {
                        var selected = data.instance.get_node(data.selected[0]);
                        var id = selected.id;
                        I.selectedId = id;
                        clearValid();
                        vm.o(ko.mapping.fromJS(I.DB_ep[id]));
                        vm.canDelete(!selected.children.length);
                        vm.canGLXJ(selected.children.length);
                        getselectedCMlist(id);
                        I.GLXJ.attr("checked",false);
                    }
                })
                .on("ready.jstree", function(e, data) {
                    data.instance.open_all(-1);
                     $('.page-content').height($('#jstree_Enterprise').height()+50);
                })
                .on("after_open.jstree",function(e, data){
                     $('.page-content').height($('#jstree_Enterprise').height()+50);
                })
                .on("after_close.jstree",function(e, data){
                     $('.page-content').height($('#jstree_Enterprise').height()+50);
                })
            }
            return;
        }
    }, 'json');
}
//获取全部的物流公司
function getallCMlist() {
    $.post("/area/getallCMlist.htm",
        function (data, textStatus, jqXHR) {
            vm.allCMlist.removeAll();
            if (data.code >= 1) {
                swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'});
                return;
            }
            if (data.code == 0) {
                if (isList(data.list)) {
                    $.each(data.list, function(index, val) {
                        vm.allCMlist.push(val);
                    });
                    $('.chosen-select').chosen({search_contains: true});
                }
                return;
            }
        },
        "json"
    );
}
//获取已选择的物流公司
function getselectedCMlist(bianhao) {
    $.post('/area/getcmList.htm', {bianhao:bianhao}, function(data, textStatus, xhr) {
        vm.selectedList.removeAll();
       if (data.code >= 1) {
            swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'});
            return;
        }
        if (data.code == 0) {
           if (data.item) {
                vm.selectedList(data.item.split(','));
            } else {
                vm.selectedList([]);
            }
            $('#dp').val(vm.selectedList()).trigger("chosen:updated");
            return;
        }
    }, 'json');
}
 
function delItem() {
    var id = I.selectedId;
    if (!id && !!I.AjaxDelItem) return;
    I.AjaxDelItem = true;
    $.post('/area/delItem.htm', {bianhao: id}, function(data, textStatus, xhr) {
        I.AjaxDelItem = false;
        if (data.code >= 1) {
            swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
            return;
        }
        if (data.code == 0) {
            var ref = $('#jstree_Enterprise').jstree(true),
                sel = ref.get_selected();
            if(!sel.length) { return false; }
            ref.select_node(ref.get_prev_dom(sel));
            ref.delete_node(sel);
        }
    }, 'json');
}
 
function saveItem() {
    if (!I.AjaxSaveItem && valid()) {
        I.AjaxSaveItem = true;
        var o = ko.mapping.toJS(vm.o());
        if(o.bianhao==null||o.bianhao==''){
            if(o.pbianhao==''&&o.jishu!=1){
                swal({title:'',text:'数据错误,请刷新页面重新操作!',type:'error',confirmButtonText:'确定'}); 
                return
            }
            if(o.pbianhao==''){
                o.bianhao=o.xsbianhao;
            }else{
                 o.bianhao=o.pbianhao+o.xsbianhao;
            }
        }
        o.companyNos='';
        if(!!vm.selectedList()){
            for(var i=0;i<vm.selectedList().length;i++){
                if(o.companyNos==''){
                    o.companyNos=vm.selectedList()[i];
                }else{
                     o.companyNos=o.companyNos+','+vm.selectedList()[i];
                }
            }
        }
        o.GLXJ=I.GLXJ.prop("checked");
        $.post('/area/saveItem.htm', o, function(data, textStatus, xhr) {
            I.AjaxSaveItem = false;
            if (data.code >= 1) {
                swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
                return;
            }
            if (data.code == 0) {
                parent.showErrmsg('保存成功!');
                getList();
                return;
            }
        }, 'json');
    }
}
 
function valid(){
    var isValid = false;
    var iF = $('#form');
    iF.validate({
        rules: {
            'xsbianhao': {
                required: true,
                rangelength:[2,2],
            },
            'name': {
                required: true
            }
        },
        messages: {
            'xsbianhao': {
                required: '编号不能为空!',
                rangelength:'编号必须是两位数字',
            },
            'name': {
                required: '名称不能为空!'
            }
        },
        errorPlacement: function (err, element) {
            var p = element.parents('.col-sm-9').eq(0);
            var d = p.children('.text-error').eq(0);
            d.css('color', '#D9534F');
            err.appendTo(d);
        }
    })
    isValid = iF.valid();
    return isValid;
}
 
function clearValid() {
    $('input[type="text"]').removeClass('error');
    $('span.text-error').text('');
}
 
function delConfirm() {
    $("#modalContent").html('确定要删除【' + I.DB_ep[I.selectedId].name + '】吗?');
    $("#dialog-confirm").removeClass('hide').dialog({
        resizable: false,
        width: '320',
        modal: true,
        title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-exclamation-triangle red'></i>确认信息</h4></div>",
        title_html: true,
        position: { my: "center", at: "center", of: window },
        buttons: [
            {
                html: "<i class='ace-icon fa fa-trash-o bigger-110'></i>&nbsp; 确定",
                "class" : "btn btn-danger btn-minier",
                click: function() {
                    $( this ).dialog( "close" );
                    delItem();
                }
            }
            ,
            {
                html: "<i class='ace-icon fa fa-times bigger-110'></i>&nbsp; 取消",
                "class" : "btn btn-minier",
                click: function() {
                    $( this ).dialog( "close" );
                }
            }
        ]
    });
}