<?= view('customer/menu', ['func' => $func, 'db' => $db, 'profil' => $profilSaya, 'user' => $userSaya]) ?>

<main class="wrap">
<div class="ppob-page">

<div id="informasi">
	<div class="ppob-card">
		<div class="ppob-head">
			<div class="ppob-title"><i class="material-icons" style="font-size:18px;vertical-align:-3px;">settings</i> Informasi</div>
		</div>
		<div class="container">
			<div class="row">
				<div class="col-12 col-md-6">
					<div class="btn btn-sm btn-success shadow btn-rounded">
						profil pengguna
					</div>
					<?php
					$profil = $func->getProfil(session()->get("usrid"), "semua", "usrid");
					$user = $func->getUser(session()->get("usrid"), "semua");
					?>
					<div class="row mt-3">
						<div class="col-12">
							user ID: <?php echo $profil->id; ?>
						</div>
					</div>
					<form id="profil">
						<input type="hidden" name="<?= csrf_token(); ?>" value="<?= csrf_hash(); ?>" style="display: none">
						<div class="row mt-3">
							<div class="col-12">
								<div class="form-group float-label active">
									<input class="form-control" type="text" name="nama" value="<?php echo $profil->nama; ?>">
									<label class="form-control-label" for="nama">Nama Lengkap</label>
								</div>
							</div>
						</div>
						<div class="row mt-3">
							<div class="col-12">
								<div class="form-group float-label active">
									<input class="form-control" type="text" name="markup" value="<?php echo $user->markup; ?>">
									<label class="form-control-label" for="markup">Markup (Komisi trx downline)</label>
								</div>
							</div>
						</div>
						<div class="row">
							<div class="col-12">
								<div class="form-group float-label active">
									<input class="form-control" type="text" name="email" value="<?php echo $user->username; ?> " readonly>
									<label class="form-control-label" for="email">Email</label>
								</div>
							</div>
						</div>
						<div class="row">
							<div class="col-12">
								<div class="form-group float-label active">
									<input class="form-control" type="text" name="nohp" value="<?php echo $profil->nohp; ?>" readonly>
									<label class="form-control-label" for="nohp">No Handphone</label>
								</div>
							</div>
						</div>
						<div class="row">
							<div class="col-12">
								<div class="form-group">
									<label class="form-control-label" for="kelamin">Jenis Kelamin</label>
									<select class="form-control js-select2" name="kelamin">
										<option value="">Kelamin</option>
										<option value="1" <?php if ($profil->kelamin == 1) {
															echo "selected";
														} ?>>Laki-laki</option>
										<option value="2" <?php if ($profil->kelamin == 2) {
															echo "selected";
														} ?>>Perempuan</option>
									</select>

									<div class="dropDownSelect2"></div>
								</div>
							</div>
						</div>
						<div class="row">
							<div class="col-12">
								<div class="form-group float-label active">
									<label class="form-control-label">Hub Admin untuk merubah email dan no HP</label>
								</div>
							</div>
						</div>
						<div class="text-center mb-5 form-group">
							<a href="javascript:void(0)" onclick="simpanProfil()" class="btn btn-md btn-default shadow btn-rounded">
								simpan pengaturan
							</a>
							<span id="profilload" class="cl1" style="display:none;"><i class='fa fa-spin fa-spinner'></i> Menyimpan...</span>
						</div>
					</form>
				</div>

				<div class="col-12 col-md-6">
					<div class="btn btn-sm btn-success shadow btn-rounded">
						ganti password
					</div>
					<form id="gantipassword">
						<input type="hidden" name="<?= csrf_token(); ?>" value="<?= csrf_hash(); ?>" style="display: none">
						<div class="row">
							<div class="col-12">
								<div class="form-group float-label">
									<input class="form-control" type="password" name="password" value="">
									<label class="form-control-label" for="password">Password Baru</label>
								</div>
							</div>
						</div>
						<div class="text-center mb-5 form-group m-t-50">
							<a href="javascript:void(0)" onclick="simpanPassword()" class="btn btn-md btn-default shadow btn-rounded">
								simpan password
							</a>
							<span id="passwload" class="cl1" style="display:none;"><i class='fa fa-spin fa-spinner'></i> Menyimpan...</span>
						</div>
					</form>
					<div class="btn btn-sm btn-success shadow btn-rounded">
						ganti pin
					</div>
					<form id="gantipin">
						<input type="hidden" name="<?= csrf_token(); ?>" value="<?= csrf_hash(); ?>" style="display: none">
						<div class="row">
							<div class="col-12">
								<div class="form-group float-label">
									<input class="form-control" maxlength="4" minlength="4" type="password" name="pinlama" value="">
									<label class="form-control-label" for="password">PIN Lama (4 digit)</label>
								</div>
							</div>
						</div>
						<div class="row">
							<div class="col-12">
								<div class="form-group float-label">
									<input class="form-control" maxlength="4" minlength="4" type="password" name="pin" value="">
									<label class="form-control-label" for="password">PIN Baru (4 digit)</label>
								</div>
							</div>
						</div>
						<div class="text-center mb-2 form-group m-t-50">
							<a href="javascript:void(0)" onclick="simpanPin()" class="btn btn-md btn-default shadow btn-rounded">
								simpan pin
							</a>
							<span id="pinload" class="cl1" style="display:none;"><i class='fa fa-spin fa-spinner'></i> Menyimpan...</span>
						</div>
					</form>

				</div>

			</div>
		</div>
	</div>
