                         )})))
 
 
-(defn resources-grid [key resource-atom state]
+(defn resources-grid [key resource-atom state data init-data]
   (let [evidence-url-atom (cursor state [:evidence :url])
-        {:keys[files]} @resource-atom]
-    [:div.resource-upload
-     (if files
-       [:div [:label
-                       [:span [:i.fa.fa-upload] (t :file/Upload) ]
-                       [:input {:id "grid-file-upload"
-                                :type "file"
-                                :name "file"
-                                :on-change #(upload-file resource-atom state)
-                                :style {:display "none"}}]
-                       ]])
-     [:div.col-md-9.resource-container
-      (reduce (fn [r resource]
-                (conj r [grid-element resource state key])
-                ) [:form] (if files files @resource-atom))
-      ]]))
-
-
-(defn init-evidence-form [evidence state show-url?]
-  (let [ {:keys [id name url audience narrative genre description]} evidence
-         evidence { :id (:id evidence)
-                   :name (:name evidence)
-                   :url (:url evidence)
-                   :audience (:audience evidence)
-                   :narrative (:narrative evidence)
-                   :genre (:genre evidence)
-                   :description (:description evidence)
-                   }]
-    (if (every? #(blank? %) (vector name audience narrative genre description))
-      (swap! state assoc :show_form nil)
-      (swap! state assoc :show_form true))
-    (if show-url?
-      (swap! state assoc :evidence (assoc evidence :show_url true :input_mode nil) )
-      (swap! state assoc :evidence evidence))))
-
-(defn save-badge-evidence [data state init-data]
-  (let [{:keys [id name description url update-page-visibility]} (:evidence @state)
-        badge-id (:id data)
-        init-settings (first (plugin-fun (session/get :plugins) "modal" "show_settings_dialog"))]
-    (swap! state assoc :evidence {:message nil})
-    (if (blank? url)
-      (swap! state assoc :evidence {:message [:div.alert.alert-warning [:p "Resource field can't be empty!"]]})
-
-      (ajax/POST
-        (path-for (str "/obpv1/badge/evidence/" badge-id))
-        {:params {:id id
-                  :name (if name (trim name))
-                  :description description
-                  :url (if url (trim url))}
-         :handler (fn [resp]
-                    (when (= "success" (:status resp))
-                      (init-settings (:id data) state init-data "settings"))
-
-
-                    )}))))
-
-(defn selected-url [state init-data]
-  (let [evidence-url-atom (cursor state [:evidence :url])
-        show-url? (cursor state [:evidence :show_url])
-        evidence (cursor state [:evidence])]
-    (fn []
-      (when (and (not (empty? @evidence-url-atom )) @show-url?)
-        [:div.col-md-9
-
-
-         [:button.close {:type "button"
-                         :aria-label "OK"
-                         :on-click #(do
-                                      (.preventDefault %)
-                                      (reset! show-url? false)
-                                      (reset! evidence (assoc @evidence :url nil :show_url false
-                                                         ))
-                                      (init-evidence-form @evidence state false)
-                                      )
-                         }
-          [:span {:aria-hidden "true"
-                  :dangerouslySetInnerHTML {:__html "&times;"}}]]
-         [:div.selected-url (hyperlink (trim @evidence-url-atom))]]
-        ))))
-
-(defn resource-input [state]
+        {:keys[files]} @resource-atom
+        init-container (case key
+                         :file_input [:div [:label
+                                            [:span [:i.fa.fa-upload] (t :file/Upload) ]
+                                            [:input {:id "grid-file-upload"
+                                                     :type "file"
+                                                     :name "file"
+                                                     :on-change #(upload-file resource-atom state)
+                                                     :style {:display "none"}}]]]
+                         :page_input [:div])]
+    [:div.col-md-9.resource-container
+     (reduce (fn [r resource]
+               (conj r [grid-element resource state key data init-data])
+               ) init-container (if files files @resource-atom))]))
+
+(defn resource-input [data state init-data]
   (let [resource-input-mode (cursor state [:input_mode])
         evidence-url-atom (cursor state [:evidence :url])
         resource-atom (atom {})

       [:div.form-horizontal
        [:div.form-group
         [:label.col-md-9.sub-heading "Add evidence resource"]
         [:div.col-md-9 {:style {:margin-top "10px" :margin-bottom "10px"}}
          [:div {:style {:float "left"}}
           [:div {:style {:float "left" :margin-right "10px"}}[:a {:style {:margin "10px"} :href "#"  :on-click #(toggle-input-mode :url_input state)} [:i.fa.fa-link] (t :admin/Url)]]
           [:div {:style {:float "left" :margin-right "10px"}} [:a {:href "#" :on-click #(do (toggle-input-mode :page_input state)
                                                                                           #_(swap! state assoc :tab [resource-tab data state init-data])
                                                                                           ) } [:i.fa.fa-file] (t :page/Mypages)]]
           [:div {:style {:float "left"}} [:a {:href "#"
                                               :on-click #(do
                                                            (.preventDefault %)
                                                            (toggle-input-mode :file_input state))
                                               } [:i.fa.fa-file] (t :page/Files)]]]]]

         [selected-url state init-data]
         [resource-input state]

        (if @show-form
          [:div
           [:div.form-group
            [:label.col-md-3 "Name"]
            [:div.col-md-9
             [input {:name "name" :atom evidence-name-atom :type "text"} nil]]]
           [:div.form-group
            [:label.col-md-3 (t :page/Description)]
            [:div.col-md-9
             [input {:name "Description" :rows 5 :cols 40 :atom evidence-description-atom } true]]]]

          [:div [:i.fa.fa-plus]
           [:a {:href "#" :on-click #(do (.preventDefault %) (reset! show-form true))} "Add more info about this evidence"]])
         [:hr]]
        [:div
         [:button {:type         "button"
                   :class        "btn btn-primary"
                   :on-click     #(save-badge-evidence data state init-data)}
          (t :badge/Save)]
         [:button {:type "button"
                   :class "btn btn-primary"
                   :on-click #(do (.preventDefault %) (swap! state assoc :tab [settings-tab (dissoc data :evidence) state init-data] :tab-no 2))}
          (t :core/Cancel)]]]]]))

(defn delete-evidence! [id data state init-data]
  (let [init-settings (first (plugin-fun (session/get :plugins) "modal" "show_settings_dialog"))]
    (ajax/DELETE
      (path-for (str "/obpv1/badge/evidence/" id))
      { :params {:user_badge_id (:id data)}
        :handler (fn [r]
                   (when (= "success" (:status r))
                     (init-settings (:id data) state init-data "settings")))})))

(defn evidence-list [data state init-data]
  (reduce (fn [r evidence]
            (let [{:keys [genre description narrative name audience id url mtime ctime]} evidence
                  text ()]
              (conj r
                    [:div.panel.panel-default
                     [:div.panel-heading {:id (str "heading" id)
                                          :role "tab"}
                      [:div.panel-title
                       [:a {:href "#" :on-click #(do (.preventDefault %)
                                                   (init-evidence-form evidence state true)
                                                   (swap! state assoc :tab [evidence-form data state init-data] :input_mode nil))}
                        [:div.url [:div {:style {:float "left"}} [:i.fa.fa-link]]  url]]
                       (when-not (every? #(blank? %) (vector genre description narrative name audience))
                         [:div.show-more [:a {:role "button" :data-toggle "collapse"
                                              :data-parent "#accordion"
                                              :href (str "#collapse" id)
                                              :aria-expanded "true"
                                              :aria-controls (str "collapse" id)} (t :admin/Showmore)]])]
                      [:div [:button {:type "button"
                                      :aria-label "OK"
                                      :class "close panel-close"
                                      :on-click #(delete-evidence! id data state init-data)}
                             [:span {:aria-hidden "true"
                                     :dangerouslySetInnerHTML {:__html "&times;"}}]]]
                      ]
                     (when-not (every? #(blank? %) (vector genre description narrative name audience))
                       [:div {:id (str "collapse" id)
                              :class "panel-collapse collapse"
                              :role "tabpanel"
                              :aria-labelledby (str "heading" id)}
                        [:div.panel-body.evidence-panel-body {:style {:padding-top "10px"}}
                         (when-not (blank? name) [:div.inline [:label (t :badge/Name) ": "] name])
                         (when-not (blank? description) [:div [:label (t :admin/Description)]  [:div.description description]])
                         ]])])
  )
            )[:div {:id "accordion" :class "panel-group evidence-list" :role "tablist" :aria-multiselectable "true"}] (:evidences data)))

(defn evidence-block [data state init-data]
  [:div#badge-settings
   [:div.form-group
    [:label {:class "col-md-12 sub-heading" :for "evidence"} (t :badge/Evidence)]


    [:div.col-md-9 {:class "new-evidence"}
     [:i.fa.fa-plus-square.fa-sm]
     [:a {:href "#" :on-click #(do (.preventDefault %)
                                 (swap! state assoc :evidence nil
                                        :show_form nil
                                        :input_mode nil)
                                 (swap! state assoc :tab [evidence-form data state init-data] :tab-no 2))} (t :badge/Addnewevidence)]]]


   [:div.form-group
    [:div.col-md-9 [evidence-list data state init-data]]]
   ])

(defn evidence-modal [state]
  (fn []

        (case @resource-input-mode
          :file_input (do
                        (init-resources :file_input resource-atom)
                        [:div [resources-grid :file_input resource-atom state data init-data]])
          :page_input (do
                        (init-resources :page_input resource-atom)
                        [:div [resources-grid :page_input resource-atom state data init-data]])
          :url_input [:div.resource-container {:style {:margin-bottom "10px"}}
                      [input {:name "evidence-url" :atom evidence-url-atom :type "url" :placeholder "http://"}]]
          nil)]])))

(defn evidence-form [data state init-data]
  (let [settings-tab (first (plugin-fun (session/get :plugins) "settings" "settings_tab_content"))
        evidence-name-atom (cursor state [:evidence :name])
        evidence-description-atom (cursor state [:evidence :description])
        evidence-url-atom (cursor state [:evidence :url])
        message (cursor state [:evidence :message])
        input-mode (cursor state [:input_mode])
        {:keys [image_file name]} data]

    [:div {:id "badge-settings" :class "row flip"}
