<cfinclude template="/includes/init.cfm">

<!--- Handle logout --->
<cfif structKeyExists(url,'logout')>
  <cfset StructClear(session)>
  <cflocation url="index.cfm">
</cfif>

<!--- Ensure user is logged in --->
<cfif NOT structKeyExists(session, 'user')>
  <cflocation url="index.cfm">
</cfif>

<!--- Update last_seen for this user --->
<cfquery name="updateLastSeen" datasource="MC">
  UPDATE imagine
  SET last_seen = <cfqueryparam value="#Now()#" cfsqltype="cf_sql_timestamp">
  WHERE email = <cfqueryparam value="#session.user#" cfsqltype="cf_sql_varchar">
</cfquery>

<!--- Pagination & sorting params --->
<cfparam name="url.page" default="1">
<cfparam name="url.limit" default="20">
<cfparam name="url.sort" default="business_name">
<cfparam name="url.dir" default="asc">
<cfparam name="url.query" default="">
<cfparam name="url.field" default="business_name">
<cfset offset = (url.page - 1) * url.limit>

<!--- Count total records --->
<cfquery name="qTotal" datasource="MC">
  SELECT COUNT(*) AS total FROM business
  <cfif url.query NEQ "">
    WHERE #url.field# LIKE <cfqueryparam value="%#url.query#%" cfsqltype="cf_sql_varchar">
  </cfif>
</cfquery>
<cfset total = qTotal.total>

<!--- Fetch page of records --->
<cfquery name="qBiz" datasource="MC">
  SELECT business_id, business_name, address_1, city, state, zip
  FROM business
  <cfif url.query NEQ "">
    WHERE #url.field# LIKE <cfqueryparam value="%#url.query#%" cfsqltype="cf_sql_varchar">
  </cfif>
  ORDER BY #url.sort# #url.dir#
  LIMIT <cfqueryparam cfsqltype="cf_sql_integer" value="#offset#">,
        <cfqueryparam cfsqltype="cf_sql_integer" value="#url.limit#">
</cfquery>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Dashboard</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <cfoutput>
<style>
    /* reuse existing index styles */
    html, body { background-color: #session.siteStyle.BG_Color#;
      background-image: url('/images/bg1.png'); background-repeat:no-repeat;
      background-position:center top; background-size:cover;
      margin:0; padding:0; min-height:100vh; display:flex; flex-direction:column;
    }
    
    
 /* —— Header & Footer centering only —— */
.site-header,
.site-footer {
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}

/* —— Nav: transparent 12px bar, no padding —— */
.site-nav {
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;

  /* remove any bg so page‐background shows through */
  background: none !important;

  /* lock the height at 12px */
  height: 50px !important;

  /* kill any vertical padding */
  padding: 0 !important;

  /* you probably don’t need float‐containment any more */
  overflow: visible;
}
   

/* —— Header box sizing & rounding —— */
.site-header header {
  /* match your container width */
  max-width: 1400px;
  width: 100%;
  /* push it down a little from the top of the viewport */
  margin: 10px auto 0 auto;
  /* ensure it’s tall enough to hold your logo */
  height: 140px;
  /* white‐background box (or swap this for your session color) */
  background: white;
  /* same shadow you’re using elsewhere, if desired */
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
  /* round both top corners */
  border-radius: 20px 20px 0 0;
  box-sizing: border-box;
}
/* ——————————————————————
   Contain floats & show background
   —————————————————————— */
.site-nav {
  /* your session-driven menu color */
  background-color: #session.siteStyle.menu_background#;
  /* guarantee some height even if children float */
  padding: 10px 0;
  /* optional shadow if you like */
  /* box-shadow: 0 2px 5px rgba(0,0,0,0.2); */
  /* make sure floats don’t collapse it */
  overflow: visible;
}

/* clearfix to wrap any floated <li> or other elements */
.site-nav::after {
  content: "";
  display: table;
  clear: both;
}

.site-nav a {
  font-family: Helvetica, Arial, sans-serif;
}


/* ——————————————————————
   Reinforce on small screens
   —————————————————————— */
