<?php

  $branch = $conn->query(" select count(*) from branches     ")->fetchColumn();
  $groups = $conn->query(" select count(*) from groups     ")->fetchColumn();
  $products = $conn->query(" select count(*) from products     ")->fetchColumn();
  $clients = $conn->query(" select count(*) from clients     ")->fetchColumn();
  $orders = $conn->query(" select count(*) from orders     ")->fetchColumn();

  $categories = $conn->query(" select count(*) from categories     ")->fetchColumn();
  $users      = $conn->query(" select count(*) from sys_user     ")->fetchColumn();
  

  $ordersnew       = $conn->query(" select count(*) from orders WHERE STATUS=1  ")->fetchColumn();
  $ordersnewp      = ( ( $ordersnew / $orders ) * 100 );
 
  $ordersapproved  = $conn->query(" select count(*) from orders WHERE STATUS=2  ")->fetchColumn();
  $ordersapprovedp      = ( ( $ordersapproved / $orders ) * 100 );

  $ordersrejected  = $conn->query(" select count(*) from orders WHERE STATUS=3  ")->fetchColumn();
  $ordersrejectedp      = ( ( $ordersrejected / $orders ) * 100 );

  $ordersclosed  = $conn->query(" select count(*) from orders WHERE STATUS=4  ")->fetchColumn();
  $ordersclosedp      = ( ( $ordersclosed / $orders ) * 100 );


  $neworders = $conn->prepare( "  SELECT a.id,a.client_id,a.branch_id,a.group_id,a.total_price,a.status,
                                         b.name bname,c.name cname,d.name dname,e.name ename 
                                   FROM orders a
                                   LEFT JOIN branches e ON a.branch_id = e.id
                                   LEFT JOIN groups b ON a.group_id = b.id
                                   LEFT JOIN clients c ON a.client_id = c.id
                                   LEFT JOIN orderstatus d ON a.status=d.id
                                   ORDER BY a.id DESC LIMIT  5    ");
  $neworders->execute();
  $norders = $neworders->fetchAll();
  

?>
 
     <!-- First Row -->
      <div class="row">  
        <div class="col-md-12">
       
            <!-- Start -->
              <div class="box box-success" style="border:solid;border-color:purple;border-width:thin; padding-top:0px; padding-bottom:0px;">
                <div class="box-body without-border" style="border-bottom:solid; border-width:thin;border-color:green;">
           
                  <div class="col-lg-3 col-md-6 col-xs-12">
                      <div class="small-box bg-green"   >
                          <div class="inner">
                          <h4 style="font-size:18px;  font-weight:800px; font-family:'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;" > New Orders </h4>

                          <p style="font-size:15px;"> <?php echo number_format( $ordersnew, 0); ?> </p>
                              <div class="icon">
                                        <i class="ion-android-contacts"></i>
                              </div>
                          </div>
                      </div>
                  </div>
                  
                  <div class="col-lg-3 col-md-6 col-xs-12">
                      <div class="small-box bg-purple" >
                          <div class="inner">
                          <h4 style="font-size:18px; font-weight:800px; font-family:'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;" > Approved Orders </h4>

                          <p style="font-size:15px;"> <?php echo number_format( $ordersapproved, 0); ?>  </p>
                              <div class="icon">
                                        <i class="ion-ios-people-outline"></i>
                              </div>
                          </div>
                          <!-- <a href="#" class="small-box-footer"> <i class="fa fa-arrow-LOCK"></i></a> -->
                      </div>
                  </div>
                  <div class="col-lg-3 col-md-6 col-xs-12">
                      <div class="small-box bg-purple" >
                          <div class="inner">
                          <h4 style="font-size:18px; font-weight:800px; font-family:'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;" > Delivered Orders </h4>
                          <p style="font-size:15px;"> <?php echo number_format( $ordersclosed, 0); ?> </p>
                          </div>
                      
                      </div>
                  </div>

                  <div class="col-lg-3 col-md-6 col-xs-12">
                      <div class="small-box bg-maroon" >
                          <div class="inner">
                          <h4 style="font-size:18px; font-weight:800px; font-family:'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;" > All Orders  </h4>
                          <p style="font-size:15px;"> <?php echo number_format( $orders, 0); ?> </p>
                          </div>
                      
                      </div>
                  </div>

                </div>

                 
                 <!-- /.row -->
                  <div class="box-footer ">
                  <div class="col-sm-2 col-xs-6">
                    <div class="description-block border-right" style="border-color:green;" >
                      <h5 class="description-header"  ><?php echo "$branch"; ?></h5>
                      <span class="description-text"  > BRANCHES </span>
                    </div>
                  </div>
                  
                  <div class="col-sm-2 col-xs-6">
                    <div class="description-block border-right" style="border-color:green;" >
                      <h5 class="description-header"><?php echo "$categories"; ?></h5>
                      <span class="description-text"> Categories </span>
                    </div>
                  </div>

                  <div class="col-sm-2 col-xs-6">
                    <div class="description-block border-right " style="border-color:green;" >
                      <h5 class="description-header"><?php echo "$products"; ?></h5>
                      <span class="description-text"> PRODUCTS  </span>
                    </div>
                  </div>
                  <!-- /.col -->
               
                  <!-- /.col -->
                  <div class="col-sm-2 col-xs-6">
                    <div class="description-block border-right" style="border-color:green;">
                      <h5 class="description-header"> <?php echo "$groups"; ?> </h5>
                      <span class="description-text">Groups </span>
                    </div>
                  </div>

                  <!-- /.col -->
                  <div class="col-sm-2 col-xs-6">
                    <div class="description-block border-right" style="border-color:green;" >
                      <h5 class="description-header"><?php echo "$clients"; ?></h5>
                      <span class="description-text"> Clients </span>
                    </div>
                  </div>

                  <div class="col-sm-2 col-xs-6">
                    <div class="description-block" style="border-color:green;" >
                      <h5 class="description-header"><?php echo "$users"; ?></h5>
                      <span class="description-text"> Users  </span>
                    </div>
                  </div>

              

                  </div>
                 <!-- /.box-footer -->         
              </div>
              <!-- End --->
         
          <!-- /.box -->

        </div>
   
            <!-- <div class="col-md-4 ">
              <div class="box box-default"  >
                <div class="box-body without-border">
                    <div class="">
                            <a href="admin/allgroup" class="btn bg-purple btn-flat btn-md  btn-block" style="border-radius:30px;"  > <i class="fa fa-users"></i> ADD GROUP </a>
                      <a href="admin/allclient" class="btn bg-purple btn-flat btn-md  btn-block" style="border-radius:30px;" > <i class="fa fa-user-plus"></i> ADD CLIENT </a>
                      <a href="admin/allorder" class="btn bg-olive btn-flat btn-md  btn-block"  style="border-radius:30px;"> <i class="fa fa-plus"></i> ADD ORDER</a>
                      
                      

                      
                    </div>
                </div>  

                

              </div>
            </div>   -->
      </div>
     <!-- First Row -->

     <!-- second roow-->
      <div class="row">  
         <div class="col-md-8">
          <div class="box box-info" style="border:solid;border-width:thin;border-color:purple;">
            <div class="box-header with-border bg-purple">
              <h3 class="box-title"> <i class="fa fa-calendar"></i> Recent Orders</h3>

              <div class="box-tools pull-right">
                <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
                </button>
                <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
              </div>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
              <div class="table-responsive">
                <table class="table no-margin">
                  <thead>
                  <tr>
                    <th>#ID</th>
                    <th>BRANCH</th>
                    <th>GROUP</th>
                    <th>CLIENT_NAME</th>
                    <th>AMOUNT(KES)</th>
                    <th>STATUS</th>
                    <!-- <th>Popularity</th> -->
                  </tr>
                  </thead>
                  <tbody>                 
                    <?php foreach($norders as $neworder)
                     { ?>
                
                  <tr>
                    <td><?php echo $neworder["id"]; ?> </td>
                    <td><?php echo $neworder["ename"]; ?> </td>
                    <td><?php echo $neworder["bname"]; ?> </td>
                    <td><?php echo $neworder["cname"]; ?> </td>
                    <td><?php echo $neworder["total_price"]; ?> </td>
                    <td><span class="label label-success"> <?php echo $neworder["dname"]; ?> </span></td>
                    <!-- <td>
                      <div class="sparkbar" data-color="#00a65a" data-height="20">90,80,90,-70,61,-83,63</div>
                    </td> -->
                  </tr>
                    <?php } ?>

                  </tbody>
                </table>
              </div>
              <!-- /.table-responsive -->
            </div>
            <!-- /.box-body -->
            <!-- <div class="box-footer clearfix">
              <a href="javascript:void(0)" class="btn btn-sm btn-info btn-flat pull-left">Place New Order</a>
              <a href="javascript:void(0)" class="btn btn-sm btn-default btn-flat pull-right">View All Orders</a>
            </div> -->
            <!-- /.box-footer -->
          </div>
         </div>                  
         <div class="col-md-4">
            <div class="box box-info" style="border:solid;border-width:thin;border-color:purple;">
              <div class="box-header with-border bg-purple">
                <h3 class="box-title"> <i class="fa fa-bar-chart"></i> Orders Statistics  </h3>
              </div>
              <!-- /.box-header -->
            <div class="box-body">

                  <!-- /.progress-group -->
                  <div class="progress-group">
                    <span class="progress-text"> New Orders </span>
                    <span class="progress-number"><b> <?php echo "$ordersnew"; ?> </b>/<?php echo "$orders"; ?></span>

                    <div class="progress sm">
                      <div class="progress-bar progress-bar-green" style="width:<?php echo $ordersnewp;?>%"></div>
                    </div>
                  </div>

                  <!-- /.progress-group -->
                  <div class="progress-group">
                    <span class="progress-text"> Rejected Orders  </span>
                    <span class="progress-number"><b><?php echo "$ordersrejected"; ?> </b>/<?php echo "$orders"; ?></span>

                    <div class="progress sm">
                      <div class="progress-bar bg-maroon " style="width:<?php echo $ordersrejectedp;?>%"></div>
                    </div>
                  </div>

                  <div class="progress-group">
                    <span class="progress-text"> Approved Orders  </span>
                    <span class="progress-number"><b><?php echo "$ordersapproved"; ?> </b>/<?php echo "$orders"; ?> </span>

                    <div class="progress sm">
                      <div class="progress-bar bg-purple " style="width:<?php echo $ordersapprovedp;?>%"></div>
                    </div>
                  </div>


                  <!-- /.progress-group -->
                  <div class="progress-group">
                    <span class="progress-text"> Delivered & Closed orders </span>
                    <span class="progress-number"><b> <?php echo "$ordersclosed"; ?> </b>/<?php echo "$orders"; ?> </span>

                    <div class="progress sm">
                      <div class="progress-bar bg-purple " style="width:<?php echo $ordersclosedp;?>%"></div>
                    </div>
                  </div>
                  <!-- /.progress-group -->
                   
                <!-- </div> -->
                <!-- /.col -->              
              <!-- /.table-responsive -->
            </div>
            <!-- /.box-body -->
            <!-- <div class="box-footer clearfix">
              <a href="javascript:void(0)" class="btn btn-sm btn-info btn-flat pull-left">Place New Order</a>
              <a href="javascript:void(0)" class="btn btn-sm btn-default btn-flat pull-right">View All Orders</a>
            </div> -->
            <!-- /.box-footer -->
          </div>    

         </div>
      </div>
     <!-- end second roow-->

     <!-- Third Row -->
    
     <!-- end second roow-->









  


  

</script>