<commit_before>class GroupProductSettingsView extends JView

  constructor:->
    super
    @setClass "payment-settings-view"
    group = @getData()
    
    @name = new KDInputView
      cssClass   : "product-title"
      placeholder: "Product name"
      
    @price = new KDInputView
      cssClass   : "product-price"
      placeholder: "0.00"

    @button = new KDButtonView
      cssClass   : "product-button"
      title      : "+"
      callback   : =>
        group.addPlan
          name  : __utils.slugify @name.getValue()
          price : @price.getValue()
          title : @name.getValue()
        , (err,plan)=>
          if err
            new KDNotificationView
              title: "Unable to create plan: #{err}"
          else
            @name.setValue ""
            @price.setValue ""
            @controller.loadItems()

    @controller = new GroupProductListController
      group     : group
      itemClass : GroupProductListItem

    @list = @controller.getListView()

    @controller.loadItems()

  pistachio:->
    """
    {{> this.button}}
    {{> this.name}}
    {{> this.price}}
    <br>
    {{> this.controller.getView()}}
    """
  viewAppended: ->
    @setTemplate do @pistachio


class GroupProductListController extends KDListViewController

  constructor:(options = {}, data)->
    @group = options.group
    super

  loadItems:(callback)->
    @removeAllItems()
    @customItem?.destroy()
    @showLazyLoader no

    KD.remote.api.JRecurlyPlan.getPlans 'groupplan', @group._id, (err,plans)=>
      if err or plans.length is 0
        @hideLazyLoader()
        @addCustomItem "This group has no products."
      else
        @hideLazyLoader()
        @instantiateListItems plans

  addCustomItem:(message)->
    @removeAllItems()
    @customItem?.destroy()
    @scrollView.addSubView @customItem = new KDCustomHTMLView
      cssClass : "no-item-found"
      partial  : message


class GroupProductListItem extends KDListItemView
  constructor:(options,data)->
    super options, data

    listView = @getDelegate()

    {code} = @getData()

    codeCheck  = """
                 KD.remote.api.JRecurlySubscription.checkUserSubscription '#{code}', (err, subscriptions)->
                   if not err and subscriptions.length > 0
                     console.log "User is subscribed to the plan."
                 """

    codeGet    = """..."""

    codeWidget = """@content = new KDButtonView
                    cssClass   : "clean-gray test-input"
                    title      : "Subscribed!"
                    callback   : ->

                    @payment = new PaymentWidget
                      planCode        : '#{code}'
                      contentCssClass : 'modal-clean-green'
                      content         : @content

                    @payment.on "subscribed", ->
                  """

    @embedButton = new KDButtonView
      title    : "View Embed Code"
      callback : =>
        if @embedView.hasClass "hidden"
          @embedView.unsetClass "hidden"
        else
          @embedView.setClass "hidden"

    @embedView = new KDTabView
      cssClass             : "hidden product-embed"
      hideHandleCloseIcons : yes
      paneData             : [
        { name : "Check Subscription", partial: "<pre>#{codeCheck}</pre>" }
        { name : "Get Subscribers",    partial: "<pre>#{codeGet}</pre>" }
        { name : "Subscribe Widget",   partial: "<pre>#{codeWidget}</pre>" }
      ]

    @clientsButton = new KDButtonView
      title    : "View Buyers"
      callback : ->
        new KDNotificationView
          title: "Coming soon!"

    @deleteButton = new KDButtonView
      title    : "-"
      callback : ->
        new KDNotificationView
          title: "Coming soon!"
 
  viewAppended: JView::viewAppended

  pistachio:->
    plan = @getData()

    code  = plan.code
    title = plan.title
    price = plan.feeMonthly / 100

    """
    <div class="product-item">
      #{title} $#{price.toFixed(2)}
      {{> @embedButton}}
      {{> @deleteButton}}
      {{> @clientsButton}}
      <br>
      {{> @embedView}}
    </div>
    """<commit_msg>Confirmation for delete button, also fix addProduct method name.<commit_after>class GroupProductSettingsView extends JView

  constructor:->
    super
    @setClass "payment-settings-view"
    group = @getData()
    
    @name = new KDInputView
      cssClass   : "product-title"
      placeholder: "Product name"
      
    @price = new KDInputView
      cssClass   : "product-price"
      placeholder: "0.00"

    @button = new KDButtonView
      cssClass   : "product-button"
      title      : "+"
      callback   : =>
        group.addProduct
          name  : __utils.slugify @name.getValue()
          price : @price.getValue()
          title : @name.getValue()
        , (err,plan)=>
          if err
            new KDNotificationView
              title: "Unable to create plan: #{err}"
          else
            @name.setValue ""
            @price.setValue ""
            @controller.loadItems()

    @controller = new GroupProductListController
      group     : group
      itemClass : GroupProductListItem

    @list = @controller.getListView()

    @controller.loadItems()

  pistachio:->
    """
    {{> this.button}}
    {{> this.name}}
    {{> this.price}}
    <br>
    {{> this.controller.getView()}}
    """
  viewAppended: ->
    @setTemplate do @pistachio