</div>
<!-- /section informasi -->

<!-- Section Saldo dihilangkan dulu dari halaman ini sesuai permintaan (judul, riwayat, & tombol Top Up).
     Modal #topupsaldo + JS topupSaldoModal()/historySaldo()/historyTopup() di bawah masih ada (dorman),
     tinggal panggil ulang kalau bagian ini mau dikembalikan. -->

<div id="rekening" class="mt-3">
	<div class="ppob-card">
		<div class="ppob-head">
			<div class="ppob-title"><i class="material-icons" style="font-size:18px;vertical-align:-3px;">account_balance</i> Rekening</div>
		</div>
		<div class="container">
			<div class="row">
				<div class="col-md-4 mb-2">
					<a href="javascript:tambahRekening();"><button class="btn btn-sm btn-success shadow btn-rounded"><i class="material-icons">add</i> Tambah Rekening</button></a>
				</div>
			</div>

			<?php foreach ($daftarRekening as $res): ?>
				<div class="row">
					<div class="col-lg-12">
						<ul class="list-items mt-2">
							<li>
								<div class="col-12">
									<div class="row">
										<div class="col-8">
											Nama Bank: <?= esc($func->getBank($res->idbank, 'nama')) ?>
											<p class="text-secondary small mb-2">Atas Nama: <?= esc($res->atasnama) ?></p>
											<h5 class="text-success font-weight-normal mb-0"><?= esc($res->norek) ?></h5>
											<span>Cabang: <?= esc($res->kcp) ?></span>
										</div>
										<div class="col-4 text-right">
											<a href="javascript:editRekening(<?= (int) $res->id ?>)" class="btn btn-sm btn-success mb-1" title="Edit"><i class="fa fa-edit"></i> Edit</a>
											<a href="javascript:hapusRekening(<?= (int) $res->id ?>)" class="btn btn-sm btn-danger mb-1" title="Hapus"><i class="fa fa-trash-o"></i> Hapus</a>
										</div>
									</div>
								</div>
							</li>
						</ul>
					</div>
				</div>
			<?php endforeach; ?>

			<?php if (empty($daftarRekening)): ?>
				<p class="cl1"><i class="fa fa-exclamation-triangle"></i> Belum ada daftar rekening, silahkan tambah data untuk menarik saldo.</p>
			<?php endif; ?>
		</div>
	</div>
</div>
<!-- /section rekening -->

