<script tipe="module">
function switchProses(id)
{
	removeErrorCssMsg();
	let c = $('#{{$route[1]}}_check_'+id).val();
	
	if($('input[name="{{$route[1]}}_check_'+id+'"]').is(':checked'))
	{
		// checked
		// alert('aktif nih');
		setActiveElement(id);
	}else
	{
		// unchecked
		//alert('tdk aktif nih');
		setNonActiveElement(id);
	}
}

function setActiveElement(id)
{
	$('#{{$route[1]}}_no_proposal_'+id).removeAttr('disabled');
	$('#{{$route[1]}}_tgl_proposal_'+id).removeAttr('disabled');
	$('#{{$route[1]}}_biayaPenawaran_'+id).removeAttr('disabled');
	$('#{{$route[1]}}_dokumenPersetujuan_'+id).removeAttr('disabled');
	$('#{{$route[1]}}_icon_update_'+id).removeAttr('disabled');
	$('#{{$route[1]}}_icon_delete_'+id).removeAttr('disabled');
}

function setNonActiveElement(id)
{
	$('#{{$route[1]}}_no_proposal_'+id).attr('disabled', 'disabled');
	$('#{{$route[1]}}_tgl_proposal_'+id).attr('disabled', 'disabled');
	$('#{{$route[1]}}_biayaPenawaran_'+id).attr('disabled', 'disabled');
	$('#{{$route[1]}}_dokumenPersetujuan_'+id).attr('disabled', 'disabled');
	$('#{{$route[1]}}_icon_update_'+id).attr('disabled', 'disabled');
	$('#{{$route[1]}}_icon_delete_'+id).attr('disabled', 'disabled');
}

function updateDataUlang(id, kjpp_id, kjppName){
	// id ==> detail_penawaran.id
	removeErrorCssMsg();
		let no_proposal = $("#{{$route[1]}}_no_proposal_"+id).val();                            
		let tgl_proposal = $("#{{$route[1]}}_tgl_proposal_"+id).val(); 
		let biaya = $("#{{$route[1]}}_biayaPenawaran_"+id).val();                            
		let filepdf = $("#{{$route[1]}}_dokumenPersetujuan_"+id).val();       
		let passednih = true;                    
		
		if(!no_proposal){
			$("#{{$route[1]}}_no_proposal_"+id).addClass(" border-danger");
			$("#{{$route[1]}}_no_proposal_msg_"+id).text("Silahkan isi No Proposal");
			passednih = false;
		}                      
		if(!tgl_proposal){
			$("#{{$route[1]}}_tgl_proposal_"+id).addClass(" border-danger");
			$("#{{$route[1]}}_tgl_proposal_msg_"+id).text("Silahkan isi Tanggal Proposal");
			passednih = false;
		}                      
		if(!biaya){
			$("#{{$route[1]}}_rp_"+id).addClass(" border-danger");
			$("#{{$route[1]}}_biayaPenawaran_"+id).addClass(" border-danger");
			$("#{{$route[1]}}_biayaPenawaran_msg_"+id).text("Silahkan isi Biaya Penawaran nya");
			passednih = false;
		}                      
		if(!filepdf){
			$("#{{$route[1]}}_dokumenPersetujuan_"+id).addClass(" border-danger");
			$("#{{$route[1]}}_dokumenPersetujuan_msg_"+id).text("Silahkan isi dokumen nya");
			$("#{{$route[1]}}_dokumenPersetujuan_"+id).val("");
			passednih = false;
		}            
		
		if(passednih)
		{
			var file_data = $("#{{$route[1]}}_dokumenPersetujuan_"+id).prop("files")[0];
			let useURL = "{{ route($route[0].'.'.$route[1].'.updateulang', ':id') }}".replace(':id', id);
			let formData = new FormData();  
			let token   = "{{ csrf_token() }}";
			formData.append("dokumen_persetujuan", file_data ?? "");
			formData.append("no_proposal", no_proposal);
			formData.append("tgl_proposal", tgl_proposal);
			formData.append("biaya_penawaran", biaya);
			formData.append("kjpp_rekanan_id", kjpp_id);
			formData.append("_method", "PUT");
			formData.append("_token", token);
			$.ajax({
				url: useURL,
				type: "POST",         
				data: formData,
				processData: false,
				contentType: false,
				success:function(response){
					if('success' == response.status)
					{
						// toastr.success(response.message);
						// success
						var message = response.message;
						toastrku("success", message);
						setTimeout(function () {						
							 location.reload(true);
						}, 2000);
					}
					else if('error' == response.status)
					{
						// toastr.error(response.message);
						var message = response.message;
						toastrku("error", message);
					}
					
				},
				error: function (response, textStatus, errorThrown) {
					var errors = response.responseJSON.errors;
					$.each(errors, function (key, value) {
						console.log("v = " + value);
					});
				}
			});
	
		}
}

function deleteData(data, kjppName) {
	Swal.fire({
		title: 'Are you sure?',
		text: "You won't be able to revert KJPP "+kjppName+"!",
		icon: 'warning',
		showCancelButton: true,
		confirmButtonColor: '#3085d6',
		cancelButtonColor: '#d33',
		confirmButtonText: 'Yes, delete it!'
	}).then((result) => {
		if (result.isConfirmed) {
			
				//define variable
			let token = "{{ csrf_token() }}";
			let useURL = "{{ route($route[0].'.'.$route[1].'.updateKJPPStatus', ':id') }}".replace(':id', data);
				
			var input_data = new Object();
			input_data._token = token;
			input_data.id =data;
			input_data.kjppName =kjppName;
			$.ajax({
				url: useURL,
				type: "PUT",
				cache: false,
				data: input_data,
				dataType: "json",
				success: function(response) {
					console.log(response);
					if('success' == response.status)
					{
						swal.fire('Deleted!', response.message.message_success[0], 'success').then(() => {
							window.location.reload();
						});
					}
					else
					{
						Swal.fire('Error!', response.message.message_error_try_catch[0], 'error');
					}
						
					
				},
				error: function(response, textStatus, errorThrown) {
					// var errors = response.responseJSON.errors;
					// console.log(errors);
					console.log(response);

				}
			});     
		}
	})
}

