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

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Upah Produksi</h5>
			<div class="heading-elements">
				<ul class="icons-list">
		      		<li>
		      			<button type="button" class="btn btn-md btn-primary" onclick="modal_cetak($(this))"><i class="fa fa-print"></i> Cetak</button>
		      		</li>
		      	</ul>
	       	</div>
	    </div>
	    <!-- <div class="panel-body">
	    	<a href="{{url('upahproduksi/tambah')}}" class="btn btn-success" style="margin-bottom: 1%;">Tambah</a>
	    </div> -->

	    <table class="table datatable-basic table-stripped table_bonbahan">
	    	<thead>
				<tr>
					<th>No</th>
					<th class="text-center"></th>
					<th>Tanggal</th>
					<th>Nomor Laporan Hasil</th>
					<th>Nomor</th>
					<th>Tanggal Bayar</th>
					<th>Workorder</th>
					<th>Proses</th>
					<th>Pemroses</th>
					<th>Total</th>
					<!-- <th>Tanggal</th>
					<th>Makelar</th>
					<th>Rekanan</th>
					<th>Proses</th>
					<th>Hari Normal</th>
                    <th>Absen (Hari)</th>
                    <th>Upah Perhari</th>
                    <th>Upah Lembur /Jam</th>
                    <th>Total Upah</th> -->
				</tr>
			</thead>
			<tbody>
				 
			</tbody>
	    </table>
	</div>
</div>

<div class="modal fade in" role="dialog" id="modal-cetak-rekap-upah">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Rekap Upah</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('upahproduksi/cetak_rekap_upah') }}" target="_blank" id="form-cetak-rekap-upah">
      <div class="modal-body">
        <div class="form-group">
        	<label class="control-label col-sm-12 col-md-2">
        		<input type="radio" name="filter_id" value="id_rekanan" class="styled">
        		Rekanan
        	</label>
        	<div class="col-sm-12 col-md-10">
        		<select class="form-control set_select2" name="id_rekanan">
        			<option value="0">Semua</option>
        		</select>
        	</div>
        </div>
        <div class="form-group">
        	<label class="control-label col-sm-12 col-md-2">
        		<input type="radio" name="filter_id" value="id_pegawai" class="styled" checked="">
        		Pegawai
        	</label>
        	<div class="col-sm-12 col-md-10">
        		<select class="form-control set_select2" name="id_pegawai">
        			<option value="0">Semua</option>
        		</select>
        	</div>
        </div>
        <div class="form-group">
        	<label class="control-label col-sm-12 col-md-2">
        		Proses
        	</label>
        	<div class="col-sm-12 col-md-10">
        		<select class="form-control set_select2" name="id_proses">
        			<option value="0">Semua</option>
        		</select>
        	</div>
        </div>
        <div class="form-group">
        	<label class="control-label col-sm-12 col-md-2">Tgl Bayar</label>
        	<div class="col-sm-12 col-md-5">
        		<input type="text" name="tanggal_awal" class="form-control set_datepicker" value="{{ date('d/m/Y') }}" readonly="" required="">
        	</div>
        	<div class="col-sm-12 col-md-5">
        		<input type="text" name="tanggal_akhir" 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">Cetak</button>
      </div>
  	</form>

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

