<script tipe="module">
	function switchProses(id)
	{

		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]}}_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]}}_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 updateData(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(passednih)
		{
			var file_data = $("#{{$route[1]}}_dokumenPersetujuan_"+id).prop("files")[0];
			let useURL = "{{ route($route[0].'.'.$route[1].'.update', $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) {
		// data => detail_penawaran.id
		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') }}";
				useURL = useURL.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) {

						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[0], 'error');
						}


					},
					error: function(response, textStatus, errorThrown) {
						// var errors = response.responseJSON.errors;
						// console.log(errors);
						console.log(response);

					}
				});

			}
		})
	}
	</script>