<div id="alamat" class="mt-3">
	<div class="ppob-card">
		<div class="ppob-head">
			<div class="ppob-title"><i class="material-icons" style="font-size:18px;vertical-align:-3px;">location_on</i> Alamat</div>
		</div>
		<div class="container">
			<div class="row">
				<div class="col-md-4 mb-2">
					<a href="javascript:tambahAlamat();"><button class="btn btn-sm btn-success shadow btn-rounded"><i class="material-icons">add</i> Tambah Alamat</button></a>
				</div>
			</div>

			<?php foreach ($daftarAlamat as $res): ?>
				<?php
				$datakec = $func->getKec($res->idkec, 'semua');
				$datakel = $func->getKel($res->idkel, 'semua');
				?>
				<div class="row">
					<div class="col-lg-12">
						<ul class="list-items mt-2">
							<li>
								<div class="col-12">
									<div class="row">
										<div class="col-8">
											<?= esc($res->judul) ?> <?php if ((int) $res->status === 1): ?><p class="text-secondary small mb-2">Alamat Utama</p><?php endif; ?>
											<h5 class="text-success font-weight-normal mb-0"><?= esc($res->nohp) ?></h5>
											<span class="small">Kecamatan: <?= esc($datakec->nama ?? '-') ?></span><br />
											<span class="small">Kelurahan: <?= esc($datakel->nama ?? '-') ?></span><br />
											<span><?= esc($res->alamat) ?><br/><small>Kodepos <?= esc($res->kodepos) ?></small></span>
										</div>
										<div class="col-4 text-right">
											<a href="javascript:editAlamat(<?= (int) $res->id ?>)" class="btn btn-sm btn-success mb-1" title="Edit"><i class="fa fa-edit"></i> Edit</a>
											<a href="javascript:hapusAlamat(<?= (int) $res->id ?>)" class="btn btn-sm btn-danger mb-1" title="Hapus"><i class="fa fa-trash-o"></i> Hapus</a>
										</div>
									</div>
								</div>
							</li>
						</ul>
					</div>
				</div>
			<?php endforeach; ?>

			<?php if (empty($daftarAlamat)): ?>
				<p class="cl1"><i class="fa fa-exclamation-triangle"></i> Belum ada alamat tersimpan.</p>
			<?php endif; ?>
		</div>
	</div>
</div>
<!-- /section alamat -->

<div class="modal fade" id="tambahalamat" tabindex="-1" role="dialog" aria-labelledby="alamatModalTitle" aria-hidden="true">
	<form>
		<input type="hidden" name="<?= csrf_token(); ?>" value="<?= csrf_hash(); ?>" style="display: none">
		<input type="hidden" name="id" id="alamatid" value="0" />
		<div class="modal-dialog modal-dialog-centered" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title"><b>Alamat</b></h4>
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
				</div>
				<div class="col-md-12">
					<div class="row">
						<div class="col-md-12">
							<label class="mtext-102 control-label" style="padding-top: 10px;">Simpan sebagai? <small>cth: Alamat Rumah, Alamat Kantor, dll</small></label>
							<input class="form-control" id="alamatjudul" type="text" name="judul" placeholder="" required>
						</div>
					</div>
					<div class="row">
						<div class="col-md-12">
							<label class="mtext-102 control-label" style="padding-top: 10px;">Nama Penerima</label>
							<input class="form-control" id="alamatnama" type="text" name="nama" placeholder="" required>
						</div>
					</div>
					<div class="row">
						<div class="col-md-12">
							<label class="mtext-102 control-label" style="padding-top: 10px;">No Handphone</label>
							<input class="form-control" id="alamatnohp" type="text" name="nohp" placeholder="" required>
						</div>
					</div>
					<div class="row">
						<div class="col-md-12">
							<label class="mtext-102 control-label" style="padding-top: 10px;">Alamat Lengkap</label>
							<input class="form-control" id="alamatalamat" type="text" name="alamat" placeholder="" required>
						</div>
					</div>
					<div class="row">
						<div class="col-md-12">
							<label class="mtext-102 control-label" style="padding-top: 10px;">Provinsi</label>
							<select class="form-control js-select2" id="alamatprov" required>
								<option value="">Pilih Provinsi</option>
								<?php foreach ($db->table('prov')->orderBy('nama', 'ASC')->get()->getResult() as $res): ?>
									<option value="<?= (int) $res->id ?>"><?= esc($res->nama) ?></option>
								<?php endforeach; ?>
							</select>
							<div class="dropDownSelect2"></div>
						</div>
					</div>
					<div class="row">
						<div class="col-md-12">
							<label class="mtext-102 control-label" style="padding-top: 10px;">Kabupaten</label>
							<select class="form-control js-select2" id="alamatkab" required>
								<option value="">Pilih Kabupaten/Kota</option>
							</select>
							<div class="dropDownSelect2"></div>
						</div>
					</div>
					<div class="row">
						<div class="col-md-12">
							<label class="mtext-102 control-label" style="padding-top: 10px;">Kecamatan</label>
							<select class="form-control js-select2" id="alamatkec" name="idkec" required>
								<option value="">Pilih Kecamatan</option>
							</select>
							<div class="dropDownSelect2"></div>
						</div>
					</div>
					<div class="row">
						<div class="col-md-12">
							<label class="mtext-102 control-label" style="padding-top: 10px;">Kelurahan</label>
							<select class="form-control js-select2" id="alamatkel" name="idkel" required>
								<option value="">Pilih kelurahan</option>
							</select>
							<div class="dropDownSelect2"></div>
						</div>
					</div>
					<div class="row">
						<div class="col-md-12">
							<label class="mtext-102 control-label" style="padding-top: 10px;">Kodepos</label>
							<input class="form-control" id="alamatkodepos" type="text" name="kodepos" placeholder="" required>
						</div>
					</div>
					<div class="row m-t-20">
						<div class="col-md-12">
							<label class="mtext-102 control-label" style="padding-top: 10px;">Simpan Sebagai</label>
							<select class="form-control js-select2" id="alamatstatus" name="status" required>
								<option value="0">Alamat</option>
								<option value="1">Alamat Utama</option>
							</select>
							<div class="dropDownSelect2"></div>
						</div>
					</div>
					<div class="text-center">
						<button type="submit" class="btn btn-md btn-default shadow btn-rounded mb-3 mt-3"><i class="material-icons mb-18 mr-2">add</i>Simpan</button>
					</div>
				</div>
			</div>
		</div>
	</form>
