      [:div.form-group
       [:label {:for "claim"} (t :badge/Composeyourendorsement)  ]
       [:textarea.form-control {:id "claim"
-                               :rows "5"
-                               :cols "60"
+                               :rows "10"
+                               :cols "50"
                                :max-length "1500"
                                :type "text"
                                :value @(cursor state [:endorsement-comment])
                                :on-change #(do
                                              (reset! (cursor state [:endorsement-comment]) (.-target.value %))
                                              )
-                               #_:on-key-down #_(if (= (.-which %) 13)
+                               :on-key-down #(when (= (.-which %) 13)
+                                               (prn (clojure.string/split-lines @(cursor state [:endorsement-comment])))
+                                               ;(process-text "test" state)
+                                               ;(reset! (cursor state [:endorsement-comment]) (str (.-target.value %) "\n\n* ") )
+                                               ;(prn @(cursor state [:endorsement-comment]))
                                                #_(add-tag tags-atom new-tag-atom))}]]
      [:div
       [:button.btn.btn-primary {:on-click #(do

              (for [endorsement @endorsements]
                (endorsement-row endorsement)))]])))

;; User Endorsements

(defn init-user-badge-endorsement [state]
  (ajax/GET


                    ) [:div] @(cursor state [:user-badge-endorsements]))]]))))

(defn profile [element-data]
  (let [{:keys [id first_name last_name profile_picture status header]} element-data
        current-user (session/get-in [:user :id])]
    [:div.endorsement-profile.panel.panel-default
     [:a {:href "#" :on-click #(mo/open-modal [:user :profile] {:user-id id})}
      [:div.panel-body
       [:div.header [:span.label.label-default.text-center header]]
       [:div.col-md-4
        [:div.profile-image
         [:img.img-responsive.img-thumbnail
          {:src (profile-picture profile_picture)
           :alt (str first_name " " last_name)}]]]
       [:div.col-md-8
        [:h4 (str first_name " " last_name)]
        (when (= status "pending") [:p [:span.label.label-info (t :social/Pending)]])]]]]))

(defn user-endorsement-content [params]
  (fn []
    (let [{:keys [endorsement state]} @params
          {:keys [id profile_picture name first_name last_name image_file content user_badge_id endorser_id endorsee_id status]} endorsement]
      [:div.row {:id "badge-info"}
       [:div.col-md-3
        [:div.badge-image [:img.badge-image {:src (str "/" image_file)}]]]
       [:div.col-md-9.badge-info
        [:div
         [:h1.uppercase-header name]

         [:div.pull-right.delete-btn
          [:a {:on-click #(do
                            (.preventDefault %)
                            (delete-endorsement id user_badge_id nil nil)
                            )
               :data-dismiss "modal"} [:i.fa.fa-trash] (t :badge/Delete)]
          ]

         [:div (t :badge/Manageendorsementtext)]

         [profile {:id (or endorsee_id endorser_id) :profile_picture profile_picture :first_name first_name :last_name last_name :status status :header (if endorsee_id (t :badge/YouEndorsed) (t :badge/Endorsedyou))}]
         [:hr]
         (if endorsee_id
           [:div
            [:div.form-group
             [:label {:for "claim"} (t :badge/Composeyourendorsement)]
             [:textarea.form-control {:id "claim"
                                      :rows "10"
                                      :cols "50"
                                      :max-length "1500"
                                      :type "text"
                                      :value @(cursor params [:endorsement :content])
                                      :on-change #(do
                                                    (reset! (cursor params [:endorsement :content]) (.-target.value %))
                                                    )}]]
            [:div
             [:button.btn.btn-primary {:on-click #(do
                                                    (.preventDefault %)
                                                    (edit-endorsement id user_badge_id @(cursor params [:endorsement :content]))
                                                    ;;save endorsement
                                                    )
                                       :disabled (blank? @(cursor params [:endorsement :content]))
                                       :data-dismiss "modal"

                                       } (t :core/Save)]]]

           [:div
            [:div
             [:div {:dangerouslySetInnerHTML {:__html content}}]
             (when (= "pending" status)
               [:div.caption
                [:hr]
                [:div.text-center
                 [:ul.list-inline.buttons.buttons
                  [:a.button {:href "#"
                              :on-click #(do
                                           (.preventDefault %)
                                           (update-status id "accepted" user_badge_id state init-user-badge-endorsement)
                                           )} [:li [:i.fa.fa-check] ]]
                  [:a.button {:href "#"
                              :on-click #(do
                                           (.preventDefault %)
                                           (update-status id "declined" user_badge_id state init-user-badge-endorsement))}[:li.cancel [:i.fa.fa-remove]]]]]])
             ]


            ])]]])))



