<?php

require_once __DIR__ . '/../magic/auth.php';

$auth = bdr_require_privilege(24, '../logout.php');
$user = $auth['user_id'];
$father = $auth['user_id'];
$branch = $auth['branch'];
$conn = $auth['conn'];
$db = $auth['conn'];

// تحقق من وجود المستخدم في الجلسة
if (empty($_SESSION["user_id"])) {
    header("Location: ../logout.php");
    exit;
}

$branch = $_SESSION["branch"] ?? null; // استخدام null إذا لم تكن معرفة


try {

    // تحقق من صلاحيات المستخدم
} catch (PDOException $e) {
    // معالجة الخطأ بطريقة آمنة
    error_log("Database error: " . $e->getMessage());
    header("Location: ../error.php");
    exit;
}


 if(isset($_POST["search_kind"])){

    $kind       = $_POST["search_kind"];
    $model      = $_POST["search_model"];
    $sql = "";
      if($kind != 0){$sql = $sql." AND kind LIKE '$kind'" ;} //$kind = "%".$kind."%";UNHEX(str) LIKE '$kind%'"
      if($model != 0){$sql = $sql." AND model = ".$model ;}
  $output = '';
//   echo $sql;

  $stmt =$conn->prepare("SELECT carid,kind,concat(letters ,' ',numbers) as numbers,b_name,model,costday,monthly,minn,	month_cost , target , carstatus
                  from cars , branches 
                  where cars.branch = branches.branchID  AND cars.car_job_id < 2
                   $sql  order by model DESC  ");
   $stmt->execute();
                   if($stmt->rowCount()>0)
                  { $nn = 0 ;
                     while($row = $stmt->fetch())
                       { 
                        $style ="";
                        if($row['carstatus'] > 1){
                          $style = 'style="color:#420000; background-color:#c8e6c9;font-weight:bolder;"';
                        }
                        $nn++ ;
            $output .= '<tr> <td '.$style.'>' . $nn . '</td>
                            <td>
                            <a href="a/car_update.php?id=' . $row['carid'] . '"target="_blank"><button type="button" class="btn btn-success btn-xs ">تحديث</button></a>
                            </td>
                            <td class="text-center">' . $row['kind'] . '</td>
                            <td>' . $row['numbers'] . '</td>
                            <td>' . $row['b_name'] . '</td>
                            <td>' . $row['model'] . '</td>
                            <td class="text-center" >' . $row['costday'] . '</td>
                            <td class="text-center" >' . $row['monthly'] . '</td>
                            <td class="text-center" >' . $row['minn'] . '</td>
                            <td class="text-center" >' . $row['month_cost'] . '</td>
                            <td class="text-center" >' . $row['target'] . '</td>
                          </tr>';
                       }
                  }
        else {  $output .= '<tr><td colspan="11">لا يوجد سجلات ...</td></tr>'; }
                  echo $output;
  }
   if(isset($_POST["kind"])){

    $kind       = $_POST["kind"];
    $model      = $_POST["model"];
    $costday    = $_POST["costday"];
    $monthly    = $_POST["monthly"];
    $month_cost = $_POST["month_cost"];
      $output = '';
if(empty($kind) || empty($model)|| empty($costday)|| empty($monthly)|| empty($month_cost))
{
  $output .= '<tr><td colspan="11">..يجب ادخال كافةحقول البحث و التحرير.</td></tr>';;

}else{
    $sql = "";
      if($kind != 0){$sql = $sql." AND kind LIKE '$kind'" ;} //$kind = "%".$kind."%";UNHEX(str) LIKE '$kind%'"
      if($model != 0){$sql = $sql." AND model = ".$model ;}

      $minn = round(($month_cost / 1.15) / 30, 2);

      $update = $conn->prepare("UPDATE cars set costday =? , monthly = ? , month_cost = ? , minn = ? WHERE kind LIKE ? AND model = ?  ");
      $update->bindParam(1,$costday);
      $update->bindParam(2,$monthly);
      $update->bindParam(3,$month_cost);
      $update->bindParam(4,$minn);
      $update->bindParam(5,$kind);
      $update->bindParam(6,$model);
      $update->execute();

  $stmt =$conn->prepare("SELECT carid,kind,concat(letters ,' ',numbers) as numbers,b_name,model,costday,monthly,minn,	month_cost , target , carstatus
                  from cars , branches 
                  where cars.branch = branches.branchID  AND cars.car_job_id < 2
                   $sql  order by model DESC  ");
   $stmt->execute();
                   if($stmt->rowCount()>0)
                  { $nn = 0 ;
                     while($row = $stmt->fetch())
                       { 
                        $style ="";
                        if($row['carstatus'] > 1){
                          $style = 'style="color:#420000; background-color:#c8e6c9;font-weight:bolder;"';
                        }
                        $nn++ ;
            $output .= '<tr> <td '.$style.'>' . $nn . '</td>
                            <td>
                            <a href="a/car_update.php?id=' . $row['carid'] . '"target="_blank"><button type="button" class="btn btn-success btn-xs ">تحديث</button></a>
                            </td>
                            <td class="text-center">' . $row['kind'] . '</td>
                            <td>' . $row['numbers'] . '</td>
                            <td>' . $row['b_name'] . '</td>
                            <td>' . $row['model'] . '</td>
                            <td class="text-center" >' . $row['costday'] . '</td>
                            <td class="text-center" >' . $row['monthly'] . '</td>
                            <td class="text-center" >' . $row['minn'] . '</td>
                            <td class="text-center" >' . $row['month_cost'] . '</td>
                            <td class="text-center" >' . $row['target'] . '</td>
                          </tr>';
                       }
                  }
        else {  $output .= '<tr><td colspan="11">لا يوجد سجلات ...</td></tr>'; }}
                  echo $output;
  }
  ?>
