Thu Jan 05 2017
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
<template>
  <main>

    <div class="ui massive red message" v-if='editMode && !found && !isFetching'>
      <i class="warning circle icon"></i>
      This promoset no longer exists.
    </div>
  
    <!-- if not editing, or editing but found -->
    <form class="ui form" @submit.prevent="handleSubmit" v-if='(editMode && found) || !editMode'>
      <h2 class="ui header">Create a Promoset</h2>
      <div class="ui large header"></div> 
    
      <div class="required field">
        <div class="ui fluid labeled input">
          <div class="ui label">
            Promoset Name
          </div>
          <input type="text" placeholder="q3_189"  v-model='promoset'>
        </div>
      </div>          
      <table class="ui grey table">
        <thead>
          <tr>
            <th>Product</th>
            <th>CD</th>
            <th>Download</th>
          </tr>
        </thead>
        <tbody>
          <!--  Course Products -->
          <tr v-for="(course, level) in promodata.course">
            <td class="warning">{{ level.toUpperCase() }}</td>
            <td v-for="(product, key) in course.products">
              <div class="fields" style="margin-bottom: 0">
                <div class=" field">
                  <div class="ui transparent left icon input">
                    <i class="dollar icon"></i>
                    <input type="text"
                    placeholder="price"
                    name="price"
                    :data-level="level"
                    :data-delivery="key"
                    v-model="product.price"
                    @keyup="handleKeyPress">
                  </div>
                </div>
                <div class="field">
                  <div class="ui transparent left icon input">
                    <i class="code icon"></i>
                    <input type="text"
                    placeholder="code"
                    name="code"
                    :data-level="level"
                    :data-delivery="key"
                    v-model="product.code"
                    @keyup="handleKeyPress">
                  </div>
                </div>
              </div>
            </td>
          </tr>
          <!-- <tr>
            <td colspan="5">
              <h4 style="letter-spacing: 2px">Online Products</h4>
            </td>
          </tr> -->
          <!--  Online Products -->
          <tr v-for="(product, month) in promodata.online">
            <td class="warning">{{ month }}</td>
            <td>
              <div class="field">
                <div class="ui transparent left icon input">
                  <i class="dollar icon"></i>
                  <input type="text"
                  placeholder="price"
                  :data-months="month"                  
                  name="price">
                </div>
              </div>
            </td>
            <td>
              <div class=" field">
                <div class="ui transparent left icon input">
                  <i class="code icon"></i>
                  <input type="text"
                  placeholder="code"
                  :data-months="month"
                  name="code"
                  v-model="product.code"
                  @keyup="handleOnlineCodeInput">
                </div>
              </div>
            </td>
          </tr>
        </tbody>
      </table>      
      <div class="ui grid">
        <div class="thirteen wide column">
          <div
          class="ui message"
          :class="{'negative' : !formResult.result, 'positive' : formResult.result}"
          v-if="formResult.result !== null">
            <div class="header">
              <i class="warning circle icon"></i>
              {{ formResult.message || "An error occurred" }}
            </div>
            </div>
        </div>
        <div class="three wide column">
          <!-- <input type="submit" class="ui right fluid big teal button" /> -->
          <button class="ui fluid big teal button" :class="{'loading': isSubmitting}">Submit</button>
        </div>
      </div>      
    </form>
    
  </main>
</template>


