<?php
class CarHistory{
    private $db;
    public function __construct($db)
    {
        $this->db = $db;
    }
public function car_state($carid){
       $db = $this->db;
          $myday  = date("Y-m-d");
          $myday  = new DateTime($myday);
          $qty    = 0 ;

        $sql = $db->prepare( "SELECT  branch , carstatus FROM cars WHERE carid = ? ");
        $sql->bindParam(1,$carid);
        $sql->execute();
      if($sql->rowCount()>0){
              $row = $sql->fetch(PDO::FETCH_ASSOC);
              $branch        = $row["branch"];
              $carstatus     = $row["carstatus"];

          $branchsql = $db->prepare("SELECT MAX(`ac_date`) as lastbranch FROM `actions` WHERE `branch` != ? and carid = ?");
          $branchsql->bindParam(1,$branch);
          $branchsql->bindParam(2,$carid);
          $branchsql->execute();
          if($branchsql->rowCount()> 0){
            $row = $branchsql->fetch(PDO::FETCH_ASSOC);
            $lastbranch = $row['lastbranch'];
          }
          if(empty($lastbranch)){
            $sql = $db->prepare("SELECT start FROM `cars` WHERE carid = ?");
            $sql->bindParam(1,$carid);
            $sql->execute();
          if($sql->rowCount()> 0){
            $row = $sql->fetch(PDO::FETCH_ASSOC);
            $lastbranch = $row['start'];
          }}

          $statesql = $db->prepare("SELECT MAX(`ac_date`) as laststate FROM `actions` WHERE `carstatus` != ? and carid = ?");
          $statesql->bindParam(1,$carstatus);
          $statesql->bindParam(2,$carid);
          $statesql->execute();
          if($statesql->rowCount()> 0){
            $row = $statesql->fetch(PDO::FETCH_ASSOC);
            $laststate = $row['laststate'];
          }
          $lastbranch      = new DateTime($lastbranch ??'');
          $laststate       = new DateTime($laststate ?? '');
        if ($lastbranch < $laststate) {
            $qty = $myday->diff($laststate)->format("%a");
          }else{
            $qty = $myday->diff($lastbranch)->format("%a");
          }
      }
    
      return $qty ;
    }//end of cuurent branch and state etc..

public function currentCars(){
        $db = $this->db;
        $result = array();

        $sandy= $db->prepare("SELECT count(carid) as sandy  FROM cars where carstatus = 1 AND branch > 1 ");
        $sandy->execute();
    if($sandy->rowCount()>0){$wow = $sandy->fetch(PDO::FETCH_ASSOC);
        $result['sanndy'] = $wow['sandy'];
     }
    //  $this->sanndy = $san;
     $sandy= $db->prepare("SELECT count(carid) as shop FROM cars where carstatus = 3 AND branch > 1");
     $sandy->execute();
    if($sandy->rowCount()>0){$wow = $sandy->fetch(PDO::FETCH_ASSOC);
        $result['shop'] = $wow['shop'];
        }
        $sandy= $db->prepare("SELECT count(carid) as runn FROM cars where carstatus = 4 AND branch > 1");
        $sandy->execute();
       if($sandy->rowCount()>0){$wow = $sandy->fetch(PDO::FETCH_ASSOC);
        $result['runn'] = $wow['runn'];
           }
     $sandy= $db->prepare("SELECT count(carid) as service FROM cars where carstatus = 5 AND branch > 1");
     $sandy->execute();
if($sandy->rowCount()>0){$wow = $sandy->fetch(PDO::FETCH_ASSOC);
    $result['service'] = $wow['service'];
              }
              return $result ;
    }



}
?>