Mon Jul 29 2019
Copied to clipboard! Copy reply
  • 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
  • 1067
  • 1068
  • 1069
  • 1070
  • 1071
  • 1072
  • 1073
  • 1074
  • 1075
  • 1076
  • 1077
  • 1078
  • 1079
  • 1080
  • 1081
  • 1082
  • 1083
  • 1084
  • 1085
  • 1086
  • 1087
  • 1088
  • 1089
  • 1090
  • 1091
  • 1092
  • 1093
  • 1094
  • 1095
  • 1096
  • 1097
  • 1098
  • 1099
  • 1100
  • 1101
  • 1102
  • 1103
  • 1104
  • 1105
  • 1106
  • 1107
  • 1108
  • 1109
  • 1110
  • 1111
  • 1112
  • 1113
  • 1114
  • 1115
  • 1116
  • 1117
  • 1118
  • 1119
  • 1120
  • 1121
  • 1122
<template>

    <v-container class="container" grid-list-xl>
        <header>
          <h3 class="display-1">Secure Checkout</h3>
        </header>      
     
        <v-layout wrap>

          
          <!-- Left Side -->
          <v-flex xs12 sm8 md8 lg6 offset-lg1>
            
            <v-layout class="steps" row wrap>

              <!-- Guest Name -->
              <!-- <v-flex d-flex xs12>
                <v-card v-bind:class="steps.one.mode">
                  <v-card-text>
                    <v-form ref='form-one'>
                      <div class="step-header">                        
                        
                        <span class='step-title'>                          
                          <div>
                            <span class="step-number">1</span>
                            Guest's name (optional)
                          </div>
                          <a v-if="steps.one.mode == 'completed-step'" @click.prevent="editStep('one')">edit</a>
                        </span>
                      </div>
                      <div class="step-content">
                        <p class="hint">
                          If given, the guest's name will be engraved on the crate.
                          <br>
                          You are not charged for this.                          
                        </p>
                        <v-text-field   
                          placeholder="Guest's name"                          
                          class="input-group"
                          maxlength="30"
                          v-model="guest">            
                        </v-text-field>
                        <v-btn
                          class="step-button primary"
                          block
                          large                          
                          @click="goToNextStep('one','two')">
                            continue
                          </v-btn>
                      </div>
                    </v-form>
                  </v-card-text>
                </v-card>
              </v-flex> -->


              <!-- Shipping -->
              <v-flex d-flex xs12>
                <v-card v-bind:class="steps.one.mode">
                  <v-card-text>
                    <v-form ref='form-one'>
                      <div class="step-header">                        
                        <span class='step-title'>
                          <div>
                            <span class="step-number">1</span>
                            Shipping Address
                          </div>
                          <a v-if="steps.one.mode == 'completed-step'" @click.prevent="editStep('one')">edit</a>
                        </span>                        
                      </div>
                      <div class="step-content">
                        <!-- check to see if addresses.length.if so, show dropdown.
                        if not, show form with checkbox to save addr (if logged in) -->
                        <small>
                          Tip: You could ship the crate to your property and let
                          the guest know that there is a package waiting for them.
                          Or if you have a cleaning crew, you could ask them
                          to leave the crate on the bed for the guest.
                        </small>

                        <v-card-text style="padding-bottom: 0">
                          <!-- Receipient Name -->
                          <v-text-field
                            v-model='shippingName'
                            maxlength="25"
                            label="Name"                            
                            class="input-group">            
                          </v-text-field>

                          <!-- <v-tabs>
                            <v-tab>
                              Use saved address
                            </v-tab>
                            <v-tab>
                              Enter an address
                            </v-tab>
                          </v-tabs> -->

                          <!-- Saved Address Dropdown -->
                          <div v-if='this.addresses.length'>                   
                            <v-select
                              :value='$store.state.checkout.shipping.id'
                              :items="addresses"                              
                              label="Select an address"                              
                              item-text='street'
                              item-value='id'
                              @change='updateshipping'
                              return-object>
                            </v-select>
                          

                          <!-- Make new address form visible -->
                          <a href='#'
                            class="toggle-form"
                            v-if='this.addresses.length'
                            @click.prevent='showAddressForm = true'>
                            <span v-if='!showAddressForm'>{{ addressFormText[0] }}</span>
                            <span v-else>{{ addressFormText[1] }}</span>
                          </a>
                          </div>

                          <!--  New Address form -->
                          <div v-if='showAddressForm' class="new-address-form">

                            <v-text-field
                              v-model='street'
                              label="Street Address"  
                              :rules="rules.street"                                                       
                              required
                              class="input-group">            
                            </v-text-field>
                          
                            <v-text-field
                              v-model='city'
                              label="City"                              
                              :rules="rules.city"
                              required
                              class="input-group">            
                            </v-text-field>                      
                          
                            <v-select
                              v-model='state'
                              :items="states"
                              :rules="rules.state"                   
                              label="Select State"
                              single-line
                              required>
                            </v-select>                      

                            <v-text-field    
                              v-model='zip'
                              label="Zip"                              
                              :rules="rules.zip"
                              required
                              class="input-group">            
                            </v-text-field>

                            <v-checkbox
                              label="Save this address for future use"
                              v-if='$store.state.user.jwt'
                              v-model='saveAddress'>
                            </v-checkbox>
                          </div>
                        </v-card-text>
                          
                        <v-btn
                          class="step-button primary"
                          block
                          large
                          @click="goToNextStep('one','two')">
                            continue
                          </v-btn>
                      </div>
                    </v-form>
                  </v-card-text>
                </v-card>          
              </v-flex>


              <!-- Payment -->
              <v-flex d-flex xs12>
                <v-card v-bind:class="steps.two.mode">
                  <v-card-text>
                    <v-form ref='form-two'>
                      <div class="step-header">
                        <span class='step-title'>
                          <div>
                            <span class="step-number">2</span>
                            Payment  
                          </div>
                          <a v-if="steps.three.mode == 'completed-step'" @click.prevent="editStep('two')">edit</a>
                        </span>                   
                      </div>
                      <div class="step-content">

                        <!-- CC Select -->
                        <v-card-text v-if='$store.state.user.jwt && cards.length'>
                          <v-select                           
                            :items="cards"
                            v-model='selectedCard'
                            label="Select a credit card"
                            item-text='value'
                            item-value='id'
                            @change="toggleCardFormStyle('none')"
                            return-object>
                          </v-select>

                          <v-alert color="error" icon="warning" value="true" v-if='selectedCardError'>
                            {{ selectedCardError }}
                          </v-alert>

                          <v-alert color="error" icon="warning" value="true" v-if='ccSubmitError'>
                            {{ ccSubmitError }}
                          </v-alert>                            

                          <v-btn                          
                            block
                            large
                            dark
                            class="green"
                            v-if='!showCardForm'
                            :loading='isSubmitting'
                            :disabled="$store.getters['cart/cartTotal'] <= 0"
                            @click.prevent="handleCreditCardSubmit('select')">
                              <v-icon left dark>lock</v-icon>                            
                              PAY ${{ $store.getters['cart/cartTotal'] }}
                          </v-btn>             

                            <!-- Link to make credit card form visible -->
                          <a href='#'
                            class="toggle-form"
                            v-if='this.cards.length && !this.showCardForm'
                            @click.prevent='toggleCardFormStyle("block")'>
                            or click to use a new card
                          </a>
                        </v-card-text>
                        
                        <!-- Credit Card form -->           

                        <v-card-text class="cardform-container" v-bind:style='{ display: cardFormStyle }'>
                          <div class="cardform">
                            <v-card-text>
                              <label>
                                <div>Credit card number</div>
                                <div id='cardNumber-mount' class="stripe-field"></div>
                                <div class="input-error" v-if='ccNumberError'>{{ccNumberError}}</div>
                                <span class="icon"><i class="material-icons">credit_card</i></span>
                              </label>                              
                            </v-card-text>

                            <v-card-text>
                              <label>
                                <div>Expiration</div>
                                <div id='cardExpiry-mount' class="stripe-field"></div>  
                                <div class="input-error" v-if="ccExpiryError">{{ccExpiryError}}</div>
                                <span class="icon"><i class="material-icons">event_note</i></span>
                              </label>
                            </v-card-text>

                            <v-card-text>
                              <label>
                                <div>Security Code</div>
                                <div id='cardCvc-mount' class="stripe-field"></div>
                                <div class="input-error" v-if='ccCvcError'>{{ccCvcError}}</div>
                                <span class="icon"><i class="material-icons">lock</i></span>
                              </label>
                            </v-card-text>

                            <v-card-text style="padding-top: 0">                                
                              <v-text-field
                                label='Your e-mail'
                                :rules="rules.email"
                                v-model='email'
                                persistent-hint
                                hint="Your receipt will be sent here">
                              </v-text-field>                                
                            </v-card-text>

                            <v-alert color="error" icon="warning" value="true" v-if='ccSubmitError'>
                              {{ ccSubmitError }}
                            </v-alert>

                            <v-btn
                              class="green"
                              dark
                              block
                              large
                              :loading='isSubmitting'
                              :disabled='$store.getters.total <= 0'
                              @click.prevent="handleCreditCardSubmit('form')">
                                <v-icon left dark>lock</v-icon>                            
                                PAY ${{ $store.getters['cart/cartTotal'] }}
                            </v-btn>
                          
                          </div>
                        </v-card-text>                 
                        
                      </div>
                    </v-form>
                  </v-card-text>
                </v-card>
              </v-flex>              

            </v-layout>         
          </v-flex>


          <!-- Right side -->
          <v-flex xs12 sm4  md4 lg4 class="summary">
            <v-card>
              <v-card-text>
                <h5>Order Summary</h5>

                <div class="summary-items">
                  <div v-for="crate in $store.state.cart.items" :key='crate.id'>
                    <div>
                      <!--
                      <input type="text" v-model='$store.state.crates[key].quantity' :value='$store.state.crates[key].quantity'> {{key}} crate
                      -->
                      <input type="number" 
                        min='0'
                        max='9'
                        :value='crate.quantity'
                        @change="updateCrate(crate, $event)"> {{crate.name}} crate
                    </div>
                  </div>                          
                </div>
                <!--
                <div class="summary-premium">
                  <v-switch label="Premium crates?"                               
                    v-model="premium"
                    color="primary"
                    hide-details>  
                  </v-switch>
                </div>    
                -->
                <v-card-text>
                <div class="summary-shipping">
                  <div>
                    <h6>Shipping to:</h6>
                  </div>
                  <div v-if='$store.state.checkout.shipping.name'>
                    {{ $store.state.checkout.shipping.name }}
                  </div>
                  <div v-if='$store.state.checkout.shipping.street'>
                    {{ $store.state.checkout.shipping.street }}
                  </div>
                  <div v-if='$store.state.checkout.shipping.city'>
                    {{ $store.state.checkout.shipping.city }}
                    <span v-if='$store.state.checkout.shipping.state'>,</span>&nbsp;
                    {{ $store.state.checkout.shipping.state }}&nbsp;
                    {{ $store.state.checkout.shipping.zip }}
                  </div>
                  <div>{{ $store.state.checkout.email }}
                    </div>
                  <!-- <div class="address-check">Please make sure shipping address above is correct</div> -->
                </div>
                </v-card-text>
              </v-card-text>
            </v-card>
          </v-flex>

        </v-layout>
          
             
    </v-container>

