@extends('base.layout')

@section('content')
<div class="wrapper">
	<div class="container-fluid">
		<div class="row">
			<div class="col-md-12">
				<div class="card-box ribbon-box">
					<div class="ribbon ribbon-primary">Export / Download JSON Lintas Database</div>
					<p class="m-b-0"></p>
					@if(session('success'))
						<div class="alert alert-success">{{ session('success') }}</div>
					@endif
					<div class="form-row">
						<div class="form-group col-lg-4">
							<form method="POST" action="{{ route('json.generate') }}">
								@csrf
								<button type="submit" class="btn btn-primary mb-3">🔄 Generate All Tables (Timpa JSON Lama)</button>
							</form>
						</div>
						<div class="form-group col-lg-4">
							<a href="{{ route('json.downloadAll') }}" class="btn btn-success mb-4">📦 Download Semua JSON (ZIP)</a>
						</div>
						<div class="form-group col-lg-4">
							<div class="form-row">
								<div class="form-group col-lg-7">
									<input type="text" name="mulai" id="mulai" class="form-control" placeholder="Start Date" data-mask="9999-99">
								</div>
								<div class="form-group col-lg-5">
									<button type="button" class="btn btn-warning" id="btnarchive"><i class="fa fa-archive"></i></button>
								</div>
							</div>
						</div>
					</div>
					<div class="form-group">
						<h4>Download Per Tabel:</h4>
						<ul class="list-group">
							@foreach($tableInfo as $table)
								<li class="list-group-item d-flex justify-content-between align-items-center">
									<div>
										<strong>{{ $table['name'] }}</strong>
										@if($table['size'])
											<small class="text-muted">({{ number_format($table['size'] / 1024, 2) }} KB)</small>
										@else
											<small class="text-danger">(File tidak ditemukan)</small>
										@endif
									</div>
									<div class="d-flex gap-2">
										<a href="{{ route('json.downloadTable', $table['name']) }}" class="btn btn-sm btn-outline-secondary">⬇️ Download</a>

										<form action="{{ route('json.importTable', $table['name']) }}" method="POST" onsubmit="return confirm('Import ini akan menimpa data di tabel {{ $table['name'] }}. Lanjutkan?')">
											@csrf
											<button type="submit" class="btn btn-sm btn-outline-danger">⬆️ Import</button>
										</form>
									</div>
								</li>
							@endforeach
						</ul>

					</dv>
				</div>
			</div>
		</div>
	</div>
</div>
<input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">
@endsection
@push('script')
	<script type="text/javascript">
		$(document).ready(function () {
			$("#btnarchive").click(function() {
				var mulai 		= document.getElementById('mulai').value;
				var form_data = new FormData();
					form_data.append('nofoto', '-');
					form_data.append('jenis', '-');
					form_data.append('komponen', 'arsip');
					form_data.append('isi', mulai);
					form_data.append('keterangan', 'arsipkan');
					form_data.append('_token', '{{csrf_token()}}');
				$.ajax({
					url			: '{{ route("getResult") }}',
					data		: form_data,
					type		: 'POST',
					contentType	: false,
					processData	: false,
					success		: function (response) {
						swal({
							title	: 'Status',
							text	: response,
							type	: 'info',
						});
					},
					error: function (xhr, status, error) {
						var response 	= xhr.responseJSON || {};
						var traceText 	= response.trace ? JSON.stringify(response.trace) : null;
						console.log(traceText);
					}
				});
			});
		});
	</script>
@endpush
