<?php if($edit){
$q=mysqli_query($connect, "SELECT a.ID,c.nama_toko,b.nama_produk,a.stok FROM `produk_stok_toko` a, `produk` b, `toko` c WHERE b.ID_user=c.ID_user and a.ID_user=c.ID_user and c.ID=a.ID_toko and a.ID_produk=b.ID and a.ID_user='$_SESSION[id_user]' and a.ID ='$edit'") or die(mysqli_error($connect));
$b=mysqli_fetch_array($q);
?>
<div class="row clearfix">
  <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
    <div class="card">
      <div class="header">
        <h2>EDIT STOCK</h2>
      </div>
      <div class="body">
        <form class="form_ajax">
        <input type="hidden" name="p" value="<?php echo $page;?>">
        <input type="hidden" name="t" value="update">
        <input type="hidden" name="token" value="<?php echo getToken();?>">
        <input type="hidden" name="id" value="<?php echo $b['ID'];?>">
		 <div class="form-group">
            <label>Store</label>
            <div class="form-line">
              <select name="toko" class="form-control" required>
					<?php
						$qk=mysqli_query($connect, "select * from toko where ID_user='$_SESSION[id_user]'") or die(mysqli_error($connect));
						while($bk=mysqli_fetch_array($qk)){
					?>
							<option value="<?php echo $bk['ID'];?>" <?php echo ($b['ID_toko']==$bk['ID'])?'selected':'';?>><?php echo $bk['nama_toko'];?></option>
					<?php } ?>
			  </select>
            </div>
          </div>
		  <div class="form-group">
            <label>Product</label>
            <div class="form-line">
              <select name="produk" class="form-control" required>
					<?php
						$qk=mysqli_query($connect, "select * from produk where ID_user='$_SESSION[id_user]'") or die(mysqli_error($connect));
						while($bk=mysqli_fetch_array($qk)){
					?>
							<option value="<?php echo $bk['ID'];?>" <?php echo ($b['ID_produk']==$bk['ID'])?'selected':'';?>><?php echo $bk['nama_produk'];?></option>
					<?php } ?>
			  </select>
            </div>
          </div>
		   <div class="form-group">
		   	<input type="checkbox" onClick="if(this.checked==true){$('.stok').attr('disabled','true').hide();}else{$('.stok').removeAttr('disabled').show();}" name="unlimited" id="basic_checkbox_2" class="filled-in" <?php echo ($b['stok']=='-1')?'checked="checked"':'';?>>
			<label for="basic_checkbox_2">Unlimited Stock</label>
		   </div>
		   <div class="form-group stok" <?php echo ($b['stok']=='-1')?'style="display:none"':'';?>>
            <label>Stock</label>
            <div class="form-line">
              <input type="text" name="stok" value="<?php echo $b['stok'];?>" class="form-control formatNumber stok" required <?php echo ($b['stok']=='-1')?'disabled="disabled"':'';?>>
            </div>
          </div>
        <div class="form-group">
          <button type="submit" class="btn btn-sm bg-blue-grey waves-effect">Submit</button>&nbsp;
          <button type="reset" class="btn btn-sm bg-red waves-effect back">Back</button>
        <div class="preloader pl-size-xs pull-right" style="display:none;" id="loading">
            <div class="spinner-layer pl-blue-grey">
              <div class="circle-clipper left">
                <div class="circle"></div>
              </div>
              <div class="circle-clipper right">
                <div class="circle"></div>
              </div>
            </div>
          </div>
        </div>
      </form>
      </div>
    </div>
  </div>
</div>
<?php }else{?>
<div class="row clearfix">
  <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
    <div class="card">
      <div class="header">
        <h2>DATA STOCK PRODUCT</h2>
      </div>
      <div class="body">
      	<button type="button" data-toggle="modal" data-target="#defaultModal" data-controls-modal="#defaultModal" data-backdrop="static" class="btn bg-blue-grey waves-effect"><i class="material-icons">add</i><span style="top:-4px !important;">ENTRY STOCK</span></button>
        <table class="table table-bordered table-striped table-hover dataTable js-exportable">
            <thead>
              <tr>
                <th>#</th>
                <th>Store</th>
				<th>Product</th>
                <th>Stock</th>
                <th>Action</th>
              </tr>
            </thead>
            <tbody>
            </tbody>
          </table>
      </div>
    </div>
  </div>
</div>
<div class="modal" id="defaultModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title" id="defaultModalLabel">Add Stock Store</h4>
      </div>
      <form class="form_ajax">
        <input type="hidden" name="p" value="<?php echo $page;?>">
        <input type="hidden" name="t" value="create">
        <input type="hidden" name="token" value="<?php echo getToken();?>">
        <div class="modal-body">
          <div class="form-group">
            <label>Store</label>
            <div class="form-line">
              <select name="toko" class="form-control" required>
			  		<option value="">- Choose Store -</option>
					<?php
						$qk=mysqli_query($connect, "select * from toko where ID_user='$_SESSION[id_user]'") or die(mysqli_error($connect));
						while($bk=mysqli_fetch_array($qk)){
					?>
							<option value="<?php echo $bk['ID'];?>"><?php echo $bk['nama_toko'];?></option>
					<?php } ?>
			  </select>
            </div>
          </div>
		  <div class="form-group">
            <label>Product</label>
            <div class="form-line">
              <select name="produk" class="form-control" required>
			  		<option value="">- Choose Product -</option>
					<?php
						$qk=mysqli_query($connect, "select * from produk where ID_user='$_SESSION[id_user]'") or die(mysqli_error($connect));
						while($bk=mysqli_fetch_array($qk)){
					?>
							<option value="<?php echo $bk['ID'];?>"><?php echo $bk['nama_produk'];?></option>
					<?php } ?>
			  </select>
            </div>
          </div>
		   <div class="form-group">
		   	<input type="checkbox" onClick="if(this.checked==true){$('.stok').attr('disabled','true').hide();}else{$('.stok').removeAttr('disabled').show();}" name="unlimited" id="basic_checkbox_2" class="filled-in">
			<label for="basic_checkbox_2">Unlimited Stock</label>
		   </div>
		   <div class="form-group stok">
            <label>Stock In</label>
            <div class="form-line">
              <input type="text" name="stok" class="form-control formatNumber stok" required>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <div class="preloader pl-size-xs pull-left" style="display:none" id="loading">
            <div class="spinner-layer pl-blue-grey">
              <div class="circle-clipper left">
                <div class="circle"></div>
              </div>
              <div class="circle-clipper right">
                <div class="circle"></div>
              </div>
            </div>
          </div>
          <button type="submit" class="btn btn-sm bg-blue-grey waves-effect">SUBMIT</button>
          <button type="reset" class="btn btn-sm bg-blue-grey waves-effect" data-dismiss="modal">CANCEL</button>
        </div>
      </form>
    </div>
  </div>
</div>
<?php } ?>
<script type="text/javascript">
$( document ).ready(function() {
$('table').DataTable({
		 "bSort":false,
		 "bProcessing": true,
         "serverSide": true,
         "ajax":{
            url :"<?php echo getConfig($connect, 'base_url');?>processData.php",
            type: "post",
			data:{p:'<?php echo $page;?>'},
            error: function(a,e,t){
				swal({
					title: "Something Wrong!",
					text: formatErrorMessage(a,e),
					type: "error",
					showCancelButton: !1,
					closeOnConfirm: !0
				}, function() {
					if(a.responseText){
						$("#hasil").html(a.responseText);
					}else{
						$("#hasil").html("Error : "+formatErrorMessage(a,e));
					}
				});
              	$("table").css("display","none");
            },
			complete: function(a,e,t){
				noClick2();
				initAjax(true);
			}
          },
		 responsive: !1,
		 width:'100%',
		 "autoWidth": false,
		 columnDefs: [
		 	{"targets": 0,"className": "text-center"},
			{"targets": 3,"className": "text-right"},
			{"targets": 4,"className": "text-center"}
			 ]
        }); 
});
</script>