{% extends "base.html" %}
{% block title %}<title>DNS Control Panel - DOMAIN</title>{% endblock %}

{% block dashboard_stat %}
<section class="content-header">
	<h1>
		Manage domain <small>{{ domain.name }}</small>
	</h1>
	<ol class="breadcrumb">
		<li><a href="{{ url_for('dashboard') }}"><i
				class="fa fa-dashboard"></i> Home</a></li>
		<li>Domain</li>
		<li class="active">{{ domain.name }}</li>
	</ol>
</section>
{% endblock %}

{% block content %}
<section class="content">
	<div class="row">
		<div class="col-xs-12">
			<div class="box">
				<div class="box-header">
					<h3 class="box-title">Manage Records for {{ domain.name }}</h3>
				</div>
				<div class="box-body">
					<button type="button" class="btn btn-flat btn-primary pull-left button_add_record" id="{{ domain.name }}">
						Add Record&nbsp;<i class="fa fa-plus"></i>
					</button>
					<button type="button" class="btn btn-flat btn-primary pull-right button_apply_changes" id="{{ domain.name }}">
						Apply Changes&nbsp;<i class="fa fa-floppy-o"></i>
					</button>
				</div>
				<div class="box-body">
					<table id="tbl_records" class="table table-bordered table-striped">
						<thead>
							<tr>
								<th>Name</th>
								<th>Type</th>
								<th>Status</th>
								<th>TTL</th>
								<th>Data</th>
								<th>Edit</th>
								<th>Delete</th>
							</tr>
						</thead>
						<tbody>
                                {% for record in records %}
                                <tr class="odd">
                                    <td>
                                        {{ (record.name,domain.name)|display_record_name }}
                                    </td>
                                    <td>
                                        {{ record.type }}
                                    </td>
                                    <td>
                                         {{ record.status }}
                                    </td>
                                    <td>
                                         {{ record.ttl }}
                                    </td>
                                    <td>
                                         {{ record.data }}
                                    </td>
                                {% if domain.type != 'Slave' %}
                                    <td width="6%">
                                    {% if record.is_allowed() %}
	                                    <button type="button" class="btn btn-flat btn-warning button_edit" id="{{ (record.name,domain.name)|display_record_name }}">
											Edit&nbsp;<i class="fa fa-edit"></i>
										</button>
                                       {% else %}
                                       <!-- TODO: replace this link for slave records -->
                                       <a href="#" class="btn default btn-xs purple"> <i class="fa fa-exclamation-circle"></i></a>
                                    {% endif %}
                                    </td>
                                    <td width="6%">
                                    {% if record.is_allowed() %}
                                    	<button type="button" class="btn btn-flat btn-danger button_delete" id="{{ (record.name,domain.name)|display_record_name }}">
											Delete&nbsp;<i class="fa fa-trash"></i>
										</button>
                                    {% else %}
                                    	<!-- TODO: replace this link for slave records -->
                                        <a href="#" class="btn default btn-xs red"> <i class="fa fa-exclamation-circle"></i></a>
                                    {% endif %}
                                {% else %}
                                	<!-- TODO: replace these links for slave records -->
                                    <td width="6%">
                                        <a href="#" class="btn default btn-xs purple"> <i class="fa fa-exclamation-circle"></i></a>
                                    </td>
                                    <td width="6%">
                                        <a href="#" class="btn default btn-xs purple"> <i class="fa fa-exclamation-circle"></i></a>
                                    </td> 
                                {% endif %}
                                    </td>
                                </tr>
                                {% endfor %}
						</tbody>
					</table>
				</div>
				<!-- /.box-body -->
			</div>
			<!-- /.box -->
		</div>
		<!-- /.col -->
	</div>
	<!-- /.row -->