@media only screen and (max-width: 767px) {
  .container {
    width: 100% !important;
    padding: 0 !important;
  }
  .text_box {
    /* restore your interior padding if you still want it */
    padding: 20px !important;
    /* or remove entirely: padding: 0 !important; */
  }
}
.site-header header #logo {
  /* position your logo exactly as in the working stylesheet */
  margin-top: 20px;
  margin-left: 20px;
}





    .content-wrapper { flex:1; }
    .container { max-width:1400px; width:100%; margin:0 auto; padding:0; }
    .text_box { background:white; box-shadow:0 0 10px rgba(0,0,0,0.3); border-radius:0px; padding:20px; box-sizing:border-box; display:flex; flex-direction:column; align-items:flex-start; max-width:1400px; width:100%; margin:0 auto; }
    .search-bar {
      width:100%;
      margin-bottom:15px;
      display:flex;
      gap:10px;
    }
    .search-bar input,
    .search-bar select,
    .search-bar button {
      padding:8px;
      border-radius:8px;
    }
    .search-bar input, .search-bar select, .search-bar button {
      padding:8px;
      border-radius:8px;
    }
    .search-bar button {
      background-color: #session.siteStyle.button_color#;
      color: white;
      border: none;
      cursor: pointer;
    }
    .search-bar input::placeholder {
      color: ##aaa;
    }
    .table-container { width:100%; overflow-x:auto; }
    table { width:100%; border-collapse:collapse; }
    th, td { padding:8px; text-align:left; }
    /* header row background */
    thead tr { background-color: white; }
    thead th { background-color: white; }
    thead tr { background-color: white; }
    th { font-weight:bold; font-size:1.1em; color:#session.siteStyle.column_title_color#; cursor:pointer; }
    tr:nth-child(odd) { background:#session.siteStyle.table_row_color_1#; }
    tr:nth-child(even) { background:#session.siteStyle.table_row_color_2#; }
    tr:hover { background:#session.siteStyle.table_row_color_hover#; }
    .pagination { margin-top:15px; text-align:right; }
    @media only screen and (max-width: 767px) {
      .container, .text_box {
        width: 100% !important;
        padding: 0 10px;
      }
    }
    .pagination select { margin-left:10px; }



    
</style>
</cfoutput>
</head>
<body>
  <div class="site-header">
    <cfinclude template="/includes/header.cfm">
  </div>
  <div class="site-nav">
    <cfinclude template="/dashboard/includes/dashboardmenu.cfm">
  </div>

  <div class="content-wrapper">
    <div class="container">
      <div class="text_box">
        <!--- Logout link --->
        <div style="width:100%; text-align:right; margin-bottom:10px;">
          <a href="#" onclick="if(confirm('Are you sure you want to logout?')){window.location.href='?logout=1';}">Logout</a>
        </div>
        <!--- Search --->
        <div class="search-bar">
          <form method="get" style="display:flex; width:100%; gap:10px;">
            <cfoutput><input type="text" name="query" maxlength="30" value="#url.query#" placeholder="Search..." style="width:30ch; border-radius:8px; padding:8px;"></cfoutput>
            <select name="field">
              <option value="business_name" <cfif url.field EQ 'business_name'>selected</cfif>>Business</option>
              <option value="address_1" <cfif url.field EQ 'address_1'>selected</cfif>>Address</option>
              <option value="city" <cfif url.field EQ 'city'>selected</cfif>>City</option>
              <option value="state" <cfif url.field EQ 'state'>selected</cfif>>State</option>
              <option value="zip" <cfif url.field EQ 'zip'>selected</cfif>>Zip</option>
            </select>
            <button type="submit">Search</button><button type="button" onclick="window.location.href=window.location.pathname">Reset</button>
          </form>
        </div>

        <!--- Table --->
        <div class="table-container">
          <table>
            <thead>
              <tr>
                <th style="width:3ch"></th>
                <th style="width:30ch" onclick="location.href='?page=1&sort=business_name&dir=#IIF(url.sort EQ 'business_name' AND url.dir EQ 'asc','desc','asc')#&limit=#url.limit#&query=#url.query#&field=#url.field#'">Business</th>
                <th style="width:30ch" onclick="location.href='?page=1&sort=address_1&dir=#IIF(url.sort EQ 'address_1' AND url.dir EQ 'asc','desc','asc')#&limit=#url.limit#&query=#url.query#&field=#url.field#'">Address</th>
                <th style="width:20ch" onclick="location.href='?page=1&sort=city&dir=#IIF(url.sort EQ 'city' AND url.dir EQ 'asc','desc','asc')#&limit=#url.limit#&query=#url.query#&field=#url.field#'">City</th>
                <th style="width:4ch" onclick="location.href='?page=1&sort=state&dir=#IIF(url.sort EQ 'state' AND url.dir EQ 'asc','desc','asc')#&limit=#url.limit#&query=#url.query#&field=#url.field#'">State</th>
                <th style="width:10ch" onclick="location.href='?page=1&sort=zip&dir=#IIF(url.sort EQ 'zip' AND url.dir EQ 'asc','desc','asc')#&limit=#url.limit#&query=#url.query#&field=#url.field#'">Zip</th>
              </tr>
            </thead>
            <tbody>
  <cfoutput query="qBiz">
    <tr onclick="location.href='business.cfm?business_id=#qBiz.business_id#'">
      <td>#offset + qBiz.currentRow#</td>
      <td>#HTMLEditFormat(qBiz.business_name)#</td>
      <td>#HTMLEditFormat(qBiz.address_1)#</td>
      <td>#HTMLEditFormat(qBiz.city)#</td>
      <td>#HTMLEditFormat(qBiz.state)#</td>
      <td>#HTMLEditFormat(qBiz.zip)#</td>
    </tr>
  </cfoutput>
</tbody>
          </table>
        </div>

                </div> <!-- end text_box -->
      </div> <!-- end container -->
    </div> <!-- end content-wrapper -->
    <div class="site-footer" style="max-width:1400px; margin:20px auto 0 auto;">
      <cfinclude template="/includes/footer.cfm">
    </div>
        <div class="pagination">
          <cfset pages = Ceiling(total / url.limit)>
          <cfloop index="p" from="1" to="#pages#">
            <cfif p EQ url.page>
              <span><cfoutput>page #p#</cfoutput></span>
            <cfelse>
              <a href="?page=#p#&limit=#url.limit#&query=#url.query#&field=#url.field#&sort=#url.sort#&dir=#url.dir#">#p#</a>
            </cfif>
          </cfloop>
          <select onchange="location.search='?limit='+this.value+'&query=#url.query#&field=#url.field#&sort=#url.sort#&dir

