longz
2026-06-04 da780b14b8db78043d2c0f27d3bfd73111e6d550
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
/**
 * 初始化跨城站点管理详情对话框
 */
var TLineInfoDlg = {
    tLineInfoData : {},
    destinationLayerIndex: null,
    destinationEditRow: null,
    destinationMap: null,
    destinationMouseTool: null,
    destinationFenceOverlay: null,
    destinationDefaultPointMarker: null,
    destinationSelectPointMode: false,
    destinationAutoComplete: null,
    destinationPlaceSearch: null,
    validateFields: {
        name: {
            validators: {
                notEmpty: {
                    message: '线路名称不能为空'
                }
            }
        },
        startSiteId: {
            validators: {
                notEmpty: {
                    message: '请选择起点'
                }
            }
        },
        endSiteId: {
            validators: {
                notEmpty: {
                    message: '请选择终点'
                }
            }
        },
        shiftInterval: {
            validators: {
                notEmpty: {
                    message: '司机申请限制分钟数不能为空'
                },
                regexp: {
                    regexp: /^[0-9]{0,3}$/,
                    message: '司机申请限制分钟数格式不正确'
                }
            }
        },
        rakeRate: {
            validators: {
                notEmpty: {
                    message: '抽成比例不能为空'
                },
                regexp: {
                    regexp: /^([0-9]{1,2}$)|(^[0-9]{1,2}\.[0-9]{1,2}$)|100$/,
                    message: '抽成比例格式不正确'
                }
            }
        },
    }
};
 
/**
 * 验证数据是否为空
 */
TLineInfoDlg.validate = function () {
    $('#lineInfoForm').data("bootstrapValidator").resetForm();
    $('#lineInfoForm').bootstrapValidator('validate');
    return $("#lineInfoForm").data('bootstrapValidator').isValid();
};
 
/**
 * 清除数据
 */
TLineInfoDlg.clearData = function() {
    this.tLineInfoData = {};
}
 
/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TLineInfoDlg.set = function(key, val) {
    this.tLineInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}
 
/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TLineInfoDlg.get = function(key) {
    return $("#" + key).val();
}
 
/**
 * 关闭此对话框
 */
TLineInfoDlg.close = function() {
    parent.layer.close(window.parent.TLine.layerIndex);
}
 
