window.displayFunc = (column, asc) ->
  $th = $("th[filter='#{column}']")
  if $th.hasClass('date')
    asc_sort = (a,b) -> if moment(a[column], "MM/DD/YY HH:mm:ss").isAfter(moment(b[column], "MM/DD/YY HH:mm:ss")) then -1 else (if moment(a[column], "MM/DD/YY HH:mm:ss").isSame(moment(b[column], "MM/DD/YY HH:mm:ss")) then 0 else 1)
    desc_sort = (a,b) -> asc_sort(b,a)
  else
    asc_sort = (a,b) -> if a[column] > b[column] then -1 else (if a[column] == b[column] then 0 else 1)
    desc_sort = (a,b) -> asc_sort(b,a)
  accounts.sort(if asc then asc_sort else desc_sort)
  total_html = ""
  for account, index in accounts
    account_row = """
            <tr class='page-#{ Math.floor(index / 50) } main'>
                <td><input name="export[]" value="#{ account.id }" type="checkbox" /></td>
                <td>#{ account.name }</td>
                <td>#{ account.users_len }</td>
                <td>#{ account.messages }</td>
                <td>#{ account.unsubscribe }</td>
                <td>#{ account.total_sessions }</td>
                <td>#{ account.unread_sessions }</td>
                <td>#{ account.total_events }</td>
                <td>#{ account.total_comments }</td>
                <td>#{ account.emails_collected }</td>
                <td>#{ account.last_event }</td>
                <td>#{ if account.value then account.value.toFixed(2) else '-' }</td>
                <td>#{ if account.frequency then account.frequency.toFixed(2) else '-' }</td>
                <td>#{ if account.relevancy then account.relevancy.toFixed(2) else '-' }</td>
                <td>#{ if account.design then account.design.toFixed(2) else '-' }</td>
                <td>#{ if account.sentiment then account.sentiment.toFixed(2) else '-' }</td>
                <td>#{ if account.satisfaction then account.satisfaction.toFixed(2) else '-' }</td>
                <td>#{ if account.nps then account.nps else '-' }</td>
                <td>#{ if account.sxi == -1  then '-' else account.sxi.toFixed(2) }</td>
                <td>#{ (account.recommend_pct * 100).toFixed(2)}%</td>
                <td>#{ account.survey_sessions }</td>
                <td>#{ account.survey_events }</td>
                <td>#{ account.last_update }</td>
                <td>#{ account.last_login }</td>
                <td>#{ account.last_report_open }</td>
                <td>#{ account.subscription_type }</td>
                <td>#{ account.subscription_end }</td>
                <td>#{ account.cycle_opens }</td>
                <td>#{ account.lifetime_opens }</td>
                <td>#{ account.first_open }</td>
                <td>#{ account.signup_seal_opens }</td>
                <td>#{ account.custom_plan_url }</td>
            </tr>
            <tr class='page-#{ Math.floor(index / 50) } more'> """
    profile_row = ""
    for prof in account.users
      user = prof.user
      profile_row += """
                <td>
                    <label>Name</label>
                    <input type='text' disabled value='#{prof.user.first_name} #{prof.user.last_name}' />

                    <label>Email</label>
                    <input type='text' disabled value='#{prof.user.email}' />

                    <label>Phone</label>
                    <input type='text' disabled value='#{prof.phone}' />

                    <label>Last Login</label>
                    <input type='text' disabled value='#{prof.user.last_login}' />

                    <label>Active:</label>
                    <input type='checkbox' disabled#{if prof.user.is_active then ' checked' else ''} />
                </td>
                """
    profile_row += """
            </tr>
            <tr class='page-{{forloop.counter | div:50 }} more'>
                <td><label>Reply-To Email</label><input type='text' disabled value='#{account.engagement_email}' /></td>
            </tr>
            <tr class='page-{{forloop.counter | div:50 }} more' value='#{account.id}'>
                <td><label>Amount in USD</label><input type='number' class='amount' /></td>
                <td><label>Plan Type</label><select class='type'><option value='custom'>Custom</option><option value='basic'>Basic</option><option value='advanced'>Advanced</option><option value='professional'>Professional</option></td>
                <td><label>Yearly/Monthly</label><select class='interval'><option value='year'>Yearly</option><option value='month'>Monthly</option></td>
                <td><label>Users Granted</label><input type='number' class='users' /></td>
                <td><button type='button' class='create-new-plan'>Create Custom Plan</button></td>
            </tr>
            """
    total_html += account_row + profile_row 
  return total_html

window.renderFunc = () ->
  $('tr.main').click((evt) ->
    if ($(evt.srcElement).prop("tagName") == "INPUT")
      return
    next = $(this).next()[0]
    if (next.style.display == 'none')
      $(next).show()
      $(next).next().show()
      $(next).next().next().show()
    else
      $(next).hide()
      $(next).next().hide()
      $(next).next().next().hide()
  )
  $('button.create-new-plan').click ->
    amount = $(this).parents('tr.more').find('input.amount').val()
    type = $(this).parents('tr.more').find('select.type option:selected').val()
    interval = $(this).parents('tr.more').find('select.interval option:selected').val()
    users_granted = $(this).parents('tr.more').find('input.users').val()
    account_id = $(this).parents('tr.more').attr('value')
    $td = $(this).parent()
    if amount == '' or users_granted == ''
      return
    $.post('/svAdmin/accounts/create_plan/',
      {
        'csrfmiddlewaretoken': Django.csrf_token(),
        'account_id': account_id,
        'plan_type': type,
        'amount': amount,
        'interval': interval,
        'users_granted': users_granted
      }
    ).success((response) ->
      parsed = JSON.parse(response)
      $td.html(parsed.plan_url)
    )

