<input type="hidden" name="kode_invoice" id="kode_invoice" value="<?=$detail['invoice'];?>">
<input type="hidden" name="kode" id="kode" value="<?=$detail['idr'];?>">
<?php
	if(strtolower($nama_produk)=='desain' OR strtolower($nama_produk)=='design'){
		$title_produk = 'Input nama '.$nama_produk;
		$cek_desain = cek_desain($detail['invoice']);
	 
	?>
	<input type="hidden" name="tipe" id="tipe" value="desain">
	<div class="form-group row mb-1">
		<label class="col-sm-4 col-form-label">Nama Designer</label>
		<div class="col-sm-8">
			<div class="input-group">
				<select id='id_desain' name="id_desain" class="form-control">
					<?php foreach($desain AS $val){
						if($val->id_user == $cek_desain){
							echo "<option value='{$val->id_user}' selected>{$val->nama_lengkap}</option>";
							}else{
							echo "<option value='{$val->id_user}'>{$val->nama_lengkap}</option>";
						}
					}
					?>
				</select>
			</div>
		</div>
	</div>
	
	<?php }else{
		$title_produk = $nama_produk;
	?>
	<input type="hidden" name="tipe" id="tipe" value="all">
	<style>
		.thumb-preview-finishing{
			display:inline-flex;
			align-items:center;
			justify-content:center;
			width:34px;
			height:34px;
			padding:0;
			border:1px solid #d7dce2;
			border-radius:4px;
			overflow:hidden;
			background:#f8f9fa;
		}
		.thumb-preview-finishing img{
			width:100%;
			height:100%;
			object-fit:cover;
			display:block;
		}
		.finishing-action-buttons{
			display:inline-flex;
			align-items:center;
			gap:.35rem;
			flex-wrap:nowrap;
		}
		.finishing-paste-zone{
			padding:.3rem;
			border:1px dashed transparent;
			border-radius:5px;
			outline:none;
			cursor:default;
		}
		.finishing-paste-zone:focus,
		.finishing-paste-zone.is-paste-target{
			border-color:#5b8def;
			background:#f4f8ff;
		}
		.finishing-paste-hint{
			white-space:nowrap;
		}
		.finishing-paste-catcher{
			position:absolute;
			width:1px;
			height:1px;
			padding:0;
			border:0;
			overflow:hidden;
			opacity:0;
			pointer-events:none;
		}
	</style>
	<table class="table table-striped table-sm" id="tablefinish">
		<thead>
			<tr>
				<td width="10" class="p-0">
					<button class="btn btn-default btn-sm add_finishing" id="add_finishing" type="button" data-toggle="tooltip" title="Tambah"><i class="fa fa-plus-circle"></i></button>
					<button class="btn btn-default btn-sm delete_finishing" id="delete_finishing" type="button" data-toggle="tooltip" title="Hapus" style="display:none"><i class="fa fa-minus-circle"></i></button>
				</td>
				<td>Keterangan</td>
				<td width="220">Gambar Screenshoot</td>
			</tr>
		</thead>
		<?php 
			if(!empty($finishing)){
				$na = 0; 
				foreach($finishing->data AS $key=>$val) {
					$gambar_val = !empty($val->gambar) ? $val->gambar : '';
					if($val->id==0){
						$readonly = 'disabled';
						}else{
						$readonly = '';
					}
				?>
				<tr id='rowCountFinish<?=$na;?>' class="rowCountFinish" >
					<td align="center">
						<input type="hidden" id="id_finish_<?=$val->id;?>" name="id_finish[<?=$na;?>]" value="<?=$val->id;?>" />
						<input type="checkbox" class="case_finish" id="case_finish<?=$na;?>" <?=$readonly;?> >
					</td>
					<td>
						<div class="form-group p-0 m-0">
							<input class="form-control input-sm" type='text' id='isi_<?=$na;?>' name="isi[<?=$na;?>]" value="<?=$val->isi;?>" placeholder="keterangan"  />
						</div>
					</td>
					<td>
						<input type="hidden" id="gambar_<?=$na;?>" name="gambar[<?=$na;?>]" value="<?=htmlspecialchars($gambar_val, ENT_QUOTES, 'UTF-8');?>">
						<div class="d-flex align-items-center flex-wrap finishing-paste-zone" data-row="<?=$na;?>" tabindex="0" style="gap:.35rem;">
							<span class="finishing-paste-catcher" contenteditable="true" tabindex="-1" aria-hidden="true"></span>
							<div class="finishing-action-buttons">
								<button type="button" class="btn btn-outline-primary btn-sm flat btn-upload-finishing" data-row="<?=$na;?>">
									<i class="fa fa-image"></i> Upload
								</button>
							</div>
							<a id="lihat_gambar_<?=$na;?>" class="thumb-preview-finishing btn-preview-finishing<?=empty($gambar_val) ? ' d-none' : '';?>" href="<?=empty($gambar_val) ? '#' : base_url($gambar_val);?>">
								<img id="thumb_gambar_<?=$na;?>" src="<?=empty($gambar_val) ? '' : base_url($gambar_val);?>" alt="Thumbnail finishing"<?=empty($gambar_val) ? ' style="display:none;"' : '';?>>
							</a>
							<small class="text-muted" id="label_gambar_<?=$na;?>"><?=empty($gambar_val) ? 'Belum ada gambar' : basename($gambar_val);?></small>
							<small class="text-muted finishing-paste-hint"><i class="fa fa-clipboard"></i> Klik lalu Ctrl+V</small>
						</div>
					</td>
				</tr>
			<?php $na++; } }else{ ?>
			<tr id='rowCountFinish<?=$na;?>' class="rowCountFinish" >
				<td align="center">
					<input type="hidden" id="id_finish_0" name="id_finish[0]" value="0" />
					<input type="checkbox" class="case_finish" id="case_finish0" name="case_finish0" disabled >
				</td>
					<td>
						<div class="form-group p-0 m-0">
							<input class="form-control input-sm" type='text' id='isi_0' name="isi[0]" value="" placeholder="keterangan" required />
						</div>
					</td>
					<td>
						<input type="hidden" id="gambar_0" name="gambar[0]" value="">
						<div class="d-flex align-items-center flex-wrap finishing-paste-zone" data-row="0" tabindex="0" style="gap:.35rem;">
							<span class="finishing-paste-catcher" contenteditable="true" tabindex="-1" aria-hidden="true"></span>
							<div class="finishing-action-buttons">
								<button type="button" class="btn btn-outline-primary btn-sm flat btn-upload-finishing" data-row="0">
									<i class="fa fa-image"></i> Upload 
								</button>
							</div>
							<a id="lihat_gambar_0" class="thumb-preview-finishing btn-preview-finishing d-none" href="#">
								<img id="thumb_gambar_0" src="" alt="Thumbnail finishing" style="display:none;">
							</a>
							<small class="text-muted" id="label_gambar_0">Belum ada gambar</small>
							<small class="text-muted finishing-paste-hint"><i class="fa fa-clipboard"></i> Klik lalu Ctrl+V</small>
						</div>
					</td>
			</tr>
		<?php } ?>
		<tfoot>
			<tr>
				<td colspan="3"></td>
			</tr>
		</tfoot>
	</table>
	<div class="modal fade" id="modal-preview-finishing" tabindex="-1" role="dialog" aria-hidden="true">
		<div class="modal-dialog modal-dialog-centered modal-lg" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title">Preview Gambar Finishing</h5>
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
				</div>
				<div class="modal-body text-center">
					<img id="img-preview-finishing" src="" alt="Preview Finishing" style="max-width:100%;max-height:70vh;height:auto;">
				</div>
			</div>
		</div>
	</div>
	<input type="file" id="finishing_upload_input" accept=".jpg,.jpeg,.png,.webp,image/*" style="display:none;">
	 
	<script>
		var rowCount = 0;
		$(".add_finishing").on('click', function() {
			i = $('#tablefinish > tbody tr').length;
			count=$('#tablefinish tr').length;	
			// alert(i);
			// i = document.getElementById("baris").value;
			if(i >= 12){
				sweet('Peringatan!!!','Max per trx hanya 12 product','warning','warning');	
				return;
			}
			var cols = '<tbody>';
			cols += '<tr id="rowCountFinish' + i + '" class="rowCountFinish">';
			cols += '<td align="center"><input type="hidden" id="id_finish_'+i+'" name="id_finish['+i+']" value="'+i+'"/><input type="checkbox" class="case_finish" id="case_finish'+i+'" /></td>';
			cols += '<td><div class="form-group p-0 m-0"><input class="form-control input-sm" type="text" name="isi['+i+']" id="isi_'+i+'" placeholder="keterangan" required /></div></td>';
			cols += '<td><input type="hidden" id="gambar_'+i+'" name="gambar['+i+']" value=""><div class="d-flex align-items-center flex-wrap finishing-paste-zone" data-row="'+i+'" tabindex="0" style="gap:.35rem;"><span class="finishing-paste-catcher" contenteditable="true" tabindex="-1" aria-hidden="true"></span><div class="finishing-action-buttons"><button type="button" class="btn btn-outline-primary btn-sm flat btn-upload-finishing" data-row="'+i+'"><i class="fa fa-image"></i> Upload</button></div><a id="lihat_gambar_'+i+'" class="thumb-preview-finishing btn-preview-finishing d-none" href="#"><img id="thumb_gambar_'+i+'" src="" alt="Thumbnail finishing" style="display:none;"></a><small class="text-muted" id="label_gambar_'+i+'">Belum ada gambar</small><small class="text-muted finishing-paste-hint"><i class="fa fa-clipboard"></i> Klik lalu Ctrl+V</small></div></td>';
			cols += '</tr></tbody>';
			$('#tablefinish').append(cols);
			i++;
			$(".delete_finishing").on('click', function() {
				
				x = $("#tablefinish > tbody").children().length;
				for (var aa = 0; aa < x; aa++) {
					if ($('#case_finish' + aa).length) {
						if (document.getElementById("case_finish" + aa.toString()).checked == true) {
							kode_rinci = document.getElementById("id_finish_" + aa.toString()).value;
							hapus_finishing(kode_rinci);
							// console.log(kode_rinci);
							jQuery('#rowCountFinish' + aa.toString()).remove();aa--;
						}
					}
				}
				$(".delete_finishing").hide();
				$(".add_finishing").show();
				$("#tablefinish tr.rowCount input:checkbox").attr("disabled", false);
			});
			
			$('#tablefinish >tbody input[type="checkbox"]').click(function() {
				var rowCount = $("#tablefinish > tbody").children().length;
				var countcheck = $('#tablefinish > tbody input[type="checkbox"]:checked').length;
				if (countcheck == 0) {
					$(".delete_finishing").hide();
					$(".add_finishing").show();
				}
				if (countcheck > 0) {
					$(".delete_finishing").show();
					$(".add_finishing").hide();
					shortcut.add("ctrl+d",function() {
						$(".btn_Delete").click();
					});
				}
				if (countcheck >= rowCount) {
					sweet('Peringatan!!!','Sisain satu baris jangan di hapus semua','warning','warning');
					$(".delete_finishing").css("color", "#000");
					$("#delete_finishing").attr("disabled", true);
					$('input[name=case_finish0]').attr('checked', false);
					} else {
					$(".delete_finishing").css("color", "#ff0000");
					$(".delete_finishing").attr("disabled", false);
					
				}
			});
		});
		
		
		
		$(".delete_finishing").on('click', function() {
			
			b = $("#tablefinish > tbody").children().length;
			for (var aa = 0; aa < b; aa++) {
				if ($('#case_finish' + aa).length) {
					if (document.getElementById("case_finish" + aa.toString()).checked == true) {
						kode_rinci = document.getElementById("id_finish_" + aa.toString()).value;
						hapus_finishing(kode_rinci);
						// console.log(kode_rinci);
						jQuery('#rowCountFinish' + aa.toString()).remove();aa--;
					}
				}
			}
			$(".delete_finishing").hide();
			$(".add_finishing").show();
			$("#tablefinish tr.rowCount input:checkbox").attr("disabled", false);
		});
		
		$('#tablefinish >tbody input[type="checkbox"]').click(function() {
			var rowCount = $("#tablefinish > tbody").children().length;
			var countcheck = $('#tablefinish > tbody input[type="checkbox"]:checked').length;
			if (countcheck == 0) {
				$(".delete_finishing").hide();
				$(".add_finishing").show();
			}
			if (countcheck > 0) {
				$(".delete_finishing").show();
				$(".add_finishing").hide();
				shortcut.add("ctrl+d",function() {
					$(".btn_Delete").click();
				});
			}
			if (countcheck >= rowCount) {
				sweet('Peringatan!!!','Sisain satu baris jangan di hapus semua','warning','warning');
				$(".delete_finishing").css("color", "#000");
				$("#delete_finishing").attr("disabled", true);
				$('input[name=case_finish0]').attr('checked', false);
				} else {
				$(".delete_finishing").css("color", "#ff0000");
				$(".delete_finishing").attr("disabled", false);
				
			}
		});

		var uploadTargetRow = null;
		var clipboardTargetRow = null;
		var uploadInProgress = false;

		function baseFileName(name) {
			if (!name) return 'finishing';
			return name.replace(/\.[^/.]+$/, '');
		}

		function prepareFinishingUploadFile(fileObj) {
			var dfd = $.Deferred();

			// Selalu usahakan kirim WebP saat browser mendukung.
			if (!fileObj || !/^image\//i.test(fileObj.type || '') || typeof FileReader === 'undefined') {
				dfd.resolve({
					file: fileObj,
					filename: fileObj ? fileObj.name : 'finishing.jpg'
				});
				return dfd.promise();
			}

			var reader = new FileReader();
			reader.onload = function(e) {
				var img = new Image();
				img.onload = function() {
					var maxSide = 1600;
					var ratio = Math.min(maxSide / img.width, maxSide / img.height, 1);
					var newW = Math.max(1, Math.round(img.width * ratio));
					var newH = Math.max(1, Math.round(img.height * ratio));

					var canvas = document.createElement('canvas');
					canvas.width = newW;
					canvas.height = newH;
					var ctx = canvas.getContext('2d');
					ctx.drawImage(img, 0, 0, newW, newH);

					if (!canvas.toBlob) {
						dfd.resolve({ file: fileObj, filename: fileObj.name });
						return;
					}

					canvas.toBlob(function(blob) {
						if (!blob || blob.size <= 0) {
							dfd.resolve({ file: fileObj, filename: fileObj.name });
							return;
						}

						var optimizedName = baseFileName(fileObj.name) + '.webp';
						var finalFile = blob;
						if (typeof File !== 'undefined') {
							try {
								finalFile = new File([blob], optimizedName, {
									type: 'image/webp',
									lastModified: Date.now()
								});
							} catch (err) {
								finalFile = blob;
							}
						}

						dfd.resolve({
							file: finalFile,
							filename: optimizedName
						});
					}, 'image/webp', 0.82);
				};

				img.onerror = function() {
					dfd.resolve({ file: fileObj, filename: fileObj.name });
				};

				img.src = e.target.result;
			};

			reader.onerror = function() {
				dfd.resolve({ file: fileObj, filename: fileObj.name });
			};

			reader.readAsDataURL(fileObj);
			return dfd.promise();
		}

		function setFinishingPreview(rowId, href, thumbSrc, visible) {
			var $lihat = $('#lihat_gambar_' + rowId);
			var $thumb = $('#thumb_gambar_' + rowId);
			if (!$lihat.length || !$thumb.length) {
				return;
			}
			if (visible) {
				$lihat.attr('href', href || '#').removeClass('d-none');
				$thumb.attr('src', thumbSrc || href || '').show();
				return;
			}
			$lihat.attr('href', '#').addClass('d-none');
			$thumb.attr('src', '').hide();
		}

		function uploadFinishingFileByRow(rowId, fileObj, $btn) {
			if (!fileObj) {
				return;
			}

			var $label = $('#label_gambar_' + rowId);
			var btnHtml = $btn.length ? $btn.html() : '';
			var prevHref = $('#lihat_gambar_' + rowId).attr('href') || '#';
			var prevThumbSrc = $('#thumb_gambar_' + rowId).attr('src') || '';
			var prevLabel = $label.text() || '';
			var prevPath = $('#gambar_' + rowId).val() || '';
			var localPreviewUrl = '';

			uploadInProgress = true;
			if ($btn.length) {
				$btn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> Uploading...');
			}
			if ($label.length) {
				$label.text('Menyiapkan file...');
			}

			if (window.URL && URL.createObjectURL) {
				localPreviewUrl = URL.createObjectURL(fileObj);
				setFinishingPreview(rowId, localPreviewUrl, localPreviewUrl, true);
			}

			prepareFinishingUploadFile(fileObj).done(function(payload) {
				var formData = new FormData();
				formData.append('gambar', payload.file, payload.filename || fileObj.name);

				$.ajax({
					url: base_url + "produk/upload_finishing_image",
					type: "POST",
					data: formData,
					processData: false,
					contentType: false,
					dataType: "json",
					xhr: function() {
						var xhr = $.ajaxSettings.xhr();
						if (xhr.upload) {
							xhr.upload.addEventListener('progress', function(evt) {
								if (evt.lengthComputable && $label.length) {
									var pc = Math.round((evt.loaded / evt.total) * 100);
									$label.text('Uploading... ' + pc + '%');
								}
							}, false);
						}
						return xhr;
					},
					success: function(res) {
						if (!res || res.status !== true) {
							showNotif('top-right', 'Upload gambar', (res && res.msg) ? res.msg : 'Upload gagal', 'warning');
							if (prevPath) {
								setFinishingPreview(rowId, prevHref, prevThumbSrc || prevHref, true);
								$label.text(prevLabel);
							} else {
								setFinishingPreview(rowId, '#', '', false);
								$label.text('Belum ada gambar');
							}
							return;
						}

						$('#gambar_' + rowId).val(res.path);
						$label.text(res.display_name ? res.display_name : (res.name || (payload.filename || fileObj.name)));
						setFinishingPreview(rowId, res.url, res.url, true);
					},
					error: function(xhr, ajaxOptions, thrownError) {
						if (prevPath) {
							setFinishingPreview(rowId, prevHref, prevThumbSrc || prevHref, true);
							$label.text(prevLabel);
						} else {
							setFinishingPreview(rowId, '#', '', false);
							$label.text('Belum ada gambar');
						}
						sweet('Peringatan!!!', thrownError, 'warning', 'warning');
					},
					complete: function() {
						if (localPreviewUrl && window.URL && URL.revokeObjectURL) {
							URL.revokeObjectURL(localPreviewUrl);
						}
						if ($btn.length) {
							$btn.prop('disabled', false).html(btnHtml);
						}
						uploadTargetRow = null;
						uploadInProgress = false;
						$('#finishing_upload_input').val('');
					}
				});
			}).fail(function() {
				if (prevPath) {
					setFinishingPreview(rowId, prevHref, prevThumbSrc || prevHref, true);
					$label.text(prevLabel);
				} else {
					setFinishingPreview(rowId, '#', '', false);
					$label.text('Belum ada gambar');
				}
				if ($btn.length) {
					$btn.prop('disabled', false).html(btnHtml);
				}
				uploadTargetRow = null;
				uploadInProgress = false;
			});
		}

		$(document).off('click.finishingUpload', '.btn-upload-finishing').on('click.finishingUpload', '.btn-upload-finishing', function() {
			if (uploadInProgress) {
				showNotif('top-right', 'Upload gambar', 'Masih ada proses upload berjalan', 'warning');
				return;
			}
			uploadTargetRow = $(this).attr('data-row');
			$('#finishing_upload_input').val('');
			$('#finishing_upload_input').trigger('click');
		});

		$(document)
			.off('focusin.finishingPaste click.finishingPaste', '.finishing-paste-zone')
			.on('focusin.finishingPaste click.finishingPaste', '.finishing-paste-zone', function(event) {
				clipboardTargetRow = $(this).attr('data-row');
				$('.finishing-paste-zone').removeClass('is-paste-target');
				$(this).addClass('is-paste-target');
				if (event.type === 'click' && !$(event.target).closest('button,a,input').length) {
					var catcher = $(this).find('.finishing-paste-catcher').get(0);
					if (catcher) {
						catcher.focus();
					} else {
						this.focus();
					}
				}
			});

		$(document).off('paste.finishingPaste').on('paste.finishingPaste', function(event) {
			if (!$('#tablefinish').length || !$('#DetailCart').is(':visible')) {
				return;
			}

			var originalEvent = event.originalEvent || event;
			var clipboardData = originalEvent.clipboardData || window.clipboardData;
			var items = clipboardData && clipboardData.items ? clipboardData.items : [];
			var files = clipboardData && clipboardData.files ? clipboardData.files : [];
			var imageFile = null;

			for (var itemIndex = 0; itemIndex < items.length; itemIndex++) {
				var item = items[itemIndex];
				if (item && item.kind === 'file') {
					var itemFile = item.getAsFile();
					var itemType = (item.type || (itemFile && itemFile.type) || '').toLowerCase();
					var itemName = (itemFile && itemFile.name ? itemFile.name : '').toLowerCase();
					if (itemFile && (/^image\//i.test(itemType) || /\.(?:jpe?g|png|webp)$/i.test(itemName))) {
						imageFile = itemFile;
						break;
					}
				}
			}

			// Beberapa browser/aplikasi menaruh gambar hasil Ctrl+V di files,
			// bukan di items. Gunakan files sebagai fallback agar transaksi baru
			// tetap dapat menerima screenshot maupun gambar dari File Explorer.
			if (!imageFile) {
				for (var fileIndex = 0; fileIndex < files.length; fileIndex++) {
					var clipboardFile = files[fileIndex];
					var fileType = (clipboardFile && clipboardFile.type ? clipboardFile.type : '').toLowerCase();
					var fileName = (clipboardFile && clipboardFile.name ? clipboardFile.name : '').toLowerCase();
					if (clipboardFile && (/^image\//i.test(fileType) || /\.(?:jpe?g|png|webp)$/i.test(fileName))) {
						imageFile = clipboardFile;
						break;
					}
				}
			}

			if (!imageFile) {
				if (clipboardTargetRow !== null && clipboardTargetRow !== undefined) {
					event.preventDefault();
					showNotif('top-right', 'Paste gambar', 'Clipboard tidak berisi file gambar. Salin gambar atau screenshot, lalu tekan Ctrl+V.', 'warning');
				}
				return;
			}

			if (uploadInProgress) {
				event.preventDefault();
				showNotif('top-right', 'Paste gambar', 'Masih ada proses upload berjalan', 'warning');
				return;
			}

			var $eventRow = $(event.target).closest('.rowCountFinish');
			var rowId = null;
			if ($eventRow.length) {
				rowId = $eventRow.attr('id').replace('rowCountFinish', '');
			} else if (clipboardTargetRow !== null && clipboardTargetRow !== undefined) {
				rowId = clipboardTargetRow;
			} else {
				var $zones = $('#tablefinish .finishing-paste-zone');
				if ($zones.length === 1) {
					rowId = $zones.attr('data-row');
				}
			}

			if (rowId === null || rowId === undefined || !$('#gambar_' + rowId).length) {
				event.preventDefault();
				showNotif('top-right', 'Paste gambar', 'Klik area gambar pada baris tujuan, lalu tekan Ctrl+V', 'warning');
				return;
			}

			event.preventDefault();
			clipboardTargetRow = rowId;
			$('.finishing-paste-zone').removeClass('is-paste-target');
			$('.finishing-paste-zone[data-row="' + rowId + '"]').addClass('is-paste-target');

			var mimeType = (imageFile.type || '').toLowerCase();
			var extension = mimeType === 'image/jpeg' ? 'jpg' : (mimeType === 'image/webp' ? 'webp' : 'png');
			var clipboardName = 'clipboard-finishing-' + Date.now() + '.' + extension;
			if (typeof File !== 'undefined') {
				try {
					imageFile = new File([imageFile], clipboardName, {
						type: imageFile.type || 'image/png',
						lastModified: Date.now()
					});
				} catch (fileError) {}
			}

			var $btn = $('.btn-upload-finishing[data-row="' + rowId + '"]');
			uploadFinishingFileByRow(rowId, imageFile, $btn);
		});

		$(document).off('click.finishingPreview', '.btn-preview-finishing').on('click.finishingPreview', '.btn-preview-finishing', function(e) {
			e.preventDefault();
			var src = $(this).attr('href') || '';
			if (!src || src === '#') {
				return;
			}
			$('#img-preview-finishing').attr('src', src);
			$('#modal-preview-finishing').modal('show');
		});

		$(document).off('change.finishingUpload', '#finishing_upload_input').on('change.finishingUpload', '#finishing_upload_input', function() {
			if (uploadTargetRow === null || uploadTargetRow === undefined) {
				return;
			}

			var fileObj = this.files && this.files[0] ? this.files[0] : null;
			if (!fileObj) {
				return;
			}

			var rowId = uploadTargetRow;
			var $btn = $('.btn-upload-finishing[data-row="' + rowId + '"]');
			uploadFinishingFileByRow(rowId, fileObj, $btn);
		});

		function hapus_finishing(c) {
			var id = c;
			var kode = $('#kode').val();
			$.ajax({
				type: "POST",
				url: base_url + "produk/hapus_finishing",
				data: { id: id,kode:kode },
				dataType: "json",
				success: function(data) {
					// alert(str);
				},
				error: function (xhr, ajaxOptions, thrownError) {
					sweet('Peringatan!!!',thrownError,'warning','warning');
				}
			});
		}
	</script>
<?php } ?>
<script>
	var produk = '<?=$title_produk;?>';
	$('.title-finishing').html(produk);
</script>