TLineInfoDlg.set1 = function (e) {
    console.log("---------------"+e)
    let index = layer.open({
        type: 2,
        title: '抽成设置',
        area: ['80%', '80%'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/tLine/set/'+e
    });
    this.layerIndex = index;
};
 
TLineInfoDlg.set2 = function (e) {
    console.log("---------------"+e)
    let index = layer.open({
        type: 2,
        title: '抽成设置',
        area: ['80%', '80%'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/tLine/set1/'+e
    });
    this.layerIndex = index;
};
 
/**
 * 收集数据
 */
TLineInfoDlg.collectData = function() {
    this
    .set('id')
    .set('name')
    .set('shiftInterval')
    .set('dispatchPhone')
    .set('rakeRate')
    .set('state')
    .set('insertTime')
    .set('outSetting');
}
 
TLineInfoDlg.validateOutSetting = function () {
    var outSetting = $("#outSetting").val();
    if (!outSetting) {
        Feng.error("请设置司机抽成");
        return false;
    }
    try {
        var config = JSON.parse(outSetting);
        if (config.rules && config.rules.length > 0) {
            for (var i = 0; i < config.rules.length; i++) {
                if (config.rules[i].percent !== undefined && config.rules[i].percent !== "") {
                    return true;
                }
            }
        }
        if (config.time_rules && config.time_rules.length > 0) {
            for (var j = 0; j < config.time_rules.length; j++) {
                var rules = config.time_rules[j];
                if (!rules || rules.length <= 0) {
                    continue;
                }
                for (var k = 0; k < rules.length; k++) {
                    if (rules[k].percent !== undefined && rules[k].percent !== "") {
                        return true;
                    }
                }
            }
        }
    } catch (e) {
        Feng.error("司机抽成配置格式不正确");
        return false;
    }
    Feng.error("请设置司机抽成");
    return false;
}
 
/**
 * 提交添加
 */
TLineInfoDlg.addSubmit = function(e) {
 
    this.clearData();
    this.collectData();
    if(!this.validate()){
        return ;
    }
    if(!TLineInfoDlg.validateOutSetting()){
        return ;
    }
    var startSiteId = $("#startSiteId").val();
    var endSiteId = $("#endSiteId").val();
 
    if(!TLineInfoDlg.validateShiftCarNum()){
        return;
    }
    var shuArr=[];
    $(".shiftValueClass").each(function () {
        shuArr.push({
            shiftTime:$(this).find("input[name*='shiftTimeValue']").val(),
            carNum:$(this).find("input[name*='carNumValue']").val(),
            time2:$(this).find("input[name*='time2Value']").val(),
        })
    })
    if(shuArr.length <= 0){
        Feng.error("请至少设置一条班次设置");
        return;
    }
    var destArr = TLineInfoDlg.collectDestinationArr();
    if(destArr === false){
        return;
    }
    $(e).attr('disabled','disabled'); //点击后就禁用,若想再次点击需刷新页面;
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tLine/add", function(data){
        $(e).removeAttr('disabled');
        if(data.status == 200){
            Feng.success("添加成功!");
            window.parent.TLine.table.refresh();
            TLineInfoDlg.close();
        }else{
            Feng.error(data.msg);
        }
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tLineInfoData);
    ajax.set("startSiteId",startSiteId);
    ajax.set("endSiteId",endSiteId);
    ajax.set("subArr","[]");
    ajax.set("shuArr",JSON.stringify(shuArr));
    ajax.set("destArr",JSON.stringify(destArr));
    ajax.start();
}
 
/**
 * 提交修改
 */
TLineInfoDlg.editSubmit = function() {
 
    this.clearData();
    this.collectData();
    if(!this.validate()){
        return ;
    }
    if(!TLineInfoDlg.validateOutSetting()){
        return ;
    }
 
    var startSiteId = $("#startSiteId").val();
    var endSiteId = $("#endSiteId").val();
 
    if(!TLineInfoDlg.validateShiftCarNum()){
        return;
    }
    var shuArr=[];
    $(".shiftValueClass").each(function () {
        shuArr.push({
            shiftTime:$(this).find("input[name*='shiftTimeValue']").val(),
            carNum:$(this).find("input[name*='carNumValue']").val(),
            time2:$(this).find("input[name*='time2Value']").val(),
        })
    })
    if(shuArr.length <= 0){
        Feng.error("请至少设置一条班次设置");
        return;
    }
    var destArr = TLineInfoDlg.collectDestinationArr();
    if(destArr === false){
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tLine/update", function(data){
        if(data.status == 200){
            Feng.success("修改成功!");
            window.parent.TLine.table.refresh();
            TLineInfoDlg.close();
        }else{
            Feng.error(data.msg);
        }
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tLineInfoData);
    ajax.set("startSiteId",startSiteId);
    ajax.set("endSiteId",endSiteId);
    ajax.set("subArr","[]");
    ajax.set("shuArr",JSON.stringify(shuArr));
    ajax.set("destArr",JSON.stringify(destArr));
    ajax.start();
}
 
//添加公司提交
TLineInfoDlg.addCompanySubmit = function(){
    var comArr=[];
    $(".companyValueClass").each(function () {
        comArr.push({
            companyId:$(this).find("input[name*='companyIdValue']").val(),
        })
    })
    /*if(comArr.length <= 0){
        Feng.error("请至少添加一个企业");
        return;
    }*/
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tLine/addCompany", function(data){
        Feng.success("操作成功!");
        window.parent.TLine.table.refresh();
        TLineInfoDlg.close();
    },function(data){
        Feng.error("操作失败!" + data.responseJSON.message + "!");
    });
    ajax.set("lineId",$("#lineId").val());
    ajax.set("comArr",JSON.stringify(comArr));
    ajax.start();
}
 
$(function() {
    Feng.initValidator("lineInfoForm", TLineInfoDlg.validateFields);
});
 
/**
 * 添加班次
 */
TLineInfoDlg.addShift = function () {
    var shiftTime = $("#shiftTime").val();
    if ("" == shiftTime){
        Feng.info("请选择班次时间区间");
        return;
    }
    var carNum = $("#carNum").val();
    if ("" == carNum){
        Feng.info("请填写车次数");
        return;
    }else if (!regInt.test(carNum)){
        Feng.info("车次数格式不正确");
        return;
    }else if (parseInt(carNum, 10) <= 0){
        Feng.info("车次数必须为正整数");
        return;
    }
 
    times = shiftTime.split(" - ");
    var time1 = times[0];
    var time2 = times[1];
    if (time1 >= time2) {
        Feng.info("开始时间不能大于结束时间");
        return;
    }
 
    var shuArr=[];
    $(".shiftValueClass").each(function () {
        shuArr.push({
            shiftTime:$(this).find("input[name*='shiftTimeValue']").val(),
            carNum:$(this).find("input[name*='carNumValue']").val(),
            time2:$(this).find("input[name*='time2Value']").val(),
        })
    })
 
    //判断时间是否存在
    for(var i=0;i<shuArr.length;i++){
        var shiftTimeValue = shuArr[i].shiftTime;
        if (shiftTimeValue == shiftTime) {
            Feng.info("班次时间已存在,请重新选择");
            return;
        }
    }
    TLineInfoDlg.appendShiftRow(shiftTime, carNum);
    //清除数据
    $("#shiftTime").val("");
    $("#carNum").val("");
}
 
