<?php
include("../../include/db.php");
error_reporting(0);

// Fetch dynamic day value from customer_status_creation table
$daySql = "SELECT customer_status 
           FROM customer_status_creation 
           WHERE status_type = 'Active' 
             AND delete_status = '0' 
           LIMIT 1";
$dayResult = mysql_query($daySql);
$dayRow = mysql_fetch_assoc($dayResult);
$dynamicDays = $dayRow ? (int)$dayRow['customer_status'] : 60; // fallback to 60 if not found

// echo "Using dynamic days: $dynamicDays<br>";
// Auto Active → In-active (no job in X days)
$sql = "SELECT party_name 
        FROM party_creation 
        WHERE status_type = 'Active' 
          AND delete_status = '0' 
          AND approve_status = '1'";
$rs = mysql_query($sql);

while ($row = mysql_fetch_assoc($rs)) {
    $partyName = mysql_real_escape_string($row['party_name']);

    $check = "SELECT COUNT(*) AS cnt 
              FROM jobcard_sublist 
              WHERE customer_name = '$partyName'
                AND createdate >= DATE_SUB(CURDATE(), INTERVAL $dynamicDays DAY)";

    $rsCheck = mysql_query($check);
    $result = mysql_fetch_assoc($rsCheck);

    if ($result['cnt'] == 0) {
        $update = "UPDATE party_creation 
                   SET status_type = 'In-active' 
                   WHERE party_name = '$partyName'";
        mysql_query($update);
        echo "Updated: {$partyName} → In-active (No job in last {$dynamicDays} days)<br>";
    }
}
?>
