/**
 * SIMDA
 *
 * @version			1.0.1
 *
 * @author			Aby Dahana
 * @profile			abydahana.github.io
 *
 * Property of DWITRI Media
 * www.dwitrimedia.com
 */

var UA									= ($(window).outerWidth() < 1024 ? 'mobile' : 'desktop');

/**
 * Mengambil jumlah notifikasi belum terbaca
 */
function get_notification_count()
{
	$.ajax
	({
		url: config.base_url + 'xhr/notification',
		method: 'POST'
	})
	.done(function(response)
	{
		$('.ssh-count').text(response.ssh),
		$('.rka-count').text(response.rka),
		$('.kak-count').text(response.kak)
	})
}

/**
 * Mengambil notifikasi belum terbaca
 */
function get_notification(_this)
{
	if(_this.attr('aria-expanded') == 'false')
	{
		$.ajax
		({
			url: _this.attr('data-href'),
			method: 'POST',
			context: this
		})
		.done(function(response)
		{
			if(response)
			{
				var output							= '';
				$.each(response, function(key, val)
				{
					output							+= '<a href="' + val.url + '" class="nav-link border-bottom --xhr-mode --modal"' + (val.status == '0' ? ' style="background:#edf2fa;position:relative"' : ' style="position:relative"') + '><i class="mdi ' + (typeof val.komentar !== 'undefined' ? 'mdi-comment-processing-outline' : 'mdi-account-arrow-right-outline') + '"></i><b>' + val.first_name + ' ' + val.last_name + '</b> ' + (typeof val.komentar !== 'undefined' ? 'berkomentar dalam' : 'mengajukan standar harga') + ' <b>' + val.uraian + '</b></a>';
				}),
				$('.notification-item').html(output).css({overflowY: 'auto'})
			}
		});
	}
}