TLineInfoDlg.appendShiftRow = function (shiftTime, carNum) {
    var str = '<tr class="shiftValueClass">' +
        '<td style="text-align: center;"><input type="hidden" id="time2Value" name="time2Value" value="'+getNowFormatDate()+'">' + getNowFormatDate() + '</td>' +
        '<td style="text-align: center;"><input type="hidden" id="shiftTimeValue" name="shiftTimeValue" value="'+shiftTime+'">'+shiftTime+'</td>' +
        '<td style="text-align: center;">' +
        '<input type="hidden" id="carNumValue" name="carNumValue" value="'+carNum+'">' +
        '<button type="button" onclick="TLineInfoDlg.changeCarNum(this,-1)">-</button>' +
        '<input type="text" name="carNumEditValue" value="'+carNum+'" oninput="TLineInfoDlg.onCarNumInput(this)" onblur="TLineInfoDlg.onCarNumBlur(this)" style="width:60px;text-align:center;margin:0 6px;">' +
        '<button type="button" onclick="TLineInfoDlg.changeCarNum(this,1)">+</button>' +
        '</td>' +
        '<td style="text-align: center;"><button onclick="deleteSub(this)">移除</button></td></tr>';
    $("#shiftValue").append(str);
}
 
TLineInfoDlg.normalizePositiveInt = function (numStr) {
    var onlyNum = (numStr || "").replace(/[^\d]/g, "");
    onlyNum = onlyNum.replace(/^0+/, "");
    return onlyNum;
}
 
TLineInfoDlg.syncCarNumValue = function (input) {
    var $input = $(input);
    var value = TLineInfoDlg.normalizePositiveInt($input.val());
    $input.val(value);
    $input.closest("td").find("input[name='carNumValue']").val(value);
}
 
TLineInfoDlg.onCarNumInput = function (input) {
    TLineInfoDlg.syncCarNumValue(input);
}
 
TLineInfoDlg.onCarNumBlur = function (input) {
    var $input = $(input);
    var value = TLineInfoDlg.normalizePositiveInt($input.val());
    if (value == "" || parseInt(value, 10) <= 0) {
        value = "1";
    }
    $input.val(value);
    $input.closest("td").find("input[name='carNumValue']").val(value);
}
 
TLineInfoDlg.changeCarNum = function (btn, step) {
    var $td = $(btn).closest("td");
    var $input = $td.find("input[name='carNumEditValue']");
    var value = TLineInfoDlg.normalizePositiveInt($input.val());
    var num = parseInt(value, 10);
    if (isNaN(num) || num <= 0) {
        num = 1;
    }
    num = num + step;
    if (num <= 0) {
        num = 1;
    }
    $input.val(num);
    $td.find("input[name='carNumValue']").val(num);
}
 
TLineInfoDlg.validateShiftCarNum = function () {
    var valid = true;
    $(".shiftValueClass").each(function () {
        var $row = $(this);
        var $editInput = $row.find("input[name='carNumEditValue']");
        if ($editInput.length > 0) {
            TLineInfoDlg.onCarNumBlur($editInput[0]);
        }
        var carNum = $row.find("input[name*='carNumValue']").val();
        if (!/^[1-9]\d*$/.test(carNum)) {
            valid = false;
            return false;
        }
    });
    if (!valid) {
        Feng.error("班次车次数必须为正整数");
    }
    return valid;
}
 
TLineInfoDlg.toMinutes = function (timeStr) {
    var arr = timeStr.split(":");
    return parseInt(arr[0], 10) * 60 + parseInt(arr[1], 10);
}
 
TLineInfoDlg.toTimeStr = function (minutes) {
    var h = Math.floor(minutes / 60);
    var m = minutes % 60;
    return (h < 10 ? "0" + h : "" + h) + ":" + (m < 10 ? "0" + m : "" + m);
}
 
/**
 * 批量生成班次
 * 规则:
 * 第一次:始发时间 - 始发时间+间隔
 * 后续:上次后段时间 - 后段时间+间隔
 */
