             [frontend.components.forms :as forms]
             [frontend.components.pieces.card :as card]
             [frontend.components.pieces.empty-state :as empty-state]
+            [frontend.components.pieces.form :as form]
             [frontend.components.pieces.modal :as modal]
             [frontend.components.pieces.org-picker :as org-picker]
             [frontend.components.pieces.table :as table]

                                   "bitbucket" [:i.fa.fa-bitbucket]
                                   nil)])
                   :action  (html
                                (let [invite-data (:invite-data app)
                                      users (remove :circle_member (:github-users invite-data))
                                      opts {:vcs_type (:vcs_type invite-data)
                                            :org-name (:org invite-data)}
                                      count-users (count users)
                                      count-with-email (count (filter #(utils/valid-email? (:email %)) users))
                                      count-selected (count (filter #(:checked %) users))]
                                    [:div (when (:invite-teammates? (om/get-render-state owner))
                                              (component
                                                  (modal/modal-dialog {:title "Invite Teammates"
                                                                       :body
                                                                       [:div
                                                                        [:div
                                                                         "These are the people who are not using CircleCI yet:"
                                                                         [:br]
                                                                         [:span [:b count-with-email] " of " [:b count-users] " users have emails, " [:b count-selected] " are selected"]]
                                                                        [:div.constraining-modal
                                                                         (om/build table/table {:rows users
                                                                                                :key-fn :login
                                                                                                :columns [{:header "Username"
                                                                                                           :cell-fn (fn [user-map]
                                                                                                                        [:span
                                                                                                                         [:img.invite-gravatar {:src (gh-utils/make-avatar-url user-map :size 50)}]
                                                                                                                         (str "  " (:login user-map))])}
                                                                                                          {:header "Email"
                                                                                                           :cell-fn (fn [user-map]
                                                                                                                        (let [{:keys [avatar_url email login index checked]} user-map
                                                                                                                              id-name (str login "-email")
                                                                                                                              error? (and (or checked (not (empty? email)))
                                                                                                                                          (not (utils/valid-email? email)))]
                                                                                                                            [:span
                                                                                                                             [:input {:type "email"
                                                                                                                                :on-change (fn [event]
                                                                                                                                             (utils/edit-input owner (conj (state/invite-github-user-path index) :email) event)
                                                                                                                                             (when (and (not= checked true)
                                                                                                                                                        (not (empty? email)))
                                                                                                                                               (utils/toggle-input owner (conj (state/invite-github-user-path index) :checked) nil)))
                                                                                                                                :required? true
                                                                                                                                :id id-name
                                                                                                                                :value email
                                                                                                                                ;; :size "small"
                                                                                                                                ;; :long? true
                                                                                                                                ;; :error? error?
                                                                                                                                :defaultValue email}]]))}
                                                                                                          {:type :shrink
                                                                                                           :cell-fn (fn [user-map]
                                                                                                                        (let [{:keys [email login index checked]} user-map
                                                                                                                              id-name (str login "-checkbox")
                                                                                                                              email-valid? (utils/valid-email? email)]
                                                                                                                          [:input {:type "checkbox"
                                                                                                                                   :id id-name
                                                                                                                                   :disabled? (and (not (utils/valid-email? email))
                                                                                                                                                   (not (empty? email)))
                                                                                                                                   :checked? checked
                                                                                                                                   :on-click #(utils/toggle-input owner (conj (state/invite-github-user-path index) :checked) %)}]))}]
                                                                                                :striped? true
                                                                                                :fixed-header? true})]]
                                                                       :actions [(button/button {:on-click #(om/set-state! owner :invite-teammates? false)} "Cancel")
                                                                                 (forms/managed-button
                                                                                     [:button.btn.btn-success {:data-success-text "Sent"
                                                                                                               :on-click #(raise! owner [:invited-github-users (merge {:invitees (invitees users)
                                                                                                                                                                       :vcs_type (:vcs_type opts)}
                                                                                                                                                                      (if (:project-name opts)
                                                                                                                                                                          {:project-name (:project-name opts)}
                                                                                                                                                                          {:org-name (:org-name opts)}))])
                                                                                                               :disabled (or (empty? (invitees users))
                                                                                                                             (not (every? #(utils/valid-email? (:email %)) (invitees users))))}
                                                                                      "Send Invites "
                                                                                      [:i.fa.fa-envelope-o]])]
                                                                       :close-fn #(om/set-state! owner :invite-teammates? false)})))
                                     (button/button
                                         {:disabled? (nil? (:users selected-org))
                                          :primary? true
                                          :on-click (fn []
                                                        ;; TODO -ac Add tracking back in before merging
                                                        #_((om/get-shared owner :track-event)
                                                           {:event-type :invite-teammates-clicked
                                                            :properties {:view :team}})
                                                        (om/set-state! owner :invite-teammates? (not (:invite-teammates? (om/get-render-state owner)))))}
                                         "Invite Teammates")]))}
                (if-let [users (:users selected-org)]
                  (table (add-follow-counts users (:projects selected-org)))
                  (html [:div.loading-spinner common/spinner])))

        (when (not= (:selected-org-ident (om/get-render-state owner))
                    selected-org-ident)
          (api/get-org-settings-normalized name vcs-type api-chan))
        ;; TODO -ac This is doing the thing, need to make it into a component
        ;; Fire on WillMount
        (when (and selected-org
                   invite-teammates?)
          #_(api/get-org-members api-chan (:name selected-org)))))

    om/IRenderState
    (render-state [_ {:keys [selected-org-ident invite-teammates?]}]
