<div ng-controller="Umbraco.PropertyEditors.GridController" class="usky-grid umb-editor umb-grid" id="usky-grid">

    <div ng-if="contentReady">

        <!-- Template picker -->

        <div class="templates-preview"
             ng-show="!model.value || model.value == ''">

            <div class="preview-rows layout"
                 ng-repeat="template in model.config.items.templates"
                 ng-click="addTemplate(template)">

                <div class="preview-row">

                    <div class="preview-col"
                         ng-class="{last:$last}"
                         ng-repeat="section in template.sections"
                         ng-style="{width: percentage(section.grid) + '%'}">

                        <div class="preview-cell">
                        </div>

                    </div>

                </div>

                <div class="preview-overlay">
                </div>

                <small>{{layout.name}}</small>

            </div> <!-- .templates-preview-rows -->

            <p><span class="help-text"><localize key="grid_addRows"/></span></p>


        </div> <!-- .templates-preview -->
        <!-- template picker end -->
        <!-- Grids -->
        <div class="usky-grid-width">
            <div class="tb">

                <!-- for each column in model -->
                <div class="usky-column td"
                     ng-class="{last:$last}"
                     ng-repeat="section in model.value.sections"
                     ng-init="initSection(section)"
                     ng-style="{width: section.$percentage + '%'}">

                    <div ui-sortable="sortableOptions" ng-model="section.rows">

                        <!-- for each row in template section -->
                        <div ng-repeat="row in section.rows"
                             class="usky-row"
                             ng-mouseenter="setCurrentRow(row)"
                             ng-mouseleave="disableCurrentRow()">

                            <!-- Row tool -->
                            <div class="row-tools" ng-animate="'fade'" 
                                ng-if="currentRow === row && currentControl === null">

                                <!-- delete row -->
                                <div class="cell-tools-remove">
                                    <div class="iconBox"
                                         ng-click="showPrompt(row)"
                                         ng-mouseover="setWarnighlightRow(row)"
                                         ng-mouseleave="disableWarnhighlightRow(row)">

                                        <span class="prompt" ng-if="row.deletePrompt">
                                            <localize key="general_areyousure" />
                                            <a href ng-click="removeRow(section, $index)"><localize key="general_yes" /></a>
                                            <a href ng-click="clearPrompt(row, $event)"><localize key="general_no" /></a>
                                        </span>

                                        <i ng-if="!row.deletePrompt" class="icon icon-trash" title="@general_delete" localize="title"></i>
                                    </div>
                                </div>

                                <div class="cell-tools-move">
                                    <a class="iconBox"
                                       ng-mouseover="setInfohighlightRow(row)"
                                       ng-mouseleave="disableInfohighlightRow(row)"
                                       href>
                                        <i class="icon icon-navigation" title="@general_move" localize="title"></i>
                                    </a>
                                </div>

                                <div class="cell-tools-edit" ng-if="hasSettings">
                                    <a class="iconBox"
                                       ng-mouseover="setInfohighlightRow(row)"
                                       ng-mouseleave="disableInfohighlightRow(row)"
                                       ng-click="editGridItemSettings(row, 'row')"
                                       href>
                                        <i class="icon icon-settings" title="@grid_settings" localize="title"></i>
                                    </a>
                                </div>

                            </div>

                            <!-- row container -->
                            <div class="{{row.cssClass}} mainContainer usky-row-inner"
                                 ng-class="{last:$last,
                                            first:$first,
                                            warnhighlight:currentWarnhighlightRow == row, 
                                            infohighlight:currentInfohighlightRow == row,
                                            selectedRow:currentRow == row && currentControl === null}">

                                <ins class="item-label">{{row.name}}</ins>
                                    
                                <div class="mainTb">
                                    <div class="tb">
                                        <div class="tr">

                                            <!-- Areas in row -->
                                            <div ng-style="{width: area.$percentage + '%'}"
                                                 class="td mainTd usky-cell"
                                                 ng-class="{last:$last,first:$first, infohighlight:currentInfohighlightArea == area,
                                                            warnhighlight:currentWarnhighlightArea == area}"
                                                 ng-repeat="area in row.areas" ui-sortable="sortableOptionsCell" ng-model="area.controls">

                                                <!-- Controls in areas -->
                                                <div ng-repeat="control in area.controls"
                                                     ng-mouseover="setCurrentControl(control)"
                                                     ng-mouseleave="disableCurrentControl(control)"
                                                     ng-animate="'fade'"
                                                     class="usky-control">

                                                    <!-- Filled cell tools -->
                                                    <div class="cell-tools"
                                                         ng-class="{'with-prompt': control.deletePrompt}"
                                                         ng-animate="'fade'"
                                                         ng-if="control && (currentControl == control)">

                                                        <!-- delete control -->
                                                        <div class="cell-tools-remove">
                                                            <div class="iconBox"
                                                                 ng-click="showPrompt(control)"
                                                                 ng-mouseover="setWarnhighlightControl(control)"
                                                                 ng-mouseleave="disableWarnhighlightControl(control)">

                                                                <span class="prompt" ng-if="control.deletePrompt">
                                                                    <localize key="general_areyousure" />
                                                                    <a href ng-click="removeControl(area, $index)"><localize key="general_yes" /></a>
                                                                    <a href ng-click="clearPrompt(control, $event)"><localize key="general_no" /></a>
                                                                </span>

                                                                <i ng-if="!control.deletePrompt" class="icon icon-trash" title="@general_delete" localize="title"></i>
                                                            </div>
                                                        </div>

                                                        <!-- move control -->
                                                        <div class="cell-tools-move">
                                                            <a class="iconBox"
                                                               ng-mouseover="setInfohighlightControl(control)"
                                                               ng-mouseleave="disableInfohighlightControl(control)"
                                                               href>
                                                                <i class="icon icon-navigation" title="@general_move" localize="title"></i>
                                                            </a>
                                                        </div>

                                                        <!-- setting area -->
                                                        <div class="cell-tools-edit" ng-if="hasSettings && $first">
                                                            <a class="iconBox"
                                                               ng-mouseover="setInfohighlightArea(area)"
                                                               ng-mouseleave="disableInfohighlightArea(area)"
                                                               ng-click="editGridItemSettings(area, 'cell')"
                                                               href>
                                                                <i class="icon icon-settings" title="@grid_settings" localize="title"></i>
                                                            </a>
                                                        </div>
                                                        
                                                        <!-- control setting area -->
                                                        <div class="cell-tools-edit" ng-style="hasSettings && $first && {'top': '99px'}" ng-if="control.editor.config && control.editor.config.settings">
                                                            <a class="iconBox"
                                                               ng-click="editGridItemSettings(control, 'control')"
                                                               href>
                                                                <i class="icon icon-hammer"></i>
                                                            </a>
                                                        </div>

                                                    </div>

                                                    <div class="usky-control-inner" ng-class="{last:$last,
                                                               infohighlight:currentInfohighlightControl == control,
                                                               warnhighlight:currentWarnhighlightControl == control,
                                                               selectedControl:currentControl == control}">
                                                        
                                                        <ins class="item-label">{{control.editor.name}}</ins>

                                                        <!-- Redering the editor for specific control -->
                                                        <div ng-if="control && control.$editorPath"
                                                             ng-include="control.$editorPath"
                                                             class="usky-cell-{{control.editor.view}}">
                                                        </div>
                                                    </div>
                                                </div>
                                                <!-- Controls repeat end -->
                                                <!-- if area is empty tools -->
                                                <div class="usky-control usky-control-placeholder"
                                                     ng-if="area.controls.length == 0">
                                                    <div class="help-text" style="margin-bottom: 15px">
                                                        <localize key="grid_addElement"/>
                                                    </div><br />
                                                </div>

                                                <div class="cell-tools-add"
                                                     ng-class="{emptyArea:area.controls.length == 0}"
                                                     ng-if="!currentToolsControl"
                                                     ng-animate="'fade'">

                                                    <a class="iconBox" href ng-click="addItemOverlay($event, area, 0, area.$uniqueId);">
                                                        <i class=" icon icon-add" title="@general_add" localize="title"></i>
                                                    </a>

                                                    <div ng-if="overlayMenu.show && overlayMenu.key == area.$uniqueId" ng-include="'views/propertyeditors/grid/dialogs/additem.html'"></div>
                                                </div>

                                            </div>
                                            <!-- cells repeat end -->
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- row container end -->

                        </div>
                        <!-- row repeat -->

                    </div>
                    <!-- row sortable end -->
                    <!-- column tools -->

                    <div class="templates-preview">

                        <div class="preview-rows columns"
                             ng-repeat="layout in  section.$allowedLayouts"
                             ng-show="layout.areas.length > 0"
                             ng-click="addRow(section, layout)">

                            <div class="preview-row">

                                <div class="preview-col" ng-style="{width: percentage(area.grid) + '%'}" ng-repeat="area in layout.areas">

                                    <div class="preview-cell">
                                    </div>

                                </div>

                            </div>

                            <div class="preview-overlay">
                            </div>

                            <small>{{layout.name}}</small>

                        </div> <!-- .templates-preview-rows -->

                        <p>
                            <span class="help-text"><localize key="grid_addRows" /></span>
                        </p>


                    </div> <!-- .templates-preview -->
                    <!-- column tools end -->

                </div>
                <!-- column repeat end -->
            </div>
        </div>
    </div>

</div>