TLineInfoDlg.generateShiftList = function () {
    var firstShiftTime = $("#firstShiftTime").val();
    var lastShiftTime = $("#lastShiftTime").val();
    var shiftGap = $("#shiftGap").val();
    var batchCarNum = $("#batchCarNum").val();
 
    if (firstShiftTime == "") {
        Feng.info("请选择始发时间");
        return;
    }
    if (lastShiftTime == "") {
        Feng.info("请选择末班时间");
        return;
    }
    if (shiftGap == "") {
        Feng.info("请选择间隔时间");
        return;
    }
    if (batchCarNum == "") {
        Feng.info("请填写每班车次数");
        return;
    }
    if (!regInt.test(batchCarNum)) {
        Feng.info("车次数格式不正确");
        return;
    }
    if (parseInt(batchCarNum, 10) <= 0) {
        Feng.info("车次数必须为正整数");
        return;
    }
 
    var startMinute = TLineInfoDlg.toMinutes(firstShiftTime);
    var lastMinute = TLineInfoDlg.toMinutes(lastShiftTime);
    var gap = parseInt(shiftGap, 10);
 
    if (startMinute > lastMinute) {
        Feng.info("始发时间不能大于末班时间");
        return;
    }
 
    var existsMap = {};
    $(".shiftValueClass").each(function () {
        var shiftTimeValue = $(this).find("input[name*='shiftTimeValue']").val();
        if (shiftTimeValue) {
            shiftTimeValue = shiftTimeValue.replace(/\s+/g, "");
        }
        existsMap[shiftTimeValue] = true;
    });
 
    var currentStart = startMinute;
    var generated = 0;
    var duplicate = 0;
    while (currentStart <= lastMinute) {
        var currentEnd = currentStart + gap;
        if (currentEnd > 24 * 60) {
            break;
        }
        var shiftTime = TLineInfoDlg.toTimeStr(currentStart) + " - " + TLineInfoDlg.toTimeStr(currentEnd);
        var shiftTimeKey = shiftTime.replace(/\s+/g, "");
        if (!existsMap[shiftTimeKey]) {
            TLineInfoDlg.appendShiftRow(shiftTime, batchCarNum);
            existsMap[shiftTimeKey] = true;
            generated++;
        } else {
            duplicate++;
        }
        currentStart = currentEnd;
    }
 
    if (generated == 0) {
        Feng.info("未生成新班次,班次时间可能已存在");
        return;
    }
 
    if (duplicate > 0) {
        Feng.success("已生成" + generated + "条班次,跳过重复" + duplicate + "条");
    } else {
        Feng.success("已生成" + generated + "条班次");
    }
}
 
TLineInfoDlg.collectDestinationArr = function () {
    var destArr = [];
    var mainCount = 0;
    $(".destinationValueClass").each(function () {
        var item = {
            time: $(this).find("input[name='destinationTimeValue']").val(),
            fenceType: $(this).find("input[name='destinationFenceTypeValue']").val(),
            coordinate: $(this).find("input[name='destinationCoordinateValue']").val(),
            defaultPointLon: $(this).find("input[name='destinationDefaultPointLonValue']").val(),
            defaultPointLat: $(this).find("input[name='destinationDefaultPointLatValue']").val(),
            name: $(this).find("input[name='destinationNameValue']").val(),
            price5: $(this).find("input[name='destinationPrice5Value']").val(),
            price7: $(this).find("input[name='destinationPrice7Value']").val(),
            state: $(this).find("input[name='destinationStateValue']").val(),
            isMain: $(this).find("input[name='destinationIsMainValue']").val()
        };
        if (item.isMain == "1") {
            mainCount++;
        }
        destArr.push(item);
    });
    if (destArr.length <= 0) {
        Feng.error("请至少设置一个目的地");
        return false;
    }
    if (mainCount > 1) {
        Feng.error("最多只能设置一个主要目的地");
        return false;
    }
    return destArr;
}
 