</div>

<div class="modal fade" id="tambahrekening" tabindex="-1" role="dialog" aria-labelledby="rekeningModalTitle" aria-hidden="true">
	<form>
		<input type="hidden" name="<?= csrf_token(); ?>" value="<?= csrf_hash(); ?>" style="display: none">
		<input type="hidden" name="id" id="rekeningid" value="0" />
		<div class="modal-dialog modal-dialog-centered" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title"><b>Rekening</b></h4>
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
				</div>
				<div class="col-md-12">
					<div class="form-group row">
						<div class="col-sm-4 mt-2">
							<label class="btn btn-sm btn-success">Bank</label>
						</div>
						<div class="col-md-8 mt-2">
							<select class="form-control js-select2" id="rekeningidbank" name="idbank" required>
								<option value="">Pilih Bank</option>
								<?php foreach ($db->table('rekeningbank')->get()->getResult() as $res): ?>
									<option value="<?= (int) $res->id ?>"><?= esc($res->nama) ?></option>
								<?php endforeach; ?>
							</select>
							<div class="dropDownSelect2"></div>
						</div>
					</div>
					<div class="form-group row mt-3">
						<div class="col-sm-4 mt-1">
							<label class="btn btn-sm btn-success">No Rek</label>
						</div>
						<div class="col-md-8">
							<input class="form-control" id="rekeningnorek" type="text" name="norek" placeholder="" required>
						</div>
					</div>
					<div class="form-group row mt-3">
						<div class="col-sm-4 mt-1">
							<label class="btn btn-sm btn-success">Atas Nama</label>
						</div>
						<div class="col-md-8">
							<input class="form-control" id="rekeningatasnama" type="text" name="atasnama" placeholder="" required>
						</div>
					</div>
					<div class="form-group row mt-3">
						<div class="col-sm-4 mt-1">
							<label class="btn btn-sm btn-success">Cabang</label>
						</div>
						<div class="col-md-8">
							<input class="form-control" id="rekeningkcp" type="text" name="kcp" placeholder="" required>
						</div>
					</div>
					<div class="text-center">
						<button type="submit" class="btn btn-md btn-default shadow btn-rounded mb-3 mt-3"><i class="material-icons mb-18 mr-2">add</i>Simpan Rekening</button>
					</div>
				</div>
			</div>
		</div>
	</form>
