<?php if($lihat){?>
<?php
$q=mysqli_query($connect, "select * from toko where ID_user='$_SESSION[id_user]' and ID ='$lihat'") 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>VIEW DATA STORE</h2>
      </div>
      <div class="body">
        <form>
          <div class="form-group">
            <label>Store Name</label>
            <div class="form-line">
              <?php echo $b['nama_toko'];?>
            </div>
          </div>
		   <div class="form-group">
            <label>Store Address</label>
            <div class="form-line">
              <?php echo $b['alamat_toko'];?>
            </div>
          </div>
           <div class="form-group">
            <label>Picture</label>
            <div class="form-line">
              <img src="assets/images/placeholder.png" data-src="<?php echo getConfig($connect, 'base_url');?>image.php?t=toko&id=<?php echo $lihat;?>&s=90" style="height:100px;" class="center img">
            </div>
          </div>
         <div class="form-group">
            <label>Created/Modified</label>
            <div class="form-line">
              <?php echo $b['date_create']?>
            </div>
          </div>
        <div class="form-group">
          <button type="button" class="btn btn-sm bg-blue-grey waves-effect back">Back</button>
        </div>
      </form>
      </div>
    </div>
  </div>
</div>
<?php }elseif($chart){
$q=mysqli_query($connect, "select * from toko where ID_user='$_SESSION[id_user]' and ID ='$chart'") or die(mysqli_error($connect));
$b=mysqli_fetch_array($q);
?>
<script src="<?php echo getConfig($connect, 'base_url');?>assets/plugins/chartjs/Chart.bundle.js"></script> 
<div class="block-header">
  <h2>CHART <?php echo strtoupper($b['nama_toko']);?></h2>
</div>
<div class="row">
	<div class="col-md-12">
		<div class="card">
          <div class="header text-center">
            <h2>SALES CHART <?php echo strtoupper($b['nama_toko']);?> ALL THIS YEAR</h2>
          </div>
          <div class="body">
            <canvas id="bar_chart" height="100"></canvas>
          </div>
        </div>
		  <?php
			$data='';
			$y=date('Y');
			$bl=12;//date('m');
			$qi=mysqli_query($connect, "select * from toko where ID='$b[ID]' and ID_user='$_SESSION[id_user]' order by ID asc") or die(mysqli_error($connect));
			$dbul=array();
			while($bi=mysqli_fetch_array($qi)){
				$vdata = '[';
				for($i=1;$i<=$bl;$i++){
					if($i<10){$bulan='0'.$i;}else{$bulan=$i;}
					$qd=mysqli_query($connect, "select sum(grandtotal) from transaksi where MONTH(waktu)='$bulan' and YEAR(waktu)='$y' and ID_toko='$bi[ID]' and status='1'  and hapus='0'") or die(mysqli_error($connect));
					while($bd=mysqli_fetch_array($qd)){
						$vdata.=(int)$bd[0].",";
					}	
				}
				$vdata = substr($vdata,0,-1);
				$vdata.="]";
				$dbul[]=$vdata;
				$data.='{"label":"'.$bi['nama_toko'].'","data":'.$vdata.',"backgroundColor": "'.genColorCodeFromText($bi['nama_toko']).'"},';
			}
			$data=substr($data,0,-1);
		?>
		 <script>
		 	$(function(e) {
				var data='[<?php echo $data;?>]';
				new Chart(document.getElementById("bar_chart").getContext("2d"), getChartJs('line',data));
			});
        </script>
	</div>
	<div class="col-md-12">
		<div class="card">
          <div class="header text-center">
            <h2>TRANSACTION COMPARISON GRAPH <strong>SALE</strong> AND <strong>VOID</strong> <?php echo strtoupper($b['nama_toko']);?></h2>
          </div>
          <div class="body">
            <canvas id="pie_chart" height="100"></canvas>
          </div>
        </div>
		<?php
			$t = mysqli_fetch_array(mysqli_query($connect, "select sum(grandtotal) from transaksi where ID_user='$_SESSION[id_user]' and ID_toko='$b[ID]' and hapus='0'"));
			$v=mysqli_fetch_array(mysqli_query($connect, "select sum(grandtotal) from transaksi where status!=1 and ID_user='$_SESSION[id_user]' and ID_toko='$b[ID] and hapus='0''"));
            $s=mysqli_fetch_array(mysqli_query($connect, "select sum(grandtotal) from transaksi where status=1 and ID_user='$_SESSION[id_user]' and ID_toko='$b[ID]' and hapus='0'"));
			$void=(int)$v[0];
			$sale=(int)$s[0];
			$total=(int)$t[0];
			$persen_sale = round(($sale/$total)*100,2).'%';
			$persen_void = round(($void/$total)*100,2).'%';
			$data2='{"datasets":[{"data":['.$sale.','.$void.'],"backgroundColor":["rgb(50,11,223)","rgb(228,82,6)"]}],"labels":["Sale ('.$persen_sale.')","Void ('.$persen_void.')"]}';
		?>
		<script>
		$(function(e) {
			var pie_data = '<?php echo $data2;?>';
			new Chart(document.getElementById("pie_chart").getContext("2d"), getChartJs('pie',pie_data));
		});
		</script>
	</div>
</div>
<?php }elseif($edit){?>
<?php
$q=mysqli_query($connect, "select * from toko where ID_user='$_SESSION[id_user]' and 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 STORE</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 Name</label>
            <div class="form-line">
              <input type="text" name="nama_toko" class="form-control" required value="<?php echo $b['nama_toko'];?>">
            </div>
          </div>
            <div class="form-group">
            <label>Store Address</label>
            <div class="form-line">
              <textarea name="alamat_toko" class="form-control" required><?php echo $b['alamat_toko'];?></textarea>
            </div>
          </div>
          <div class="form-group">
            <label>Picture</label>
            <div class="form-line">
				<input name="gambar_toko" id="inputFile99" accept="image/*" onChange="validateImage(this);" type="file"/>
                <?php if($b['gambar_toko']){?>
                <p align="center" id="img-view99" class="img-view" style="text-align:center;"><img src="assets/images/placeholder.png" data-src="<?php echo getConfig($connect, 'base_url');?>image.php?t=toko&id=<?php echo $edit;?>&s=90" style="height:100px;" class="center">
                  <button type="button" class="btn bg-red waves-effect no-padding btn-xs" onClick="$('#img-view99').remove();$('#inputFile99').val('');"><i class="material-icons">delete</i></button>
                  <input type="hidden" name="old_img" value="1">
                </p>
                <?php } ?>
              </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 STORE</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 DATA</span></button>
        <table class="table table-bordered table-striped table-hover dataTable js-exportable">
            <thead>
              <tr>
                <th>#</th>
                <th>Picture</th>
				<th>Name</th>
                <th>Store Address</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 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 Name</label>
            <div class="form-line">
              <input type="text" name="nama_toko" class="form-control" required>
            </div>
          </div>
		   <div class="form-group">
            <label>Store Address</label>
            <div class="form-line">
              <textarea name="alamat_toko" class="form-control" required></textarea>
            </div>
          </div>
		   <div class="form-group">
            <label>Picture</label>
            <div class="form-line">
               <input name="gambar_toko" id="inputFile" accept="image/*" onChange="validateImage(this);" type="file"/>
            </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": 1,"className": "text-center"},
			{"targets": 4,"className": "text-center"}
			 ]
        }); 
});
</script>