@extends('layout')


@section('content')

<div class="page-header page-header-default">
	<div class="breadcrumb-line">
		<ul class="breadcrumb">
			<li><a href="index.html"><i class="icon-home2 position-left"></i> Master</a></li>
			<li class="active">Harga Barang</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Harga Barang</h5>
			<div class="heading-elements">
				<ul class="icons-list">
		      		<li><a data-action="collapse"></a></li>
		      	</ul>
	       	</div>
	    </div>
	    <div class="panel-body">
	    	<button class="btn btn-primary" id="btn_tambah_satuan"><i class=""></i>Tambah</button>
	    </div>

	    <table class="table table_satuan table-stripped">
	    	<thead>
				<tr>
					<th>No</th>
					<th>Tanggal</th>
					<th>Barang</th>
					{{-- <th>Warna</th> --}}
					<th>Proses</th>
					<th>Corak</th>
					<th>Motif</th>
					<th>Harga</th>
					<th class="text-center">Aksi</th>
				</tr>
			</thead>
			<tbody>
			</tbody>
	    </table>
	</div>
</div>

<div class="modal fade" id="exampleModal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog" role="document">
	    <div class="modal-content">
	    	<div class="modal-header">
	    		<h5 class="modal-title" id="exampleModalLabel">Data</h5>
	    		<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span></button>
	      	</div>
	      	<div class="modal-body">
	      		<form class="form-horizontal" id="form_popup" autocomplete="off">
	      			<input type="hidden" name="id">
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Tanggal</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_tanggal" class="form-control datepicker" value="{{ date("d-m-Y") }}" id="popup_tanggal" readonly>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Nama Barang</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_barang" id="popup_barang">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Proses Produksi</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_proses" id="popup_proses">
	      						
	      					</select>
	      				</div>
	      			</div>     			
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Corak</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_corak" id="popup_corak">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Motif</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_motif" id="popup_motif">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			{{-- <div class="form-group">
	      				<label class="control-label col-sm-4">Warna</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_warna" id="popup_warna">
	      						
	      					</select>
	      				</div>
	      			</div> --}}
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Satuan</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_satuan" id="popup_satuan">
	      						
	      					</select>
	      				</div>
	      			</div>

	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Harga</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_harga" class="form-control" id="popup_harga">
	      					<p class="help-block pull-right" id="help_popup_harga"></p>
	      				</div>
	      			</div>                  
	      		</form>
	      	</div>
	      	<div class="modal-footer">
	        	<button type="button" class="btn btn-primary" id="btn_popup_simpan">Simpan</button>
	      	</div>
	    </div>
	</div>
</div>