</div>
<div class="modal fade" id="topupsaldo" tabindex="-1" role="dialog" aria-labelledby="topupModalTitle" aria-hidden="true">
	<div class="modal-dialog modal-dialog-centered" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<h4 class="modal-title"><b>Top Up Saldo</b></h4>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
			</div>
			<div class="col-md-12">
				<div id="topupstep1">
					<form id="formtopup">
						<input type="hidden" name="<?= csrf_token(); ?>" value="<?= csrf_hash(); ?>" style="display: none">
						<div class="row mt-3">
							<div class="col-md-12">
								<label class="mtext-102 control-label">Jumlah Top Up</label>
								<input class="form-control" id="topupjumlah" type="number" name="jumlah" min="1" placeholder="cth: 50000" required>
							</div>
						</div>
						<div class="text-center mt-3 mb-3">
							<button type="submit" class="btn btn-md btn-default shadow btn-rounded"><i class="material-icons mb-18 mr-2">add</i>Lanjutkan</button>
						</div>
					</form>
				</div>
				<div id="topupstep2" style="display:none;"></div>
			</div>
		</div>
	</div>
</div>
</div>
</main>
	<!-- jquery, popper and bootstrap js -->
	<script src="<?= base_url('assets/js/jquery-3.3.1.min.js') ?>"></script>
	<script src="<?= base_url('assets/js/popper.min.js') ?>"></script>
	<script src="<?= base_url('assets/js/bootstrap.min.js') ?>"></script>
	<!-- cookie js -->
	<script src="<?= base_url('assets/js/jquery.cookie.js') ?>"></script>

	<!-- swiper js -->
	<script src="<?= base_url('assets/js/swiper.min.js') ?>"></script>

	<!-- template custom js -->
	<script src="https://kit.fontawesome.com/2baad1d54e.js" crossorigin="anonymous"></script>
	<script src="<?= base_url('assets/js/main.js') ?>"></script>
	<script src="<?= base_url('assets/js/jquery.countdown.min.js') ?>"></script>
	<!-- chosen multiselect js -->
	<script src="<?= base_url('assets/js/chosen.jquery.min.js') ?>"></script>


	<!-- page level script -->
	<script type="text/javascript" src="<?= base_url('assets/vendor/select2/select2.min.js') ?>"></script>

	<div id="tokenGenerated" style="display:none;"></div>
	<?php $setMidtrans = $func->getSetting('semua'); ?>
	<script type="text/javascript" src="<?= esc($setMidtrans->midtrans_snap ?? '') ?>" data-client-key="<?= esc($setMidtrans->midtrans_client ?? '') ?>"></script>

	<script type="text/javascript">
		$(".js-select2").each(function() {
			$(this).select2({
				minimumResultsForSearch: 20,
				dropdownParent: $(this).next('.dropDownSelect2')
			});
			/* chosen select*/
		});
		$(".chosen").chosen();
	</script>

