<script>
	var formName = "{{$form_id}}";
	$('#{{$form_id}}').submit(function (e) {

		e.preventDefault();
		if ($('#{{$form_id}}').valid()) {
			$(this).find('input.btn').prop('disabled', true);

			var count = $('#count_add').val();

			count++;
			if ($('#tblview tbody tr').length !== 0) {
				count = $('#tblview tbody tr').length;
				count++;
			}

			

			$('#count_add').val(count);
			$('#asset_count').html(count);
			if (count > 0) {
				$("#createAssetBtn").prop('disabled', false);
			} else {
				$("#createAssetBtn").prop('disabled', true);
			}
			var formData = $(this).serialize();

			$.ajax({
				type: "POST",
				url: '{{route($route_name)}}',
				data: formData,
				success: function (value) {
					$('tbody').append(value);
					$('#tblview tbody tr').each(function(index) {
						$(this).find('td:first').text(index + 1);
					});

					if (formName == 'return-asset-form' || formName == 'move-asset-form') {
						$("input[type='text'][name='asset_tag']").val("");
						var seen = {};
						$('table tr').each(function () {
							var txt = $(this).text();
							if (seen[txt])
								$(this).remove();
							else
								seen[txt] = true;
						});
					} else if (formName == 'create-asset-form') {
						$("select").val('').change();
						$('.filter-field-storage-location').trigger("chosen:updated");
						$('#{{$form_id}}')[0].reset();
						$(".extrasattributes").val('');
						$("#extra_fields").html('');
						$("#make_and_model_id").select2();
					} else {
						$("select").val('').change();
						$('#{{$form_id}}')[0].reset();
						$("#make_and_model_id").select2();
					}
					$('#{{$form_id}}').find('input.btn').prop('disabled', false);
					setTimeout(function () {
						$('.alert-danger').fadeOut('fast');
					}, 30000);
				}
			});

		}

	});
</script>