{{-- modal cetak upah --}}
<div class="modal fade in" role="dialog" id="modal_cetak_upah">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="title_upah">Cetak Slip Upah</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('upahproduksi/cetak_upah') }}" target="_blank" id="form_cetak_upah">
      <div class="modal-body">
      	<input type="hidden" name="id_upahproduksi" id="id_upahproduksi">
      	<input type="hidden" name="nomor" id="nomor">
      	<input type="hidden" name="jenis" id="jenis">
      	<input type="hidden" name="idrekanan" id="idrekanan">
      	<input type="hidden" name="idpegawai" id="idpegawai">

        <div class="form-group">
        	<label class="control-label col-sm-12 col-md-2">
        		Pemroses
        	</label>
        	<div class="col-sm-12 col-md-10">
        		<p class="form-control-static" id="nama_pemroses">
	        		Pemroses
	        	</p>
        	</div>
        </div>

        <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-3">
        				<input type="radio" name="filter_cetak" value="nomor" class="styled" checked> Nomor Upah
        			</div>
        			<div class="col-sm-12 col-md-3">
        				<input type="radio" name="filter_cetak" value="pemroses" class="styled"> Pemroses
        			</div>
        		</div>
        	</div>
        </div>

        <div id="text_nomor_upah">
        	<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">
        			<p class="form-control-static" style="font-weight: bold;" id="set_text_nomor_upah">...</p>
        		</div>
        	</div>
        </div>

        <div style="display: none;" id="range_tgl">
	        <div class="form-group">
	        	<label class="control-label col-sm-12 col-md-2">
	        		Proses
	        	</label>
	        	<div class="col-sm-12 col-md-10">
	        		<select class="form-control set_select2" name="id_proses">
	        			<option value="0">Semua</option>
	        		</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_awal" class="form-control set_datepicker" value="{{ date('d/m/Y') }}" readonly="" required="">
	        	</div>
	        	<label class="control-label col-sm-1 col-md-1">sampai</label>
	        	<div class="col-sm-12 col-md-4">
	        		<input type="text" name="tgl_akhir" class="form-control set_datepicker" value="{{ date('d/m/Y') }}" readonly="" required="">
	        	</div>
	        </div>
	        <div class="form-group">
	        	<label class="control-label col-sm-12 col-md-2">&nbsp;</label>
	        	<div class="col-sm-12 col-md-10">
		        	<?php
		        	$arr_opt = ["1"=>"Rekap", "2"=>"Detail"];
		        	?>
		        	<select class="form-control select-search" name="tipe_laporan">
		        		@foreach($arr_opt AS $key=>$value)
		        		<option value="{{$key}}">{{$value}}</option>
		        		@endforeach
		        	</select>
		        </div>
	        </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_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</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('upahproduksi/cetak_upah_tenun') }}" target="_blank" id="form_cetak_upah_tenun">
      <div class="modal-body">
      	<input type="hidden" name="id_upahproduksi" id="id_upahproduksi_tenun">
      	<input type="hidden" name="nomor" id="nomor_tenun">
      	<input type="hidden" name="jenis" id="jenis_tenun">
      	<input type="hidden" name="idrekanan" id="idrekanan_tenun">
      	<input type="hidden" name="idkelompok" id="idkelompok_tenun">
      	<input type="hidden" name="idpegawai" id="idpegawai_tenun">

        <div class="form-group">
        	<label class="control-label col-sm-12 col-md-2">
        		Pemroses
        	</label>
        	<div class="col-sm-12 col-md-10">
        		<p class="form-control-static" id="nama_pemroses_tenun">
	        		Pemroses
	        	</p>
        	</div>
        </div>

        <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-3">
        				<input type="radio" name="filter_cetak_tenun" value="Petenun" class="styled" checked> Petenun
        			</div>
        			<div class="col-sm-12 col-md-3">
        				<input type="radio" name="filter_cetak_tenun" value="Makelar" class="styled"> Makelar
        			</div>
        			<div class="col-sm-12 col-md-3">
        				<input type="radio" name="filter_cetak_tenun" value="Rekap" class="styled"> Rekap
        			</div>
        		</div>
        	</div>
        </div>

        <!-- <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-3">
        				<input type="radio" name="filter_cetak_tenun" value="nomor" class="styled" checked> Nomor Upah
        			</div>
        			<div class="col-sm-12 col-md-3">
        				<input type="radio" name="filter_cetak_tenun" value="pemroses" class="styled"> Pemroses
        			</div>
        			<div class="col-sm-12 col-md-3">
        				<input type="radio" name="filter_cetak_tenun" value="makelar" class="styled"> Makelar
        			</div>
        		</div>
        	</div>
        </div> -->

        <div id="text_nomor_upah_tenun">
        	<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">
        			<p class="form-control-static" style="font-weight: bold;" id="set_text_nomor_upah_tenun">...</p>
        		</div>
        	</div>
        </div>

        <div id="text_makelar">
        	<div class="form-group">
        		<label class="control-label col-sm-12 col-md-2">Makelar</label>
        		<div class="col-sm-12 col-md-10">
        			<p class="form-control-static" style="font-weight: bold;" id="set_text_makelar_tenun">...</p>
        		</div>
        	</div>
        </div>

        <div style="display: none;" id="range_tgl_tenun">
	        <div class="form-group" style="display: none;">
	        	<label class="control-label col-sm-12 col-md-2">
	        		Proses
	        	</label>
	        	<div class="col-sm-12 col-md-10">
	        		<select class="form-control set_select2" name="id_proses">
	        			<option value="37">Tenun</option>
	        		</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_awal" class="form-control set_datepicker" value="{{ date('d/m/Y') }}" readonly="" required="">
	        	</div>
	        	<label class="control-label col-sm-1 col-md-1">sampai</label>
	        	<div class="col-sm-12 col-md-4">
	        		<input type="text" name="tgl_akhir" class="form-control set_datepicker" value="{{ date('d/m/Y') }}" readonly="" required="">
	        	</div>
	        </div>
	        <div class="form-group" style="display: none;">
	        	<label class="control-label col-sm-12 col-md-2">&nbsp;</label>
	        	<div class="col-sm-12 col-md-10">
		        	<?php
		        	$arr_opt = ["1"=>"Rekap", "2"=>"Detail"];
		        	?>
		        	<select class="form-control select-search" name="tipe_laporan">
		        		@foreach($arr_opt AS $key=>$value)
		        		<option value="{{$key}}">{{$value}}</option>
		        		@endforeach
		        	</select>
		        </div>
	        </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>

<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('upahproduksi/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', name: 'tanggal'},
	            {data: 'nomor_upah', name: 'nomor_upah'},
	            {data: 'nomor', name: 'nomor'},
	            {data: 'tanggal_bayar', name: 'tanggal_bayar'},
	            {data: 'workorder', name: 'workorder'},
	            {data: 'proses', name: 'proses'},
	            {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-rekap-upah"));
	}

	function cetakUpahTenun(id, nomor, pemroses, nama_pemroses, makelar, idKelompok, idRekanan, idPegawai) {
		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 cetakUpah(id, nomor, pemroses, nama_pemroses, idRekanan, idPegawai) {
		const filter = $("[name='filter_cetak']:checked").val();
		$("#title_upah").text("Cetak Slip Upah "+pemroses);
		$("#id_upahproduksi").val(id);
		$("#set_text_nomor_upah").html(nomor);
		$("#nomor").val(nomor);
		$("#jenis").val(pemroses);
		$("#idrekanan").val(idRekanan);
		$("#idpegawai").val(idPegawai);
		$("#nama_pemroses").text(nama_pemroses);
		if (filter == "pemroses") {
			$("#range_tgl").show();
			$("#text_nomor_upah").hide();
		}else{
			$("#range_tgl").hide();
			$("#text_nomor_upah").show();
		}
		showModal($("#modal_cetak_upah"));
	}

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


</script>
@endsection