function addDataUlang()
{
	removeErrorCssMsg();
	// id ==> penawaran.id
	let id = $("#id").val();
	let kjpp_id = $("#{{$route[1]}}_kjpps1").val();                            
	let no_proposal = $("#{{$route[1]}}_no_proposal").val();                            
	let tgl_proposal = $("#{{$route[1]}}_tgl_proposal").val(); 
	let biaya = $("#{{$route[1]}}_biayaPenawaran").val();                            
	let filepdf = $("#{{$route[1]}}_dokumenPersetujuan").val();       
	let passednih = true;

	
	if("0"==kjpp_id){
		$("#{{$route[1]}}_kjpps1").addClass(" border-danger");
		$("#{{$route[1]}}_kjpp1_msg").text("Silahkan pilih KJPP");
		passednih = false;
	}                      
	
	if(!no_proposal){
		$("#{{$route[1]}}_no_proposal").addClass(" border-danger");
		$("#{{$route[1]}}_no_proposal_msg").text("Silahkan isi No Proposal");
		passednih = false;
	}                      
	if(!tgl_proposal){
		$("#{{$route[1]}}_tgl_proposal").addClass(" border-danger");
		$("#{{$route[1]}}_tgl_proposal_msg").text("Silahkan isi Tanggal Proposal");
		passednih = false;
	}                      
	if(!biaya){
		$("#{{$route[1]}}_rp").addClass(" border-danger");
		$("#{{$route[1]}}_biayaPenawaran").addClass(" border-danger");
		$("#{{$route[1]}}_biayaPenawaran_msg").text("Silahkan isi Biaya Penawaran");
		passednih = false;
	}                      
	if(!filepdf){
		$("#{{$route[1]}}_dokumenPersetujuan").addClass(" border-danger");
		$("#{{$route[1]}}_dokumenPersetujuan_msg").text("Silahkan isi dokumen");
		$("#{{$route[1]}}_dokumenPersetujuan").val("");
		passednih = false;
	}    

	if(passednih)
	{
		var file_data = $("#{{$route[1]}}_dokumenPersetujuan").prop("files")[0];
		let useURL = "{{ route($route[0].'.'.$route[1].'.storeUlang') }}";
		let formData = new FormData();  
		let token   = "{{ csrf_token() }}";
		formData.append("dokumen_persetujuan", file_data ?? "");
		formData.append("penawaran_id", id);
		formData.append("no_proposal", no_proposal);
		formData.append("tgl_proposal", tgl_proposal);
		formData.append("biaya_penawaran", biaya);
		formData.append("kjpp_rekanan_id", kjpp_id);
		formData.append("_token", token);
		
		$.ajax({
			url: useURL,
			type: "POST",         
			data: formData,
			processData: false,
			contentType: false,
			success:function(response){
				if('success' == response.status)
				{
					// toastr.success(response.message);
					// success
					var message = response.message;
					toastrku("success", message);
					setTimeout(function () {						
							location.reload(true);
					}, 2000);
				}
				else if('error' == response.status)
				{
					// toastr.error(response.message);
					var message = response.message;
					toastrku("error", message);
				}
				
			},
			error: function (response, textStatus, errorThrown) {
				var errors = response.responseJSON.errors;
				$.each(errors, function (key, value) {
					console.log("v = " + value);
				});
			}
		});

	}
}

function deleteDataUlang(data, kjppName) {
	Swal.fire({
		title: 'Are you sure?',
		text: "You won't be able to revert KJPP "+kjppName+"!",
		icon: 'warning',
		showCancelButton: true,
		confirmButtonColor: '#3085d6',
		cancelButtonColor: '#d33',
		confirmButtonText: 'Yes, delete it!'
	}).then((result) => {
		if (result.isConfirmed) {
			
				//define variable
			let token = "{{ csrf_token() }}";
			let useURL = "{{ route($route[0].'.'.$route[1].'.updateKJPPStatusUlang','') }}/"+data;
				
			var input_data = new Object();
			input_data._token = token;
			input_data.id =data;
			input_data.kjppName =kjppName;
			$.ajax({
				url: useURL,
				type: "PUT",
				cache: false,
				data: input_data,
				dataType: "json",
				success: function(response) {
					// console.log(response);
					if('success' == response.status)
					{
						swal.fire('Deleted!', response.message.message_success[0], 'success').then(() => {
							window.location.reload();
						});
					}
					else
					{
						var arr =response.message; // JSON Object
						const keyToCheck1 = "active_date_range";
						const keyToCheck2 = "message_error_try_catch";
						const exists1 = keyToCheck1 in arr;
						const exists2 = keyToCheck2 in arr;
						
						if(exists1)
						{
							Swal.fire('Error!', response.message.active_date_range[0], 'error').then(() => {
								// window.location.reload();
								setTimeout(function () {
									var url = "{{ route('tender.prosespenawaran.index') }}";
									$(location).attr('href',url);
								}, 2000);
							});
						}
						else if(exists2)
						{
							Swal.fire('Error!', response.message.message_error_try_catch[0], 'error').then(() => {
								window.location.reload();
							});
						}
					}
						
					
				},
				error: function(response, textStatus, errorThrown) {
					// var errors = response.responseJSON.errors;
					// console.log(errors);
					console.log(response);

				}
			});     
		}
	})
}
</script>