<div class="wrapper wrapper-content animated fadeInRight">
	<div class="row">
		<div class="col-lg-12">
			<div class="ibox float-e-margins ">
				<?= form_header('7') ?>
				<div class="ibox-content">
					<div class="">
						<form method="post" class="" id="formBP">
							<input type="hidden" name="t_action_new" id="action_new" value="<?=$action_new?>">
							<input type="hidden" name="t_business_unit_code" id="business_unit_code" value="<?=$head['businessUnitCode']?>">
							<div class="panel-body">
								<div class="form-horizontal">
									<div class="form-group">
										<label class="col-sm-2 control-label">Role as</label>
										<div class="col-sm-2">
										<div <?=$action_new?'hidden':''?>>
											<input class="form-control" readonly type="text" value="<?=$head['roleCode']?> - <?=$bp_role['roleName']?>">
										</div>
											<div <?=$action_new?'':'hidden'?> > 
											<select id="rolecode" name="t_rolecode" data-placeholder="Choose..." class="chosen-select" tabindex="0">
											</select>
											
											</div>
										</div>
									</div>
									<div class="form-group get-no-urut-section" style="display: none;">
										<label class="col-sm-2 control-label">No Urut Vendor</label>
										<div class="col-sm-4">
											<div class="input-group m-b">
												<span class="input-group-btn">
													<button type="button" class="btn btn-primary btn-find-last-no"><i class="fa fa-search"></i> Cari</button>
												</span>
												<input type="text" class="form-control no-urut-last" placeholder="No urut terakhir + 1">
												<span class="input-group-btn"> <button type="button" class="btn btn-primary btn-copy-text"> <i class="fa fa-copy"></i> Copy
													</button>
												</span>
											</div>
											<span class="help-block m-b-none last-vendor"></span>
										</div>
									</div>
									<div class="form-group">
										<label class="col-sm-2 control-label">Business Partner Code</label>
										<div class="col-sm-4">
											<div class="input-group m-b">
												<input type="text" placeholder="Masukan kode vendor" class="form-control" id="bpcode" name="t_bpcode" value="<?= $head['businessPartnerCode'] ?>" <?= ($head['businessPartnerCode']) ? 'readonly' : ''; ?>>
												<span class="input-group-btn <?=$action_new?'':'hidden'?>"> <button type="button" class="btn btn-primary btn-check-availability-code"> <i class="fa fa-search"></i> Cek
													</button>
												</span>
											</div>
										</div>
									</div>
									<div class="form-group">
										<label class="col-sm-2 control-label">Business Partner Name</label>
										<div class="col-sm-4">
											<input rows="2" type="text" placeholder="masukan nama vendor" class="form-control" name="t_bpname" value="<?= $head['businessPartnerName'] ?>">
										</div>
									</div>
								</div>
							</div>

							<div class="hr-line-dashed"></div>

							<div class="panel-body">
								<div class="form-horizontal">
									<div class="form-group">
										<label class="col-sm-2 control-label">Address</label>
										<div class="col-sm-6">
											<textarea id="address" name="t_address" rows="4" class="form-control" placeholder="address"><?= $head['address'] ?></textarea>
										</div>
									</div>
									<div class="form-group">
										<label class="col-sm-2 control-label">City</label>
										<div class="col-sm-2">
											<input rows="2" type="text" placeholder="city" class="form-control" name="t_city" value="<?= $head['city'] ?>">
										</div>

										<label class="col-sm-2 control-label">Zip Code</label>
										<div class="col-sm-2">
											<input rows="0" type="text" placeholder="zip code" class="form-control" name="t_zip_code" value="<?= $head['zipCode'] ?>">
										</div>
									</div>
									<div class="form-group">
										<label class="col-sm-2 control-label">e-mail</label>
										<div class="col-sm-4">
											<input rows="2" type="text" placeholder="email" class="form-control" name="t_email" value="<?= $head['email'] ?>">
										</div>
									</div>
									<div class="form-group">
										<label class="col-sm-2 control-label">Phone</label>
										<div class="col-sm-3">
											<input type="text" class="form-control" placeholder="handphone" name="t_phone" value="<?= $head['phone'] ?>">
										</div>
									</div>
									<div class="form-group">
										<label class="col-sm-2 control-label">Fax</label>
										<div class="col-sm-3">
											<input type="text" class="form-control" placeholder="fax" name="t_fax" value="<?= $head['fax'] ?>">
										</div>
									</div>
									<div class="form-group">
										<label class="col-sm-2 control-label">is Active</label>
										<div class="col-sm-3">
											<div class="i-checks"><label> <input <?=($head['status']==='1')?'checked=""':''?>   type="radio" value="1" name="t_status"> <i></i> Active </label></div>
											<div class="i-checks"><label> <input <?=($head['status']==='0')?'checked=""':''?> type="radio" value="0" name="t_status"> <i></i> Non Active </label></div>
										</div>
									</div>
									<div class="form-group">
										<label class="col-sm-2 control-label">Terms Payment</label>
										<div class="col-sm-2">
											<div class="input-group m-b">
												<input type="number" style="text-align:right" class="form-control" placeholder="terms payment" name="t_terms_pay" value="<?= $head['termsPayment'] ?>">
												<span class="input-group-addon">days</span>
											</div>
										</div>
									</div>
									<div class="form-group">
										<label class="col-sm-2 control-label">Bank Account Number</label>
										<div class="col-sm-3">
											<input type="text" class="form-control" placeholder="acc number" name="t_acc_number" value="<?= $head['bankAccountNumber'] ?>">
										</div>
									</div>
									<div class="form-group">
										<label class="col-sm-2 control-label">Status PKP</label>
										<div class="col-sm-2">
											<div class="i-checks"><label> <input <?=($head['statusPKP']==='PKP')?'checked=""':''?>   type="radio" value="PKP" name="t_status_pkp"> <i></i> PKP </label></div>
											<div class="i-checks"><label> <input <?=($head['statusPKP']==='Non PKP')?'checked=""':''?> type="radio" value="Non PKP" name="t_status_pkp"> <i></i> Non PKP </label></div>
										</div>

										<label class="col-sm-1 control-label">NPWP</label>
										<div class="col-sm-3">
											<input type="text" class="form-control" placeholder="NPWP" name="t_npwp" value="<?= $head['npwp'] ?>">
										</div>
									</div>
								</div>
							</div>

							<div class="hr-line-dashed"></div>

						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<script>
	$('.btn-find-last-no').ladda();
	$('.btn-check-availability-code').ladda();
	$(document).ready(function() {
			$('.i-checks').iCheck({
				checkboxClass: 'icheckbox_square-green',
				radioClass: 'iradio_square-green',
			});
		$('.btn-check-availability-code').click(function() {
			let btn = $(this);
			startLoadingButton(btn);
			$.ajax({
				type: "POST",
				url: site_url + "master/business_partner/check_available_number",
				data: {
					bpcode: $('#bpcode').val()
				},
				dataType: 'json',
				success: function(response) {
					swal.fire(response['header'] || 'success', response['message'] || '', "success").then(() => {
						stopLoadingButton(btn);
					});
				},
				error: function(jqXhr, textStatus, errorThrown) {
					var error_message = 'internal error';
					if (jqXhr.responseJSON !== undefined) {
						error_message = jqXhr['responseJSON']['message'];
					}
					swal.fire('error', error_message, "error");
					stopLoadingButton(btn);
				}
			});
		})
		$('.btn-copy-text').click(function() {
			$('.no-urut-last').select();
			document.execCommand("copy");
			swal.fire({
				toast: true,
				position: 'top-end',
				showConfirmButton: false,
				timer: 3000,
				timerProgressBar: true,
				didOpen: (toast) => {
					toast.addEventListener('mouseenter', Swal.stopTimer)
					toast.addEventListener('mouseleave', Swal.resumeTimer)
				},
				icon: 'success',
				title: 'No terakhir disalin'
			})
		})
		getRoleAs();
		$('#rolecode').on('change', function(e) {
			var role = $(this).val();
			if (role == 'C')
				$('.get-no-urut-section').show();
			else
				$('.get-no-urut-section').hide();
		})
		$('.btn-find-last-no').click(function() {
			let btn = $(this);
			startLoadingButton(btn);
			$.ajax({
				type: "POST",
				url: site_url + 'master/business_partner/get_last_no_urut',
				data:{
					bpcode: $('#bpcode').val()
				},
				success: function(response) {
					$('.no-urut-last').val(response['last_no']);
					$('.last-vendor').text(response['last_vendor']);
					stopLoadingButton(btn);
				},
				error: function(jqXhr, textStatus, errorThrown) {
					var error_message = 'internal error';
					if (jqXhr.responseJSON !== undefined) {
						error_message = jqXhr['responseJSON']['message'];
					}
					swal.fire('error', error_message, "error");
					stopLoadingButton(btn);
				}
			})
		})

		saveBtn.click(function() {
			let btn = $(this);
			startLoadingButton(btn);
			swal.fire({
				title: "Apakah data sudah benar?",
				text: "Perubahan akan disimpan",
				icon: "warning",
				showCancelButton: true,
				confirmButtonText: 'Simpan',
			}).then(isConfirm => {
				if (isConfirm.value) {
					$('#rolecode').removeAttr('disabled');
					$('#bpcode').removeAttr('disabled');
					$.ajax({
						type: "POST",
						url: site_url + "master/business_partner/save",
						data: $('#formBP').serialize(),
						dataType: 'json',
						success: function(response) {
							swal.fire(response['header'] || 'success', response['message'] || '', "success").then(() => {
								window.location = site_url + "master/business_partner/input/" + response['business_partner_code'] + "/"+ response['business_unit'];
								stopLoadingButton(btn);
							});
						},
						error: function(jqXhr, textStatus, errorThrown) {
							var error_message = 'internal error';
							if (jqXhr.responseJSON !== undefined) {
								error_message = jqXhr['responseJSON']['message'];
							}
							swal.fire('error', error_message, "error");
							stopLoadingButton(btn);
						}
					});
				} else {
					stopLoadingButton(btn);
					return false;
				}
			});
		});

	});

	function getRoleAs() {
		$.ajax({
			type: 'POST',
			url: site_url + "main/getComboRoleAs/",
			dataType: 'html',
			data: {},
			success: function(result) {
				$('#rolecode').empty();
				$('#rolecode').append(result);
				$('#rolecode').val('<?= $head['roleCode'] ?>').trigger("chosen:updated");
			}
		});
	}
</script>