中扬CRM客户关系管理系统
#
luxiaotao1123
2022-11-22 45286ba61d7df92ca3d60bdf187b94d5a4349874
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
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
/**
 @ Name:layui.cron Cron表达式解析器
 @ Author:贝哥哥
 @ License:MIT
 */
 
layui.define(['lay', 'element', 'form'], function(exports){ //假如该组件依赖 layui.form
  var $ = layui.$
      ,layer = layui.layer
      ,lay = layui.lay
      ,element = layui.element
      ,form = layui.form
 
 
      //字符常量
      ,MOD_NAME = 'cron', ELEM = '.layui-cron', THIS = 'layui-this', SHOW = 'layui-show', HIDE = 'layui-hide'
 
      ,ELEM_STATIC = 'layui-cron-static', ELEM_FOOTER = 'layui-cron-footer', ELEM_CONFIRM = '.cron-btns-confirm', ELEM_HINT = 'layui-cron-hint'
 
      ,ELEM_RUN_HINT = 'layui-cron-run-hint'
 
      //外部接口
      ,cron = {
        v:'2.0.0' // cron 组件当前版本
        ,index: layui.cron ? (layui.cron.index + 10000) : 0 // corn 实例标识
 
        //设置全局项
        ,set: function(options){
          var that = this;
          that.config = $.extend({}, that.config, options);
          return that;
        }
 
        //事件监听
        ,on: function(events, callback){
          return layui.onevent.call(this, MOD_NAME, events, callback);
        }
 
        //主体CSS等待事件
        ,ready: function (fn) {
          var cssPath = layui.cache.base + "/cron.css?v=" + cron.v;
          layui.link(cssPath, fn, "cron"); //此处的“cron”要对应 cron.css 中的样式: html #layuicss-cron{}
          return this;
        }
      }
 
      //操作当前实例
      ,thisIns = function(){
        var that = this
            ,options = that.config
            ,id = options.id || options.index;
 
        return {
          //提示框
          hint: function(content){
            that.hint.call(that, content);
          }
          ,config: options
        }
      }
 
      //构造器,创建实例
      ,Class = function(options){
        var that = this;
        that.index = ++cron.index;
        that.config = $.extend({}, that.config, cron.config, options);
        cron.ready(function () {
          that.init();
        });
      };
 
  //默认配置
  Class.prototype.config = {
    value: null // 当前表达式值,每秒执行一次
    ,isInitValue: true //用于控制是否自动向元素填充初始值(需配合 value 参数使用)
    ,lang: "cn" //语言,只支持cn/en,即中文和英文
    ,tabs:[{key:'seconds',range:'0-59'},{key:'minutes',range:'0-59'},{key:'hours',range:'0-23'},{key:'days',range:'1-31'},{key:'months',range:'1-12'},{key:'weeks',range:'1-7'},{key:'years'}]
    ,defaultCron: {seconds:"*",minutes:"*",hours:"*",days:"*", months:"*", weeks:"?", years:""}
    ,trigger: "click" //呼出控件的事件
    ,btns: ['run', 'confirm'] //右下角显示的按钮,会按照数组顺序排列
    ,position: null //控件定位方式定位, 默认absolute,支持:fixed/absolute/static
    ,zIndex: null //控件层叠顺序
    ,show: false //是否直接显示,如果设置 true,则默认直接显示控件
    ,showBottom: true //是否显示底部栏
    ,done: null //控件选择完毕后的回调,点击运行/确定也均会触发
    ,run: null // 最近运行时间接口
  };
 
  //多语言
  Class.prototype.lang = function(){
    var that = this
        ,options = that.config
        ,text = {
      cn: {
        tabs: [{title: "秒"}
          , {title: "分"}
          , {title: "时"}
          , {title: "日"}
          , {title: "月"}
          , {title: "周", rateBegin: "第", rateMid: "周的星期", rateEnd:""}
          , {title: "年"}]
        , every: "每"
        , unspecified: "不指定"
        , period: "周期"
        , periodFrom: "从"
        , rate: "按照"
        , rateBegin: "从"
        , rateMid: "开始,每"
        , rateEnd: "执行一次"
        , weekday: "工作日"
        , weekdayPrefix: "每月"
        , weekdaySuffix: "号最近的那个工作日"
        , lastday: "本月最后一日"
        , lastweek: "本月最后一个星期"
        , custom: "指定"
        ,tools: {
          confirm: '确定'
          ,run: '运行'
        }
        ,formatError: ['Cron格式不合法', '<br>已为你重置']
      }
      ,en: {
        tabs: [{title:"Seconds"}
          , {title:"Minutes"}
          , {title:"Hours"}
          , {title:"Days"}
          , {title:"Months"}
          , {title:"Weeks"}
          , {title:"Years"}]
        , every:"Every "
        , unspecified:"Unspecified"
        , period:"Period"
        , periodFrom: "From"
        , rate: "According to"
        , rateBegin: "begin at"
        , rateMid: ", every"
        , rateEnd: " execute once"
        , weekday: "Weekday"
        , weekdayPrefix: "Every month at "
        , weekdaySuffix: "号最近的那个工作日"
        , lastday: "Last day of the month"
        , lastweek: "本月最后一个星期"
        , custom: "Custom"
        ,tools: {
          confirm: 'Confirm'
          ,run: 'Run'
        }
        ,formatError: ['The cron format error', '<br>It has been reset']
      }
    };
    return text[options.lang] || text['cn'];
  };
 
  //初始准备
  Class.prototype.init = function(){
    var that = this
        ,options = that.config
        ,isStatic = options.position === 'static';
 
    options.elem = lay(options.elem);
 
    options.eventElem = lay(options.eventElem);
 
    if(!options.elem[0]) return;
 
    //如果不是input|textarea元素,则默认采用click事件
    if(!that.isInput(options.elem[0])){
      if(options.trigger === 'focus'){
        options.trigger = 'click';
      }
    }
 
    // 设置渲染所绑定元素的唯一KEY
    if(!options.elem.attr('lay-key')){
      options.elem.attr('lay-key', that.index);
      options.eventElem.attr('lay-key', that.index);
    }
 
    // 当前实例主面板ID
    that.elemID = 'layui-icon'+ options.elem.attr('lay-key');
 
    //默认赋值
    if(options.value && options.isInitValue){
      that.setValue(options.value);
    }
    if(!options.value){
      options.value = options.elem[0].value||'';
    }
    var cronArr = options.value.split(' ');
    if(cronArr.length >= 6){
      options.cron = {
        seconds:cronArr[0],
        minutes:cronArr[1],
        hours:cronArr[2],
        days:cronArr[3],
        months:cronArr[4],
        weeks:cronArr[5],
        years:"",
      };
    }else{
      options.cron = lay.extend({},options.defaultCron);
    }
 
 
    if(options.show || isStatic) that.render();
    isStatic || that.events();
 
 
  };
 
 
  // 控件主体渲染
  Class.prototype.render = function(){
    var that = this
        ,options = that.config
        ,lang = that.lang()
        ,isStatic = options.position === 'static'
        ,tabFilter = 'cron-tab' + options.elem.attr('lay-key')
        //主面板
        ,elem = that.elem = lay.elem('div', {
          id: that.elemID
          ,'class': [
            'layui-cron'
            ,isStatic ? (' '+ ELEM_STATIC) : ''
          ].join('')
        })
 
        // tab 内容区域
        ,elemTab = that.elemTab = lay.elem('div', {
          'class': 'layui-tab layui-tab-card',
          'lay-filter':tabFilter
        })
        ,tabHead = lay.elem('ul',{
          'class': 'layui-tab-title'
        })
        ,tabContent = lay.elem('div',{
          'class': 'layui-tab-content'
        })
 
        //底部区域
        ,divFooter = that.footer = lay.elem('div', {
          'class': ELEM_FOOTER
        });
 
    if(options.zIndex) elem.style.zIndex = options.zIndex;
 
    // 生成tab 内容区域
    elemTab.appendChild(tabHead);
    elemTab.appendChild(tabContent);
    lay.each(lang.tabs, function(i,item){
      // 表头
      var li = lay.elem('li',{
        'class':i===0?THIS:"",
        'lay-id':i
      });
      li.innerHTML = item.title;
      tabHead.appendChild(li);
 
      // 表体
      tabContent.appendChild(that.getTabContentChildElem(i));
    });
 
    // 主区域
    elemMain = that.elemMain = lay.elem('div', {
      'class': 'layui-cron-main'
    });
    elemMain.appendChild(elemTab);
 
    //生成底部栏
    lay(divFooter).html(function(){
      var html = [], btns = [];
      lay.each(options.btns, function(i, item){
        var title = lang.tools[item] || 'btn';
        btns.push('<span lay-type="'+ item +'" class="cron-btns-'+ item +'">'+ title +'</span>');
      });
      html.push('<div class="cron-footer-btns">'+ btns.join('') +'</div>');
      return html.join('');
    }());
 
    //插入到主区域
    elem.appendChild(elemMain);
 
    options.showBottom && elem.appendChild(divFooter);
 
 
    //移除上一个控件
    that.remove(Class.thisElemCron);
 
    //如果是静态定位,则插入到指定的容器中,否则,插入到body
    isStatic ? options.elem.append(elem) : (
        document.body.appendChild(elem)
            ,that.position()
    );
 
 
    that.checkCron();
 
    that.elemEvent(); // 主面板事件
 
    Class.thisElemCron = that.elemID;
 
    form.render();
 
  }
 
  // 渲染 tab 子控件
  Class.prototype.getTabContentChildElem = function(index){
    var that = this,
        options = that.config,
        tabItem = options.tabs[index],
        tabItemKey = tabItem.key,
        lang = that.lang(),
        tabItemLang = lang.tabs[index],
        cron = options.cron,
        formFilter = 'cronForm'+tabItemKey+options.elem.attr('lay-key')
        ,data = function(){
          if(cron[tabItemKey].indexOf('-') != -1){
            // 周期数据
            var arr = cron[tabItemKey].split('-');
            return {
              type:'range',
              start:arr[0],
              end:arr[1]
            };
          }
          if(cron[tabItemKey].indexOf('/') != -1){
            // 频率数据
            var arr = cron[tabItemKey].split('/');
            return {
              type:'rate',
              begin:arr[0],
              rate:arr[1]
            };
          }
          if(cron[tabItemKey].indexOf(',') != -1 || /^\+?[0-9][0-9]*$/.test(cron[tabItemKey])){
            // 按照指定执行
            var arr = cron[tabItemKey].split(',').map(Number);
            return {
              type:'custom',
              values:arr
            };
          }
          if(cron[tabItemKey].indexOf('W') != -1){
            // 最近的工作日
            var value = cron[tabItemKey].replace('W','');
            return {
              type:'weekday',
              value: value
            };
          }
          if(index===3 && cron[tabItemKey] === 'L'){
            // 本月最后一日
            return {
              type:'lastday',
              value: 'L'
            };
          }
          if(index===5 && cron[tabItemKey].indexOf('L') != -1){
            // 本月最后一个周 value
            var value = cron[tabItemKey].replace('L','');
            return {
              type:'lastweek',
              value: value
            };
          }
          if(cron[tabItemKey] === '*'){
            // 每次
            return {
              type:'every',
              value:'*'
            };
          }
          if(cron[tabItemKey] === '?'||cron[tabItemKey]===undefined||cron[tabItemKey]===''){
            // 不指定
            return {
              type:'unspecified',
              value:cron[tabItemKey]
            };
          }
        }()
        , rangeData = function(){
          if(tabItem.range){
            var arr = tabItem.range.split('-');
            return {
              min:parseInt(arr[0]),
              max:parseInt(arr[1])
            };
          }
        }();
    var elem = lay.elem('div', {
      'class': 'layui-tab-item layui-form '+(index===0?SHOW:"")
      ,'lay-filter': formFilter
    });
 
    // 每次
    elem.appendChild(function(){
      var everyRadio = lay.elem('input',{
        'name': tabItemKey+'[type]'
        ,'type': 'radio'
        ,'value': 'every'
        ,'title': lang.every+tabItemLang.title
      });
      if(data.type === 'every'){
        lay(everyRadio).attr('checked', true);
      }
      var everyDiv = lay.elem('div',{
        'class':'cron-row'
      });
      everyDiv.appendChild(everyRadio);
      return everyDiv;
    }());
 
    // 不指定,从日开始
    if(index >= 3){
      elem.appendChild(function(){
        var unspecifiedRadio = lay.elem('input',{
          'name': tabItemKey+'[type]'
          ,'type': 'radio'
          ,'value': 'unspecified'
          ,'title': lang.unspecified
        });
        if(data.type==='unspecified'){
          lay(unspecifiedRadio).attr('checked', true);
        }
        var unspecifiedDiv = lay.elem('div',{
          'class':'cron-row'
        });
        unspecifiedDiv.appendChild(unspecifiedRadio);
        return unspecifiedDiv;
      }());
    }
 
    // 周期
    var rangeChild = [function(){
      var rangeRadio = lay.elem('input',{
        'name': tabItemKey+'[type]'
        ,'type': 'radio'
        ,'value': 'range'
        ,'title': lang.period
      });
      if(data.type === 'range'){
        lay(rangeRadio).attr('checked', true);
      }
      return rangeRadio;
    }(),function(){
      var elem = lay.elem('div',{
        'class':'cron-input-mid'
      });
      elem.innerHTML = lang.periodFrom;
      return elem;
    }(),function(){
      var elem = lay.elem('input',{
        'class':'cron-input',
        'type': 'number',
        'name': 'rangeStart',
        'value': data.start||''
      });
      return elem;
    }(),function(){
      var elem = lay.elem('div',{
        'class':'cron-input-mid'
      });
      elem.innerHTML = '-';
      return elem;
    }(),function(){
      var elem = lay.elem('input',{
        'class':'cron-input',
        'type': 'number',
        'name': 'rangeEnd',
        'value': data.end||''
      });
      return elem;
    }(),function(){
      var elem = lay.elem('div',{
        'class':'cron-input-mid'
      });
      elem.innerHTML = tabItemLang.title;
      return elem;
    }()]
 
        ,rangeDiv = lay.elem('div',{
      'class':'cron-row'
    });
    lay.each(rangeChild,function(i,item){
      rangeDiv.appendChild(item);
    });
    if(tabItem.range){
      var rangeTip = lay.elem('div',{
        'class':'cron-tips'
      });
      rangeTip.innerHTML = ['(',tabItem.range,')'].join('');
      rangeDiv.appendChild(rangeTip);
    }
    elem.appendChild(rangeDiv);
 
    // 频率,年没有
    if(index<6){
      var rateChild = [function(){
        var rateRadio = lay.elem('input',{
          'name': tabItemKey+'[type]'
          ,'type': 'radio'
          ,'value': 'rate'
          ,'title': lang.rate
        });
        if(data.type === 'rate'){
          lay(rateRadio).attr('checked', true);
        }
        return rateRadio;
      }(),function(){
        var elem = lay.elem('div',{
          'class':'cron-input-mid'
        });
        elem.innerHTML = tabItemLang.rateBegin || lang.rateBegin;
        return elem;
      }(),function(){
        var elem = lay.elem('input',{
          'class':'cron-input',
          'type': 'number',
          'name': 'begin',
          'value': data.begin||''
        });
        return elem;
      }(),function(){
        var elem = lay.elem('div',{
          'class':'cron-input-mid'
        });
        elem.innerHTML = tabItemLang.rateMid || (tabItemLang.title+lang.rateMid);
        return elem;
      }(),function(){
        var elem = lay.elem('input',{
          'class':'cron-input',
          'type': 'number',
          'name': 'rate',
          'value': data.rate||''
        });
        return elem;
      }(),function(){
        var elem = lay.elem('div',{
          'class':'cron-input-mid'
        });
        elem.innerHTML = undefined!=tabItemLang.rateEnd ? tabItemLang.rateEnd:(tabItemLang.title+lang.rateEnd);
        if(undefined!=tabItemLang.rateEnd&&tabItemLang.rateEnd===''){
          lay(elem).addClass(HIDE);
        }
        return elem;
      }()]
 
          ,rateDiv = lay.elem('div',{
        'class':'cron-row'
      });
      lay.each(rateChild,function(i,item){
        rateDiv.appendChild(item);
      });
      if(tabItem.range){
        var rateTip = lay.elem('div',{
          'class':'cron-tips'
        });
        if(index===5){
          // 周
          rateTip.innerHTML = '(1-4/1-7)';
        }else{
          rateTip.innerHTML = ['(',rangeData.min,'/',(rangeData.max+(index<=2?1:0)),')'].join('');
        }
        rateDiv.appendChild(rateTip);
      }
      elem.appendChild(rateDiv);
    }
 
    // 特殊:日(最近的工作日、最后一日),周(最后一周)
    if(index===3){
      // 日
      // 最近的工作日
      var weekChild = [function(){
        var weekRadio = lay.elem('input',{
          'name': tabItemKey+'[type]'
          ,'type': 'radio'
          ,'value': 'weekday'
          ,'title': lang.weekday
        });
        if(data.type === 'weekday'){
          lay(weekRadio).attr('checked', true);
        }
        return weekRadio;
      }(),function(){
        var elem = lay.elem('div',{
          'class':'cron-input-mid'
        });
        elem.innerHTML = lang.weekdayPrefix;
        return elem;
      }(),function(){
        var elem = lay.elem('input',{
          'class':'cron-input',
          'type': 'number',
          'name': 'weekday',
          'value': data.value||''
        });
        return elem;
      }(),function(){
        var elem = lay.elem('div',{
          'class':'cron-input-mid'
        });
        elem.innerHTML = lang.weekdaySuffix;
        return elem;
      }(),function(){
        var elem = lay.elem('div',{
          'class':'cron-tips'
        });
        elem.innerHTML = ['(',tabItem.range,')'].join('');
        return elem;
      }()]
 
          ,weekDiv = lay.elem('div',{
        'class':'cron-row'
      });
      lay.each(weekChild,function(i,item){
        weekDiv.appendChild(item);
      });
      elem.appendChild(weekDiv);
 
      // 本月最后一日
      elem.appendChild(function(){
        var lastRadio = lay.elem('input',{
          'name': tabItemKey+'[type]'
          ,'type': 'radio'
          ,'value': 'lastday'
          ,'title': lang.lastday
        });
        if(data.type === 'lastday'){
          lay(lastRadio).attr('checked', true);
        }
        var lastDiv = lay.elem('div',{
          'class':'cron-row'
        });
        lastDiv.appendChild(lastRadio);
        return lastDiv;
      }());
 
    }
 
    if(index===5){
      // 本月最后一个周几
      var lastWeekChild = [function(){
        var lastWeekRadio = lay.elem('input',{
          'name': tabItemKey+'[type]'
          ,'type': 'radio'
          ,'value': 'lastweek'
          ,'title': lang.lastweek
        });
        if(data.type === 'lastweek'){
          lay(lastWeekRadio).attr('checked', true);
        }
        return lastWeekRadio;
      }(),function(){
        var elem = lay.elem('input',{
          'class':'cron-input',
          'type': 'number',
          'name': 'lastweek',
          'value': data.value||''
        });
        return elem;
      }(),function(){
        var elem = lay.elem('div',{
          'class':'cron-tips'
        });
        elem.innerHTML = ['(',tabItem.range,')'].join('');
        return elem;
      }()]
 
          ,lastWeekDiv = lay.elem('div',{
        'class':'cron-row'
      });
      lay.each(lastWeekChild,function(i,item){
        lastWeekDiv.appendChild(item);
      });
      elem.appendChild(lastWeekDiv);
 
    }
 
    // 指定
    if(index <= 5){
      elem.appendChild(function(){
        var customRadio = lay.elem('input',{
          'name': tabItemKey+'[type]'
          ,'type': 'radio'
          ,'value': 'custom'
          ,'title': lang.custom
        });
        if(data.type === 'custom'){
          lay(customRadio).attr('checked', true);
        }
        var customDiv = lay.elem('div',{
          'class':'cron-row'
        });
        customDiv.appendChild(customRadio);
        return customDiv;
      }());
 
      // 指定数值,时分秒显示两位数,自动补零
      elem.appendChild(function(){
        var customGrid = lay.elem('div',{
          'class': 'cron-grid'
        });
        var i = rangeData.min;
        while(i<=rangeData.max){
          // 时分秒显示两位数,自动补零
          var gridItemValue = index<=2 ? lay.digit(i,2) : i;
          var gridItem = lay.elem('input',{
            'type': 'checkbox',
            'title': gridItemValue,
            'lay-skin': 'primary',
            'name':tabItemKey+'[custom]',
            'value':i
          });
          if(data.values && data.values.includes(i)){
            lay(gridItem).attr('checked',true);
          }
          customGrid.appendChild(gridItem);
          i++;
        }
        return customGrid;
      }());
    }
 
 
    return elem;
  }
 
  //是否输入框
  Class.prototype.isInput = function(elem){
    return /input|textarea/.test(elem.tagName.toLocaleLowerCase());
  };
 
  // 绑定的元素事件处理
  Class.prototype.events = function(){
    var that = this
        ,options = that.config
 
        //绑定呼出控件事件
        ,showEvent = function(elem, bind){
          elem.on(options.trigger, function(){
            bind && (that.bindElem = this);
            that.render();
          });
        };
 
    if(!options.elem[0] || options.elem[0].eventHandler) return;
 
    showEvent(options.elem, 'bind');
    showEvent(options.eventElem);
 
    //绑定关闭控件事件
    lay(document).on('click', function(e){
      if(e.target === options.elem[0]
          || e.target === options.eventElem[0]
          || e.target === lay(options.closeStop)[0]){
        return;
      }
      that.remove();
    }).on('keydown', function(e){
      if(e.keyCode === 13){
        if(lay('#'+ that.elemID)[0] && that.elemID === Class.thisElemDate){
          e.preventDefault();
          lay(that.footer).find(ELEM_CONFIRM)[0].click();
        }
      }
    });
 
    //自适应定位
    lay(window).on('resize', function(){
      if(!that.elem || !lay(ELEM)[0]){
        return false;
      }
      that.position();
    });
 
    options.elem[0].eventHandler = true;
  };
 
  // 主面板事件
  Class.prototype.elemEvent = function(){
    var that = this
        ,options = that.config
        ,tabFilter = 'cron-tab' + options.elem.attr('lay-key');
 
    // 阻止主面板点击冒泡,避免因触发文档事件而关闭主面
    lay(that.elem).on('click', function(e){
      lay.stope(e);
    });
 
    // tab选项卡切换
    var lis = lay(that.elemTab).find('li');
    lis.on('click',function(){
      var layid = lay(this).attr('lay-id');
      if(undefined === layid){
        return;
      }
      element.tabChange(tabFilter, layid);
    });
 
    // cron选项点击
    form.on('radio', function(data){
      var $parent = data.othis.parent();
      var formFilter = $parent.parent().attr('lay-filter');
      var formData = form.val(formFilter);
      var radioType = data.value;
      if('range'===radioType){
        // 范围
        form.val(formFilter,{
          rangeStart: formData.rangeStart||0,
          rangeEnd: formData.rangeEnd||2
        });
      }
      if('rate'===radioType){
        // 频率
        form.val(formFilter,{
          begin: formData.begin||0,
          rate: formData.rate||2
        });
      }
      if('custom'===radioType){
        // custom
        var $grid = $parent.next();
        if($grid.find(':checkbox:checked').length<=0){
          $grid.children(':checkbox:first').next().click()
        }
      }
      if('weekday'===radioType){
        // weekday
        form.val(formFilter,{
          weekday: formData.weekday||1
        });
      }
      if('lastweek'===radioType){
        // lastweek
        form.val(formFilter,{
          lastweek: formData.lastweek||1
        });
      }
 
    });
 
    //点击底部按钮
    lay(that.footer).find('span').on('click', function(){
      var type = lay(this).attr('lay-type');
      that.tool(this, type);
    });
  };
 
  //底部按钮点击事件
  Class.prototype.tool = function(btn, type){
    var that = this
        ,options = that.config
        ,lang = that.lang()
        ,isStatic = options.position === 'static'
        ,active = {
      //运行
      run: function(){
        var value = that.parse();
        var loading = layer.load();
        $.get(options.run,{cron:value},function(res){
          layer.close(loading);
          if(res.code !== 0){
            return that.hint(res.msg);
          }
          that.runHint(res.data);
        },'json').fail(function(){
          layer.close(loading);
          that.hint('服务器异常!');
        });
      }
 
      //确定
      ,confirm: function(){
        var value = that.parse();
        that.done([value]);
        that.setValue(value).remove()
      }
    };
    active[type] && active[type]();
  };
 
  //执行 done/change 回调
  Class.prototype.done = function(param, type){
    var that = this
        ,options = that.config;
 
    param = param || [that.parse()];
    typeof options[type || 'done'] === 'function' && options[type || 'done'].apply(options, param);
 
    return that;
  };
 
  // 解析cron表达式
  Class.prototype.parse = function(){
    var that = this
        ,options = that.config
        ,valueArr = [];
 
    lay.each(options.tabs, function(index, item){
      var key = item.key;
      var formFilter = 'cronForm' + key + options.elem.attr('lay-key');
      var formData = form.val(formFilter);
      var radioType = (key+'[type]');
      var current = "";
      if(formData[radioType] === 'every'){
        // 每次
        current = "*";
      }
      if(formData[radioType] === 'range'){
        // 范围
        current = formData.rangeStart + "-" + formData.rangeEnd;
      }
      if(formData[radioType] === 'rate'){
        // 频率
        current = formData.begin + "/" + formData.rate;
      }
      if(formData[radioType] === 'custom'){
        // 指定
        var checkboxName = (item.key+'[custom]');
        var customArr = [];
        $('input[name="' + checkboxName + '"]:checked').each(function() {
          customArr.push($(this).val());
        });
        current = customArr.join(',');
      }
      if(formData[radioType] === 'weekday'){
        // 每月 formData.weekday 号最近的那个工作日
        current = formData.weekday + "W";
      }
      if(formData[radioType] === 'lastday'){
        // 本月最后一日
        current = "L";
      }
      if(formData[radioType] === 'lastweek'){
        // 本月最后星期
        current = formData.lastweek + "L";
      }
 
      if(formData[radioType] === 'unspecified' && index != 6){
        // 不指定
        current = "?";
      }
      if(current !== ""){
        valueArr.push(current);
        options.cron[key] = current;
      }
    });
    return valueArr.join(' ');
  };
 
  //控件移除
  Class.prototype.remove = function(prev){
    var that = this
        ,options = that.config
        ,elem = lay('#'+ (prev || that.elemID));
    if(!elem[0]) return that;
 
    if(!elem.hasClass(ELEM_STATIC)){
      that.checkCron(function(){
        elem.remove();
      });
    }
    return that;
  };
 
  //定位算法
  Class.prototype.position = function(){
    var that = this
        ,options = that.config;
    lay.position(that.bindElem || options.elem[0], that.elem, {
      position: options.position
    });
    return that;
  };
 
  //提示
  Class.prototype.hint = function(content){
    var that = this
        ,options = that.config
        ,div = lay.elem('div', {
      'class': ELEM_HINT
    });
 
    if(!that.elem) return;
 
    div.innerHTML = content || '';
    lay(that.elem).find('.'+ ELEM_HINT).remove();
    that.elem.appendChild(div);
 
    clearTimeout(that.hinTimer);
    that.hinTimer = setTimeout(function(){
      lay(that.elem).find('.'+ ELEM_HINT).remove();
    }, 3000);
  };
 
  //运行提示
  Class.prototype.runHint = function(runList){
    var that = this
        ,options = that.config
        ,div = lay.elem('div', {
      'class': ELEM_RUN_HINT
    });
    // debugger;
    if(!that.elem||!runList||!runList.length) return;
 
 
    lay(div).html(function(){
      var html = [];
      lay.each(runList, function(i, item){
        html.push('<div class="cron-run-list">'+ item +'</div>');
      });
      return html.join('');
    }());
 
    lay(that.elem).find('.'+ ELEM_RUN_HINT).remove();
    that.elem.appendChild(div);
  };
 
  //赋值
  Class.prototype.setValue = function(value=''){
    var that = this
        ,options = that.config
        ,elem = that.bindElem || options.elem[0]
        ,valType = that.isInput(elem) ? 'val' : 'html'
 
    options.position === 'static' || lay(elem)[valType](value || '');
    elem.textContent = '生成';
    return this;
  };
 
  //cron校验
  Class.prototype.checkCron = function(fn){
    var that = this
        ,options = that.config
        ,lang = that.lang()
        ,elem = that.bindElem || options.elem[0]
        ,value = that.isInput(elem) ? elem.value : (options.position === 'static' ? '' : elem.innerHTML)
 
        ,checkValid = function(value=""){
 
    };
 
    // cron 值,多个空格替换为一个空格,去掉首尾空格
    value = value || options.value;
    if(typeof value === 'string'){
      value = value.replace(/\s+/g, ' ').replace(/^\s|\s$/g, '');
    }
 
    if(fn==='init') return checkValid(value),that;
 
    value = that.parse();
    if(value){
      that.setValue(value);
    }
    fn && fn();
    return that;
  };
 
  //核心入口
  cron.render = function(options){
    var ins = new Class(options);
    return thisIns.call(ins);
  };
 
  exports('cron', cron);
});