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

$father = bdr_current_user_id('../logout.php');
$user = $father;
$branch = $_SESSION['branch'] ?? null;
$db = bdr_database();
$conn = $db;

if(isset($_POST["ad_a"])){
  $ad_a = $_POST["ad_a"] ;
  $master = bdr_user_has_privilege($db, $father, [20, 40]) ? 1 : 0;
  $output = '';
  $ad_a = "%".$ad_a."%";
  $sql =$db->prepare("SELECT cust_name , cust_id , idSA , phone , address , lic_expire from customers
  where working_woner like ? OR  working_address like ? OR address like ? OR comm like ? LIMIT 30");
  $sql->bindParam(1,$ad_a);
  $sql->bindParam(2,$ad_a);
  $sql->bindParam(3,$ad_a);
  $sql->bindParam(4,$ad_a);
  $sql->execute();
   if($sql->rowCount()>0)
  {
     while($row = $sql->fetch())
       {
         if($master > 0){
           $output .= '
                <tr>
                     <td>
                     <a href="acc/cust_update.php?id=' . $row['cust_id'] . '"target="_blank"><button type="button" class="btn btn-success btn-xs ">تحديث</button></a>
                     <a href="a/cust_print.php?cust_id=' . $row['cust_id'] . '"target="_blank"><button type="button" class="btn btn-info btn-xs ">الرخصة</button></a>
                     </td>' ;
         }else{
           $output .= '
                <tr>
                     <td>
                     <a href="a/cust_update.php?id=' . $row['cust_id'] . '"target="_blank"><button type="button" class="btn btn-success btn-xs ">تحديث</button></a>
                     <a href="a/cust_print.php?cust_id=' . $row['cust_id'] . '"target="_blank"><button type="button" class="btn btn-info btn-xs ">الرخصة</button></a>
                     </td>' ;
         }
            $output .= '
                      <td >' . $row['cust_name'] . '</td>
                      <td class="text-center" >' . $row['phone'] . '</td>
                      <td>' . $row['address'] . '</td>
                      <td class="text-center">' . $row['idSA'] . '</td>
                      <td class="text-center" >' . $row['lic_expire'] . '</td>
                  </tr>
            ';
       }
  }
  else
  {
       $output .= '<tr>
                           <td colspan="7"><h3>لا يوجد سجلات ...</h3></td>
                      </tr>';
  }
  echo $output;
}

if(isset($_POST["name"])){
  $name = $_POST["name"] ;
  $name = "%".$name."%";
  $master = bdr_user_has_privilege($db, $father, [20, 40]) ? 1 : 0;
  $output = '';
  $sql =$db->prepare("select cust_name , cust_id , idSA , phone , address , lic_expire from customers
  where cust_name like ? ");
  $sql->bindParam(1,$name);
  $sql->execute();
   if($sql->rowCount()>0)
  {
     while($row = $sql->fetch())
       {
         if($master > 0){
           $output .= '
                <tr>
                     <td>
                     <a href="acc/cust_update.php?id=' . $row['cust_id'] . '"target="_blank"><button type="button" class="btn btn-success btn-xs ">تحديث</button></a>
                     <a href="a/cust_print.php?cust_id=' . $row['cust_id'] . '"target="_blank"><button type="button" class="btn btn-info btn-xs ">الرخصة</button></a>
                     </td>' ;
         }else{
           $output .= '
                <tr>
                     <td>
                     <a href="a/cust_update.php?id=' . $row['cust_id'] . '"target="_blank"><button type="button" class="btn btn-success btn-xs ">تحديث</button></a>
                     <a href="a/cust_print.php?cust_id=' . $row['cust_id'] . '"target="_blank"><button type="button" class="btn btn-info btn-xs ">الرخصة</button></a>
                     </td>' ;
         }
            $output .= '
                      <td >' . $row['cust_name'] . '</td>
                      <td class="text-center" >' . $row['phone'] . '</td>
                      <td>' . $row['address'] . '</td>
                      <td class="text-center">' . $row['idSA'] . '</td>
                      <td class="text-center" >' . $row['lic_expire'] . '</td>
                  </tr>
            ';
       }
  }
  else
  {
       $output .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
  }
  echo $output;
}

if(isset($_POST["numb"])){
  $numb = $_POST["numb"] ;
  $numb = "%".$numb."%";
  $master = bdr_user_has_privilege($db, $father, [20, 40]) ? 1 : 0;
  $output = '';
  $sql =$db->prepare("SELECT cust_name , cust_id , idSA , phone , address , lic_expire from customers
  where idSA like ? OR phone like ? OR phone2 like ? OR license like ? ");
  $sql->bindParam(1,$numb);
  $sql->bindParam(2,$numb);
  $sql->bindParam(3,$numb);
  $sql->bindParam(4,$numb);
  $sql->execute();
   if($sql->rowCount()>0)
  {
     while($row = $sql->fetch())
       {
         if($master > 0){
           $output .= '
                <tr>
                     <td>
                     <a href="acc/cust_update.php?id=' . $row['cust_id'] . '"target="_blank"><button type="button" class="btn btn-success btn-xs ">تحديث</button></a>
                     <a href="a/cust_print.php?cust_id=' . $row['cust_id'] . '"target="_blank"><button type="button" class="btn btn-info btn-xs ">الرخصة</button></a>
                     </td>' ;
         }else{
           $output .= '
                <tr>
                     <td>
                     <a href="a/cust_update.php?id=' . $row['cust_id'] . '"target="_blank"><button type="button" class="btn btn-success btn-xs ">تحديث</button></a>
                     <a href="a/cust_print.php?cust_id=' . $row['cust_id'] . '"target="_blank"><button type="button" class="btn btn-info btn-xs ">الرخصة</button></a>
                     </td>' ;
         }
            $output .= '
                      <td >' . $row['cust_name'] . '</td>
                      <td class="text-center" >' . $row['phone'] . '</td>
                      <td>' . $row['address'] . '</td>
                      <td class="text-center">' . $row['idSA'] . '</td>
                      <td class="text-center" >' . $row['lic_expire'] . '</td>
                  </tr>
            ';
       }
  }
  else
  {
       $output .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
  }
  echo $output;
}
  ?>