<script>
  export default {
    name: 'CreatePromoset',
    data() {
      return {
        isSubmitting: false,
        isFetching: false,
        editMode: false,
        autofillCompleted: false,
        canSubmit: false,
        found: null,
        formResult: {
          result: null,
          message: null
        },
        promoset: '',
        promosets: [],
        promodata: {
          course: {
            s5: {
              name: 'S5',
              products: {
                cd: { price: null, code: null },
                dl: { price: null, code: null },
              }
            },
            s3: {
              name: 'S3',
              products: {
                cd: { price: null, code: null },
                dl: { price: null, code: null },
              }
            },
            s2: {
              name: 'S2',
              products: {
                cd: { price: null, code: null },
                dl: { price: null, code: null },
              }
            },
            l1: {
              name: 'L1',
              products: {
                cd: { price: null, code: null },
                dl: { price: null, code: null },
              }
            }
          },
          online: {
            '1M': { price: null, code: null },
            '3M': { price: null, code: null },
            '6M': { price: null, code: null },
            '9M': { price: null, code: null },
            '12M': { price: null, code: null },
            '24M': { price: null, code: null },
            '36M': { price: null, code: null },
          }
        }
      }
    },


    created() {
      
      console.log('fetching ', this.$route.params.id)

      /*
       * If we are editing (create/flip/id/some-date in the url)
       * fetch all the info to polulate the state with
       */
      
      if (this.$route.params.id) {
        this.editMode = true
        this.isFetching = true
      }
    },

    mounted() {
      const self = this
      
      if (this.editMode) {
        $.getJSON(`${window.server}/promosets/`, r => {
          r = Object.keys(r)
          if (r.includes(this.$route.params.id)) {
            this.promoset = this.$route.params.id
            this.found = true

            this.promodata.course.s5.cd.price =
            this.promodata.course.s5.cd.code =
            this.promodata.course.s5.dl.price =
            this.promodata.course.s5.dl.code =

            this.promodata.course.s3.cd.price =
            this.promodata.course.s3.cd.code =
            this.promodata.course.s3.dl.price =
            this.promodata.course.s3.dl.code =
            
            this.promodata.course.s2.cd.price =
            this.promodata.course.s2.cd.code =
            this.promodata.course.s2.dl.price =
            this.promodata.course.s2.dl.code =

            this.promodata.course.l1.cd.price =
            this.promodata.course.l1.cd.code =
            this.promodata.course.l1.dl.price =
            this.promodata.course.l1.dl.code =            

            this.promodata.online['1M'].price =
            this.promodata.online['1M'].code =

            this.promodata.online['3M'].price =
            this.promodata.online['3M'].code =

            this.promodata.online['6M'].price =
            this.promodata.online['6M'].code =

            this.promodata.online['9M'].price =
            this.promodata.online['9M'].code =

            this.promodata.online['12M'].price =
            this.promodata.online['12M'].code =

            this.promodata.online['24M'].price =
            this.promodata.online['24M'].code =

            this.promodata.online['36M'].price =
            this.promodata.online['36M'].code =
          } 
          this.isFetching = false
        })
      }

      

      $(this.$el).find('form[name=create-promoset]').form({
        fields: { 'promoset-name': 'empty' }
      })
    },


    methods: {

      /*
       * When a CD price and Code is typed,
       * prefill the download fields, as they
       * are usually the same
       */
      
      handleKeyPress(e) {        
        const
         $e = $(e.target)
         , level = $e.data('level')
         , delivery = $e.data('delivery')
         , input = $e.attr('name')        
         ;

        if (delivery == 'cd') {        
          this.promodata.course[level].products.dl[input] = $e.val()
        }
      },


      /*
       * When an online promocode is typed, prefill
       * all the other ones if they are blank.
       */
      
      handleOnlineCodeInput(e) {
        if (this.autofillCompleted) return

        const $e = $(e.target)
        const months = $e.data('months')
        const regex = /(_\d+)m/i
        var value = $e.val()

        for (let o in this.promodata.online) {
          if (value.match(regex)) {
            value = value.replace(regex, '_' + o)
            this.autofillCompleted = true
          }
          this.promodata.online[o].code = value
        }
      },

      
      /*
       * Submit form
       *
       */
      
      handleSubmit(e) {      
        e.preventDefault()
        console.log('submitting')
        if (this.isSubmitting) return
        console.log('2 submitting')
        this.isSubmitting = true
        
        const name = ($('input[name=promoset-name]').val() || '').trim()
        
        if (!name) {
          return this.isSubmitting = false        
        }

        /* Check if a set with this name already exists */        
        $.getJSON(`${window.server}/promosets`, r => {
          if (Object.keys(r).includes(name)) {
            this.formResult = {
              result: false,
              message: "A promoset with this name already exists."
            }
            this.isSubmitting = false
          }
        })
        
        const self = this
        const promoset = {}
        const promoObj = {}
          promoObj.BOX_S5 = {
            price: this.promodata.course.s5.products.cd.price,
            code: this.promodata.course.s5.products.cd.code
          }
          promoObj.DOWNLOAD_S5 = {
            price: this.promodata.course.s5.products.dl.price,
            code: this.promodata.course.s5.products.dl.code
          }
          promoObj.BOX_S3 = {
            price: this.promodata.course.s3.products.cd.price,
            code: this.promodata.course.s3.products.cd.code
          }
          promoObj.DOWNLOAD_S3 = {
            price: this.promodata.course.s3.products.dl.price,
            code: this.promodata.course.s3.products.dl.code
          }
          promoObj.BOX_S2 = {
            price: this.promodata.course.s3.products.cd.price,
            code: this.promodata.course.s3.products.cd.code
          }
          promoObj.DOWNLOAD_S2 = {
            price: this.promodata.course.s2.products.dl.price,
            code: this.promodata.course.s2.products.dl.code
          }
          promoObj.BOX_L1 = {
            price: this.promodata.course.l1.products.cd.price,
            code: this.promodata.course.l1.products.cd.code
          }
          promoObj.DOWNLOAD_L1 = {
            price: this.promodata.course.l1.products.dl.price,
            code: this.promodata.course.l1.products.dl.code
          }      
          promoObj.TOSUB_01 = {
            price: this.promodata.online['1M'].price,
            code: this.promodata.online['1M'].code
          }
          promoObj.TOSUB_03 = {
            price: this.promodata.online['3M'].price,
            code: this.promodata.online['3M'].code
          }
          promoObj.TOSUB_06 = {
            price: this.promodata.online['6M'].price,
            code: this.promodata.online['6M'].code
          }
          promoObj.TOSUB_09 = {
            price: this.promodata.online['9M'].price,
            code: this.promodata.online['9M'].code
          }
          promoObj.TOSUB_12 = {
            price: this.promodata.online['12M'].price,
            code: this.promodata.online['12M'].code
          }
          promoObj.TOSUB_24 = {
            price: this.promodata.online['24M'].price,
            code: this.promodata.online['24M'].code
          }
          promoObj.TOSUB_36 = {
            price: this.promodata.online['36M'].price,
            code: this.promodata.online['36M'].code
          }

        promoset[name] = promoObj
        
        const request = new XMLHttpRequest()
        request.open('POST', `${window.server}/promoset`)
        request.send(JSON.stringify(promoset))
        request.onload = function() {
          const result = JSON.parse(request.responseText)

          self.formResult = {
            result: result.result,
            message: result.payload
          }
          self.isSubmitting = false
        }
        
        
        
        

        
      }
    
    }
  }
</script>