</template>

<script>

import { isEmail, isCreditCard, isInt, isLength, isAlpha } from 'validator'
import { mapState } from 'vuex'

export default {  


  data ()
  {
    return {
      jwt: null,

      //email: this.$store.state.user.email || this.$store.state.checkout.email || '',
      addresses: [],
      cards: [],

      // If user checks checkbox to save address in list (logged in only)
      saveAddress: false,
      isSubmitting: false,
      addressFormText: [
        'or click here to use a different address', 
        'Enter an address below, or select one above.'
      ],

      // Card dropdown. Error will show if user submits and `selectCard` is null,
      // which means a card wasn't selected from the dropdown
      selectedCard: null,
      selectedCardError: null,
      showAddressForm: true,
      cardFormStyle: 'block',
      showCardForm: true,
      cardFormVisibility: 'hidden',

      // Initial step states. 
      steps: {
        "one": {
          mode: 'active-step'
        },
        "two": {
          mode: 'inactive-step'
        },
        "three": {
          mode: 'inactive-step'
        },
      },
      
      // Applied when user focus and leaves a field, or submits form.
      rules: {
        email: [
          v => !!v || 'E-mail is required for receipt.',
          v => isEmail(v) || 'This e-mail is not valid'
        ],
        name: [
          v => !!v || 'Name is required',
        ],
        street: [
          v => !!v || 'City is required',
          v => isLength(v, { min: 2 }) || 'This field is required'
        ],
        zip: [
          v => !!v || 'Zip code is required',
          v =>  /[0-9]+/i.test(v) || 'Enter a valid zip code'
        ],
        city: [
          v => !!v || 'City is required',
          v => isLength(v, { min: 1 }) || 'Enter a valid city'
        ],
        state: [
          v => !!v || 'This field is required',
          v => isAlpha(v) && isLength(v, { min: 2, max: 2 }) || 'Use 2 letters'
        ]
        
      },
      states: [
        "AL", "AK", "AS", "AZ", "AR", "CA", "CO", "CT", 
        "DE", "DC", "FM", "FL", "GA", "GU", "HI", "ID", 
        "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MH", 
        "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", 
        "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "MP", 
        "OH", "OK", "OR", "PW", "PA", "PR", "RI", "SC", 
        "SD", "TN", "TX", "UT", "VT", "VI", "VA", "WA", 
        "WV", "WI", "WY"
      ],
      ccSubmitError: null, // Handles response from server
      ccNumberError: '',
      ccCvcError: '',
      ccExpiryError: ''
    }
  },

  
  async beforeMount () {
    

   this.$store.watch( state => state, async (newValue, oldValue) => {
     if (!newValue.cart.items.length)
    {
      console.log('NO ITEMS')
      //return
      //return this.$router.replace('/')     
    }
     this.jwt = newValue.user.jwt
     console.log('~~~~ setting new jwt ~~~~~~~')

     // If 
     if (this.jwt) {
       console.log('------ logged in')
      // Addresses
      let req = await this.$axios({
        method: 'get',
        url: `/user/addresses` 
      })
      
      if (req.data.result == 0 && req.data.payload.code == 'NO_USER')
      {
        this.$store.commit('user/LOGOUT')
      }
      
      
      if (req.data.payload.addresses.length)
      {     
        this.addresses = req.data.payload.addresses
        this.showAddressForm = false
      }
      if (this.$store.state.user.addresses.length)
      {     
        this.addresses = this.$store.state.user.addresses
        this.showAddressForm = false
      }
        

      // Credit Card
      
      req = await this.$axios({
        method: 'get',
        url: `/user/cards`          
      })
      
      if (req.data.payload.cards.length)
      { 
        this.cards = req.data.payload.cards.map( o => {            
          return {id: o.id, value: `${o.brand} ending in ${o.last4}`}
        })
      }
      else
      {
        this.showCardForm = 'block'
      }        
     } else {
       this.showAddressForm = true
     }

      // User not logged in, or is logged in but no CC on file
      if (!this.jwt || (this.jwt && !this.cards.length))
      {
        this.toggleCardFormStyle('block')
      }
      else if (this.jwt && this.cards.length)
      {      
        this.toggleCardFormStyle('none')
      }
      
   })
    
  },

  

  async mounted()
  {    
    
    // Stripe element styling    
    window.stripe = window.Stripe(process.env.STRIPE_PUBLIC_KEY);
    var elements = stripe.elements();    
    var style = {
      base: {
        color: '#32325d',
        fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
        fontSmoothing: 'antialiased',
        fontSize: '16px',
        '::placeholder': {
          color: '#aab7c4'
        }
      },
      invalid: {
        color: '#fa755a',
        iconColor: '#fa755a'
      }
    };

    /**
     * Mount each card element individually so 
     * that they can be responsive and stack in mobile
     */
    
    window.cardNumber = elements.create('cardNumber', {style: style})
    window.cardCvc = elements.create('cardCvc', {style: style})
    window.cardExpiry = elements.create('cardExpiry', {style: style})
    
    window.cardNumber.mount('#cardNumber-mount');
    window.cardCvc.mount('#cardCvc-mount');
    window.cardExpiry.mount('#cardExpiry-mount'); 
    
    window.cardNumber.addEventListener('change', ({ error }) => {      
      if (error)
      {
        this.ccNumberError = error.message
      }
      else
      {
        this.ccNumberError = ''
      }
    });

    window.cardCvc.addEventListener('change', ({ error }) => {      
      if (error)
      {
        this.ccCvcError = error.message
      }
      else
      {
        this.ccCvcError = ''
      }
    });

    window.cardExpiry.addEventListener('change', ({ error }) => {      
      if (error)
      {
        this.ccExpiryError = error.message
      }
      else
      {
        this.ccExpiryError = ''
      }
    });


    
  },

  

  computed: {
    ...mapState({
     jwt: {
        get () {
          jwt: state => state.user.jwt
        },
        set (v) {        
          this.jwt = v
        }
      },      
    }),    
    email:
    {
      get ()
      {
        return this.$store.state.checkout.email
      },
      set (value)
      {
        this.$store.commit('checkout/UPDATE_EMAIL', value)
      }
    },
    guestName:
    {
      get ()
      {
        return this.$store.state.checkout.guestName
      },
      set (value)
      {
        this.$store.commit('checkout/UPDATE_GUEST_NAME', value)
      }
    },
    shippingName:
    {
      get ()
      {
        return this.$store.state.checkout.shipping.name
      },
      set (value)
      {
        this.$store.commit('checkout/UPDATE_SHIPPING_NAME', value)
      }
    },
    street:
    {
      get ()
      {
        return this.$store.state.checkout.shipping.street
      },
      set (value)
      {
        this.$store.commit('checkout/UPDATE_SHIPPING_STREET', value)
      }
    },
    city:
    {
      get ()
      {
        return this.$store.state.checkout.shipping.city
      },
      set (value)
      {
        this.$store.commit('checkout/UPDATE_SHIPPING_CITY', value)
      }
    },
    state:
    {
      get ()
      {
        return this.$store.state.checkout.shipping.state
      },
      set (value)
      {
        this.$store.commit('checkout/UPDATE_SHIPPING_STATE', value)
      }
    },
    zip:
    {
      get ()
      {
        return this.$store.state.checkout.shipping.zip
      },
      set (value)
      {
        this.$store.commit('checkout/UPDATE_SHIPPING_ZIP', value)
      }
    }
    
   
  },

  methods: {
    
    /**
     * Sets credit card form's css display style
     * to either 'block' or 'none'. The form
     * has to be shown this way because using Vue
     * to show/hide (render/destroy) will unmount
     * Stripe's fields.
     * 
     * @param {str} style - 'block' or 'none'
     */

    toggleCardFormStyle (style) {      
      if (style == 'block') {
        this.showCardForm = true

        // Reset v-select
        this.selectedCard = null

        // If there was an error from v-select, clear it.
        this.selectedCardError = null

        // Clear previous cc submit error
        this.ccSubmitError = false
      } else {        
        this.showCardForm = false
      }
      this.cardFormStyle = style
    },


    updateshipping (obj) {
      this.$store.commit('checkout/SET_SHIPPING_ADDRESS', obj)
    },


    /**
     * User has the option to change crate quantity
     * on the right side of checkout page.
     * 
     * @param {obj} crate - the crate object
     * @param {String} key - the name of the crate
     */

    updateCrate (crate, e) { 
      // If user tries to set < 1, keep original quantity      
      let current = this.$store.state.cart.items.filter(o => o.name == crate.name)[0].quantity             
      if (e.target.value < 1) {
        console.log('-- LESS THAN 1. SETTING TO ', current)
        this.$store.commit('cart/UPDATE_CART_ITEM', {
          name: crate.name,
          quantity:  current
        })
      } else {
        this.$store.commit('cart/UPDATE_CART_ITEM', {
          name: crate.name,
          quantity:  e.target.value
        })
      }
      this.$forceUpdate()      
    }, 
   

    /**
     * Makes a section (step) editable again when user
     * clicks the 'edit' link.
     * 
     * @param {String} stepNumber - number of the step we're editing ('one', 'two', etc)
     */
    editStep(stepNumber)
    {      
      this.steps[stepNumber].mode = 'active-step'
    },
  

    /**
     * Goes to next step only if form validates.
     * @param {String} formNumber - number of form ref that called this
     * @param {String} stepNumber - number of step to go to
     */    
    goToNextStep(formNumber, stepNumber)
    {
      // Because form-two is inside a v-if,
      // it won't get checked for validation, and
      // so will always pass. To fix, show
      // the form before going to next step
      if (formNumber == 'two')
      {
        this.showAddressForm = true
      }

      // We need this tick to give vue time to render
      // if we set this.showAddressForm to true in the
      // if condition above.
      this.$nextTick(() => {      
        if (this.$refs[`form-${formNumber}`].validate())
        {        
          this.steps[formNumber].mode = 'completed-step'
          this.steps[stepNumber].mode = 'active-step'
        }
      })
    
    },


    /**
     * Handles payment submission.
     * There are two credit card button payments:
     * One for the v-select, and one for the form.
     * We validate differently depending on which.
     * If it's from the v-select, we must check that
     * there is a value for `selectedCard`
     * If it's from the form, we must verify form with Stripe.
     *
     * @param {String} which - which pay button called this. 
     *   'select' or 'form'
     */
    async handleCreditCardSubmit(which)
    {

      
      this.isSubmitting = true
      var error = false
      var self = this
      var stripeToken
      
      const postData = {
        jwt: this.$store.state.user.jwt,
        orderData: {
          email: this.email,
          guestName: this.$store.state.checkout.guestName,
          shipping: this.$store.state.checkout.shipping,
          crates: this.$store.state.cart.items,         
          // This goes to Stripe
          metadata: {
            name: this.$store.state.checkout.shipping.name,
            address: this.$store.state.checkout.shipping.street,
            city: this.$store.state.checkout.shipping.city,
            state: this.$store.state.checkout.shipping.state,
            zip: this.$store.state.checkout.shipping.zip
          }
          //premium: this.$store.state.checkout.premium
        },
      }
    
     

      // User is paying with a card from the dropdown
      if (which == 'select')
      {
        // Show/hide error message if `selectedCard` is null (no card id)
        if (!this.selectedCard)
        {
          this.selectedCardError = 'Please select a card from the menu above'
          this.isSubmitting = false
          return
        }
        
        postData.cardId = this.selectedCard.id
        postData.stripeId = this.$store.state.user.stripeId       
      }

      else if (which == 'form')
      {       
        try
        {
          // New card, so we must obtain a Stripe Token for it
          var result = await stripe.createToken(cardNumber)
          postData.stripeToken = result.token                 
        }
        catch (e)
        {
          this.isSubmitting = false
          this.ccSubmitError = 'Error acquiring token. Please try again.'
        }

        if (!this.$refs['form-two'].validate() || !postData.stripeToken)
        //if (!this.$refs['form-three'].validate() || !postData.stripeToken)
        {
          this.isSubmitting = false
          error = true
          this.isSubmitting = false
          return
        }  
      }      
    
      this.selectedCardError = null
      
      
      await this.$axios({
        method: 'post',
        url: `/cart/checkout`,
        data: postData,
      })      
      .then( response => {
        if (response.data.result)
        {
          this.$router.replace('/checkout/complete')
          if (this.saveAddress)
          {
            this._saveAddress()
          }
          this.ccSubmitError = null
          
        }
        else
        {
          this.ccSubmitError = response.data.payload.message
        }
        this.isSubmitting = false
      })
      .catch(error => {
        // TODO log
        console.log(error)
        this.isSubmitting = false
        this.ccSubmitError = 'Could not charge. Please try again.'
      })
    },
    

    /**
     * Adds the shipping address to the user's list.
     * Called when the checkbox is checked.
     */

    async _saveAddress ()
    {
      try
      {
        const address = {
          street: this.$store.state.checkout.shipping.street,
          city: this.$store.state.checkout.shipping.city,
          zip: this.$store.state.checkout.shipping.zip,
          state: this.$store.state.checkout.shipping.state,
          country: this.$store.state.checkout.shipping.country
        }       
        

        let req = await this.$axios({
          method: 'post',
          url: `/user/address`,
          data: address
        })

        // The reason we use the response's address instead of the form's,
        // is that response contains the address ID created by the server
        if (req.data.result)
        {
          let addresses = [ ...this.$store.state.user.addresses ]
          addresses.push(req.data.payload.address)
          this.$store.commit('user/SET_USER_DATA', { addresses })
        }
      }
      catch (e)
      {
        // couldn't inset address into db
      }
    }

  }
}
</script>

