<?php
function mk_history(){
include "../actions/dbPDO.php";
$ac_date = date("Y-m-d");
// $ac_time = date("H:i");
// check if its written before
$ww = $db->prepare("SELECT * from actions WHERE ac_date = ?") ;
$ww->bindParam(1,$ac_date);
$ww->execute();
if($ww->rowCount()< 1){

  // maxDate
  $maxDD= $db->prepare("SELECT max(ac_date) as maxdate FROM actions");
  $maxDD->execute();
  if($maxDD->rowCount()>0){$day = $maxDD->fetch(PDO::FETCH_ASSOC);
      $maxDate = $day['maxdate'] ;}
      if(empty($maxDate)){ $maxDate = date('Y-m-d', strtotime($ac_date. ' - 1 days')); }
  while($maxDate < $ac_date){
//maxD
$maxDate = date('Y-m-d', strtotime($maxDate. ' + 1 days'));
$maxsql= $db->prepare("SELECT max(ac_id) as maxd FROM actions");
$maxsql->execute();
if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
    $maxD = $row['maxd']+1;} else {$maxD = 1 ; }

// catch cars working
$sql =$db->prepare( "SELECT cars.carid, cars.branch , cars.carstatus , contracts.contract_id FROM cars , contracts
  WHERE contracts.carid = cars.carid AND contracts.cont_status = 1 ");
$sql->execute();
if($sql->rowCount()>0)
{
  while($row = $sql->fetch())
  {
      $carid        = $row["carid"];
      $branch       = $row["branch"];
      $carstatus    = $row["carstatus"];
      $contract_id  = $row["contract_id"];

      $ac = $db->prepare(" INSERT INTO actions
      (ac_id, ac_date, carid, branch, contract_id, carstatus )
       VALUES (?, ?, ? , ? ,  ? ,? )");
       $ac->bindParam(1,$maxD);
       $ac->bindParam(2,$maxDate);
       $ac->bindParam(3,$carid);
       $ac->bindParam(4,$branch);
       $ac->bindParam(5,$contract_id);
       $ac->bindParam(6,$carstatus);
       $ac->execute();
$maxD = $maxD + 1 ;
}}//end of catch working cars

// catch other cars standbay workshope servece etc..
$sql =$db->prepare( "SELECT carid, branch , carstatus  FROM cars
  WHERE branch > 1 AND carid NOT IN (SELECT carid from contracts where cont_status = 1 )");
$sql->execute();
if($sql->rowCount()>0)
{
  while($row = $sql->fetch())
  {
      $carid        = $row["carid"];
      $branch       = $row["branch"];
      $carstatus    = $row["carstatus"];

      $ac = $db->prepare(" INSERT INTO actions
      (ac_id, ac_date, carid, branch,  carstatus )
       VALUES (?, ?, ? ,  ? , ?  )");
       $ac->bindParam(1,$maxD);
       $ac->bindParam(2,$maxDate);
       $ac->bindParam(3,$carid);
       $ac->bindParam(4,$branch);
       $ac->bindParam(5,$carstatus);
       $ac->execute();
$maxD = $maxD + 1 ;
}}//end of catch other cars standbay workshope servece etc..
}//maxDate
}//end or check written
  }
  // how many days in this status
  function car_state($carid){
    include "../actions/dbPDO.php";
    $dd = date("Y-m-d");
    $qty = 0 ;
      $ss = 0 ;
      $bb = 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"];
           $pass = 1 ;
      //     while($qty < 90 ){
      // $ac = $db->prepare(" SELECT branch , carstatus FROM  actions  WHERE carid = ? AND ac_date = ?  ");
      //     $ac->bindParam(1,$carid);
      //     $ac->bindParam(2,$dd) ;
      //     $ac->execute();
      //   if($ac->rowCount()>0){$day = $ac->fetch(PDO::FETCH_ASSOC);
      //          $bb = $day['branch'];
      //          $ss = $day['carstatus'];
      //        if($ss != 3){
      //        if(($branch != $bb)||($carstatus != $ss)){ break; }
      //      }else{if($branch != $bb){ break; }}
      //    }else{break ;}
      //   $dd = date('Y-m-d', strtotime($dd. ' - 1 days'));
      //   $qty = $qty + 1 ;
      //            }
  }
  return $qty ;
}//end of cuurent branch and state etc..

// insert new cars at naql table
function naql_cars(){
  include "../actions/dbPDO.php";
      $maxsql= $db->prepare("SELECT max(naqid) as maxd FROM naql");
      $maxsql->execute();
    if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
        $maxD = $row['maxd']+1;} else {$maxD = 1 ; }

        // select cars working in rent that not at naql table 
      $sql = $db->prepare( "SELECT carid, insurance_no , insurance_expire ,  insurance_kind  FROM cars 
            WHERE cars.car_job_id = 1  AND model > 2022 AND branch > 1 AND carid NOT IN(SELECT carid from naql)");
      $sql->execute();
  if($sql->rowCount()>0)
    {
    while($row = $sql->fetch())
      {
      $carid              = $row["carid"];
      $insurance_no       = $row["insurance_no"];
      $insurance_expire   = $row["insurance_expire"];
      $insurance_kind     = $row["insurance_kind"];

      $ac = $db->prepare(" INSERT INTO naql(naqid, carid, insurance_no, insurance_expire, insurance_kind )
                            VALUES (?, ?, ? , ? ,  ? )");
       $ac->bindParam(1,$maxD);
       $ac->bindParam(2,$carid);
       $ac->bindParam(3,$insurance_no);
       $ac->bindParam(4,$insurance_expire);
       $ac->bindParam(5,$insurance_kind);
       $ac->execute();
      $maxD = $maxD + 1 ;
    }}
}