class GroupProductListController extends KDListViewController

  constructor:(options = {}, data)->
    @group = options.group
    super

  loadItems:(callback)->
    @removeAllItems()
    @customItem?.destroy()
    @showLazyLoader no

    KD.remote.api.JRecurlyPlan.getPlans 'groupplan', @group._id, (err,plans)=>
      if err or plans.length is 0
        @hideLazyLoader()
        @addCustomItem "This group has no products."
      else
        @hideLazyLoader()
        @instantiateListItems plans

  addCustomItem:(message)->
    @removeAllItems()
    @customItem?.destroy()
    @scrollView.addSubView @customItem = new KDCustomHTMLView
      cssClass : "no-item-found"
      partial  : message


class GroupProductListItem extends KDListItemView
  constructor:(options,data)->
    super options, data

    listView = @getDelegate()

    {code} = @getData()

    codeCheck  = """
                 KD.remote.api.JRecurlySubscription.checkUserSubscription '#{code}', (err, subscriptions)->
                   if not err and subscriptions.length > 0
                     console.log "User is subscribed to the plan."
                 """

    codeGet    = """..."""

    codeWidget = """@content = new KDButtonView
                    cssClass   : "clean-gray test-input"
                    title      : "Subscribed!"
                    callback   : ->

                    @payment = new PaymentWidget
                      planCode        : '#{code}'
                      contentCssClass : 'modal-clean-green'
                      content         : @content

                    @payment.on "subscribed", ->
                  """

    @embedButton = new KDButtonView
      title    : "View Embed Code"
      callback : =>
        if @embedView.hasClass "hidden"
          @embedView.unsetClass "hidden"
        else
          @embedView.setClass "hidden"

    @embedView = new KDTabView
      cssClass             : "hidden product-embed"
      hideHandleCloseIcons : yes
      paneData             : [
        { name : "Check Subscription", partial: "<pre>#{codeCheck}</pre>" }
        { name : "Get Subscribers",    partial: "<pre>#{codeGet}</pre>" }
        { name : "Subscribe Widget",   partial: "<pre>#{codeWidget}</pre>" }
      ]

    @clientsButton = new KDButtonView
      title    : "View Buyers"
      callback : ->
        new KDNotificationView
          title: "Coming soon!"

    @deleteButton = new KDButtonView
      title    : "-"
      callback : =>
        @confirmDelete =>

  confirmDelete:(callback) ->

    @deleteModal = new KDModalView
      title        : "Warning"
      content      : "<div class='modalformline'>Are you sure you want to delete this item?</div>"
      height       : "auto"
      overlay      : yes
      buttons      :
        Yes        :
          loader   :
            color  : "#ffffff"
            diameter : 16
          style    : "modal-clean-gray"
          callback : callback
 
  viewAppended: JView::viewAppended

  pistachio:->
    plan = @getData()

    code  = plan.code
    title = plan.title
    price = plan.feeMonthly / 100

    """
    <div class="product-item">
      #{title} $#{price.toFixed(2)}
      {{> @embedButton}}
      {{> @deleteButton}}
      {{> @clientsButton}}
      <br>
      {{> @embedView}}
    </div>
    """
{'fraction_non_alphanumeric': 0.11799105607155143, 'fraction_numerical': 0.002293314986813439, 'mean_word_length': 1.8521909744931329, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1376056', 'n_tokens_mistral': 2539, 'n_tokens_neox': 2431, 'n_words': 550}