li(bindonce='list', id='task-{{::task.id}}', ng-repeat='task in obj[list.type+"s"]', class='task {{Shared.taskClasses(task, user.filters, user.preferences.dayStart, user.lastCron, list.showCompleted, main)}}', ng-click='spell && (list.type != "reward") && castEnd(task, "task", $event)', ng-class='{"cast-target":spell && (list.type != "reward")}', popover-trigger='mouseenter', data-popover-html="{{task.notes | markdown}}", data-popover-placement="top", ng-show='shouldShow(task, list, user.preferences)')
  // right-hand side control buttons
  .task-meta-controls

    // Due Date
    span(ng-if='task.type=="todo" && task.date')
      span(ng-class='{"label label-danger":(moment(task.date).isBefore(_today, "days") && !task.completed)}') {{moment(task.date).format('MM/DD')}}
    // Streak
    | &nbsp;
    span(ng-show='task.streak') {{task.streak}}&nbsp;
      span(tooltip=env.t('streakCounter'))
        span.glyphicon.glyphicon-forward
        | &nbsp;

    // Icons only available if you own the tasks (aka, hidden from challenge stats)
    span(ng-if='!obj._locked')
      a(ng-click='pushTask(task,$index,"top")', tooltip=env.t('pushTaskToTop'))
        span.glyphicon.glyphicon-open
      // a(ng-click='pushTask(task,$index,"bottom")', tooltip=env.t('pushTaskToBottom'))
      //   span.glyphicon.glyphicon-import
      //   // glyphicon-import or glyphicon-save or glyphicon-sort-by-attributes
      a.badge(ng-if='task.checklist[0]', ng-class='{"badge-success":checklistCompletion(task.checklist) == task.checklist.length}', ng-click='collapseChecklist(task)', tooltip=env.t('expandCollapse'))
        |{{checklistCompletion(task.checklist)}}/{{task.checklist.length}}
      span.glyphicon.glyphicon-tags(tooltip='{{Shared.appliedTags(user.tags, task.tags)}}', ng-hide='Shared.noTags(task.tags)')
      // edit
      a(ng-hide='task._editing', ng-click='editTask(task)', tooltip=env.t('edit'))
        | &nbsp;
        span.glyphicon.glyphicon-pencil(ng-hide='task._editing')
      | &nbsp;
      a(ng-hide='!task._editing', ng-click='editTask(task)', tooltip=env.t('cancel'))
        span.glyphicon.glyphicon-remove(ng-hide='!task._editing')
        | &nbsp;
      // save
      a(ng-hide='!task._editing', ng-click='editTask(task);saveTask(task)', tooltip=env.t('save'))
        span.glyphicon.glyphicon-ok(ng-hide='!task._editing')
      | &nbsp;
      //challenges
      span(ng-if='task.challenge.id')
        span(ng-if='task.challenge.broken')
          span.glyphicon.glyphicon-bullhorn(style='background-color:red;', ng-click='task._editing = true', tooltip=env.t('brokenChaLink') tooltip-placement='right')
          | &nbsp;
        span(ng-if='!task.challenge.broken')
          span.glyphicon.glyphicon-bullhorn(tooltip=env.t('challenge'))
          | &nbsp;
      // delete
      a(ng-if='!task.challenge.id', ng-click='removeTask(obj[list.type+"s"], $index)', tooltip=env.t('delete'))
        span.glyphicon.glyphicon-trash
        | &nbsp;

    // chart
    a(ng-show='task.history', ng-click='toggleChart(obj._id+task.id, task)', tooltip=env.t('progress'))
      span.glyphicon.glyphicon-signal
      | &nbsp;
    // notes
    span.task-notes(ng-show='task.notes && !task._editing')
      span.glyphicon.glyphicon-comment
      | &nbsp;

  // left-hand side checkbox
  .task-controls.task-primary(ng-if='!task._editing')

    // Habits
    span(ng-if='::task.type=="habit"')
      // score() is overridden in challengesCtrl to do nothing
      a.task-action-btn(ng-if='task.up', ng-click='score(task,"up")') +
      a.task-action-btn(ng-if='task.down', ng-click='score(task,"down")') -

    // Rewards
    span(ng-show='task.type=="reward"')
      a.money.btn-buy(ng-click='score(task, "down")')
        span.reward-cost {{task.value}}
        span.shop_gold

    // Daily & Todos
    span.task-checker.action-yesno(ng-if='::task.type=="daily" || task.type=="todo"')
      input.visuallyhidden.focusable(ng-if='$state.includes("tasks")', id='box-{{obj._id}}_{{task.id}}', type='checkbox', ng-model='task.completed', ng-change='task.type=="todo" && pushTask(task,$index,"bottom"); changeCheck(task)')
      input.visuallyhidden.focusable(ng-if='!$state.includes("tasks")', id='box-{{obj._id}}_{{task.id}}', type='checkbox')
      label(for='box-{{obj._id}}_{{task.id}}')

  // main content
  div.task-text(ng-dblclick='task._editing ? saveTask(task) : editTask(task)')
    markdown(ng-model='task.text',target='_blank')
    //-| {{task.text}}

    div(ng-if='task.checklist && !$state.includes("options.social.challenges") && !task.collapseChecklist && !task._editing')
      fieldset.option-group
        label.checkbox(ng-repeat='item in task.checklist')
          input(type='checkbox',ng-model='item.completed',ng-change='saveTask(task,true)')
          markdown(ng-model='item.text',target='_blank')

  // edit/options dialog
  div(ng-if='task._editing')
    .task-options

      // Broken Challenge
      .well(ng-if='task.challenge.broken')
        div(ng-if='task.challenge.broken=="TASK_DELETED"')
          p=env.t('brokenTask')
          p
            a(ng-click='unlink(task, "keep")')=env.t('keepIt')
            | &nbsp;&nbsp;
            a(ng-click="removeTask(obj[list.type+'s'], $index)")=env.t('removeIt')
        div(ng-if='task.challenge.broken=="CHALLENGE_DELETED"')
          p
            |&nbsp;
            =env.t('brokenChallenge')
          p
            a(ng-click='unlink(task, "keep-all")')=env.t('keepThem')
            | &nbsp;|&nbsp;
            a(ng-click='unlink(task, "remove-all")')=env.t('removeThem')
        div(ng-if='task.challenge.broken=="CHALLENGE_CLOSED"')
          p
            !=env.t('challengeCompleted', {user: "{{task.challenge.winner}}"})
          p
            a(ng-click='unlink(task, "keep-all")')=env.t('keepThem')
            | &nbsp;|&nbsp;
            a(ng-click='unlink(task, "remove-all")')=env.t('removeThem')
        //div(ng-if='task.challenge.broken=="UNSUBSCRIBED"')
          p=env.t('unsubChallenge')
          p
            a(ng-click="unlink(task, 'keep-all')")=env.t('keepThem')
            | &nbsp;|&nbsp;
            a(ng-click="unlink(task, 'remove-all')")=env.t('removeThem')

      // Checklists
      div(ng-if='!$state.includes("options.social.challenges")')
        button.btn.btn-default.btn-sm(ng-if='!task.checklist[0] && (task.type=="daily" || task.type=="todo")',ng-click='addChecklist(task)')
          span.glyphicon.glyphicon-tasks
          | &nbsp; &nbsp;
          =env.t('addChecklist')
        form.checklist-form(ng-if='task.checklist')
          fieldset.option-group.task-checklist(ng-if='!$state.includes("options.social.challenges")')
            legend.option-title
              span.hint(popover=env.t('checklistText'),popover-trigger='mouseenter',popover-placement='bottom')=env.t('checklist')
            ul(hrpg-sort-checklist).list-unstyled
              li(ng-repeat='item in task.checklist') 
                a.pull-right.checklist-icons(ng-click='removeChecklistItem(task,$event,$index,true)')
                  span.glyphicon.glyphicon-trash(tooltip=env.t('delete'))
                span.checklist-icons.glyphicon.glyphicon-resize-vertical()
                | &nbsp;
                input(type='checkbox',ng-model='item.completed',ng-change='saveTask(task,true)')
                //-,ng-blur='saveTask(task,true)')
                input.inline-edit(type='text',ng-model='item.text',ui-keydown="{'8 46':'removeChecklistItem(task,$event,$index)'}",ui-keyup="{'13':'addChecklistItem(task,$event,$index)','38 40':'navigateChecklist(task,$index,$event)'}")


      form(ng-submit='saveTask(task,false,true)')
        // text & notes
        fieldset.option-group
          label.option-title=env.t('text')
          input.option-content(type='text', ng-model='task.text', required, ng-disabled='task.challenge.id')

          label.option-title=env.t('extraNotes')
          textarea.option-content(rows='3', ng-model='task.notes', ng-model-options="{debounce: 1000}")

        // if Habit, plus/minus command options
        fieldset.option-group(ng-if='task.type=="habit" && !task.challenge.id')
          legend.option-title=env.t('direction/Actions')
          span.task-checker.action-plusminus.select-toggle
            input.visuallyhidden.focusable(id='{{obj._id}}_{{task.id}}-option-plus', type='checkbox', ng-model='task.up')
            label(for='{{obj._id}}_{{task.id}}-option-plus')
          span.task-checker.action-plusminus.select-toggle
            input.visuallyhidden.focusable(id='{{obj._id}}_{{task.id}}-option-minus', type='checkbox', ng-model='task.down')
            label(for='{{obj._id}}_{{task.id}}-option-minus')

        // if Daily, calendar
        fieldset(ng-if='::task.type=="daily"', class="option-group")
          legend.option-title=env.t('repeat')
          .task-controls.tile-group.repeat-days(bindonce)
            // note, does not use data-toggle="buttons-checkbox" - it would interfere with our own click binding
            button.task-action-btn.tile(ng-class='{active: task.repeat.su}', type='button', ng-click='task.challenge.id || (task.repeat.su = !task.repeat.su)') {{::moment.weekdaysMin(0)}}
            button.task-action-btn.tile(ng-class='{active: task.repeat.m}', type='button', ng-click='task.challenge.id || (task.repeat.m = !task.repeat.m)') {{::moment.weekdaysMin(1)}}
            button.task-action-btn.tile(ng-class='{active: task.repeat.t}', type='button', ng-click='task.challenge.id || (task.repeat.t = !task.repeat.t)') {{::moment.weekdaysMin(2)}}
            button.task-action-btn.tile(ng-class='{active: task.repeat.w}', type='button', ng-click='task.challenge.id || (task.repeat.w = !task.repeat.w)') {{::moment.weekdaysMin(3)}}
            button.task-action-btn.tile(ng-class='{active: task.repeat.th}', type='button', ng-click='task.challenge.id || (task.repeat.th = !task.repeat.th)') {{::moment.weekdaysMin(4)}}
            button.task-action-btn.tile(ng-class='{active: task.repeat.f}', type='button', ng-click='task.challenge.id || (task.repeat.f= !task.repeat.f)') {{::moment.weekdaysMin(5)}}
            button.task-action-btn.tile(ng-class='{active: task.repeat.s}', type='button', ng-click='task.challenge.id || (task.repeat.s = !task.repeat.s)') {{::moment.weekdaysMin(6)}}

        // if Reward, pricing
        fieldset.option-group.option-short(ng-if='task.type=="reward" && !task.challenge.id')
          legend.option-title=env.t('price')
          input.option-content(type='number', size='16', min='0', step="any", ng-model='task.value')
          .money.input-suffix
            span.shop_gold

        // if Todos, the due date
        fieldset.option-group(ng-if='task.type=="todo" && !task.challenge.id')
          legend.option-title=env.t('dueDate')
          input.option-content.datepicker(type='text', datepicker-popup='MM/dd/yyyy', ng-model='task.date', is-open='datepickerOpened', ng-click='datepickerOpened = true')

        // Tags
        fieldset.option-group(ng-if='!$state.includes("options.social.challenges")')
          p.option-title.mega(ng-click='task._tags = !task._tags', tooltip=env.t('expandCollapse'))=env.t('tags')
          label.checkbox(ng-repeat='tag in user.tags', ng-class="{visuallyhidden: task._tags}")
            input(type='checkbox', ng-model='task.tags[tag.id]')
            markdown(ng-model='tag.name')

        // Advanced Options
        span(ng-if='::task.type!="reward"')
          p.option-title.mega(ng-click='task._advanced = !task._advanced', tooltip=env.t('expandCollapse'))=env.t('advancedOptions')
          fieldset.option-group.advanced-option(ng-class="{visuallyhidden: task._advanced}")
            legend.option-title
              a.hint.priority-multiplier-help(href='https://trello.com/card/priority-multiplier/50e5d3684fe3a7266b0036d6/17', target='_blank', popover-title=env.t('difficultyHelpTitle'), popover-trigger='mouseenter', popover=env.t('difficultyHelpContent'))=env.t('difficulty')
            .task-controls.tile-group.priority-multiplier
              button.task-action-btn.tile(type='button', ng-class='{active: task.priority==1 || !task.priority}', ng-click='task.challenge.id || (task.priority=1)')=env.t('easy')
              button.task-action-btn.tile(type='button', ng-class='{active: task.priority==1.5}', ng-click='task.challenge.id || (task.priority=1.5)')=env.t('medium')
              button.task-action-btn.tile(type='button', ng-class='{active: task.priority==2}', ng-click='task.challenge.id || (task.priority=2)')=env.t('hard')
            //span(ng-if='task.type=="daily" && !task.challenge.id')
            span(ng-if='task.type=="daily"')
              legend.option-title.pull-left=env.t('restoreStreak')
              input.option-content(type='number', ng-model='task.streak')

            div(ng-if='(user.preferences.allocationMode == "taskbased" && user.preferences.automaticAllocation) || $state.is("options.social.challenges")')
              legend.option-title.pull-left=env.t('attributes')
              .task-controls.tile-group.task-attributes
                button.task-action-btn.tile(type='button', ng-class='{active: task.attribute=="str"}', ng-click='task.attribute="str"')=env.t('physical')
                button.task-action-btn.tile(type='button', ng-class='{active: task.attribute=="int"}', ng-click='task.attribute="int"')=env.t('mental')
                button.task-action-btn.tile(type='button', ng-class='{active: task.attribute=="con"}', ng-click='task.attribute="con"')=env.t('social')
                button.task-action-btn.tile(type='button', ng-class='{active: task.attribute=="per"}', ng-click='task.attribute="per"', popover=env.t('otherExamples'), popover-trigger='mouseenter', popover-placement='top')=env.t('other')

        button.task-action-btn.tile.spacious(type='submit')=env.t('saveAndClose')

  div(class='{{obj._id}}{{task.id}}-chart', ng-show='charts[obj._id+task.id]')
