<?php
session_start();
/*
try {
    if (isset($_COOKIE["session"])) { 
        //echo 'Cookie is active!';
        $lifetime=60*60;
        setcookie("session", "1", time() + $lifetime, "/");
    } else{ 
        //echo 'Cookie is NOT active!';
        header("Location: /login.php"); 
    }
    $auth = $_SESSION["auth"];
    if (!$auth){
        header("Location: /login.php"); 
    }
} catch(Exception $e){
    header("Location: /login.php"); 
}
*/

//receiving parameter
$id = $_GET['id'];
//echo 'id='.$id;

$_SESSION["manu"]="M00001";
?>

<div class="container-fluid">
	<div class="row">

		<div class="col-md-12">
			<div class="card card-plain">
			
				<div>
					<form id="formid">
						<div class="row">
							<div class="col-md-4" style="padding:10px;">
								<label>Teks Carian</label>
								<input id="search_text" name="search_text" type="text" class="form-control border-input" placeholder="Teks carian" value="" >
							</div>
							<div class="col-md-4" style="padding:10px;">
								<label>Status COA</label>
								<select id="search_status" name="berat_unit" class="form-control border-input">
									<option value="-" selected>SEMUA</option>
									<option value="0" >PROSES COA</option>
									<option value="1" >AKTIF</option>
									<option value="2" >SEMAK SEMULA</option>
									<option value="3" >GAGAL</option>
									<option value="4" >COA LUPUT</option>
									<option value="5" >PENGGANTUNGAN</option>
								</select>  
							</div>
						</div>  
						<div class="row">
							<div class="col-md-8" style="padding:10px;text-align:center;">
								<button id="search" type="button" class="btn btn-info btn-fill btn-wd">Cari Produk</button>
							</div>
						</div>
					</form>
					<hr/>
				</div>
			
				<div class="header">
					<p class="category">Jumlah Rekod: <span id="totalrow">0</span></p>
				</div>
				<div class="content table-responsive table-full-width">
					<table id="listing" class="table table-hover listing">
						<thead style="background-color:#E0E0E0;">
							<th>#</th>
							<th><b>Kategori</b></th>
							<th><b>No. SKU</b></th>
							<th><b>Jenama</b></th>
							<th><b>Model / Produk / Tahun</b></th>
							<th><b>No. Sijil COA</b></th>
							<th><b>Status COA</b></th>
							<th>[Tindakan]</th>
						</thead>
						<tbody>
							<!-- jquery to fill -->
						</tbody>
					</table>
				</div>
				<hr/>
			</div>
		</div>

	</div>
</div>


    <!--   Core JS Files   -->
    <script src="/assets/js/jquery-1.10.2.js" type="text/javascript"></script>
	<script src="/assets/js/bootstrap.min.js" type="text/javascript"></script>

	<!--  Checkbox, Radio & Switch Plugins -->
	<script src="/assets/js/bootstrap-checkbox-radio.js"></script>

    <!--  Notifications Plugin    -->
    <script src="/assets/js/bootstrap-notify.js"></script>

    <!-- Paper Dashboard Core javascript and methods for Demo purpose -->
	<script src="/assets/js/paper-dashboard.js"></script>

	<script src="/assets/js/app.js"></script>
	

<script type="text/javascript">
	$(document).ready(function() {

		$.ajaxSetup({
			cache: false
		});	

		var url = "";
		url = getBase();

		getData();
		

		function getData(){
			$.ajax({
				type: "get",
				url: url + "/m/ws/get_products.php"
				}).done(function(json) { 
				//console.log(json);
				var data = $.parseJSON(json);
				//console.log('list_produk:getdata...', data);
				
				if (data && data.length > 0) {
					$("#listing tbody").empty();

					var loop = 0;
					$.each(data, function(k, v) {

						coa_status = "";
						if (v.coa_status){
							var selectOption = $("#search_status option[value=" + v.coa_status + "]").text();
							coa_status = selectOption;
						}

						var tr = "";
						loop++;
						tr = tr + "<tr>";
						tr = tr + "    <td>" +loop+ "</td>";
						tr = tr + "    <td>" +v.type+ "</td>";
						tr = tr + "    <td>" +(v.sku ? v.sku.toUpperCase() : v.sku)+ "</td>";
						tr = tr + "    <td>" +(v.jenama ? v.jenama.toUpperCase() : v.jenama)+ "</td>";
						tr = tr + "    <td>" +(v.model?v.model.toUpperCase():v.model)+" / "+(v.produk?v.produk.toUpperCase():v.produk)+ " / " +v.prod_year+ "</td>";
						tr = tr + "    <td>" +(v.coa_cert?v.coa_cert:'')+ "</td>";
						tr = tr + "    <td>" +coa_status+ "</td>";
						tr = tr + "    <td>";
						tr = tr + "        <span style='cursor:pointer;'>";
						tr = tr + "        <i class='ti-info-alt show-info' title='Info Produk' data1='" +v.nfc+ "'></i>&nbsp;&nbsp;";
						//tr = tr + "        <i class='ti-pencil edit-info' title='Kemaskini Produk' data1='" +v.nfc+ "'></i>&nbsp;&nbsp;";
						tr = tr + "        </span>";
						tr = tr + "    </td>";
						tr = tr + "</tr>";

						//console.log('tr...', tr);
						$("#listing tbody").append(tr);
					});

					$("#totalrow").html(loop).change();
				}                
			});	
		}
		
		
		
		$(document).on('click', '.show-info' , function() {
            event.preventDefault();
			$(document).off('click', '.show-info');
            var data1 = $(this).attr('data1');
			//console.log('data1:', data1);
			setPage(this, 'Info Produk', './info_produk.php?id='+data1);
        });   

		$(document).on('click', '.edit-info' , function() {
            event.preventDefault();
			$(document).off('click', '.edit-info');
            var data1 = $(this).attr('data1');
			//console.log('data1:', data1);
			setPage(this, 'Kemaskini Produk', './edit_produk.php?id='+data1);
        });	


        $('#search_text').keyup(function() {
            var txtVal = this.value;
            //$("#output").text("You have entered " + txtVal);
            //console.log("You have entered " + txtVal);
            searchFilter(txtVal);
        });

        function searchFilter(input) {
            var rex = new RegExp(input, 'i');
            //var rows = $('.clients tr');
            var rows = $('.listing').find('tr').not(':first') 

            rows.hide();
            rows.filter(function() {
                var tester = true;
                tester = rex.test($(this).text());
                //tester = tester && filterOnDays(this);
                return tester;
            }).show();
        }   			

	});
</script>    