 (defn select-all [state]
   (if (:badges-all @state)
     (swap! state assoc :badges-selected [] :badges-all false)
-    (swap! state assoc :badges-selected (map :id (:current-view-badges @state) #_(:badges @state)) :badges-all true)))
-
-(defn select-all-button [state]
-  (fn []
+    (swap! state assoc :badges-selected (map :id (filter #(badge-visible? % state)(badges-for-grid state))) :badges-all true)))
+
+(defn badge-export-buttons [state]
+  [:div
+   [:button {:class    "btn btn-primary"
+             :on-click #(select-all state)}
+    (if (:badges-all @state) (t :badge/Clearall) (t :badge/Selectall))]
+   (if (not (nil? (:backpack_id (first (filter #(= (:email %) (:email-selected @state)) (:emails @state))))))
      [:button {:class    "btn btn-primary"
-               :on-click #(select-all state)}
-      (if (:badges-all @state) (t :badge/Clearall) (t :badge/Selectall))]))
+               :on-click #(export-badges state)
+               :disabled (= 0 (count (:badges-selected @state)))}
+      (t :badge/Exportselected)])
+   [:button {:class "btn btn-primary"
+             :on-click #(do (.preventDefault %)
+                          (swap! state assoc :pdf-option "default")
+                          (m/modal![export-to-pdf-modal state] {:size :lg}))
+             :disabled (= 0 (count (:badges-selected @state)))}
+    (t :badge/Exporttopdf)]])
+
 
 (defn content [state]
   [:div {:id "export-badges"}

             (empty? (:badges @state)) (t :badge/Nobadgestoexport))]
          [:div
           [badge-grid-form state]
           [select-all-button state]
           (if (not (nil? (:backpack_id (first (filter #(= (:email %) (:email-selected @state)) (:emails @state))))))
             [:button {:class    "btn btn-primary"
                       :on-click #(export-badges state)
                       :disabled (= 0 (count (:badges-selected @state)))}
              (t :badge/Exportselected)])
           [:button {:class "btn btn-primary"
                     :on-click #(do (.preventDefault %)
                                     (swap! state assoc :pdf-option "default")
                                         (m/modal![export-to-pdf-modal state] {:size :lg}))
                     :disabled (= 0 (count (:badges-selected @state)))}
            (t :badge/Exporttopdf)]
           [badge-grid state]])]))])

(defn init-data [state]

    (conj (mapv #(hash-map :value % :title %) emails)  {:value "all" :title (t :badge/All)})))

(defn badges-for-grid [state]
  (if (= "all" (:email-selected @state))(:badges @state) (filter #(= (:email-selected @state) (:email %)) (:badges @state))))

(defn visibility-options []
  [{:value "all" :title (t :badge/All)}