<!-- Container -->
<script type="text/javascript">
	var csfrData = {};
	csfrData['<?php echo csrf_token(); ?>'] = '<?php echo
																			csrf_hash(); ?>';
	$.ajaxSetup({
		data: csfrData
	});

	function simpanProfil() {
		$("#profil a").hide();
		$("#profilload").show();
		$.post("<?php echo site_url("assync/updateprofil"); ?>", $("#profil").serialize(), function(msg) {
			var data = eval("(" + msg + ")");
			$("#profil a").show();
			$("#profilload").hide();
			if (data.success == true) {
				swal("Berhasil!", "Berhasil menyimpan informasi pengguna", "success");
			} else {
				swal("Gagal!", "Gagal menyimpan informasi pengguna", "error");
			}
		});
	}

	function simpanPassword() {
		$("#gantipassword a").hide();
		$("#passwload").show();
		$.post("<?php echo site_url("assync/updatepass"); ?>", $("#gantipassword").serialize(), function(msg) {
			var data = eval("(" + msg + ")");
			$("#gantipassword a").show();
			$("#passwload").hide();
			if (data.success == true) {
				$("#gantipassword input").val("");
				swal("Berhasil!", "Berhasil menyimpan password baru", "success");
			} else {
				swal("Gagal!", "Gagal menyimpan informasi password", "error");
			}
		});
	}

	function simpanPin() {
		$("#gantipin a").hide();
		$("#pinload").show();
		$.post("<?php echo site_url("assync/updatepin"); ?>", $("#gantipin").serialize(), function(msg) {
			var data = eval("(" + msg + ")");
			$("#gantipin a").show();
			$("#pinload").hide();
			if (data.success == true) {
				$("#gantipin input").val("");
				swal("Berhasil!", data.message, "success");
			} else {
				swal("Gagal!", data.message, "error");
			}
		});
	}

	// ALAMAT
	function changeKab(proval, valu) {
		$("#alamatkab").html("<option value=''>Loading...</option>").trigger("change");
		$("#alamatkec").html("<option value=''>Kecamatan</option>").trigger("change");
		$.post("<?php echo site_url("assync/getkab"); ?>", { "id": proval }, function(msg) {
			var data = eval("(" + msg + ")");
			$("#alamatkab").html(data.html).promise().done(function() {
				$("#alamatkab").val(valu).trigger("change");
			});
		});
	}

	function changeKec(kabval, valu) {
		$("#alamatkec").html("<option value=''>Loading...</option>").trigger("change");
		$("#alamatkel").html("<option value=''>Kelurahan</option>").trigger("change");
		$.post("<?php echo site_url("assync/getkec"); ?>", { "id": kabval }, function(msg) {
			var data = eval("(" + msg + ")");
			$("#alamatkec").html(data.html).promise().done(function() {
				$("#alamatkec").val(valu).trigger("change");
			});
		});
	}

	function changeKel(kecval, valu) {
		$("#alamatkel").html("<option value=''>Loading...</option>").trigger("change");
		$.post("<?php echo site_url("assync/getkel"); ?>", { "id": kecval }, function(msg) {
			var data = eval("(" + msg + ")");
			$("#alamatkel").html(data.html).promise().done(function() {
				$("#alamatkel").val(valu);
			});
		});
	}

	function tambahAlamat() {
		$("#alamatid").val(0);
		$("#alamatnama").val("");
		$("#alamatnohp").val("");
		$("#alamatstatus").val(0).trigger("change");
		$("#alamatalamat").val("");
		$("#alamatkodepos").val("");
		$("#alamatjudul").val("");
		$("#alamatprov").val("").trigger("change");
		$("#alamatkab").html("<option value=''>Pilih Kabupaten/Kota</option>");
		$("#alamatkec").html("<option value=''>Pilih Kecamatan</option>");
		$("#alamatkel").html("<option value=''>Pilih kelurahan</option>");
		$('#tambahalamat').modal("show");
	}

	function editAlamat(rek) {
		$.post("<?php echo site_url("assync/getalamat"); ?>", { "rek": rek }, function(msg) {
			var data = eval("(" + msg + ")");
			if (data.success == true) {
				$("#alamatid").val(rek);
				$("#alamatnama").val(data.nama);
				$("#alamatnohp").val(data.nohp);
				$("#alamatstatus").val(data.status).trigger("change");
				$("#alamatalamat").val(data.alamat);
				$("#alamatkodepos").val(data.kodepos);
				$("#alamatjudul").val(data.judul);
				$("#alamatprov").val(data.prov).trigger("change");
				changeKab(data.prov, data.kab);
				changeKec(data.kab, data.idkec);
				changeKel(data.idkec, data.idkel);
				$('#tambahalamat').modal("show");
			} else {
				swal("Error!", "terjadi kesalahan silahkan ulangi beberapa saat lagi.", "error");
			}
		});
	}

	function hapusAlamat(rek) {
		swal({
				title: "Anda yakin?",
				text: "menghapus alamat ini dari akun Anda?",
				icon: "warning",
				buttons: true,
				dangerMode: true,
			})
			.then((willDelete) => {
				if (willDelete) {
					$.post("<?php echo site_url("assync/hapusalamat"); ?>", { "rek": rek }, function(msg) {
						var data = eval("(" + msg + ")");
						if (data.success == true) {
							swal("Berhasil!", "Berhasil menghapus alamat", "success").then((value) => {
								location.reload();
							});
						} else {
							swal("Error!", "terjadi kesalahan silahkan ulangi beberapa saat lagi.", "error");
						}
					});
				}
			});
	}

	// REKENING
	function tambahRekening() {
		$("#rekeningid").val(0);
		$("#rekeningidbank").val("").trigger("change");
		$("#rekeningnorek").val("");
		$("#rekeningatasnama").val("");
		$("#rekeningkcp").val("");
		$('#tambahrekening').modal("show");
	}

	function editRekening(rek) {
		$.post("<?php echo site_url("assync/getrekening"); ?>", { "rek": rek }, function(msg) {
			var data = eval("(" + msg + ")");
			if (data.success == true) {
				$("#rekeningid").val(rek);
				$("#rekeningidbank").val(data.idbank).trigger("change");
				$("#rekeningatasnama").val(data.atasnama);
				$("#rekeningnorek").val(data.norek);
				$("#rekeningkcp").val(data.kcp);
				$('#tambahrekening').modal("show");
			} else {
				swal("Error!", "terjadi kesalahan silahkan ulangi beberapa saat lagi.", "error");
			}
		});
	}

	function hapusRekening(rek) {
		swal({
				title: "Anda yakin?",
				text: "menghapus rekening ini dari akun Anda?",
				icon: "warning",
				buttons: true,
				dangerMode: true,
			})
			.then((willDelete) => {
				if (willDelete) {
					$.post("<?php echo site_url("assync/hapusrekening"); ?>", { "rek": rek }, function(msg) {
						var data = eval("(" + msg + ")");
						if (data.success == true) {
							swal("Berhasil!", "Berhasil menghapus rekening", "success").then((value) => {
								location.reload();
							});
						} else {
							swal("Error!", "terjadi kesalahan silahkan ulangi beberapa saat lagi.", "error");
						}
					});
				}
			});
	}

	$(function() {
		$("#tambahrekening form").on("submit", function(e) {
			e.preventDefault();
			$(".submitbutton", this).parent().append("<span class='cl1'>Memproses...</span>");
			$(".submitbutton", this).hide();
			var submitbtn = $(".submitbutton", this);

			$.post("<?php echo site_url("assync/tambahrekening"); ?>", $(this).serialize(), function(msg) {
				var data = eval("(" + msg + ")");
				if (data.success == true) {
					swal("Berhasil!", "Rekening telah disimpan", "success").then((value) => {
						location.reload();
					});
				} else {
					swal("Gagal!", "Gagal menambah rekening baru, silahkan ulangi beberapa saat lagi.", "error");
					submitbtn.show();
					submitbtn.parent().find("span").remove();
				}
			});
		});

		$("#tambahalamat form").on("submit", function(e) {
			e.preventDefault();
			$(".submitbutton", this).parent().append("<span class='cl1'>Memproses...</span>");
			$(".submitbutton", this).hide();
			var submitbtn = $(".submitbutton", this);

			$.post("<?php echo site_url("assync/tambahalamat"); ?>", $(this).serialize(), function(msg) {
				var data = eval("(" + msg + ")");
				if (data.success == true) {
					swal("Berhasil!", "Berhasil menyimpan alamat", "success").then((value) => {
						location.reload();
					});
				} else {
					swal("Gagal!", "Gagal menyimpan alamat, silahkan ulangi beberapa saat lagi.", "error");
					submitbtn.show();
					submitbtn.parent().find("span").remove();
				}
			});
		});

		$("#alamatprov").on("change", function() {
			changeKab($(this).val(), "");
		});
		$("#alamatkab").on("change", function() {
			changeKec($(this).val(), "");
		});
		$("#alamatkec").on("change", function() {
			changeKel($(this).val(), "");
		});
	});
