@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">Warna</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Warna</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" type="button" id="btn_tambah_gudang">Tambah</button>
	    </div>
	    <table class="table table-striped tabel_warna">
	    	<thead>
				<tr>
					<th>No</th>
					<th>Warna</th>
					<th class="text-center">Aksi</th>
				</tr>
			</thead>
			<tbody>
				
			</tbody>
	    </table>
	</div>
</div>

<div class="modal fade" id="exampleModal" tabindex="-1" 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" autocomplete="off" id="form_warna">
	      			<input type="hidden" name="id_warna">
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Warna</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_warna" class="form-control">
	      				</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 tabel_warna;

	$(document).ready(function(){
		tabel_warna = $(".tabel_warna").DataTable({
			processing: true,
	        serverSide: true,
	        ajax: '{{ url('warna/get_warna') }}',
	        columns: [
	            {data: 'rownum', name: 'rownum', orderable: false, searchable: false, render : function(data, type, row, meta){
			  		return meta.row+1;
			  	}},
	            {data: 'warna', name: 'warna'},
	            {data: 'aksi', name: 'aksi', orderable: false, searchable: false}
	        ],
		});

		// tabel_warna.on( 'order.dt search.dt', function () {
  //           tabel_warna.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
  //               cell.innerHTML = i+1;
  //           } );
  //       } ).draw();
  //       tabel_warna.clear().draw();

  //       get_warna();
	})

	$("#btn_tambah_gudang").click(function(){
		$("[name=id_warna]").val('');
		$("[name=popup_warna]").val('');
		$("#exampleModal").modal('show');
	})

	function get_warna(){
		$.ajax({
			url: "{{ url('warna/get_warna')}}  ",
			type: 'get',
			dataType: 'json',
			success: function(respon){
				tabel_warna.clear().draw();
				for(i in respon){
					tabel_warna.row.add(['<div><center></center></div>',
						'<div>'+respon[i].warna+'</div>',
						'<div><center>'+respon[i].aksi+'</center></div>'+
						'<input type="hidden" id="table_idwarna'+respon[i].id_warna+'" value="'+respon[i].id_warna+'">'+
						'<input type="hidden" id="table_nama'+respon[i].id_warna+'" value="'+respon[i].warna+'">'
						]).draw(false);
				}
			}
		})
	}


	function edit(id){
		$("[name=popup_warna]").val($("#table_nama"+id).val());
		$("[name=id_warna]").val(id);

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

	$("#btn_popup_simpan").click(function(){
		var id = $("[name=id_warna]").val();
		var nama = $("[name=popup_warna]").val();

		if(nama != ''){
			$.ajax({
				url: "{{ url('warna/simpan') }}",
				type: 'post',
				dataType: 'json',
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				data: $("#form_warna").serialize(),
				success: function(respon){
					tabel_warna.ajax.reload();
					$("#exampleModal").modal('hide');
				}
			})
		}
	})

	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('warna/hapus')}} ",
					type: 'post',
					data: {id : id},
					headers : {
						'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
					},
					success: function(respon){
						tabel_warna.ajax.reload();
					}
				})
		 	}
		})
	}
</script>

@endsection