<style lang='stylus'> 
  $steps-color = #777
  $steps-inactive-color = #999!important

  .active-step  
    .step-content
      display block

  .inactive-step
  .completed-step
    color $steps-inactive-color  
    .step-content
      display none

  .inactive-step
    background alpha(#fff, 40%)!important
    box-shadow none
    border 1px dashed #ccc!important

  .completed-step    
    .step-number    
      border-color transparent


  .toggle-form
    //background #eee
    overflow auto
    //padding 0.5em 0
    text-align center
    margin 0 auto
    display block
    border-radius 3px
    text-decoration none
    margin-bottom 1em
    font-size 80%
  
  p.hint
    font-size 80%
    color #999
    padding 0.5em  4.2em
    
  .steps
    font-size 1.2em
    
    & > div
      margin-bottom 1em
    
    .step-title
      display flex
      align-items baseline
      justify-content space-between
      font-size 120%
      a
        font-size 80%
    .step-number
      display inline-block
      border 1px solid $steps-color
      margin 0 1em 0 0
      width 2em
      height 2em
      line-height 2em
      border-radius 20px
      text-align center
      font-size 80%
    .step-content
      //padding-top 1em
      small
        display none
        font-size 65%
        color lighten($steps-color, 10%)       

    button
      margin-top 2em
    
  #cc-errors
    display none


  .stripe-field
    background rgba(238, 238, 238, 0.35)
    padding 8px 5px 

  .summary
    //font-size 1em
    //background rgba(228, 227, 227, 0.2)
    //padding 1em 2em
    //border-radius 5px
    color #444
    .card__text > div
      margin-bottom 1.4em
    h5
      font-size 140%
      font-weight normal
      //border-bottom 1px solid #ccc
      text-align  center
      margin-bottom 1em
    h6
      font-size 1em
      margin-bottom 7px
      font-weight bold    
    
    .summary-items
      line-height 32px
    .input-group
      padding-top 0
    
    input
      border-bottom 1px solid #ccc
      padding 0 10px
      max-width 55px
      text-align center
      margin 0 5px
      color $blue
      line-height 15px
      font-size 1.2em


  .address-check
    text-align center
    background #eeeeee29
    padding 0.2em 0
    margin-top 1.5em
    border 1px dashed #ccc
    display none

  .input-error
    color #ff5252
    font-weight light

  
    
  .cardform-container
    margin-top 1em
    //background rgba(0,128,0,0.11)
        
    
  .cardform 
    background #fff
    label
      position: relative;
      color #8a8a8a //#8898AA;
      display block
      font-weight 300
      font-size 85%
      & > :first-of-type
        color #666
        padding-bottom 3px
        font-weight bold
  

  label span.icon     
    position absolute
    top 31px
    right 10px
</style>