</section>
{% endblock %}
{% block extrascripts %}
<script>
	// superglobals
	window.records_allow_edit = {{ editable_records|tojson }};
	window.nEditing = null;
	window.nNew = false;

	// set up user data table
	$("#tbl_records").DataTable({
		"paging" : true,
		"lengthChange" : true,
		"searching" : true,
		"ordering" : true,
		"info" : true,
		"autoWidth" : false,
		"lengthMenu": [	[5, 15, 20, -1],
		                [5, 15, 20, "All"]],
		"pageLength": 15,
		"language": {
		    "lengthMenu": " _MENU_ records"
		},
		"retrieve" : true
	});
	
	// handle delete button
	$(document).on("click", ".button_delete", function() {
		var modal = $("#modal_delete");
		var table = $("#tbl_records").DataTable();
		var record = $(this).prop('id');
		var info = "Are you sure you want to delete " + record + "?"; 
		modal.find('.modal-body p').text(info);
		modal.find('#button_delete_confirm').click(function() {
			table.row($(this).parents('tr')[0]).remove().draw();
			modal.modal('hide');
		})
		modal.modal('show');
		
	});
	// handle edit button
	$(document).on("click", ".button_edit", function() {
		 var nRow = $(this).parents('tr')[0];
		 var table = $("#tbl_records").DataTable();
		 
		 if (nEditing !== null && nEditing != nRow && nNew == false) {
             /* Currently editing - but not this row - restore the old before continuing to edit mode */
             restoreRow(table, nEditing);
             editRow(table, nRow);
             nEditing = nRow;
		 } else if (nNew == true) {
			 /* adding a new row, delete it and start editing */
             table.row(nEditing).remove().draw();
             nNew = false;
             editRow(table, nRow);
             nEditing = nRow;
         } else {
             /* No edit in progress - let's start one */
             editRow(table, nRow);
             nEditing = nRow;
         }
	});
	
	// handle apply changes button
	$(document).on("click",".button_apply_changes", function() {
		var modal = $("#modal_apply_changes");
		var table = $("#tbl_records").DataTable();
		var domain = $(this).prop('id');
		var info = "Are you sure you want to apply your changes?"; 
		modal.find('.modal-body p').text(info);
		modal.find('#button_apply_confirm').click(function() {
			var data = getTableData(table);
			applyChanges(data, '/domain/' + domain + '/apply', true);
			modal.modal('hide');
		})
		modal.modal('show');
		
	});
	
	// handle add record button
	$(document).on("click", ".button_add_record", function (e) {
            if (nNew || nEditing) {
            	// TODO: replace this alert with modal
                alert("Previous record not saved. Please save it before adding more record.")
                return;
            }
            var table = $("#tbl_records").DataTable();

            var aiNew = table.row.add(['', 'A', 'Active', 3600, '', '', '']).draw();
            var nRow = aiNew.index();
            editRow(table, nRow);
            nEditing = nRow;
            nNew = true;
        });
	
	//handle cancel button
	$(document).on("click", ".button_cancel", function (e) {
			var oTable = $("#tbl_records").DataTable();
            if (nNew) {
                oTable.row(nEditing).remove().draw();
                nEditing = null;
                nNew = false;
            } else {
                restoreRow(oTable, nEditing);
                nEditing = null;
            }
        });
	
	//handle save button
	$(document).on("click", ".button_save", function (e) {
			var table = $("#tbl_records").DataTable();
            saveRow(table, nEditing);
            nEditing = null;
            nNew = false;
        });

</script>
{% endblock %}
{% block modals %}
<div class="modal fade modal-warning" id="modal_delete">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
				<h4 class="modal-title">Confirmation</h4>
			</div>
			<div class="modal-body">
				<p></p>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default pull-left"
					data-dismiss="modal">Close</button>
				<button type="button" class="btn btn-danger" id="button_delete_confirm">Delete</button>
			</div>
		</div>
		<!-- /.modal-content -->
	</div>
	<!-- /.modal-dialog -->
</div>
<div class="modal fade modal-primary" id="modal_apply_changes">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
				<h4 class="modal-title">Confirmation</h4>
			</div>
			<div class="modal-body">
				<p></p>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-flat btn-default pull-left"
					data-dismiss="modal">Close</button>
				<button type="button" class="btn btn-flat btn-primary" id="button_apply_confirm">Apply</button>
			</div>
		</div>
		<!-- /.modal-content -->
	</div>
	<!-- /.modal-dialog -->
</div>
{% endblock %}