<style type="text/css">
table {
	border-collapse: collapse;
}

th {
	border: 1px solid #000;
}

.center {
  text-align: center;
  vertical-align: middle;
}

table{
    table-layout: fixed;
}

td{
    word-wrap:break-word
}

.border{
	border: 1px solid #000;
}

.align-right{
	text-align: right;
}

.align-center{
	text-align: center;
}
</style>

<table>
	<tbody>
	<tr>
		<td colspan="10" class="center"><h3><b>KARTU MUTASI PERKATEGORI</b></h3></td>
	</tr>
	@php
	$barang = "Semua Barang";
	if(count($induk['barang']) > 0){
        $barang = $induk['barang']->nama_barang;
    }
    if($induk['tanggal_awal'] != '' && $induk['tanggal_akhir'] != ''){
    	$tgl = date_range($induk['tanggal_awal'], $induk['tanggal_akhir']);
    }
	@endphp
	<tr>
		<td width="5%"></td>
		<td width="15%">Nama Barang</td>
		<td width="5%">:</td>
		<td width="30%">{{ $barang }}</td>

		<td width="15%">Nama Kategori</td>
		<td width="5%">:</td>
		<td colspan="4">{{ ($induk['kategori'] != '') ? $induk['kategori']:" " }}</td>
	</tr>
	<tr>
		<td width="5%"></td>
		<td width="15%">Periode</td>
		<td width="5%">:</td>
		<td width="30%">{{ $tgl }}</td>

		<td width="15%">Warna</td>
		<td width="5%">:</td>
		<td colspan="4">{{ ($induk['nama_komposisi'] != '') ? $induk['nama_komposisi']:"Semua Warna" }}</td>
	</tr>
	<tr>
		<td width="5%"></td>
		<td width="15%">Gudang</td>
		<td width="5%">:</td>
		<td width="30%">{{ ($induk['nama_gudang'] != '') ? $induk['nama_gudang']:"-" }}</td>

		<td width="15%">Lokasi</td>
		<td width="5%">:</td>
		<td colspan="4">{{ ($induk['nama_lokasi'] != '') ? $induk['nama_lokasi']:$induk['lokasi_gudang'] }}</td>
	</tr>

	<tr>
		@php
		if($induk['nama_proses'] != ''){
		@endphp
		<td width="5%"></td>
		<td width="15%">Nama Proses</td>
		<td width="5%">:</td>
		<td width="30%">{{ $induk['nama_proses'] }}</td>
		@php
		}else{
		@endphp
		<td colspan="5"></td>
		@php
		}
		@endphp		
		<td colspan="5"></td>
	</tr>
	</tbody>
</table>
<br>
<table>
	<thead>
	<tr>
		<th class="center">NO</th>
		<th colspan="2" class="center">JENIS BARANG</th>
		<th class="center" colspan="2">NAMA BARANG</th>
		<th class="center" colspan="2">STOK</th>
	</tr>
	</thead>
	<tbody>
		@php 
		$no = 1;
		$jumlah = array();
		if(count($detail) > 0){
			foreach($detail as $k){
				$nama_barang = $k->concat_nama;
				$row = "";
				if($k->id_sort_detail == 0){
					if($k->jumlah_mutasi > 0){
						$row = "
							<tr>
								<td class='center border'>".$no."</td>
								<td colspan='2' class='center border'>".$k->kategori."</td>
								<td colspan='2' class='border'>".$nama_barang."</td>
								<td class='align-right border' colspan='2'>".format_angkakoma($k->jumlah_mutasi)." ".$k->inisial_satuan."</td>
							</tr>";
							$no++;
					}else{
						$row = "";
					}
				}else if($k->id_sort_detail == 1){
					if($k->jumlah_mutasi > 0){
						$row = "
							<tr>
								<th class='center border' colspan='5'>S U B  T O T A L</th>
								<th class='align-right border' colspan='2'>".format_angkakoma($k->jumlah_mutasi)." ".$k->inisial_satuan."</th>
							</tr>";
						if(isset($jumlah[$k->id_satuan])){
	                        $jumlah[$k->id_satuan] = ['nama_satuan' => $k->inisial_satuan, 'sisa' => $jumlah[$k->id_satuan]['sisa']+$k->jumlah_mutasi];
	                    }else{
	                    	$jumlah[$k->id_satuan] = ['nama_satuan' => $k->inisial_satuan, 'sisa' => $k->jumlah_mutasi];
	                    }
					}else{
						
					}

					
				}
				echo $row;
			@endphp
			
			@php
							
			}
		}else{
			echo "
				<tr>
					<td class='center border'></td>
					<td colspan='2' class='center border'></td>
					<td colspan='2' class='border'></td>
					<td class='align-right border' colspan='2'></td>
				</tr>
				";
		}

		$jum = array_merge($jumlah, array());
        $jumlah_total = 0;
        if($jumlah > 0){
            for($i=0; $i<count($jumlah); $i++){
                echo "
					<tr>
						<th class='center border' colspan='5'>T O T A L</th>
						<th class='align-right border' colspan='2'>".format_angkakoma($jum[$i]['sisa'])." ".$jum[$i]['nama_satuan']."</th>
					</tr>";
            }
        }else{
            foreach($jumlah as $j){
                echo "
					<tr>
						<th class='center border' colspan='5'>T O T A L</th>
						<th class='align-right border' colspan='2'>".format_angkakoma($j['sisa'])." ".$j['nama_satuan']."</th>
					</tr>";  
            }  
        }
		@endphp

		
	</tbody>
</table>