@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> Produksi</a></li>
			<li class="active">Upah Tenun</li>
		</ul>
	</div>
</div>

<div class="content">

	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Slip Upah Tenun</h5>
	    </div>

	    <div class="row">
	    	<div class="col-lg-12">
		    	@if(Session::has('flash_message_error'))
				<div class="alert alert-error alert-block">
					<button type="button" class="close" data-dismiss="alert">x</button>
					<strong>{!! session('flash_message_error') !!}</strong>
				</div>
				@endif
				@if(Session::has('flash_message_success'))
				<div class="alert alert-success alert-block">
					<button type="button" class="close" data-dismiss="alert">x</button>
					<strong>{!! session('flash_message_success') !!}</strong>
				</div>
				@endif 
		    </div>
	    </div>

	    <div class="panel-body">
	    	<a href="{{url('upahproduksitenun/tambah')}}" class="btn btn-success" style="margin-bottom: 1%;"><i class="fa fa-plus"></i> Tambah</a>
	    	<button type="button" class="btn btn-primary" style="margin-bottom: 1%;" onclick="modal_cetak($(this))"><i class="fa fa-print"></i> Cetak Slip Upah</button>
	    </div>

	    <table class="table datatable-basic table-stripped table_bonbahan">
	    	<thead>
				<tr>
					<th>No</th>
					<th class="text-center"></th>
					<th>Tanggal Upah</th>
					<th>Nomor</th>
					<th>Tanggal Bayar</th>
					<th>Pemroses</th>
					<th>Total</th>
				</tr>
			</thead>
			<tbody>
				 
			</tbody>
	    </table>
	</div>
</div>

<div class="modal fade in" role="dialog" id="modal_cetak_upah_tenun">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="title_upah_tenun">Cetak Slip Upah Tenun</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
    
      <form class="form-horizontal" method="get" action="{{ url('upahproduksitenun/cetak/slipupah') }}" target="_blank" id="form_cetak_upah_tenun">
      <div class="modal-body">

        <div class="form-group">
          <label class="control-label col-sm-12 col-md-2">
            Berdasarkan
          </label>
          <div class="col-sm-12 col-md-10 control-label">
            <div class="row">
              <div class="col-sm-12 col-md-6">
                <input type="radio" name="filter_cetak_upah" value="Petenun" class="styled" checked> Makelar (Rekap Perpetenun)
              </div>
              <div class="col-sm-12 col-md-3">
                <input type="radio" name="filter_cetak_upah" value="Makelar" class="styled"> Makelar
              </div>
              <div class="col-sm-12 col-md-3">
                <input type="radio" name="filter_cetak_upah" value="Rekap" class="styled"> Rekap
              </div>
              <div class="col-sm-12 col-md-6">
                <input type="radio" name="filter_cetak_upah" value="Petenunlain" class="styled"> Petenun (Lainnya)
              </div>
            </div>
          </div>
        </div>

        <div class="form-group">
          <label class="control-label col-sm-12 col-md-2">Makelar</label>
          <div class="col-sm-12 col-md-6">
            <select class="form-control select-search" name="makelar">
              <option value="0">--- Pilih Makelar ---</option>
              @foreach($makelar as $key => $makelar)
                <option value="{{$makelar->id_kelompok}}">{{$makelar->nama}}</option>
              @endforeach
            </select>
          </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-12 col-md-2">Tgl. Upah</label>
            <div class="col-sm-12 col-md-4">
              <input type="text" name="tgl_upah" class="form-control set_datepicker" value="{{ date('d/m/Y') }}" readonly="" required="">
            </div>
          </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
        <button type="submit" class="btn btn-primary">Tampilkan</button>
      </div>
    </form>

    </div>
  </div>
</div>

<div class="modal fade in" role="dialog" id="modal_edit_nomor">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="title_upah_tenun">Edit nomor slip upah tenun</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
    
      <form class="form-horizontal" method="post" action="{{ url('upahproduksitenun/edit/nomor') }}" id="form_edit_nomor">{{csrf_field()}}
      <div class="modal-body">
      	<input type="hidden" name="id_upahtenun" id="id_upahtenun" value="">
        <div class="form-group">
          <label class="control-label col-sm-12 col-md-2">
            Nomor Upah
          </label>
          <div class="col-sm-12 col-md-10 control-label">
            <div class="row">
              <div class="col-sm-12 col-md-6">
                <input type="text" name="nomor" id="nomor_upah" value="" class="form-control" required />
              </div>
            </div>
          </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-12 col-md-2">Tgl. Upah</label>
            <div class="col-sm-12 col-md-6">
              <input type="text" name="tgl_upah" id="tgl_upah" class="form-control set_datepicker" value="{{ date('d/m/Y') }}" readonly="" required="">
            </div>
          </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
        <button type="submit" class="btn btn-primary">Simpan</button>
      </div>
    </form>

    </div>
  </div>
