   (:require [dbquery.commons :as c :refer [button input]]
             [dbquery.sql-utils :refer [sort-icons]]
             [re-frame.core :refer [dispatch subscribe]]
-            [reagent.core :as r :refer [atom]]))
+            [reagent.core :as r :refer [atom]]
+            [clojure.string :as str]))
 
 (defn toggle [x y]
   (if-not (= x y) y))

                [c/button-group {:bsSize "xsmall" :style {:display "flex"}}
                 [c/button {:title "refresh" :on-click #(dispatch [:reload])} [:i.fa.fa-refresh]]
                 [c/button {:title "more options"
                            :on-click #(swap! col-toolbar-on toggle -1)} "..."]]
                (if (= @col-toolbar-on -1)
                  [more-toolbar model])]
           (doall

         "")])))

(defn filters [model]
  (r/with-let [pred (atom nil)]
    [:div.panel.panel-default
     [:div.panel-heading "Filters"]
     [:ul.list-group
      (for [[col pred] (get-in model [:query :conditions])]
        ^{:key col} [:li.list-group-item (str col " " pred)
                     [:span.badge {:on-click #(dispatch [:set-filter col nil])} "x"]])
      [:li.list-group-item
       [:form.form-inline
        {:on-submit #(do (.preventDefault %)
                         (dispatch [:set-filter (:col @pred)
                                    (str/replace-first (:value @pred) (:col @pred) "")])
                         (reset! pred nil))}
        [input {:type "typeahead" :model [pred :value] :data-source #(-> model :data :columns)
                :choice-fn #(swap! pred assoc :col %)
                :placeholder "more filters..."}]
        [button {:bs-style "default" :type "submit"} "+"]]]]
     ]))

(defn more-toolbar [model]
  (r/with-let [active (atom nil)]
    [:div.my-popover
     [c/button-group {:bsSize "small" :style {:display "flex"}}
      [c/button {:title "Filters:" :on-click #(reset! active :filters)} [:i.fa.fa-filter]]
      [c/button {:title "SQL"} [:i.fa.fa-paste]]]
     (case @active