</script>

<!-- SALDO / TOP UP -->
<script type="text/javascript">
	function historySaldo(page) {
		$("#loadhistorysaldo").html("Loading...");
		$("#loadhistorysaldo").load("<?php echo site_url("assync/gethistorytarik"); ?>?page=" + page);
	}

	function historyTopup(page) {
		$("#loadhistorytopup").html("Loading...");
		$("#loadhistorytopup").load("<?php echo site_url("assync/gethistorytopup"); ?>?page=" + page);
	}

	function topupSaldoModal() {
		$("#formtopup")[0].reset();
		$("#topupstep1").show();
		$("#topupstep2").hide().html("");
		$('#topupsaldo').modal("show");
	}

	function batalTopup(id) {
		swal({
				title: "Anda yakin?",
				text: "membatalkan topup saldo ini?",
				icon: "warning",
				buttons: true,
				dangerMode: true,
			})
			.then((willDelete) => {
				if (willDelete) {
					$.post("<?php echo site_url("assync/bataltopup"); ?>", { "id": id }, function(msg) {
						var data = eval("(" + msg + ")");
						if (data.success == true) {
							swal("Berhasil!", "Berhasil membatalkan topup saldo", "success");
							historyTopup(1);
						} else {
							swal("Error!", "terjadi kesalahan silahkan ulangi beberapa saat lagi.", "error");
						}
					});
				}
			});
	}

	function renderCaraBayar(data) {
		var html = "<div class='text-center mb-3'>" +
			"<b>Total: Rp " + data.totalFormatted + "</b><br/>" +
			"<span class='small text-secondary'>Batas Bayar: " + data.kadaluarsa + "</span>" +
			"</div>";

		if (data.paymentMidtrans) {
			html += "<div class='text-center mb-3'>" +
				"<a href='javascript:void(0)' onclick=\"bayarTopupMidtrans('" + data.trxid + "')\" class='btn btn-md btn-success shadow btn-rounded'>Bayar Otomatis (Midtrans)</a>" +
				"</div>";
		}

		if (data.paymentTransfer && data.bankTujuan.length > 0) {
			html += "<div class='mb-2'><b>Atau transfer manual ke:</b></div>";
			data.bankTujuan.forEach(function(b) {
				html += "<div class='mb-2'>Bank " + b.bank + ": <b>" + b.norek + "</b><br/>" +
					"<span class='small'>a/n " + b.atasnama + " - KCP " + b.kcp + "</span></div>";
			});
			html += "<div class='mb-2'>Cantumkan kode unik <b>" + data.trxid + "</b> pada jumlah transfer, lalu konfirmasi ke admin:</div>" +
				"<div class='text-center'><a target='_blank' href='https://wa.me/" + data.wasap + "?text=Konfirmasi%20TopUp%20" + data.trxid + "' class='btn btn-success btn-rounded shadow btn-md'><i class='fa fa-whatsapp'></i> Konfirmasi WA</a></div>";
		}

		$("#topupstep2").html(html).show();
		$("#topupstep1").hide();
	}

	function bayarTopupMidtrans(trxid) {
		$.post("<?php echo site_url("assync/midtranstokentopup"); ?>", { "trxid": trxid }, function(token) {
			snap.pay(token, {
				onSuccess: function(result) {
					swal("Berhasil!", "Pembayaran diterima, saldo akan bertambah otomatis dalam beberapa saat.", "success");
					$('#topupsaldo').modal("hide");
					historyTopup(1);
					historySaldo(1);
				},
				onPending: function(result) {
					swal("Menunggu Pembayaran", "Selesaikan pembayaran sesuai instruksi, saldo akan bertambah otomatis setelah pembayaran diterima.", "warning");
					$('#topupsaldo').modal("hide");
					historyTopup(1);
				},
				onError: function(result) {
					swal("Gagal", "Pembayaran gagal diproses, silahkan ulangi.", "error");
				},
				onClose: function() {}
			});
		});
	}

	$(function() {
		// historySaldo(1) / historyTopup(1) -- riwayat saldo & top up disembunyikan dulu, lihat catatan di view.

		$("#formtopup").on("submit", function(e) {
			e.preventDefault();
			$.post("<?php echo site_url("assync/topupsaldo"); ?>", $(this).serialize(), function(msg) {
				var data = eval("(" + msg + ")");
				if (data.success == true) {
					renderCaraBayar(data);
				} else {
					swal("Gagal!", data.pesan, "error");
				}
			});
		});
	});
</script>
