<?php
use yii\helpers\Html;
use yii\helpers\Url;

use yii\bootstrap\Progress;
use yii\bootstrap\Modal;
use myretail\models\SyncToJurnal;
use myretail\models\SyncConfig;
use kartik\date\DatePicker;

$this->title = 'Auto Sync';
$this->params['breadcrumbs'][] = $this->title;

$total = SyncToJurnal::getTotalSyncDataTransaction();
?>
<div class="import-transaksi-index">
    <h1><?= Html::encode($this->title) ?></h1>

    <?php
    if (SyncConfig::isEmpty()) {
    ?>
    	<div>Please fill the configuration before starting the sync process.</div>
    <?php
    } else {
    ?>

	    <p class="notice text-danger">
	        <b>Do not refresh or go to another page!</b> <br>
	        Please click the "Stop Sync" button below before you do so.
	    </p>

	    <button class="btn btn-danger stop-import">Stop Sync</button>

	    <p class="result bg-black"></p>
	<?php
    }
    ?>
</div>

<?php
if (!SyncConfig::isEmpty()) {
?>
	<script type="text/javascript">
		var continueSync = true;
		var lineCount = 0;

		function appendResult(message) {
			$('.result').append(message);
			$('.result').animate({
		        scrollTop: $('.result')[0].scrollHeight
		    }, 100);
		    lineCount++;

		    if (lineCount > 100) {
		    	$('.result div').first().remove();
	    		lineCount = 100;
		    }
		}

		function importDataSalesReturn() {
			appendResult('<div>[SALES RETURN] begin import -- Please DO NOT REFRESH or GO TO ANOTHER PAGE!</div>');
			var data = '';

			$.ajax({
				url: '<?= Url::to(['sales-return/index']); ?>',
				success: function(result) {
					if (result == 'ret_tran') {
						appendResult('<div>[SALES RETURN] done import</div>');
					} else {
						appendResult('<div>[SALES RETURN] continue import</div>');
					}

					data = result;
					console.log(data);
				},
				complete: function() {
					if (continueSync) {
						if (data != 'ret_tran')
							importDataSalesReturn();
						else
							importDataTransaction();
					}
				}
			});
		}

		function importDataPayment() {
			appendResult('<div>[PAYMENT] begin import -- Please DO NOT REFRESH or GO TO ANOTHER PAGE!</div>');
			var data = '';

			$.ajax({
				url: '<?= Url::to(['import-payment/index']); ?>',
				success: function(result) {
					if (result == 'pay') {
						appendResult('<div>[PAYMENT] done import</div>');
					} else {
						appendResult('<div>[PAYMENT] continue import</div>');
					}

					data = result;
					console.log(data);
				},
				complete: function() {
					if (continueSync) {
						if (data != 'pay')
							importDataPayment();
						else
							importDataSalesReturn();
					}
				}
			});
		}

		function importDataTransaction() {
			appendResult('<div>[TRANSACTION] begin import -- Please DO NOT REFRESH or GO TO ANOTHER PAGE!</div>');
			var data = '';

			$.ajax({
				url: '<?= Url::to(['import-transaction/index']); ?>',
				success: function(result) {
					if (result == 'tran') {
						appendResult('<div>[TRANSACTION] done import</div>');
					} else {
						appendResult('<div>[TRANSACTION] continue import</div>');
					}

					data = result;
					console.log(data);
				},
				complete: function() {
					if (continueSync) {
						if (data != 'tran')
							importDataTransaction();
						else
							importDataPayment();
					}
				}
			});
		}

		$('.stop-import').click(function() {
			continueSync = false;
		});

		$(window).on('load', function() {
			importDataTransaction();
		});
	</script>
<?php
}
?>