
<script type="text/javascript" src="/admin/assets/js/fileTree.min.js"></script>
<script type="text/javascript" src="/admin/assets/js/codemirror/codemirror.js"></script>
<script type='text/javascript' src="/admin/assets/js/codemirror/mode/htmlmixed/htmlmixed.js"></script>
<script type='text/javascript' src="/admin/assets/js/codemirror/mode/xml/xml.js"></script>
<script type='text/javascript' src="/admin/assets/js/codemirror/mode/javascript/javascript.js"></script>
<script type='text/javascript' src="/admin/assets/js/codemirror/mode/css/css.js"></script>

<div class="modal animated fadeIn" id="new_file" tabindex="-1" role="dialog" aria-labelledby="smallModalHead" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header"><h4 class="modal-title" id="smallModalHead">{templates:newTemplate}</h4></div>
			<form action="{$adminUrl}/templates" method="post" id="template" class="newTemplate">
				<input type="hidden" name="template[isNew]" value="1" />
				<div class="col-md-12  push-down-20 push-up-20">
					<div class="form-group">
						<input class="form-control" autocomplete="off" id="fileName" type="text" name="template[fileName]" value="{if isset($template.fileName)}{$template.fileName}{/if}" placeholder="{templates:templateNewFile}"/>
					</div>
				</div>
				<div class="modal-footer">
					<button type="submit" class="btn btn-primary" name="action" value="save">{general:save}</button> &nbsp;
					<button type="button" class="btn btn-primary" data-dismiss="modal">{general:cancel}</button>
				</div>
			</form>
		</div>
	</div>
</div>

<!-- Page header -->
{capture name="buttons"}
    {*        		<button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#new_file"><i class="icon-file-plus"></i> {templates:newTemplateFile}</button>*}
{/capture}

{capture name="breadcrumb"}
	<a href="{$adminUrl}/assets" class="breadcrumb-item">{$title}</a>
{/capture}

{capture name="showBy"}
{/capture}

{include file="components/page-header.tpl" buttons=$smarty.capture.buttons breadcrumb=$smarty.capture.breadcrumb showBy=$smarty.capture.showBy}
<!-- /page header -->

<div class="content">
	<div class="row">
		<div class="col-md-12">
			<div class="card border shadow-sm">
				<div class="row">
					<div class="col-md-2 p-0">
						<div id="filetree" class="filetree"></div>
					</div>
					<div class="col-md-10 pl-0">
						<div id="fileedit" style="border-left: solid 1px #BBB;min-height: 565px; padding:0px;"></div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<script>

    $(document).ready(function () {
        {if isset($errors.not_found)} new Noty({ text: '{templates:errors:0}', type: 'error', timeout: 1500 }).show(); {/if}
        {if isset($errors.not_saved)} new Noty({ text: '{templates:errors:1}', type: 'error', timeout: 1500 }).show(); {/if}
        {if isset($errors.fileName)} new Noty({ text: '{templates:errors:2}', type: 'error', timeout: 1500 }).show(); {/if}
        {if isset($errors.fileNameExists)} new Noty({ text: '{templates:errors:3}', type: 'error', timeout: 1500 }).show(); {/if}
        {if isset($errors.fileNameCharacters)} new Noty({ text: '{templates:errors:4}', type: 'error', timeout: 1500 }).show(); {/if}

        {if isset($messages.saved)} new Noty({ text: '{templates:messages:0}', type: 'success', timeout: 1500 }).show(); {/if}
        {if isset($messages.template_changed)} new Noty({ text: '{templates:messages:3}', type: 'success', timeout: 1500 }).show(); {/if}
        {if isset($messages.saved_new_file)} new Noty({ text: '{templates:messages:4}', type: 'success', timeout: 1500  }).show(); {/if}
    });


    $(function($){
        $('#filetree').fileTree({
                root: '/',
                script: '/admin/handlers/assets-files-tree.php?root=assets',
                folderEvent: 'click',
                expanded: '/',
                expandSpeed: 300,
                collapseSpeed: 250,
                multiFolder: false,
            },
            function(path) {
                $.post('/admin/handlers/assets-files-tree.php?root=assets&action=load', { "file[path]": path }, function(response) {
                    // console.log(data);
                    $('#fileedit').html(response);
                }, 'html');
            }
        );

    });

    function updateFile(path) {
        var content = editor.getValue();
        $.post('/admin/handlers/assets-files-tree.php?root=assets&action=save', { "file[path]": path, "file[content]": content }, function(response) {
            new Noty({ text: response.message, type: response.type }).show();
        });
    }

    $(".newTemplate").submit(function() {
        var form_data = $(this).serialize();
        $.ajax({
            type: $(".newTemplate").attr("method"),
            url: $(".newTemplate").attr("action"),
            data: form_data,
            success: function() {

            }
        });
    });
</script>



