<!--
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you 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.
-->

<div ng-controller="SourceMetaCtrl" class="nav-tabs-custom">
  <div class="col-xs-12" ng-show="tableModel.selectedSrcDb&&tableModel.selectedSrcTable.name">
    <h3 class="text-info">Table Schema:{{ tableModel.selectedSrcTable.name}}</h3>
    <a class="btn btn-primary pull-right" ng-if="userService.hasRole('ROLE_ADMIN') || hasPermission('project',projectModel, permissions.ADMINISTRATION.mask)" ng-click="unloadTable(tableModel.selectedSrcTable.database+'.'+tableModel.selectedSrcTable.name)" style="margin-left:10px;" ><span class="fa fa-remove"></span>&nbsp;Unload Table</a>
    <a class="btn btn-success pull-right" ng-if="tableModel.selectedSrcTable.source_type==0&&(userService.hasRole('ROLE_ADMIN') || hasPermission('project',projectModel, permissions.ADMINISTRATION.mask))"  ng-click="openReloadModal(tableModel.selectedSrcTable.database+'.'+tableModel.selectedSrcTable.name)" style="margin-left:10px;" ><span class="fa fa-download"></span>&nbsp;Reload Table</a>
    <div class="tabbable nav-tabs-custom">
      <ul class="nav nav-tabs">
        <li class="active">
          <a data-toggle="tab" href="#column">Columns</a>
        </li>
        <li>
          <a data-toggle="tab" href="#schema">Extend Information</a>
        </li>
        <li>
          <a data-toggle="tab" ng-if="tableModel.selectedSrcTable.source_type==1" href="#streaming">Streaming Cluster</a>
        </li>
        <li>
          <a data-toggle="tab"  href="#access">Access</a>
        </li>
        <li>
          <a data-toggle="tab" href="#snapshot">Snapshot</a>
        </li>
      </ul>
      <div class="tab-content">
        <!--Schema-->
        <div id="schema" class="tab-pane">
          <div ng-if="tableModel.selectedSrcTable.uuid" class="table-responsive">
            <table class="table">
              <tbody>
              <tr>
                <th style="width:20%">TABLE NAME</th>
                <td>{{ tableModel.selectedSrcTable.name}}</td>
              </tr>
              <tr>
                <th>DATABASE</th>
                <td>{{tableModel.selectedSrcTable.database}}</td>
              </tr>
              <tr>
                <th>TABLE TYPE</th>
                <td>{{tableModel.selectedSrcTable.table_type}}</td>
              </tr>
              <tr>
                <th>SNAPSHOT TIME</th>
                <td>{{tableModel.selectedSrcTable.exd.lastUpdateTime | utcToConfigTimeZone}}</td>
              </tr>
              <tr>
                <th>LOCATION</th>
                <td>{{tableModel.selectedSrcTable.exd.location}}</td>
              </tr>
              <tr>
                <th>INPUT FORMAT</th>
                <td>{{tableModel.selectedSrcTable.exd.hive_inputFormat}}</td>
              </tr>
              <tr>
                <th>OUTPUT FORMAT</th>
                <td>{{tableModel.selectedSrcTable.exd.hive_outputFormat}}</td>
              </tr>
              <tr>
                <th>OWNER</th>
                <td><a
                  href="mailto:{{tableModel.selectedSrcTable.exd.owner}}">{{tableModel.selectedSrcTable.exd.owner}}</a>
                </td>
              </tr>
              <tr>
                <th>TOTAL FILE NUMBER</th>
                <td>{{tableModel.selectedSrcTable.exd.total_file_number}}</td>
              </tr>
              <tr>
                <th>TOTAL FILE SIZE</th>
                <td>{{tableModel.selectedSrcTable.exd.total_file_size}}</td>
              </tr>
              <tr>
                <th>PARTITIONED</th>
                <td>{{tableModel.selectedSrcTable.exd.partitioned}}</td>
              </tr>
              <tr>
                <th>PARTITION COLUMNS</th>
                <td>{{tableModel.selectedSrcTable.exd.partition_column}}</td>
              </tr>
              </tbody>
            </table>
          </div>
        </div>
        <!--Columns-->
        <div id="column" class="tab-pane active">
          <div class="profile-user-info">
            <div>
              <label class="table-header-text">Columns</label>
                            <span class="input-icon form-search nav-search pull-right">
                                <input type="text" placeholder="Filter ..." class="nav-search-input"
                                       ng-model="columnName"/>
                                <i class="ace-icon fa fa-search nav-search-icon"></i>
                            </span>
            </div>
            <div class="space-6"></div>
            <div ng-if="(tableModel.selectedSrcTable.columns | filter: columnName).length>0">
              <table class="table table-hover table-striped list">
                <thead>
                <tr style="cursor: pointer">
                  <th ng-repeat="theaditem in tableConfig.theaditems"
                      ng-click="state.filterAttr= theaditem.attr;state.reverseColumn=theaditem.attr;state.filterReverse=!state.filterReverse;">
                    {{theaditem.name}}
                    <i ng-if="state.reverseColumn!= theaditem.attr"
                       class="fa fa-unsorted"></i>
                    <i ng-if="state.reverseColumn== theaditem.attr && !state.filterReverse"
                       class="fa fa-sort-asc"></i>
                    <i ng-if="state.reverseColumn== theaditem.attr && state.filterReverse"
                       class="fa fa-sort-desc"></i>
                  </th>
                </tr>
                </thead>

                <tr
                  ng-repeat="column in tableModel.selectedSrcTable.columns | filter: columnName | orderObjectBy:state.filterAttr:state.filterReverse">
                  <td
                    style="{{(tableModel.selectedSrcTable.selectedSrcColumn.id == column.id)? 'background-color:#EBF9FE':''}}">
                    {{ column.id}}
                  </td>
                  <td
                    style="{{(tableModel.selectedSrcTable.selectedSrcColumn.id == column.id)? 'background-color:#EBF9FE':''}}">
                    {{ column.name}}
                  </td>
                  <td
                    style="{{(tableModel.selectedSrcTable.selectedSrcColumn.id == column.id)? 'background-color:#EBF9FE':''}}">
                    {{ column.datatype}}
                  </td>
                  <td
                    style="{{(tableModel.selectedSrcTable.selectedSrcColumn.id == column.id)? 'background-color:#EBF9FE':''}}">
                    <!--{{ tableModel.selectedSrcTable.cardinality[column.name]}}-->
                    {{column.cardinality}}
                  </td>
                  <td
                    style="{{(tableModel.selectedSrcTable.selectedSrcColumn.id == column.id)? 'background-color:#EBF9FE':''}}">
                    {{ column.comment}}
                  </td>
                </tr>
              </table>
            </div>
            <div ng-if="(tableModel.selectedSrcTable.columns | filter: columnName).length == 0" no-result
                 text="No Matched Table Column."></div>
            <div ng-if="!!!tableModel.selectedSrcTable.uuid">
              <div no-result text="No Table Selected."></div>
            </div>
          </div>
        </div>

        <!--streaming-->
        <div id="streaming" ng-show="tableModel.selectedSrcTable.source_type==1" class="tab-pane">
            <div class="col-xs-12 pdb-12 pdt-12" ng-if="userService.hasRole('ROLE_ADMIN')">
              <div class="ofh">
                <a class="btn btn-info floatR" ng-click="editStreamingConfig(tableModel.selectedSrcTable.database+'.'+tableModel.selectedSrcTable.name)"><i class="fa fa-edit"></i>Edit</a>
              </div>
            </div>

          <div  ng-include="'partials/cubeDesigner/streamingConfig.html'" ng-init="state.mode='view'"></div>
        </div>
       <!--access-->
        <div id="access" class="tab-pane">

          <div class="tabbable nav-tabs">
            <ul class="nav nav-tabs">
              <li class="active">
                <a data-toggle="tab" href="#accesstable">Table</a>
              </li>
            </ul>
            <div class="tab-content">
              <div id="accesstable" class="tab-pane active">
                <div  ng-include="'partials/tables/table_access.html'"></div>
              </div>
            </div>
          </div>
        </div>

        <!--snapshot-->
        <div id="snapshot" class="tab-pane" ng-controller="TableSnapshotCtrl">
          <div ng-if="tableSnapshots.length > 0">
            <table class="table table-hover table-striped list">
              <thead>
                <tr style="cursor: pointer">
                  <th ng-repeat="theaditem in tableConfig.snapshotTheaditems"
                    ng-click="state.filterAttr= theaditem.attr;state.reverseColumn=theaditem.attr;state.filterReverse=!state.filterReverse;">
                  {{theaditem.name}}
                    <i ng-if="state.reverseColumn!= theaditem.attr"
                       class="fa fa-unsorted"></i>
                    <i ng-if="state.reverseColumn== theaditem.attr && !state.filterReverse"
                       class="fa fa-sort-asc"></i>
                    <i ng-if="state.reverseColumn== theaditem.attr && state.filterReverse"
                       class="fa fa-sort-desc"></i>
                  </th>
                </tr>
              </thead>
              <tbody>
                <tr ng-repeat="snapshot in tableSnapshots | filter: columnName | orderObjectBy:state.filterAttr:state.filterReverse">
                  <td style="{{(snapshot.snapshotID == snapshot.snapshotID)? 'background-color:#EBF9FE':''}}">
                    {{snapshot.snapshotID}}
                  </td>
                  <td style="{{(snapshot.snapshotID == snapshot.snapshotID)? 'background-color:#EBF9FE':''}}">
                    {{snapshot.storageType}}
                  </td>
                  <td style="{{(snapshot.snapshotID == snapshot.snapshotID)? 'background-color:#EBF9FE':''}}">
                    {{snapshot.lastBuildTime | utcToConfigTimeZone}}
                  </td>
                  <td style="{{(snapshot.snapshotID == snapshot.snapshotID)? 'background-color:#EBF9FE':''}}">
                    {{snapshot.sourceTableLastModifyTime | utcToConfigTimeZone}}
                  </td>
                  <td style="{{(snapshot.snapshotID == snapshot.snapshotID)? 'background-color:#EBF9FE':''}}">
                    {{snapshot.sourceTableSize | bytes}}
                  </td>
                  <td style="{{(snapshot.snapshotID == snapshot.snapshotID)? 'background-color:#EBF9FE':''}}" class="snapshot-usage">
                    <i class="fa fa-list text-aqua" style="cursor: pointer;"  aria-hidden="true" tooltip-placement="left" tooltip-html-unsafe="<div style='text-align:left'>{{snapshot.usageInfo}}</div>"></i>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
          <div ng-if="tableSnapshots.length == 0">
            <div no-result text="No Snapshot Info."></div>
          </div>
        </div>
      </div>

      </div>
  </div>


  <!--show load hive table tip when no models list-->
  <div ng-show="!tableModel.selectedSrcDb.length" style="margin-top:40px;">

    <!--project selected tip-->
    <div ng-if="projectModel.getSelectedProject();" class="box box-primary">
      <div class="box-header with-border">
        <i class="fa fa-bullhorn"></i>

        <h3 class="box-title">No tables</h3>
      </div>
      <div class="box-body">
        <div>
          <a tooltip="Load Table" href="javascript:void(0);" ng-if="userService.hasRole('ROLE_ADMIN') || hasPermission('project',projectModel, permissions.ADMINISTRATION.mask)"
             ng-click="openModal()">Click here to load your table</a>
        </div>
      </div>
      <!-- /.box-body -->
    </div>
    <!--project not selected tip-->
    <div ng-if="!projectModel.getSelectedProject();" class="box box-primary">
      <div class="box-header with-border">
        <i class="icon fa fa-warning"></i>

        <h3 class="box-title">No project selected</h3>
      </div>
      <div class="box-body">
        <div class="callout callout-danger">
          <p class="text-danger">
            Please select your project first
          </p>
        </div>
      </div>
      <!-- /.box-body -->
    </div>
  </div>

  <!--show load hive table tip when no models list-->
  <div ng-show="tableModel.selectedSrcDb.length&&!tableModel.selectedSrcTable.name" style="margin-top:40px;">
    <div class="box box-primary">
      <div class="box-header with-border">
        <i class="fa fa-bullhorn"></i>

        <h3 class="box-title">No table selected</h3>
      </div>
      <div class="box-body">
        Select your table
      </div>
      <!-- /.box-body -->
    </div>
  </div>

</div>
