
>         public function searchLookup()
      {
          $type = $this->request->getVar('type');
          $keyword = trim($this->request->getVar('keyword') ?? '');
          $db = \Config\Database::connect();
          
          $table = '';
          $nameField = '';
          $query = null;
          
          if (stripos($type, 'Origin') !== false || stripos($type, 'Destination') !== false || stripos($type, 'City') 
!== false) {
              $nameField = 'c.name';
              $query = $db->table('cities c')
                  ->select("c.id, c.name, s.name as state_name, s.state_code as code")
                  ->join('states s', 'c.state_id = s.id', 'left');
          } else if (stripos($type, 'State') !== false) {
              $nameField = 'name';
              $query = $db->table('states')->select("id, {$nameField} as name, state_code as code");
          } else if (stripos($type, 'Company') !== false) {
              $nameField = 'c.company_name';
              $query = $db->table('customers c')
                  ->select("c.id, c.company_name as name, CONCAT(c.first_name, ' ', IFNULL(c.last_name, '')) as 
contact_name, c.email, c.phone as mobile, c.gst_no, ca.address, ca.city, ca.state, p.pincode")
                  ->join('customer_addresses ca', 'c.id = ca.customer_id AND ca.is_default = 1', 'left')
                  ->join('pincodes p', 'ca.pincode_id = p.id', 'left');
          } else {
              // Default to cities if unknown
              $nameField = 'c.name';
              $query = $db->table('cities c')
                  ->select("c.id, c.name, s.name as state_name, s.state_code as code")
                  ->join('states s', 'c.state_id = s.id', 'left');
          }
  
          if (!empty($keyword)) {
              $query->like($nameField, $keyword);
          }
          // Exclude empty names
          $query->where("{$nameField} !=", '');
          $query->where("{$nameField} IS NOT NULL");
          
          $results = $query->limit(50)->get()->getResultArray();
          foreach ($results as &$r) {
              if (isset($r['name']) && empty($r['code'])) {
                  $r['code'] = strtoupper(substr(preg_replace('/[^a-zA-Z]/', '', $r['name']), 0, 3));
              }
          }
          return $this->response->setJSON(['status' => 'success', 'data' => $results]);