(defn endorsements [state]
  (let [endorsements (->> (list* @(cursor state [:received]) @(cursor state [:given]))
                          flatten
                          (sort-by :mtime >))]
    [:div.panel
     [:div.panel-heading
      [:h3
       (str (t :badge/Endorsements) ;" (" (count endorsements) ")"
            ) [:span.badge (count endorsements)]]]
     [:div.panel-body
      [:table.table  {:summary (t :badge/Endorsements)}
       (reduce (fn [r endorsement]
                 (let [{:keys [id endorsee_id endorser_id profile_picture first_name last_name name image_file content status user_badge_id mtime]} endorsement
                       endorser (str first_name " " last_name)]
                   (conj r [:div.list-item
                            [:a {:href "#" :on-click #(do
                                                        (.preventDefault %)
                                                        (mo/open-modal [:badge :user-endorsement] (atom {:endorsement endorsement :state state}) {:hidden (fn [] (init-user-endorsements state))} ))}
                             [:div.col-md-12 [:small.pull-right [:i (date-from-unix-time (* 1000 mtime) "days")]]]
                             [:div.row
                              [:div.labels [:span.label.label-default (if endorser_id
                                                                        (t :badge/Endorsedyou)
                                                                        (t :badge/YouEndorsed))]
                               [:span.label {:class (case status
                                                      "pending" "label-info"
                                                      "accepted" "label-success"
                                                      nil)}
                                (case status
                                  "pending" (t :badge/Pending)
                                  "accepted" (t :badge/Accepted)
                                  nil)
                                ]]
                              ]
                             [:div.media
                              [:div.media-left.media-top.list-item-body
                               [:img.profile-pic.media-object {:src (profile-picture profile_picture)}]
                               ]
                              [:div.media-body
                               [:h4.media-heading  endorser]
                               [:div.media
                                [:div.media-left.media-middle
                                 [:img.media-object.badge-img {:src (str "/" image_file)}]]
                                [:div.media-body
                                 [:p.badge-name name]
                                 ]]]
                              ]]]))) [:div] endorsements)]]]))

(defn received-endorsements [state]
  (let [endorsements @(cursor state [:received])]
    [:div.panel
     [:div.panel-heading
      [:h3
       (str (t :badge/Receivedendorsements) " (" (count endorsements) ")")]]
     [:div.panel-body
      [:table.table  {:summary (t :badge/Receivedendorsements)}
       (reduce (fn [r endorsement]
                 (let [{:keys [id endorser_id profile_picture first_name last_name name image_file content status user_badge_id mtime]} endorsement
                       endorser (str first_name " " last_name)]
                   (conj r [:div.list-item
                            [:div.col-md-12 [:small.pull-right [:i (date-from-unix-time (* 1000 mtime) "days")]]]
                            [:div [:span.label.label-info.table-cell (t :badge/Endorsedyou)]
                             [:span.label.table-cell {:class (case status
                                                               "pending" "label-info"
                                                               "accepted" "label-success"
                                                               nil)}
                              (case status
                                "pending" (t :badge/Pending)
                                "accepted" (t :badge/Accepted))
                              ]]
                            [:div.media
                             [:div.media-left.media-middle
                              [:img.profile-pic.media-object {:src (profile-picture profile_picture)}]
                              ]
                             [:div.media-body

                              [:h4.media-heading  endorser #_[:small.pull-right [:i (date-from-unix-time (* 1000 mtime) "days")]]]
                              ;[:span.label.label-info (t :badge/Endorsedyou)]
                              [:div.media
                               [:div.media-left.media-middle
                                [:img.media-object.badge-img {:src (str "/" image_file)}]]
                               [:div.media-body
                                [:p.media-heading.badge-name name]
                                #_[:span.label.label-info (t :badge/Endorsedyou)]
                                #_[:span.label {:class (case status
                                                         "pending" "label-info"
                                                         "accepted" "label-success"
                                                         nil)}
                                   (case status
                                     "pending" (t :badge/Pending)
                                     "accepted" (t :badge/Accepted))
                                   ]]]]
                             ]]))) [:div] endorsements)]]]))