TLineInfoDlg.escapeHtml = function (str) {
    return (str || "").replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
 
TLineInfoDlg.escapeAttr = function (str) {
    return TLineInfoDlg.escapeHtml(str).replace(/"/g, "&quot;");
}
 
TLineInfoDlg.openDestinationDialog = function () {
    if ($(".destinationValueClass").length >= 3) {
        Feng.error("最多只能添加3个目的地");
        return;
    }
    TLineInfoDlg.destinationEditRow = null;
    TLineInfoDlg.resetDestinationForm();
    TLineInfoDlg.openDestinationLayer("添加目的地");
}
 
TLineInfoDlg.openDestinationLayer = function (title) {
    TLineInfoDlg.destinationLayerIndex = layer.open({
        type: 1,
        title: title,
        area: ['1000px', '760px'],
        btn: ['保存', '取消'],
        content: $("#destinationDialogContent"),
        success: function () {
            $("#destinationDialogContent").show();
            TLineInfoDlg.initDestinationMap();
            setTimeout(function () {
                if (TLineInfoDlg.destinationMap) {
                    TLineInfoDlg.destinationMap.resize();
                    TLineInfoDlg.destinationMap.setFitView();
                }
            }, 100);
        },
        yes: function (index) {
            TLineInfoDlg.saveDestination(index);
        },
        end: function () {
            $("#destinationDialogContent").hide();
            TLineInfoDlg.destinationSelectPointMode = false;
            if (TLineInfoDlg.destinationMouseTool) {
                TLineInfoDlg.destinationMouseTool.close();
            }
        }
    });
}
 
TLineInfoDlg.resetDestinationForm = function () {
    $("#destinationEditIndex").val("");
    $("#destinationName").val("");
    $("#destinationPrice5").val("");
    $("#destinationPrice7").val("");
    $("#destinationAddressKeyword").val("");
    $("#destinationCoordinate").val("");
    $("#destinationDefaultPointLon").val("");
    $("#destinationDefaultPointLat").val("");
    $("#destinationDefaultPointText").text("");
    $("#destinationFenceType1").prop("checked", true);
    $("#destinationState1").prop("checked", true);
    $("#destinationIsMain0").prop("checked", true);
    TLineInfoDlg.destinationFenceTypeChange(1);
    TLineInfoDlg.clearDestinationFence();
}
 
TLineInfoDlg.destinationFenceTypeChange = function (type) {
    if (type == 1) {
        $("#destinationFenceTip").text("圆形围栏默认点为圆心");
    } else {
        $("#destinationFenceTip").text("多边形围栏需要确认一个默认点");
    }
}
 
TLineInfoDlg.initDestinationMap = function () {
    if (TLineInfoDlg.destinationMap) {
        return;
    }
    TLineInfoDlg.destinationMap = new AMap.Map("destinationMapContainer", {
        resizeEnable: true,
        zoom: 13
    });
    TLineInfoDlg.destinationMouseTool = new AMap.MouseTool(TLineInfoDlg.destinationMap);
    TLineInfoDlg.destinationMouseTool.on('draw', function (e) {
        var type = $("input[name='destinationFenceType']:checked").val();
        TLineInfoDlg.clearDestinationFence();
        TLineInfoDlg.destinationFenceOverlay = e.obj;
        if (type == "1") {
            var center = e.obj.getCenter();
            var radius = e.obj.getRadius();
            if (radius > 5000) {
                Feng.error("圆形围栏半径不能大于5000米");
                TLineInfoDlg.clearDestinationFence();
                return;
            }
            var coordinate = center.lng + "," + center.lat + ";" + radius;
            $("#destinationCoordinate").val(coordinate);
            $("#destinationDefaultPointLon").val(center.lng);
            $("#destinationDefaultPointLat").val(center.lat);
            TLineInfoDlg.setDestinationDefaultPointMarker(center.lng, center.lat, true);
        } else {
            var path = e.obj.getPath();
            var coordinate = TLineInfoDlg.pathToCoordinate(path);
            $("#destinationCoordinate").val(coordinate);
        }
    });
    TLineInfoDlg.destinationMap.on('click', function (e) {
        if (!TLineInfoDlg.destinationSelectPointMode) {
            return;
        }
        var fenceType = $("input[name='destinationFenceType']:checked").val();
        if (fenceType != "2") {
            Feng.info("圆形围栏默认点为圆心,无需单独设置");
            TLineInfoDlg.destinationSelectPointMode = false;
            return;
        }
        $("#destinationDefaultPointLon").val(e.lnglat.getLng());
        $("#destinationDefaultPointLat").val(e.lnglat.getLat());
        TLineInfoDlg.setDestinationDefaultPointMarker(e.lnglat.getLng(), e.lnglat.getLat(), false);
        TLineInfoDlg.destinationSelectPointMode = false;
        Feng.success("默认点设置成功");
    });
    TLineInfoDlg.destinationAutoComplete = new AMap.Autocomplete({
        city: "",
        input: "destinationAddressKeyword"
    });
    TLineInfoDlg.destinationPlaceSearch = new AMap.PlaceSearch({
        map: TLineInfoDlg.destinationMap,
        pageSize: 1,
        pageIndex: 1,
        citylimit: false
    });
    AMap.event.addListener(TLineInfoDlg.destinationAutoComplete, "select", function (e) {
        if (!e.poi || !e.poi.location) {
            return;
        }
        var lon = e.poi.location.lng;
        var lat = e.poi.location.lat;
        TLineInfoDlg.destinationMap.setCenter([lon, lat]);
        TLineInfoDlg.destinationMap.setZoom(15);
        $("#destinationDefaultPointLon").val(lon);
        $("#destinationDefaultPointLat").val(lat);
        TLineInfoDlg.setDestinationDefaultPointMarker(lon, lat, false);
    });
}
 
TLineInfoDlg.searchDestinationAddress = function () {
    var keyword = $("#destinationAddressKeyword").val();
    if (!keyword) {
        Feng.info("请输入地址关键词");
        return;
    }
    if (!TLineInfoDlg.destinationPlaceSearch) {
        return;
    }
    TLineInfoDlg.destinationPlaceSearch.search(keyword, function (status, result) {
        if (status !== "complete" || !result || !result.poiList || result.poiList.count <= 0) {
            Feng.error("未找到相关地址");
            return;
        }
        var poi = result.poiList.pois[0];
        if (!poi || !poi.location) {
            Feng.error("地址定位失败");
            return;
        }
        var lon = poi.location.lng;
        var lat = poi.location.lat;
        TLineInfoDlg.destinationMap.setCenter([lon, lat]);
        TLineInfoDlg.destinationMap.setZoom(15);
        $("#destinationDefaultPointLon").val(lon);
        $("#destinationDefaultPointLat").val(lat);
        TLineInfoDlg.setDestinationDefaultPointMarker(lon, lat, false);
    });
}
 
TLineInfoDlg.pathToCoordinate = function (path) {
    var arr = [];
    for (var i = 0; i < path.length; i++) {
        arr.push(path[i].lng + "," + path[i].lat);
    }
    return arr.join(";");
}
 
TLineInfoDlg.selectDestinationDefaultPoint = function () {
    var fenceType = $("input[name='destinationFenceType']:checked").val();
    if (fenceType == "1") {
        Feng.info("圆形围栏默认点为圆心,无需单独设置");
        return;
    }
    TLineInfoDlg.destinationSelectPointMode = true;
    Feng.info("请在地图上点击设置默认点");
}
 
TLineInfoDlg.startDrawDestinationFence = function () {
    TLineInfoDlg.initDestinationMap();
    var type = $("input[name='destinationFenceType']:checked").val();
    TLineInfoDlg.clearDestinationFence();
    if (type == "1") {
        TLineInfoDlg.destinationMouseTool.circle({
            fillColor: '#00b0ff',
            strokeColor: '#80d8ff'
        });
    } else {
        TLineInfoDlg.destinationMouseTool.polygon({
            fillColor: '#00b0ff',
            strokeColor: '#80d8ff'
        });
    }
}
 
TLineInfoDlg.clearDestinationFence = function (keepValue) {
    if (TLineInfoDlg.destinationFenceOverlay) {
        TLineInfoDlg.destinationMap.remove(TLineInfoDlg.destinationFenceOverlay);
        TLineInfoDlg.destinationFenceOverlay = null;
    }
    if (TLineInfoDlg.destinationDefaultPointMarker) {
        TLineInfoDlg.destinationMap.remove(TLineInfoDlg.destinationDefaultPointMarker);
        TLineInfoDlg.destinationDefaultPointMarker = null;
    }
    if (!keepValue) {
        $("#destinationCoordinate").val("");
        $("#destinationDefaultPointLon").val("");
        $("#destinationDefaultPointLat").val("");
    }
    $("#destinationDefaultPointText").text("");
}
 
TLineInfoDlg.setDestinationDefaultPointMarker = function (lon, lat, auto) {
    if (!TLineInfoDlg.destinationMap) {
        return;
    }
    if (TLineInfoDlg.destinationDefaultPointMarker) {
        TLineInfoDlg.destinationMap.remove(TLineInfoDlg.destinationDefaultPointMarker);
    }
    TLineInfoDlg.destinationDefaultPointMarker = new AMap.Marker({
        map: TLineInfoDlg.destinationMap,
        position: new AMap.LngLat(lon, lat)
    });
    var text = "默认点:" + lon + "," + lat;
    if (auto) {
        text += "(圆心)";
    }
    $("#destinationDefaultPointText").text(text);
}
 
TLineInfoDlg.validateDestinationForm = function () {
    var name = $("#destinationName").val();
    if (!name) {
        Feng.error("请填写目的地名称");
        return false;
    }
    var priceReg = /^(0|[1-9]\d{0,6})(\.\d{1,2})?$/;
    var price5 = $("#destinationPrice5").val();
    var price7 = $("#destinationPrice7").val();
    if (!priceReg.test(price5)) {
        Feng.error("5座车费用格式不正确");
        return false;
    }
    if (!priceReg.test(price7)) {
        Feng.error("7座车费用格式不正确");
        return false;
    }
    var coordinate = $("#destinationCoordinate").val();
    if (!coordinate) {
        Feng.error("请先设置目的地围栏");
        return false;
    }
    var fenceType = $("input[name='destinationFenceType']:checked").val();
    var defaultPointLon = $("#destinationDefaultPointLon").val();
    var defaultPointLat = $("#destinationDefaultPointLat").val();
    if (fenceType == "2" && (!defaultPointLon || !defaultPointLat)) {
        Feng.error("多边形围栏必须设置默认点");
        return false;
    }
    if ($("input[name='destinationIsMain']:checked").val() == "1") {
        var sameMainCount = 0;
        $(".destinationValueClass").each(function () {
            if ($(this).find("input[name='destinationIsMainValue']").val() == "1") {
                if (!TLineInfoDlg.destinationEditRow || this != TLineInfoDlg.destinationEditRow[0]) {
                    sameMainCount++;
                }
            }
        });
        if (sameMainCount > 0) {
            Feng.error("已存在主要目的地,请先调整后再保存");
            return false;
        }
    }
    return true;
}
 
TLineInfoDlg.saveDestination = function (layerIndex) {
    if (!TLineInfoDlg.validateDestinationForm()) {
        return;
    }
    var data = {
        time: getNowFormatDate(),
        fenceType: $("input[name='destinationFenceType']:checked").val(),
        coordinate: $("#destinationCoordinate").val(),
        defaultPointLon: $("#destinationDefaultPointLon").val(),
        defaultPointLat: $("#destinationDefaultPointLat").val(),
        name: $("#destinationName").val(),
        price5: $("#destinationPrice5").val(),
        price7: $("#destinationPrice7").val(),
        state: $("input[name='destinationState']:checked").val(),
        isMain: $("input[name='destinationIsMain']:checked").val()
    };
    if (TLineInfoDlg.destinationEditRow) {
        TLineInfoDlg.destinationEditRow.replaceWith(TLineInfoDlg.buildDestinationRowHtml(data));
    } else {
        $("#destinationValue").append(TLineInfoDlg.buildDestinationRowHtml(data));
    }
    layer.close(layerIndex);
}
 
TLineInfoDlg.buildDestinationRowHtml = function (data) {
    var fenceTypeText = data.fenceType == "1" ? "圆形围栏" : "多边形围栏";
    var stateText = data.state == "1" ? "启用" : "关闭";
    var isMainText = data.isMain == "1" ? "是" : "否";
    var nameHtml = TLineInfoDlg.escapeHtml(data.name);
    var coordinateAttr = TLineInfoDlg.escapeAttr(data.coordinate);
    var nameAttr = TLineInfoDlg.escapeAttr(data.name);
    return '<tr class="destinationValueClass">' +
        '<td style="text-align: center;">' +
        '<input type="hidden" name="destinationTimeValue" value="' + TLineInfoDlg.escapeAttr(data.time) + '">' +
        '<input type="hidden" name="destinationFenceTypeValue" value="' + data.fenceType + '">' +
        '<input type="hidden" name="destinationCoordinateValue" value="' + coordinateAttr + '">' +
        '<input type="hidden" name="destinationDefaultPointLonValue" value="' + TLineInfoDlg.escapeAttr(data.defaultPointLon) + '">' +
        '<input type="hidden" name="destinationDefaultPointLatValue" value="' + TLineInfoDlg.escapeAttr(data.defaultPointLat) + '">' +
        '<input type="hidden" name="destinationNameValue" value="' + nameAttr + '">' +
        '<input type="hidden" name="destinationPrice5Value" value="' + TLineInfoDlg.escapeAttr(data.price5) + '">' +
        '<input type="hidden" name="destinationPrice7Value" value="' + TLineInfoDlg.escapeAttr(data.price7) + '">' +
        '<input type="hidden" name="destinationStateValue" value="' + data.state + '">' +
        '<input type="hidden" name="destinationIsMainValue" value="' + data.isMain + '">' +
        fenceTypeText + '</td>' +
        '<td style="text-align: center;">' + nameHtml + '</td>' +
        '<td style="text-align: center;">' + TLineInfoDlg.escapeHtml(data.price5) + '</td>' +
        '<td style="text-align: center;">' + TLineInfoDlg.escapeHtml(data.price7) + '</td>' +
        '<td style="text-align: center;">' + stateText + '</td>' +
        '<td style="text-align: center;">' + isMainText + '</td>' +
        '<td style="text-align: center;">' +
        '<button type="button" onclick="TLineInfoDlg.editDestination(this)">编辑</button> ' +
        '<button type="button" onclick="TLineInfoDlg.deleteDestination(this)">删除</button>' +
        '</td></tr>';
}
 
TLineInfoDlg.editDestination = function (btn) {
    var $row = $(btn).closest("tr");
    TLineInfoDlg.destinationEditRow = $row;
    TLineInfoDlg.resetDestinationForm();
    $("#destinationName").val($row.find("input[name='destinationNameValue']").val());
    $("#destinationPrice5").val($row.find("input[name='destinationPrice5Value']").val());
    $("#destinationPrice7").val($row.find("input[name='destinationPrice7Value']").val());
    $("#destinationCoordinate").val($row.find("input[name='destinationCoordinateValue']").val());
    $("#destinationDefaultPointLon").val($row.find("input[name='destinationDefaultPointLonValue']").val());
    $("#destinationDefaultPointLat").val($row.find("input[name='destinationDefaultPointLatValue']").val());
 
    var fenceType = $row.find("input[name='destinationFenceTypeValue']").val();
    var state = $row.find("input[name='destinationStateValue']").val();
    var isMain = $row.find("input[name='destinationIsMainValue']").val();
    $("input[name='destinationFenceType'][value='" + fenceType + "']").prop("checked", true);
    $("input[name='destinationState'][value='" + state + "']").prop("checked", true);
    $("input[name='destinationIsMain'][value='" + isMain + "']").prop("checked", true);
    TLineInfoDlg.destinationFenceTypeChange(parseInt(fenceType, 10));
    TLineInfoDlg.openDestinationLayer("编辑目的地");
 
    setTimeout(function () {
        TLineInfoDlg.restoreDestinationMapFence();
    }, 150);
}
 
TLineInfoDlg.restoreDestinationMapFence = function () {
    TLineInfoDlg.initDestinationMap();
    TLineInfoDlg.clearDestinationFence(true);
    var fenceType = $("input[name='destinationFenceType']:checked").val();
    var coordinate = $("#destinationCoordinate").val();
    if (!coordinate) {
        return;
    }
    if (fenceType == "1") {
        var arr = coordinate.split(";");
        if (arr.length != 2) {
            return;
        }
        var centerArr = arr[0].split(",");
        if (centerArr.length != 2) {
            return;
        }
        var center = new AMap.LngLat(parseFloat(centerArr[0]), parseFloat(centerArr[1]));
        var radius = parseFloat(arr[1]);
        TLineInfoDlg.destinationFenceOverlay = new AMap.Circle({
            center: center,
            radius: radius,
            fillColor: '#00b0ff',
            strokeColor: '#80d8ff'
        });
        TLineInfoDlg.destinationMap.add(TLineInfoDlg.destinationFenceOverlay);
        TLineInfoDlg.setDestinationDefaultPointMarker(center.getLng(), center.getLat(), true);
        TLineInfoDlg.destinationMap.setFitView();
    } else {
        var pointsArr = coordinate.split(";");
        var points = [];
        for (var i = 0; i < pointsArr.length; i++) {
            var point = pointsArr[i].split(",");
            if (point.length == 2) {
                points.push([parseFloat(point[0]), parseFloat(point[1])]);
            }
        }
        if (points.length > 0) {
            TLineInfoDlg.destinationFenceOverlay = new AMap.Polygon({
                path: points,
                fillColor: '#00b0ff',
                strokeColor: '#80d8ff'
            });
            TLineInfoDlg.destinationMap.add(TLineInfoDlg.destinationFenceOverlay);
            var lon = $("#destinationDefaultPointLon").val();
            var lat = $("#destinationDefaultPointLat").val();
            if (lon && lat) {
                TLineInfoDlg.setDestinationDefaultPointMarker(parseFloat(lon), parseFloat(lat), false);
            }
            TLineInfoDlg.destinationMap.setFitView();
        }
    }
}
 
TLineInfoDlg.deleteDestination = function (btn) {
    $(btn).closest("tr").remove();
}
 
/**
 * 添加公司
 */
TLineInfoDlg.addCompany = function () {
    var companyId = $("#companyId").val();
    var companyText = $("#companyId option:selected").text();
    if ("" == companyId){
        Feng.info("请选择企业");
        return;
    }
    var comArr=[];
    $(".companyValueClass").each(function () {
        comArr.push({
            companyId:$(this).find("input[name*='companyIdValue']").val(),
        })
    })
    //判断公司是否存在
    for(var i=0;i<comArr.length;i++){
        var companyIdValue = comArr[i].companyId;
        if (companyIdValue == companyId) {
            Feng.info("企业已存在,请重新选择");
            return;
        }
    }
    //封装数据
    var str = '<tr class="companyValueClass">' +
        '<td style="text-align: center;"><input type="hidden" id="companyIdValue" name="companyIdValue" value="'+companyId+'">'+companyText+'</td>' +
        '<td style="text-align: center;"><button onclick="deleteSub(this)">移除</button></td></tr>';
    $("#companyValue").append(str);
    //清除数据
    $("#companyId").val("");
 
}
 
function deleteSub(e) {
    $(e).parent().parent().remove();
}