$(document).ready(function()
{
	"use strict";
	
	/**
	 * Mathematical Operation
	 */
	$('body').on('keyup propertychange paste cut change', '.sum_input', function(e)
	{
		/* throwback the operation into total input */
		$(this).parents('form').find('.sum_input_total').trigger('change')
	}),
	
	$('body').on('change', '.sum_input_total', function(e)
	{
		e.preventDefault();
		var total							= 0;
		$('.sum_input').each(function()
		{
			var val							= $(this).val();
			val								= val.replace(',', '');
			val								= val.replace(/,/g, '');
			val								= (val > 0 ? val : 0);
			total							+= parseFloat(val);
		});
		$(this).text(total.toLocaleString('en'))
	}).trigger('change'),
	
	/**
	 * Sum the field and get total
	 */
	$('body').on('keyup propertychange paste cut change', '.sum_field', function(e)
	{
		/* throwback the operation into total input */
		$(this).parents('form').find('.sum_total').trigger('change'),
		$(this).parents('form').find('.sum_volume').trigger('change')
	}),
	
	$('body').on('change', '.sum_total', function(e)
	{
		e.preventDefault();
		var total							= $(this).closest('form').find('.sum_field:first').val(),
			total							= total.replace(/\,/g, ''),
			exec							= false;
		$(this).closest('form').find('.sum_field').not(':first').each(function()
		{
			var val							= $(this).val(),
				val							= val.replace(/\,/g, '');
			total							*= parseFloat((val > 0 ? val : 1));
			if(val > 0)
			{
				exec						= true;
			}
		}),
		$(this).val((exec ? total.toLocaleString('en') : 0))
	}).trigger('change'),
	
	$('body').on('change', '.sum_volume', function(e)
	{
		e.preventDefault();
		var total							= 0,
			exec							= false;
		$(this).closest('form').find('.sum_field').not(':first').each(function()
		{
			if(total)
			{
				total						*= parseFloat(($(this).val() > 0 ? $(this).val() : 1));
			}
			else
			{
				total						= parseFloat(($(this).val() > 0 ? $(this).val() : 1));
			}
			if($(this).val() > 0)
			{
				exec						= true;
			}
		}),
		$(this).val((exec ? total : 0))
	}).trigger('change'),
	
	/**
	 * Prepend / append the string and merge into field
	 */
	$('body').on('keyup propertychange paste cut change', '.merge-text', function(e)
	{
		/* throwback the operation into total input */
		$(this).parents('form').find('.merge-text-result').trigger('change')
	}),
	$('body').on('change', '.merge-text-result', function(e)
	{
		e.preventDefault();
		var merged							= '';
		$('.merge-text').each(function()
		{
			if($(this).val())
			{
				merged						+= (merged ? '/' : '') + $(this).val()
			}
		}),
		$('.merge-text-result').val(merged);
	}).trigger('change'),
	
	/**
	 * Average by fields
	 */
	$('body').on('click', '.average', function(e)
	{
		e.preventDefault();
		var amount							= $(this).attr('data-amount');
		if(amount <= 0)
		{
			amount							= 0;
		}
		var percent							= amount / 12, /* 12 is total month */
			total							= percent * 12; /* 12 is total month */
		
		/* return the average results */
		$(this).closest('form').find('.sum_input').val(percent.toFixed(2).toLocaleString('en')),
		$(this).closest('form').find('.percent-total').text(total.toFixed(2).toLocaleString('en')),
		$(this).closest('form').find('.sum_input_total').trigger('change')
	}),
	
	/**
	 * Reset input
	 */
	$('body').on('click', '.reset-input', function(e)
	{
		e.preventDefault(),
		$(this).parents("form").find('.sum_input').val(0),
		$(this).parents("form").find('input[type=text]:not(.sum_input), textarea').val(''),
		$(this).parents("form").find('.sum_input_total, .sum_total').trigger('change')
	}),
	
	/**
	 * Tolak data
	 */
	$('body').on('click', '.--confirm-tolak', function(e)
	{
		e.preventDefault(),
		
		/* open confirm modal */
		$(
			'<div class="modal" id="delete-modal" role="dialog" aria-labelledby="delete-modal-title" aria-hidden="true">' +
				'<div class="modal-dialog modal-dialog-centered" role="document" style="max-width:400px">' +
					'<div class="modal-content border">' +
						'<div class="modal-header">' +
							'<h5 class="modal-title" id="delete-modal-title">' +
								'<i class="mdi mdi mdi-hand"></i>' +
								'&nbsp;' +
								'<span class="modal-title-text">' +
									'Tolak Data' +
								'</span>' +
							'</h5>' +
							'<button type="button" class="close" data-dismiss="modal" aria-label="' + phrase.close + '">' +
								'<span aria-hidden="true">&times;</span>' +
							'</button>' +
						'</div>' +
						'<div class="modal-body text-center">' +
							'Anda yakin akan menolak data ini? Tindakan ini tidak dapat diurungkan!' +
						'</div>' +
						'<div class="modal-footer">' +
							'<button type="button" class="btn btn-link" data-dismiss="modal">' +
								phrase.cancel +
							'</button>' +
							'<a href="' + $(this).attr('href') + '" class="btn btn-danger --xhr --prevent-modify">' +
								'<i class="mdi mdi-check"></i>' +
								'&nbsp;' +
								phrase.submit +
							'</a>' +
						'</div>' +
					'</div>' +
				'</div>' +
			'</div>'
		)
		.appendTo('body')
		.modal({backdrop:'static', keyboard: false})
	}),
	
	/*
	 * btn order notification
	 */
	$('body').on('click', '.notification-btn', function(e)
	{
		e.preventDefault(),
		e.stopPropagation(),
		$.ajax
		({
			url: $(this).attr('href'),
			method: 'POST',
			context: this
		})
		.done(function(response)
		{
			if(response)
			{
				var output							= '';
				$.each(response, function(key, val)
				{
					output							+= '<a href="' + val.url + '" class="nav-link border-bottom --xhr-mode --modal"' + (val.status == '0' ? ' style="background:#edf2fa;position:relative"' : ' style="position:relative"') + '><i class="mdi ' + (typeof val.komentar !== 'undefined' ? 'mdi-comment-processing-outline' : 'mdi-account-arrow-right-outline') + '"></i><b>' + val.first_name + ' ' + val.last_name + '</b> ' + (typeof val.komentar !== 'undefined' ? 'berkomentar dalam' : 'mengajukan standar harga') + ' <b>' + val.uraian + '</b></a>';
				}),
				$('.notification-item').html(output).css({overflowY: 'auto'}),
				$('.notification-btn').removeClass('btn-primary').addClass('btn-outline-primary'),
				($(this).hasClass('btn-outline-primary') ? $(this).removeClass('btn-outline-primary').addClass('btn-primary') : $(this).removeClass('btn-primary').addClass('btn-outline-primary'))
			}
		})
	}),
	
	/*
	 * SHT
	 */
	$('body').on('click touch', '.notification-action', function(e)
	{
		e.preventDefault(),
		$.ajax
		({
			url: $(this).attr('href'),
			method: 'POST',
			data:
			{
				method: $(this).attr('data-action'),
				reason: $(this).closest('.form-group').find('.reason').val()
			},
			context: this
		})
		.done(function(response)
		{
			if('accept' == response.action)
			{
			}
			else if('decline' == response.action)
			{
			}
		})
	}),
	
	/*
	 * Menyiapkan pengambilan jumlah notifikasi
	 */
	get_notification_count(),
	setInterval(get_notification_count, 10000),
	
	$('body').on('change', '.get-dropdown-content', function(e)
	{
		e.preventDefault();
		var label									= $('option:selected', this).attr('data-label'),
			value									= $(this).val();
		if(!value) return false;
		$.ajax
		({
			url: $(this).parents('form').attr('action'),
			dataType: 'JSON',
			method: 'POST',
			context: this,
			cache: !1,
			data:
			{
				get_total_barang: !0,
				id_barang: value,
				kode: label
			},
			beforeSend: function()
			{
				$('.nilai-rekening').slideToggle(200, function()
				{
					/* $(this).closest('form').find('#nilai_input').val(0); */
					$(this).remove();
				});
			},
			success: function(t)
			{
				$(this).closest('form').find('#uraian_input').val(t.uraian).trigger('change');
				$(this).closest('form').find('#jumlah_barang_input').val(t.value);
				$(this).closest('form').find('#nilai_input').val(t.nilai);
				$(this).closest('form').find('#total_input').val(t.total);
				$(t.html).insertBefore($(this).closest('.form-group'));
			}
		});
	}),
	
	$('body').on('change', '.get-potongan-pajak', function(e)
	{
		e.preventDefault();
		var label									= $('option:selected', this).attr('data-label');
		$.ajax
		({
			url: $(this).parents('form').attr('action'),
			dataType: 'JSON',
			method: 'POST',
			context: this,
			cache: !1,
			data:
			{
				get_potongan_pajak: !0,
				id_rekening: $(this).val()
			},
			beforeSend: function()
			{
				$(this).closest('form').find('#nilai_input').val(0)
			},
			success: function(t)
			{
				$(this).closest('form').find('#nilai_input').val(t.html)
			}
		});
	}),
	
	$('body').on('change', '.rw', function(e)
	{
		e.preventDefault();
		$.ajax
		({
			url: $(this).parents('form').attr('action'),
			dataType: 'JSON',
			method: 'POST',
			context: this,
			cache: !1,
			data:
			{
				method: 'rw',
				primary: $(this).val()
			},
			beforeSend: function()
			{
				$('.rt').attr('disabled', 1);
			},
			success: function(data)
			{
				if(data.html)
				{
					$('.rt').html(data.html).attr('disabled', null);
				}
			}
		}).fail(function()
		{
			$('.rt').attr('disabled', 1);
		});
	}),
	
	$('body').on('keyup change', '.variable_value', function(e)
	{
		e.preventDefault();
		var _nilai									= $('.nilai_awal').attr('data-nilai');
		$('.variable_value').each(function()
		{
			_nilai									*= $(this).val();
		}),
		$('.nilai_awal').val(_nilai).number(_nilai)
	}).trigger('change'),
	
	$('body').on('change', '.isu', function(e)
	{
		e.preventDefault(),
		$.ajax
		({
			url: $(this).attr('data-url'),
			dataType: 'JSON',
			method: 'POST',
			context: this,
			cache: !1,
			data:
			{
				method: 'isu',
				primary: $(this).val()
			},
			beforeSend: function()
			{
				$('.jenis_pekerjaan').attr('readonly', 1);
				$('.jenis_pekerjaan-variable').html(null);
				if($(this).val())
				{
					/* $('.hahahihi').val(''); */
				}
			},
			success: function(data)
			{
				if(data.html && !$('.jenis_pekerjaan.readonly').length)
				{
					$('.jenis_pekerjaan').html(data.html).attr('readonly', null);
				}
			}
		}).fail(function()
		{
			$('.jenis_pekerjaan').attr('readonly', 1);
		});
	}),
	
	$('body').on('change', '.jenis_pekerjaan', function(e)
	{
		e.preventDefault();
		$.ajax
		({
			url: $(this).closest('form').attr('action'),
			dataType: 'JSON',
			method: 'POST',
			context: this,
			cache: !1,
			data:
			{
				method: 'jenis_pekerjaan',
				primary: $(this).val()
			},
			beforeSend: function()
			{
				$('.jenis_pekerjaan-variable').addClass('preloader');
				$('#alamat_detail_input').removeClass('input_alamat_detail').attr('data-pekerjaan', null);
				if($(this).val())
				{
					$('.hahahihi').val('');
				}
			},
			success: function(data)
			{
				$(this).parents('form').find('.survey-holder').html(data.survey);
				$('.jenis_pekerjaan-variable').removeClass('preloader').html(data.variable);
				if($('[role="price-format"]').length)
				{
					var _nilai						= 0;
					$('[role="price-format"]').each(function()
					{
						_nilai						= $(this).val();
						$(this).number(true)
					}),
					$('.variable_value').first().trigger('keyup change'),
					$('.nilai_awal').number(_nilai)
				}
				if(2 == $('option:selected', this).attr('data-pilihan') && data.kegiatan)
				{
					$('#alamat_detail_input').addClass('input_alamat_detail').attr('data-pekerjaan', data.kegiatan).trigger('change');
					var address						= '';
					if($('.input_alamat_detail').val())
					{
						address						= $('.input_alamat_detail').val();
					}
					$('.hahahihi').attr('data-pekerjaan', data.kegiatan).val(data.kegiatan + address).trigger('change');
				}
				else if(1 == $('option:selected', this).attr('data-pilihan') && data.kegiatan)
				{
					var address						= '';
					if($('.address-placeholder').val())
					{
						address						= $('.address-placeholder').val();
					}
					$('.hahahihi').attr('data-pekerjaan', data.kegiatan).val(data.kegiatan + address).trigger('change');
				}
				else
				{
					var address						= '';
					if($('.address-placeholder').val())
					{
						address						= ' (' + $('.address-placeholder').val() + ')';
					}
					$('.input_pekerjaan').trigger('change');
					$('.hahahihi-reses').attr('data-pekerjaan', data.kegiatan).val(data.kegiatan + address).trigger('change');
				}
				if(data.pagu)
				{
					$(this).closest('form').find('#pagu_input').val(data.pagu);
				}
			}
		}).fail(function()
		{
			$('.jenis_pekerjaan-variable').removeClass('preloader');
		});
	}).trigger('change'),
	
	$('body').on('change keydown keyup paste cut', '.input_pekerjaan', function(e)
	{
		var val										= $(this).val();
		if(val)
		{
			val										= ' ' + val;
		}
		$('.hahahihi').attr('data-pekerjaan', $(this).attr('data-pekerjaan')).val($(this).attr('data-pekerjaan') + val).trigger('change');
	}).trigger('change keydown keyup paste cut'),
	
	$('body').on('change keydown keyup paste cut', '.input_alamat_detail', function(e)
	{
		var val										= $(this).val();
		if(val)
		{
			val										= ' ' + val;
		}
		$('.hahahihi').attr('data-pekerjaan', $(this).attr('data-pekerjaan')).val($(this).attr('data-pekerjaan') + val).trigger('change');
	}).trigger('change keydown keyup paste cut'),
	
	$('body').on('change', '.program:not(.report-dropdown)', function(e)
	{
		$.ajax
		({
			url: $(this).parents('form').attr('action'),
			context: this,
			method: 'POST',
			data:
			{
				method: 'program',
				id: $(this).val()
			},
			beforeSend: function()
			{
				$('.kegiatan').attr('disabled', true),
				$(this).closest('.form-group').prev('table').remove()
			},
			success: function(data)
			{
				if('ignore' != data.last_insert)
				{
					$(this).closest('form').find('#kd_keg_input').val(data.last_insert).trigger('change')
				}
				(data.detail_program ? $(data.detail_program).insertBefore($(this).closest('.form-group')) : null),
				$(this).closest('.form-group').find('.checkbox-wrapper').remove(),
				$(this).closest('.form-group').append(data.html),
				(data.kegiatan ? $(this).closest('form').find('.kegiatan').html(data.kegiatan).attr('disabled', false).select2() : null)
			}
		})
	}).trigger('change'),
	
	$('body').on('change', '.indikator_bank', function(e)
	{
		$.ajax
		({
			url: $(this).parents('form').attr('action'),
			context: this,
			method: 'POST',
			data:
			{
				method: 'indikator_bank',
				id: $(this).val()
			},
			beforeSend: function()
			{
				$('input[name=satuan]').val('')
			},
			success: function(data)
			{
				$('input[name=satuan]').val(data.satuan)
			}
		})
	}),
	
	$('body').on('change', 'select[name=jns_indikator]', function(e)
	{
		$.ajax
		({
			url: $(this).parents('form').attr('action'),
			context: this,
			method: 'POST',
			data:
			{
				method: 'jenis_indikator',
				id: $(this).val()
			},
			beforeSend: function()
			{
				$(this).closest('form').find('#kd_indikator_input').val(1)
			},
			success: function(data)
			{
				$(this).closest('form').find('#kd_indikator_input').val(data.kode)
			}
		})
	}).trigger('change'),
	
	$('body').on('change', '.sumber_dana', function(e)
	{
		$.ajax
		({
			url: $(this).parents('form').attr('action'),
			context: this,
			method: 'POST',
			data:
			{
				method: 'sumber_dana',
				id: $(this).val()
			},
			beforeSend: function()
			{
				$(this).closest('.form-group').prev('table').remove(),
				$(this).closest('.form-group').find('.alert').remove()
			},
			success: function(data)
			{
				(data.detail_sumber_dana ? $(data.detail_sumber_dana).insertBefore($(this).closest('.form-group')) : null),
				$(this).closest('.form-group').append(data.html)
			}
		})
	}).trigger('change'),
	
	$('body').on('click change', 'input[name=pilihan]', function(e)
	{
		if(1 == $(this).val())
		{
			$(this).closest('.form-group').next('.form-group').removeClass('d-none')
			/*
			$('.model').attr('name', 'id_model');
			$('.load_model').fadeIn('slow');
			*/
		}
		else
		{
			$(this).closest('.form-group').next('.form-group').addClass('d-none')
			/*
			$('.model').attr('name', null);
			$('.load_model').fadeOut('slow');
			*/
		}
	}),
	
	$('body').on('change', '.model-isu', function(e)
	{
		e.preventDefault();
		$.ajax(
		{
			url: $(this).attr('data-url'),
			dataType: 'JSON',
			method: 'POST',
			context: this,
			cache: !1,
			data:
			{
				method: 'model_isu',
				primary: $(this).val()
			},
			beforeSend: function()
			{
				$('.model_pilihan').attr('readonly', 1);
				$('.model-variable').html(null);
			},
			success: function(data)
			{
				if(data.html && !$('.model_pilihan.readonly').length)
				{
					$('.model_pilihan').html(data.html).attr('readonly', null).trigger('change');
				}
			}
		}).fail(function()
		{
			$('.model_pilihan').attr('readonly', 1);
		})
	}),
	
	$('body').on('change', '.model_pilihan', function(e)
	{
		e.preventDefault();
		$.ajax(
		{
			url: $(this).attr('data-url'),
			dataType: 'JSON',
			method: 'POST',
			context: this,
			cache: !1,
			data:
			{
				method: 'model_pilihan',
				primary: $(this).val()
			},
			beforeSend: function()
			{
				$('.model-variable').remove();
			},
			success: function(data)
			{
				$('<div class="model-variable"></div>').appendTo($(this).closest('.form-group')),
				$('.model-variable').html(data.html);
			}
		}).fail(function()
		{
			$('.model-variable').remove();
		})
	}).trigger('change'),
	
	$('body').on('change', '.model', function(e)
	{
		e.preventDefault();
		$.ajax(
		{
			url: $(this).attr('data-url'),
			dataType: 'JSON',
			method: 'POST',
			context: this,
			cache: !1,
			data:
			{
				model: $(this).val(),
				variable: $(this).attr('data-default')
			},
			beforeSend: function()
			{
				$('.model-variable').addClass('preloader');
			},
			success: function(data)
			{
				$('.model-variable').removeClass('preloader').html(data.variable);
			}
		}).fail(function()
		{
			$('.model-variable').removeClass('preloader');
		})
	}).trigger('change'),

	$('body').on('click', '.button-answer', function(e)
	{
		e.preventDefault();
		$(this).parents('.item').removeClass('active').find('.input-answer').val($(this).attr('data-answer'));
		if($(this).parents('.item').next('.item').length)
		{
			$(this).parents('.item').next('.item').addClass('active');
		}
		else
		{
			$('<div class="item fadeIn active">Jawaban telah disimpan...</div>').insertAfter($(this).parents('.item'));
		}
	}),

	$('body').on('change', '.trigger_kode', function(e)
	{
		e.preventDefault();
		var _url									= $(this).parents('form').attr('action'),
			_default								= $('#kode_input').val();
		$.ajax(
		{
			url: _url,
			dataType: 'JSON',
			method: 'POST',
			context: this,
			cache: !1,
			data:
			{
				method: 'isu',
				isu: $(this).val()
			},
			beforeSend: function()
			{
				$('.kode_input').val('1');
			},
			success: function(data)
			{
				$('.kode_input').val(data.html);
			}
		}).fail(function()
		{
			$('.kode_input').removeClass(_default);
		})
	}).trigger('change'),

	$('body').on('change', '.trigger-program', function(e)
	{
		var code									= $('option:selected', this).attr('data-label'),
			code									= code.split(' ')[0],
			code									= code.split('.').slice(0,2).join("."),
			code									= code.split('.')[0] + sprintf(code.split('.')[1], 2);
		$(this).parents('form').find('.kode-program').val(code).trigger('change');
	}),

	$('body').on('click change', '.pilih-model', function(e)
	{
		if(1 == $(this).val())
		{
			$('.model').attr('name', 'id_model'),
			$('.load_model').fadeIn('slow')
		}
		else
		{
			$('.model').attr('name', null),
			$('.load_model').fadeOut('slow')
		}
	}),

	$('body').on('keyup', '.fetch_standar_harga', function(e)
	{
		var table									= $(this).attr('data-table'),
			field									= $(this).attr('data-field');
		$.ajax
		({
			url: $(this).parents('form').attr('action'),
			context: this,
			method: 'POST',
			data:
			{
				table: table,
				field: field,
				method: 'fetch_standar_harga'
			},
			success: function(data)
			{
				/* */
			}
		})
	}),

	$('body').on('change', '.report-dropdown', function(e)
	{
		e.preventDefault();
		$.ajax
		({
			url: $(this).closest('form').attr('action'),
			method: 'POST',
			context: this,
			beforeSend: function()
			{
				$(this).closest('form').find($(this).attr('to-change')).attr('disabled', 1);
			},
			data:
			{
				trigger: 'dropdown',
				element: $(this).attr('to-change'),
				primary: $(this).val() + ($(this).attr('data-kode-perubahan') ? '.' + $(this).closest('form').find('select[name=kd_perubahan]').find(':selected').attr('value') : ''),
				referer: $(this).attr('name')
			},
			success: function(data)
			{
				if(data.results)
				{
					$(this).closest('form').find(data.element).html(data.html).attr('disabled', null);
				}
				else
				{
					$(this).closest('form').find(data.element).html(data.html).attr('disabled', 1);
				}
			}
		})
	}),

	$('body').on('change', '.jenis-anggaran', function(e)
	{
		console.log($(this).val());
		if($(this).val() > 9)
		{
			$('#latar_belakang_perubahan_input').closest('.form-group').removeClass('d-none')
		}
		else
		{
			$('#latar_belakang_perubahan_input').closest('.form-group').addClass('d-none')
		}
	}),

	$('body').on('click', '.tambah-kak-input', function(e)
	{
		e.preventDefault();
		$(this).siblings('.kak-input.d-none').clone().removeClass('d-none').insertBefore($(this)),
		$(this).siblings('.kak-input').not('.d-none').find('textarea').removeAttr('disabled')
	}),

	$('body').on('click', '.tambah-metode-persiapan', function(e)
	{
		e.preventDefault();
		$(this).closest('tr').siblings('.metode-persiapan-input.d-none').clone().removeClass('d-none').insertBefore($(this).closest('tr')),
		$(this).closest('tr').siblings('.metode-persiapan-input').not('.d-none').find('input, textarea').removeAttr('disabled')
	}),

	$('body').on('click', '.tambah-metode-pelaksanaan', function(e)
	{
		e.preventDefault();
		$(this).closest('tr').siblings('.metode-pelaksanaan-input.d-none').clone().removeClass('d-none').insertBefore($(this).closest('tr')),
		$(this).closest('tr').siblings('.metode-pelaksanaan-input').not('.d-none').find('input, textarea').removeAttr('disabled')
	}),

	$('body').on('click', '.tambah-metode-pelaporan', function(e)
	{
		e.preventDefault();
		$(this).closest('tr').siblings('.metode-pelaporan-input.d-none').clone().removeClass('d-none').insertBefore($(this).closest('tr')),
		$(this).closest('tr').siblings('.metode-pelaporan-input').not('.d-none').find('input, textarea').removeAttr('disabled')
	}),

	$('body').on('click', '.tambah-kebutuhan-belanja', function(e)
	{
		e.preventDefault();
		var id										= $(this).attr('data-number'),
			initial_class							= $(this).attr('data-initial'),
			last_initial							= $('.' + initial_class).length,
			element									= $(this).attr('data-insert-before'),
			html									= $(this).closest('table').find('.kebutuhan-belanja-input.d-none').clone();
		
		html.removeClass('d-none').addClass(initial_class),
		html.find('ol').attr('start', (last_initial + 1)),
		html.find('#label').attr('name', 'metode_pelaksanaan[c][' + id + '][' + last_initial + '][label]'),
		html.find('#isi').attr('name', 'metode_pelaksanaan[c][' + id + '][' + last_initial + '][isi]'),
		html.find('#satuan').attr('name', 'metode_pelaksanaan[c][' + id + '][' + last_initial + '][satuan]'),
		html.find('#volume').attr('name', 'metode_pelaksanaan[c][' + id + '][' + last_initial + '][volume]'),
		html.insertBefore(element)
	}),

	$('body').on('click', '.kak-remove', function(e)
	{
		$(this).closest('.kak-input').remove()
	}),

	$('body').on('click', '.metode-persiapan-remove, .metode-pelaksanaan-remove, .metode-pelaporan-remove, .kebutuhan-belanja-remove', function(e)
	{
		$(this).closest('tr').remove(),
		$('*').tooltip('hide')
	}),
	
	$('body').on('click touch', '.submit-data button[type=submit]', function(e)
	{
		e.preventDefault();
		$.ajax
		({
			url: $(this).closest('form').attr('action'),
			data: new FormData($(this).closest('form')[0]),
			contentType: false,
			processData: false,
			context: this,
			cache: true,
			method: 'POST',
			beforeSend: function()
			{
				$('*').tooltip('hide');
			},
			success: function(data)
			{
				$(this).closest('form').find('textarea').val('');
				$(this).closest('form').find('.id-to-change').remove();
				if($('.item-' + data.id + '-wrapper').length)
				{
					var _parent							= $(this).closest('li').before();
					$('.item-' + data.id + '-wrapper').remove();
					$(data.html).prependTo(_parent);
				}
				else
				{
					$(data.html).prependTo($(this).closest('li').before());
				}
				
				var tooltip								= $(this).closest('td').find('.toggle-tooltip').attr('data-original-title');
				var regex								= new RegExp('<span class="tooltip-' + data.id + '-wrapper">(.*?)<\/span>', 'ig');
				tooltip									= tooltip.trim().replace(regex, '<span class="tooltip-' + data.id + '-wrapper">' + data.tooltip + '</span>');
				$(this).closest('td').find('.toggle-tooltip').attr('title', tooltip).attr('data-original-title', tooltip);
			},
			error: function(data)
			{
				alert('Error:\n' + data.responseText)
			}
		})
	}),
	
	$('body').on('click touch', '.dropdown-menu .prevent-close', function (e)
	{
		e.stopPropagation();
	}),
	
	$('body').on('click touch', '.dropdown-menu .update-comment', function (e)
	{
		e.stopPropagation();
		$('.id-to-change').remove();
		var name										= $(this).closest('.relative').find('textarea').attr('name');
		$(this).closest('.relative').find('textarea').val($(this).attr('data-text'));
		$('<input type="hidden" name="' + name.replace('[comments]', '[id]') + '" class="id-to-change" value="' + $(this).attr('data-id') + '" />').insertAfter($(this).closest('.relative').find('textarea'));
	}),
	
	$('body').on('click touch', '.dropdown-menu .remove-comment', function (e)
	{
		e.stopPropagation();
		$.ajax
		({
			url: $(this).closest('form').attr('action'),
			method: 'POST',
			data:
			{
				method: 'remove-comment',
				id: $(this).attr('data-id')
			},
			context: this,
			beforeSend: function()
			{
				$(this).closest('li').fadeOut(500)
			},
			success: function(data)
			{
				if(data.status == 200)
				{
					$(this).closest('li').remove()
				}
				else
				{
					$(this).closest('li').fadeIn(500)
				}
			}
		})
	}),
	
	$('[data-toggle="tooltip"], .toggle-tooltip').tooltip
	({
		container: "body",
		placement: ($(this).attr("data-placement") ? $(this).attr("data-placement") : "auto"),
		title: ($(this).attr("data-title") ? $(this).attr("data-title") : $(this).attr("title"))
	}),
	
	$('body').on('click touch', '.inout', function(e)
	{
		e.preventDefault();
		$.ajax
		({
			url: $(this).attr('href'),
			method: 'POST',
			context: this,
			beforeSend: function()
			{
				if($(this).find('.mdi').hasClass('mdi-lock') || $(this).find('.mdi').hasClass('mdi-lock-open'))
				{
					$(this).find('.mdi').removeClass('mdi-lock mdi-lock-open').addClass('icon-to-change');
				}
				$(this).find('.mdi').removeClass('mdi-check mdi-lock mdi-unlock').addClass('mdi-loading mdi-spin');
			},
			success: function(data)
			{
				$('*').modal('hide');
				$('*').tooltip('hide');
				
				if(data.button)
				{
					$(this).removeClass('btn-success btn-outline-success').addClass(data.button);
				}
				
				if($(this).find('.mdi').hasClass('icon-to-change'))
				{
					$(this).find('.mdi').removeClass('icon-to-change mdi-loading mdi-spin').addClass(data.icon);
				}
				else
				{
					$(this).find('.mdi').removeClass('mdi-loading mdi-spin').addClass('mdi-check');
				}
				
				if($(this).find('span.btn-label').length)
				{
					$(this).find('span.btn-label').text(data.label);
				}
				else
				{
					$(this).attr('title', data.label).attr('data-original-title', data.label);
				}
				if($(this).hasClass('btn-toggle'))
				{
					if(data.status == 'on')
					{
						$(this).removeClass('active inactive').addClass('inactive');
					}
					else
					{
						$(this).removeClass('active inactive').addClass('active');
					}
				}
				else
				{
					$(this).find('.mdi').removeClass('mdi-check mdi-window-close').addClass(data.icon);
					if($(this).hasClass('btn-success') || $(this).hasClass('btn-danger'))
					{
						$(this).removeClass('btn-success btn-danger').addClass(data.button)
					}
				}
				
				if(toggle_btn && toggle_btn.hasClass('btn-toggle'))
				{
					if(data.status == 'on')
					{
						toggle_btn.removeClass('active inactive').addClass('inactive');
					}
					else
					{
						toggle_btn.removeClass('active inactive').addClass('active');
					}
					toggle_btn.closest('td').find('.verifikatur').html(data.messages),
					toggle_btn.closest('tr').find('.verifikatur-ttd').html(data.messages)
				}
				else
				{
					if($('.--btn-refresh').length)
					{
						$('.--btn-refresh').trigger('click')
					}
				}
				
				/*
				if(200 == data.status)
				{
					_throw_message(data.status, data.messages);
				}
				*/
			}
		})
	}),
	
	$('body').on('change', ".filter-tanggapan", function(e)
	{
		e.preventDefault();
		var t										= $(this).attr('data-href');
		
		$.ajax
		({
			url: t,
			data:
			{
				method: 'filter',
				sub_unit: $(this).val(),
				pagination: true
			},
			dataType: "JSON",
			context: this,
			cache: true,
			method: 'POST',
			beforeSend: function()
			{
			},
			success: function(data)
			{
				if(403 == data.status || 404 == data.status || 500 == data.status)
				{
					_throw_message(data.status, data.messages)
				}
				else
				{
					$(this).closest('.box-body').html(data.html),
					$(this).closest('.box-body').closest('.modal').animate({ scrollTop: 0 }, 'fast'),
					$('select').select2()
				}
			}
		})
	}),
	
	$('body').on('click touch', ".notification-action", function(e)
	{
		e.preventDefault();
		var t										= $(this).attr('href'),
			action									= $(this).attr('data-action'),
			item									= $(this).attr('data-item'),
			reason									= $(this).closest('.notification-item').find('.reason').val();
		
		$.ajax
		({
			url: t,
			data:
			{
				method: 'notification',
				action: action,
				item: item,
				reason: reason
			},
			dataType: "JSON",
			context: this,
			cache: true,
			method: 'POST',
			beforeSend: function()
			{
				$(this).find('i').removeClass(('accept' == action ? 'mdi-check' : 'mdi-window-close')).addClass('mdi-spinner mdi-spin')
			},
			success: function(data)
			{
				$(this).find('i').removeClass('mdi-spinner mdi-spin').addClass(('accept' == action ? 'mdi-check' : 'mdi-window-close'));
				
				if(200 == data.status)
				{
					$(this).closest('.notification-item').fadeIn('slow', function()
					{
						$(this).remove()
					})
				}
			}
		})
	}),
	
	$('body').on('submit', '.notification-form', function(e)
	{
		e.preventDefault();
		$.ajax
		({
			url: $(this).attr('action'),
			method: "POST",
			data: new FormData(this),
			contentType: false,
			cache: true,
			processData: false,
			context: this,
			beforeSend: function()
			{
				$(this).find('input').attr('readonly', true).addClass('disabled bg-danger'),
				$(this).find('button.btn-primary').find('i').removeClass('mdi-send').addClass('mdi-loading mdi-spin')
			},
			success: function(data)
			{
				if(200 == data.status)
				{
					$(this).find('.status-tanggapan, .timestamp-tanggapan').removeClass('d-none'),
					$(this).find('.timestamp-tanggapan').text(data.timestamp),
					$(this).find('input').removeClass('bg-danger bg-warning')
				}
				else if(400 == data.status)
				{
					$.each(data.exception, function(key, val)
					{
						alert(val)
					})
				}
				else
				{
					$(this).find('.status-tanggapan, .timestamp-tanggapan').addClass('d-none')
				}
				$(this).find('input').attr('readonly', false).removeClass('disabled'),
				$(this).find('button.btn-primary').find('i').removeClass('mdi-loading mdi-spin').addClass('mdi-send')
			}
		})
	})
});

function sprintf(str, max)
{
	str											= str.toString();
	return str.length < max ? sprintf("0" + str, max) : str;
}