(defn given-endorsements [state]
  (let [endorsements @(cursor state [:given])]
    [:div.panel
     [:div.panel-heading
      [:h3
       (str (t :badge/Givenendorsements) " (" (count endorsements) ")")]]
     [:div.panel-body
      [:table.table  {:summary (t :badge/Givenendorsements)}
       (reduce (fn [r endorsement]
                 (let [{:keys [id endorsee_id profile_picture first_name last_name name image_file content status user_badge_id mtime]} endorsement
                       endorser (str first_name " " last_name)]
                   (conj r [:div.list-item
                            [:div.col-md-12 [:small.pull-right [:i (date-from-unix-time (* 1000 mtime) "days")]]]
                            [:div.row
                             ;[:div.col-md-3 [:small.pull-right [:i (date-from-unix-time (* 1000 mtime) "days")]]]
                             [:div [:span.label.label-info.table-cell (t :badge/YouEndorsed)]
                              [:span.label.table-cell {:class (case status
                                                                "pending" "label-info"
                                                                "accepted" "label-success"
                                                                nil)}
                               (case status
                                 "pending" (t :badge/Pending)
                                 "accepted" (t :badge/Accepted))
                               ]]
                             ]
                            [:div.media
                             [:div.media-left.media-top
                              [:img.profile-pic.media-object {:src (profile-picture profile_picture)}]
                              ]
                             [:div.media-body.list-item-body
                              ;[:span.label.label-info (t :badge/YouEndorsed)]

                              [:h4.media-heading  endorser #_[:small.pull-right ]]

                              [:div.media
                               [:div.media-left.media-middle
                                [:img.media-object.badge-img {:src (str "/" image_file)}]]
                               [:div.media-body
                                [:p.badge-name name]
                                ]]]
                             ]]))) [:div] endorsements)]]]))

(defn user-endorsements-content [state]
  [:div
   [m/modal-window]
   [:div#badge-stats

    (if (or (seq @(cursor state [:received]) ) (seq @(cursor state [:given]))) (endorsements state) [:div (t :badge/Youhavenoendorsements)])
    ;(when (seq @(cursor state [:received]))(received-endorsements state))
    ;(when (seq @(cursor state [:given]))(given-endorsements state))

    ]])

(defn request-endorsement [])


(defn handler [site-navi]
  (let [state (atom {:initializing true
                     :permission "initial"})
        user (session/get :user)]
    (init-user-endorsements state)
    (fn []
      (cond
        (= "initial" (:permission @state)) [:div]
        (and user (= "error" (:permission @state))) (layout/default-no-sidebar site-navi (err/error-content))
        (= "error" (:permission @state)) (layout/landing-page site-navi (err/error-content))
        :else (layout/default site-navi (user-endorsements-content state)))
      )
    )
  )

;;;;;;
;;     [:div#endorsebadge
;;      (reduce (fn [r endorsement]
;;                (let [{:keys [id endorsee_id profile_picture first_name last_name name image_file content status user_badge_id]} endorsement
;;                      endorser (str first_name " " last_name)]
;;                  (conj r

;;                        #_[:div.col-xs-12.col-sm-6.col-md-4
;;                           [:div.thumbnail
;;                            [:div.endorser
;;                             [:div.row [:span.pull-right.label {:class (case status
;;                                                                         "pending" "label-info"
;;                                                                         "accepted" "label-success"
;;                                                                         nil)}
;;                                        (case status
;;                                          "pending" (t :badge/pending)
;;                                          "accepted" (t :badge/accepted))
;;                                        ]]
;;                             [:div [:a {:href "#"
;;                                        :on-click #(mo/open-modal [:user :profile] {:user-id endorsee_id})}
;;                                    [:img {:src (profile-picture profile_picture)}]
;;                                    (str first_name " " last_name " ")]
;;                              ]

;;                             #_[profile-link-inline endorsee_id first_name last_name profile_picture id]
;;                             [:hr.border]
;;                             ]
;;                            [:div.caption
;;                             [:div.position-relative.text-center.row
;;                              [:img{:src (str "/" image_file)}]]

;;                             [:div
;;                              [:p.heading.text-center name]
;;                              [:div.thumbnail-description.smaller {:dangerouslySetInnerHTML {:__html content}}]]
;;                             ]

;;                            (when (= "pending" status)
;;                              [:div.caption.card-footer.text-center
;;                               [:hr]
;;                               [:ul.list-inline.buttons
;;                                [:a.button {:href "#"
;;                                            :on-click #(do
;;                                                         (.preventDefault %)
;;                                                         (update-status id "accepted" user_badge_id state init-pending-endorsements)
;;                                                         )} [:li [:i.fa.fa-check] ]]
;;                                [:a.button {:href "#"
;;                                            :on-click #(do
;;                                                         (.preventDefault %)
;;                                                         (update-status id "declined" user_badge_id state init-pending-endorsements))}[:li.cancel [:i.fa.fa-remove]]]]])]
;;                           ]))) [:div.panel]
;;              endorsements)]