<script type="text/javascript">
	var table_satuan;

	$(document).ready(function(){
		table_satuan = $(".table_satuan").DataTable({
			processing: true,
	        serverSide: true,
	        ajax: '{{ url('harga/get_data') }}',
	        searchDelay : 2000,
	        columns: [
	            {data: 'id_harga', name: 'id_harga', orderable: false, searchable: false, render : function(data, type, row, meta){
			  		return meta.row+1;
			  	}},
			  	{data: 'tgl_input', name: 'tgl_input'},
	            {data: 'nama_barang', name: 'nama_barang'},
	            // {data: 'inisial_komposisi', name: 'inisial_komposisi'},
	            {data: 'nama_proses', name: 'nama_proses'},
	            {data: 'nama_corak', name: 'nama_corak'},
	            {data: 'nama_motif', name: 'nama_motif'},
	            {data: 'harga', name: 'harga'},
	            {data: 'aksi', name: 'aksi', orderable: false, searchable: false}
	        ],
	        createdRow: function(row, data, index){
	        	$('td', row).eq(6).html(accounting.formatMoney(data.harga)).attr('align','right'); // 6 is index of column
	        }
		});
		
		get_proses();
		get_satuan();
		get_motif();
		get_corak();
		get_barang();
		// get_warna();
	})
	$("#btn_tambah_satuan").click(function(){
		$("[name=id]").val('');
		$("[name=popup_tanggal]").val('{{ date('d-m-Y') }}').change();
		$("[name=popup_barang]").val(null).trigger('change');
		$("[name=popup_proses]").val('').trigger('change');
		$("[name=popup_harga]").val('0');
		$("[name=popup_satuan]").val(null).trigger('change');
		$("[name=popup_motif]").val(null).trigger('change');
		$("[name=popup_corak]").val(null).trigger('change');
		$("[name=popup_lokasi]").val(null).trigger('change');
		// $("[name=popup_warna]").val(null).trigger('change');
		rp();

		$("#exampleModal").modal("show");
	})

	function get_edit(id){
		if(id != ''){
			$.ajax({
				url : "{{ url('harga/get_edit') }}",
				type : "POST",
				dataType : "json",
				data : { id : id },
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success : function(respon){
					if(respon.status == 1){
						edit(respon.data);
					}else{
						swal("", "Gagal mendapat data", "warning");
					}
				}
			})
		}
	}
	

	function edit(data){
		$("[name=id]").val(data.id_harga);
		$("[name=popup_proses]").val(data.id_proses).trigger('change');
		
		$("[name=popup_motif]").val(data.id_motif).trigger('change');
		$("[name=popup_corak]").val(data.id_corak).trigger('change');
		$("[name=popup_barang]").val(data.id_barang).trigger('change');
		$("[name=popup_satuan]").val(data.id_satuan).trigger('change');
		$("[name=popup_warna]").val(data.id_komposisi).trigger('change');
		$("[name=popup_harga]").val(data.harga);
		$("[name=popup_tanggal]").val(data.tgl);

		rp();

		$("#exampleModal").modal('show');

	}

	function hapus(id){
		swal({
			title: 'Anda yakin akan menghapus item ini?',
			type: 'warning',
			showCancelButton: true,
			confirmButtonColor: '#3085d6',
			cancelButtonColor: '#d33',
			confirmButtonText: 'Ya',
			cancelButtonText: 'Tidak',
		}).then((result) => {
			if(result.value){
			 	$.ajax({
					url: "{{ url('harga/hapus')}} ",
					type: 'post',
					data: {id : id},
					headers : {
						'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
					},
					success: function(respon){
						table_satuan.ajax.reload();
					}
				})
			}
		})
	}

	$("#btn_popup_simpan").click(function(){
		var id = $("[name=id]").val();
		var proses = $("[name=popup_proses]").val();
		var satuan = $("[name=popup_satuan]").val();
		var motif = $("[name=popup_motif]").val();
		var corak = $("[name=popup_corak]").val();
		var barang = $("[name=popup_barang]").val();
		var harga = $("[name=popup_harga]").val();

		if(harga != ''){
			$.ajax({
				url: "{{ url('harga/simpan')}} ",
				type: 'post',
				data: $("#form_popup").serialize(),
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success: function(respon){
					if(respon.status == 0){
						swal("", respon.keterangan, "warning");
					}else{
						table_satuan.ajax.reload();
						$("#exampleModal").modal('hide');
					}
				}
			})
		}
	})

	function get_proses(){
		$.ajax({
			url: "{{ url('harga/get_proses') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_proses]").html('');
					$("[name=popup_proses]").append('<option value="" nama="Tanpa Proses">Tanpa Proses</option>');
					for(i in result.data){
						$("[name=popup_proses]").append('<option value="'+result.data[i].id_proses+'" nama="'+result.data[i].nama_proses+'">'+result.data[i].nama_proses+'</option>');
					}
				}
			}
		})
	}

	function get_motif(){
		$.ajax({
			url: "{{ url('harga/get_motif') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_motif]").html('');
					$("[name=popup_motif]").append('<option value="" kode="">--- Pilih Motif ---</option>');
					for(i in result.data){
						$("[name=popup_motif]").append('<option value="'+result.data[i].id_motif+'" nama="'+result.data[i].nama_motif+'">'+result.data[i].nama_motif+'</option>');
					}
				}
			}
		})
	}

	function get_corak(){
		$.ajax({
			url: "{{ url('harga/get_corak') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_corak]").html('');
					$("[name=popup_corak]").append('<option value="" kode="">--- Pilih Corak ---</option>');
					for(i in result.data){
						$("[name=popup_corak]").append('<option value="'+result.data[i].id_motif_barang+'" nama="'+result.data[i].motif_keterangan+'">'+result.data[i].motif_keterangan+'</option>');
					}
				}
			}
		})
	}

	function get_satuan(){
		$.ajax({
			url: "{{ url('harga/get_satuan') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_satuan]").html('');
					$("[name=popup_satuan]").append('<option value="" kode="">--- Pilih Satuan ---</option>');
					for(i in result.data){
						$("[name=popup_satuan]").append('<option value="'+result.data[i].id_satuan+'" nama="'+result.data[i].nama_satuan+'">'+result.data[i].nama_satuan+'</option>');
					}
				}
			}
		})
	}

	function get_warna(){
		$.ajax({
			url: "{{ url('harga/get_warna') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_warna]").html('');
					$("[name=popup_warna]").append('<option value="" kode="">--- Pilih Warna ---</option>');
					for(i in result.data){
						$("[name=popup_warna]").append('<option value="'+result.data[i].id_komposisi+'" nama="'+result.data[i].nama+'">'+result.data[i].nama+'</option>');
					}
				}
			}
		})
	}

	function get_barang(){
		$.ajax({
			url: "{{ url('harga/get_barang') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_barang]").html('');
					$("[name=popup_barang]").append('<option value="" kode="">--- Pilih barang ---</option>');
					for(i in result.data){
						$("[name=popup_barang]").append('<option value="'+result.data[i].id+'" nama="'+result.data[i].nama_barang+'" id_satuan="'+result.data[i].id_satuan+'">'+result.data[i].nama_kategori+' - '+result.data[i].nama_sub+' - '+result.data[i].nama_barang+'</option>');
					}
				}
			}
		})
	}

	$("[name=popup_harga]").keyup(function(){
		var rp = $("[name=popup_harga]").val();
		if(rp == '' || rp == 0 || isNaN(rp)){
			rp = parseInt(0);
		}
		$("#help_popup_harga").text(curr(rp));
		
	})

	function rp(){
		var rp = $("[name=popup_harga]").val();
		if(rp == '' || rp == 0 || isNaN(rp)){
			rp = parseInt(0);
		}
		$("#help_popup_harga").text(curr(rp));
	}

	$("[name=popup_barang]").change(function(){
		var id_satuan = $("[name=popup_barang] :selected").attr("id_satuan");
		var id = $(this).val();
		var id_data = $("[name=id]").val();
		if(id != ''){
			$("[name=popup_satuan]").val(id_satuan).trigger('change');
		}
	})
</script>

@endsection