</div>

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

	$(document).ready(function(){
		$(".set_select2").select2();
		$(".set_datepicker").datepicker({
			format : "dd/mm/yyyy",
			autoclose : true,
			zOfIndex : 9999,
		});

		$("[name='filter_cetak']").on("change", function(evt){
			evt.preventDefault();
			if ($(this).val() == "pemroses") {
				$("#range_tgl").show();
				$("#text_nomor_upah").hide();
			}else{
				$("#range_tgl").hide();
				$("#text_nomor_upah").show();
			}
		})

		$("[name='filter_cetak_tenun']").on("change", function(evt){
			evt.preventDefault();
			if ($(this).val() == "pemroses") {
				$("#range_tgl_tenun").show();
				$("#text_nomor_upah_tenun").hide();
				$("#text_makelar").hide();
			} else if ($(this).val() == "nomor"){
				$("#range_tgl_tenun").hide();
				$("#text_nomor_upah_tenun").show();
				$("#text_makelar").hide();
			} else if ($(this).val() == "makelar") {
				$("#text_nomor_upah_tenun").hide();
				$("#text_makelar_tenun").show();
				$("#range_tgl_tenun").show();
				$("#text_makelar").show();
			}
		})

		tb = $(".table_bonbahan").DataTable({
			processing: true,
	        serverSide: true,
	        deferRender : true,
	        ajax: '{{ url("upahproduksitenun/get-data") }}',
	        columns: [
	            {data: 'rownum', name: 'rownum', orderable: false, searchable: false, render : function(data, type, row, meta){
			  		return meta.row+1;
			  	}},	            
	            {data: 'aksi', name: 'aksi', orderable: false, searchable: false},
	            {data: 'tanggal_upah', name: 'tanggal_upah'},
	            {data: 'nomor', name: 'nomor'},
	            {data: 'tanggal_bayar', name: 'tanggal_bayar'},
	            {data: 'pemroses', name: 'pemroses'},
	            {data: 'total', name: 'total'},
	        ],
	        createdRow: function(row, data, index) {
                $('td', row).eq(9).html(accounting.formatMoney(data.total)).attr('align','right'); // 6 is index of column
            }
		});

		// select data pegawai
		$.ajax({
			type : "post",
			url : "{{ url('upahproduksi/get_data_pegawai') }}",
			dataType : "json",
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success : function(result){
				$.each(result, function (id, item) {
					var newOption = new Option(item.nomor_pegawai+" - "+item.nama_pegawai, item.id_pegawai, true, true);
					$("#form-cetak-rekap-upah select[name=id_pegawai]").append(newOption).trigger("change");
				});
				$("#form-cetak-rekap-upah select[name=id_pegawai]").val(0).trigger("change");

			}
		});

		// select data rekanan
		$.ajax({
			type : "post",
			url : "{{ url('upahproduksi/get_data_rekanan') }}",
			dataType : "json",
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success : function(result){
				$.each(result, function (id, item) {
					var newOption = new Option(item.nama, item.id_rekanan, true, true);
					newOption.setAttribute("nama",item.nama);
					$("#form-cetak-rekap-upah select[name=id_rekanan]").append(newOption).trigger("change");
				});
				$("#form-cetak-rekap-upah select[name=id_rekanan]").val(0).trigger("change");

			}
		});
		
		// select data proses
		$.ajax({
			type : "post",
			url : "{{ url('upahproduksi/get_data_proses') }}",
			dataType : "json",
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success : function(result){
				var newOption = "";
				$.each(result, function (id, item) {
					if(item.parent>0){
					newOption = new Option(item.nama_proses, item.id_proses, false, false);
					$("#form-cetak-rekap-upah select[name=id_proses]").append(newOption);
					
					newOption = new Option(item.nama_proses, item.id_proses, false, false);
					$("#form_cetak_upah select[name=id_proses]").append(newOption);
					}
				});

				$("#form-cetak-rekap-upah select[name=id_proses]").val(0).trigger("change");
				$("#form_cetak_upah select[name=id_proses]").val(0).trigger("change");

			}
		});
		
	})

	function modal_cetak(this_) {
		showModal($("#modal_cetak_upah_tenun"));
	}

	function cetakUpahTenun(id_upahtenun) {
		const filter = $("[name='filter_cetak_tenun']:checked").val();
		$("#title_upah_tenun").text("Cetak Slip Upah "+pemroses);
		$("#id_upahproduksi_tenun").val(id);
		$("#set_text_nomor_upah_tenun").html(nomor);
		$("#nomor_tenun").val(nomor);
		$("#jenis_tenun").val(pemroses);
		$("#set_text_makelar_tenun").html(makelar);
		$("#idrekanan_tenun").val(idRekanan);
		$("#idkelompok_tenun").val(idKelompok);
		$("#idpegawai_tenun").val(idPegawai);
		$("#nama_pemroses_tenun").text(nama_pemroses);
		if (filter == "pemroses") {
			$("#range_tgl_tenun").show();
			$("#text_nomor_upah_tenun").hide();
			$("#text_makelar").hide();
		} else if (filter == "nomor"){
			$("#range_tgl_tenun").hide();
			$("#text_nomor_upah_tenun").show();
			$("#text_makelar").hide();
		} else if (filter == "makelar") {
			$("#text_nomor_upah_tenun").hide();
			$("#text_makelar_tenun").show();
			$("#range_tgl_tenun").show();
			$("#text_makelar").show();
		}
		showModal($("#modal_cetak_upah_tenun"));
	}

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

	function edit(id, nomor, tanggal)
	{
		$("#id_upahtenun").val(id);
		$("#nomor_upah").val(nomor);
		$("#tgl_upah").val(tanggal);
		showModal($("#modal_edit_nomor"));
	}


</script>
@endsection