<!--
  #%L
  thinkbig-ui-feed-manager
  %%
  Copyright (C) 2017 ThinkBig Analytics
  %%
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  #L%
  -->
<div flex layout="column"
     ng-class="{'visual-query-auto-complete-centered':$vq.chartViewModel.data.nodes.length ==0,'visual-query-auto-complete-top':$vq.chartViewModel.data.nodes.length >0}">
    <div layout="row" flex layout-fill layout-align="center center" ng-hide="$vq.advancedMode" class="layout-padding-top-bottom">
        <md-input-container style="right: 16px;" flex="5">
            <md-select ng-model="$vq.model.selectedDatasourceId" placeholder="Data Source" ng-change="$vq.tablesAutocomplete.searchText = ''">
                <md-option ng-repeat="datasource in $vq.availableDatasources" ng-value="datasource.id">{{datasource.name}}</md-option>
            </md-select>
            <div class="hint">Data Source</div>
        </md-input-container>
        <div flex="30" layout="column" flex>
            <md-autocomplete flex="30"
                             md-input-id="tables-auto-complete"
                             md-no-cache="$vq.tablesAutocomplete.noCache"
                             md-selected-item="$vq.tablesAutocomplete.selectedTable"
                             md-search-text-change="$vq.tablesAutocomplete.searchTextChange($vq.tablesAutocomplete.searchText)"
                             md-search-text="$vq.tablesAutocomplete.searchText"
                             md-selected-item-change="$vq.tablesAutocomplete.selectedItemChange(item)"
                             md-items="item in $vq.tablesAutocomplete.querySearch($vq.tablesAutocomplete.searchText)"
                             md-item-text="item.fullName"
                             md-input-min-length="1"
                             md-min-length="1"
                             placeholder="Search tables"
                             md-menu-class="autocomplete-hive-table-template">
                <md-item-template>
                    <span class="item-title">
                       <ng-md-icon icon="table_grid" size="20" style="fill:#F08C38"></ng-md-icon>
                        <span md-highlight-text="$vq.tablesAutocomplete.searchText" md-highlight-flags="^i" style="padding-left:5px;"> {{item.fullName}} </span>
                    </span>
                    <span class="item-metadata">
                        <span class="item-metastat"><span style="color:grey;">Schema:</span><span md-highlight-text="$vq.tablesAutocomplete.searchText" md-highlight-flags="^i">
                            {{item.schema}}</span></span>
                        <span class="item-metastat"><span style="color:grey;">Table:</span><span md-highlight-text="$vq.tablesAutocomplete.searchText" md-highlight-flags="^i">
                            {{item.tableName}}</span></span>
                    </span>
                </md-item-template>
                <md-not-found>
                    No tables matching "{{$vq.tablesAutocomplete.searchText}}" were found.  Try <a href="" ng-click="$vq.refreshAutocompleteCache()">refreshing</a> the cache.
                </md-not-found>
            </md-autocomplete>
            <div class="hint md-warn-text" ng-if="$vq.databaseConnectionError">Unable to connect to selected data source.</div>
          </div>
          <md-button flex="5" ng-click="$vq.onAddTable()" class="md-raised md-accent" ng-disabled="!$vq.tablesAutocomplete.selectedTable">Add Table</md-button>
          <md-button flex="5" ng-click="$vq.toggleAdvancedMode()" class="md-raised">Edit SQL</md-button>
          <span flex-auto layout-align="end center">
              <thinkbig-step-buttons  can-continue="$vq.isValid" step-index="{{::$vq.stepIndex}}" style="margin-top:-22px"></thinkbig-step-buttons>
          </span>
    </div>
  <div layout="row" layout-align="end center" ng-hide="!$vq.advancedMode">
      <thinkbig-step-buttons can-continue="$vq.isValid" step-index="{{::$vq.stepIndex}}"></thinkbig-step-buttons>
      <span flex="5"></span>
  </div>
</div>

<div class="visual-query-canvas" style="margin-top:8px;" layout="column" layout-fill flex mouse-capture ng-hide="$vq.chartViewModel.data.nodes.length == 0 || $vq.advancedMode == true">
    <div class="svg-container" browser-height browser-height-scroll-y="true" browser-height-scroll-x="true" browser-height-offset="300">
        <flow-chart class="flowchart" id="visual-query-canvas"
                    chart="$vq.chartViewModel"
                    on-create-connection-callback="$vq.onCreateConnectionCallback"
                    on-edit-connection-callback="$vq.onEditConnectionCallback">
        </flow-chart>
    </div>
</div>

<md-input-container style="margin-left: 16px;" ng-if="$vq.advancedMode == true">
    <md-select ng-model="$vq.model.selectedDatasourceId" placeholder="Data Source">
        <md-option ng-repeat="datasource in $vq.availableDatasources" ng-value="datasource.id">{{datasource.name}}</md-option>
    </md-select>
    <div class="hint">Data Source</div>
</md-input-container>

<thinkbig-hql-editor ng-model="$vq.advancedModeSql" ng-model-options="{getterSetter: true}" ng-if="$vq.advancedMode == true" allow-fullscreen="false" allow-database-browse="false"
                     allow-execute-query="false"></thinkbig-hql-editor>
