<?php
$us_states = [
    'AL' => 'Alabama','AK' => 'Alaska','AZ' => 'Arizona','AR' => 'Arkansas','CA' => 'California',
    'CO' => 'Colorado','CT' => 'Connecticut','DE' => 'Delaware','FL' => 'Florida','GA' => 'Georgia',
    'HI' => 'Hawaii','ID' => 'Idaho','IL' => 'Illinois','IN' => 'Indiana','IA' => 'Iowa','KS' => 'Kansas',
    'KY' => 'Kentucky','LA' => 'Louisiana','ME' => 'Maine','MD' => 'Maryland','MA' => 'Massachusetts',
    'MI' => 'Michigan','MN' => 'Minnesota','MS' => 'Mississippi','MO' => 'Missouri','MT' => 'Montana',
    'NE' => 'Nebraska','NV' => 'Nevada','NH' => 'New Hampshire','NJ' => 'New Jersey','NM' => 'New Mexico',
    'NY' => 'New York','NC' => 'North Carolina','ND' => 'North Dakota','OH' => 'Ohio','OK' => 'Oklahoma',
    'OR' => 'Oregon','PA' => 'Pennsylvania','RI' => 'Rhode Island','SC' => 'South Carolina',
    'SD' => 'South Dakota','TN' => 'Tennessee','TX' => 'Texas','UT' => 'Utah','VT' => 'Vermont',
    'VA' => 'Virginia','WA' => 'Washington','WV' => 'West Virginia','WI' => 'Wisconsin','WY' => 'Wyoming'
];
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Buyer Status Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background:#f9fafb; }
.card { border: none; border-radius:1rem; box-shadow:0 2px 6px rgba(0,0,0,.08);}
.table thead{background:#212529;color:#fff;}
.table tbody td{vertical-align:middle;}
</style>
</head>
<body class="p-4">
<div class="container">
  <div class="card mb-4">
    <div class="card-body">
      <form id="mainForm" class="row g-3 align-items-center">
        <div class="col-md-4">
          <label class="form-label fw-semibold">Select State</label>
          <select id="state" class="form-select">
            <option value="">Choose...</option>
            <?php foreach($us_states as $code=>$name): ?>
              <option value="<?= $code ?>"><?= $name ?></option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="col-md-4">
          <label class="form-label fw-semibold">Phone Number</label>
          <input type="text" id="phone" class="form-control" placeholder="Enter phone">
        </div>
         <div class="col-md-4 hide d-none">
          <label class="form-label fw-semibold">Zip Code</label>
          <input type="text" id="zip_code" class="form-control" placeholder="Enter phone">
        </div>
        <div class="col-md-4 d-flex align-items-end">
          <button type="button" id="fetchAll" class="btn btn-primary w-100 py-2">Fetch All Data</button>
        </div>
      </form>
    </div>
  </div>

  <div class="card">
    <div class="card-body">
      <h5 class="card-title mb-3">Results</h5>
      <div class="table-responsive">
        <table class="table table-hover align-middle">
          <thead><tr><th>Service</th><th>Response</th></tr></thead>
          <tbody>
            <!-- <tr><td>Buyer Status</td><td id="buyer_aw"></td></tr> -->
            <tr><td>OCN</td><td id="buyers_available_server_ocn"></td></tr>
            <tr><td>PMCPA</td><td id="buyers_available_server_pmcpa"></td></tr>
            <!-- <tr><td>CB</td><td id="buyers_available_server_cb"></td></tr> -->
            <tr><td>ASBON</td><td id="buyer_asb_on"></td></tr>
            <!-- <tr><td>ASBON State</td><td id="buyer_asb_state"></td></tr> -->
            <tr><td>Auren Health </td><td id="buyers_available_server_auren"></td></tr>
            <tr><td>Auren Health 1 (B)</td><td id="buyers_available_server_auren2"></td></tr>
            <tr><td>Auren 2(AV)</td><td id="buyers_available_server_auren3"></td></tr>
            <!-- <tr><td>Auren 4</td><td id="buyers_available_server_auren4"></td></tr> -->
            <tr><td>PMPL</td><td id="buyers_available_server_pmpl"></td></tr>
            <tr><td>M1</td><td id="buyers_available_server_m1"></td></tr>
            <tr><td>MM</td><td id="buyers_available_server_mm"></td></tr>
            <tr><td>PCB</td><td id="buyers_available_server_pcb"></td></tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
function getRandomColor(){
  const letters='0123456789ABCDEF';let c='#';
  for(let i=0;i<6;i++) c+=letters[Math.floor(Math.random()*16)];
  return c;
}

$('#fetchAll').on('click',function(){
  const state=$('#state').val();
    const zip_code=$('#zip_code').val();

  const phone=$('#phone').val();
  if(!state||!phone){ alert('Please select state and enter phone'); return; }

  // map main inputs to old variable names
  window.state=state;
    window.asbon_state=state;
    window.asbon_zipcode=zip_code;

    window.auren2_state=state;
    window.auren_3state=state;

    window.auren_state=state;
        window.PMPL_state=state;

        window.mm_state=state;

        window.M1_state=state;
        window.PMPL_state=state;

        window.pcb_state=state;
  window.ocn_phone=phone;
  window.pmcpa_phone=phone;
  window.cb_phone=phone;
  window.asbon_phone=phone;
  window.auren_phone=phone;
  window.auren2_phone=phone;
  window.auren3_phone=phone;
  window.auren4_phone=phone;
  window.PMPL_phone=phone;
  window.M1_phone=phone;
  window.mm_phone=phone;

  // clear old data
  $('tbody td[id]').html('');

  // call all existing ajax functions
//   fetchStatus();
  OCNFetchStatus();
  PMCPAFetchStatus();
  CBFetchStatus();
  ASBONfetchStatus();
  ASBStateFetchStatus();
  AurenFetchStatus();
  AurenFetchStatus2();
  AurenFetchStatus3();
  AurenFetchStatus4();
  PMPLfetchStatus();
  M1fetchStatus();
  MMFetchStatus();
  PCBFetchStatus();
});
</script>


    <script>
        function getRandomColor() {
            const letters = '0123456789ABCDEF';
            let color = '#';
            for (let i = 0; i < 6; i++) {
                color += letters[Math.floor(Math.random() * 16)];
            }
            return color;
        }

        function fetchStatus() {
            ///$('#buyer_aw').html('<div style="">Checking...</div>');
            state = $("select#state").val();
            asbon_state = $("select#asbon_state").val();
            asbon_zipcode = $("input#asbon_zipcode").val();
            buyer_auren_state = $("select#buyer_auren_state").val();

            $.ajax({
                url: 'buyers_available_server.php?buyer_auren_state=' + buyer_auren_state + '&state=' + state +
                    '&asbon_state=' + asbon_state + '&asbon_zipcode=' + asbon_zipcode,
                method: 'GET',
                dataType: 'json',
                async: true,
                beforeSend: function() {
                    //$('#buyer_aw').html('<div style="">&nbsp;</div>');
                },
                success: function(response) {
                    $('#buyer_aw').text(response.buyer_aw).css('color', getRandomColor());
                    $('#buyer_asb').text(response.buyer_asb).css('color', getRandomColor());
                    $('#buyer_asb_on').text(response.buyer_asb_on).css('color', getRandomColor());
                    // $('#buyer_asb_test').text(response.buyer_asb_test).css('color', getRandomColor());
                    //////////////////////////////////////////////
                    // $('#buyer_asb_state').html(response.buyer_asb_state);
                    // $('#buyer_asb_state p').each(function() {
                    //     $(this).css('color', getRandomColor());
                    // });
                    //////////////////////////////////////////////
                    $('#buyer_auren').html(response.buyer_auren);
                    $('#buyer_auren th').each(function() {
                        $(this).css('color', getRandomColor());
                    });
                    setTimeout(function() {
                        fetchStatus();
                    }, 2000);
                    // setInterval(fetchStatus, 1000);
                },
                error: function() {
                    $('#live-status').text('Error').removeClass().addClass('badge bg-danger');
                }
            });
        }

        fetchStatus();

        function OCNFetchStatus() {
            // return false;
            ///$('#buyer_aw').html('<div style="">Checking...</div>');
            // ocn_phone = $("input#ocn_phone").val();

            $.ajax({
                url: 'buyers_available_server_ocn.php?ocn_phone=' + ocn_phone,
                method: 'GET',
                dataType: 'json',
                async: true,
                beforeSend: function() {
                    //$('#buyer_aw').html('<div style="">&nbsp;</div>');
                },
                success: function(response) {
                    $('#buyers_available_server_ocn').text(response.ocn_msg).css('color',
                        getRandomColor());
                    // setTimeout(function() {
                    //     //ASBONfetchStatus();
                    // }, 20 * 1000);
                },
                error: function() {
                    $('#live-status').text('Error').removeClass().addClass('badge bg-danger');
                }
            });
        }

        function PMCPAFetchStatus() {
            // return false;
            ///$('#buyer_aw').html('<div style="">Checking...</div>');
            // pmcpa_phone = $("input#pmcpa_phone").val();

            $.ajax({
                url: 'buyers_available_server_pmcpa.php?pmcpa_phone=' + pmcpa_phone,
                method: 'GET',
                dataType: 'json',
                async: true,
                beforeSend: function() {
                    //$('#buyer_aw').html('<div style="">&nbsp;</div>');
                },
                success: function(response) {
                    $('#buyers_available_server_pmcpa').text(response.msg).css('color',
                        getRandomColor());
                    // setTimeout(function() {
                    //     //ASBONfetchStatus();
                    // }, 20 * 1000);
                },
                error: function() {
                    $('#live-status').text('Error').removeClass().addClass('badge bg-danger');
                }
            });
        }

        function CBFetchStatus() {
            // return false;
            ///$('#buyer_aw').html('<div style="">Checking...</div>');
            // cb_phone = $("input#cb_phone").val();

            $.ajax({
                url: 'buyers_available_server_cb.php?cb_phone=' + cb_phone,
                method: 'GET',
                dataType: 'json',
                async: true,
                beforeSend: function() {
                    //$('#buyer_aw').html('<div style="">&nbsp;</div>');
                },
                success: function(response) {
                    $('#buyers_available_server_ocn').text(response.ocn_msg).css('color',
                        getRandomColor());
                    // setTimeout(function() {
                    //     //ASBONfetchStatus();
                    // }, 20 * 1000);
                },
                error: function() {
                    $('#live-status').text('Error').removeClass().addClass('badge bg-danger');
                }
            });
        }

        function ASBONfetchStatus() {
            // return false;
            ///$('#buyer_aw').html('<div style="">Checking...</div>');
            state = $("select#state").val();
            // asbon_state = $("select#asbon_state").val();
            // asbon_zipcode = $("input#asbon_zipcode").val();
            // asbon_phone = $("input#asbon_phone").val();
            buyer_auren_state = $("select#buyer_auren_state").val();

            $.ajax({
                url: 'buyers_available_server_abs_on.php?buyer_auren_state=' + buyer_auren_state + '&state=' +
                    state + '&asbon_state=' + asbon_state + '&asbon_zipcode=' + asbon_zipcode + '&asbon_phone=' +
                    asbon_phone,
                method: 'GET',
                dataType: 'json',
                async: true,
                beforeSend: function() {
                    //$('#buyer_aw').html('<div style="">&nbsp;</div>');
                },
                success: function(response) {
                    // $('#buyer_aw').text(response.buyer_aw).css('color', getRandomColor());
                    // $('#buyer_asb').text(response.buyer_asb).css('color', getRandomColor());
                    $('#buyer_asb_on').text(response.buyer_asb_on).css('color', getRandomColor());
                    // $('#buyer_asb_test').text(response.buyer_asb_test).css('color', getRandomColor());
                    //////////////////////////////////////////////
                    // $('#buyer_asb_state').html(response.buyer_asb_state);
                    // $('#buyer_asb_state p').each(function() {
                    //     $(this).css('color', getRandomColor());
                    // });
                    //////////////////////////////////////////////
                    // $('#buyer_auren').html(response.buyer_auren);
                    // $('#buyer_auren th').each(function() {
                    //     $(this).css('color', getRandomColor());
                    // });
                    setTimeout(function() {
                        //ASBONfetchStatus();
                    }, 20 * 1000);
                    // setInterval(fetchStatus, 1000);
                },
                error: function() {
                    $('#live-status').text('Error').removeClass().addClass('badge bg-danger');
                }
            });
        }

        function ASBStateFetchStatus() {
            ///$('#buyer_aw').html('<div style="">Checking...</div>');
            state = $("select#buyer_asb_state_value").val();
            buyer_asb_state_phone = $("input#buyer_asb_state_phone").val();
            asbon_state = $("select#asbon_state").val();
            asbon_zipcode = $("input#asbon_zipcode").val();
            buyer_auren_state = $("select#buyer_auren_state").val();

            $.ajax({
                url: 'buyers_available_server.php?buyer_auren_state=' + buyer_auren_state + '&state=' + state +
                    '&asbon_state=' + asbon_state + '&asbon_zipcode=' + asbon_zipcode + '&buyer_asb_state_phone=' +
                    buyer_asb_state_phone,
                method: 'GET',
                dataType: 'json',
                async: true,
                beforeSend: function() {
                    //$('#buyer_aw').html('<div style="">&nbsp;</div>');
                },
                success: function(response) {
                    $('#buyer_aw').text(response.buyer_aw).css('color', getRandomColor());
                    $('#buyer_asb').text(response.buyer_asb).css('color', getRandomColor());
                    $('#buyer_asb_on').text(response.buyer_asb_on).css('color', getRandomColor());
                    // $('#buyer_asb_test').text(response.buyer_asb_test).css('color', getRandomColor());
                    //////////////////////////////////////////////
                    $('#buyer_asb_state').html(response.buyer_asb_state);
                    $('#buyer_asb_state p').each(function() {
                        $(this).css('color', getRandomColor());
                    });
                    //////////////////////////////////////////////
                    $('#buyer_auren').html(response.buyer_auren);
                    $('#buyer_auren th').each(function() {
                        $(this).css('color', getRandomColor());
                    });
                    setTimeout(function() {
                        ASBStateFetchStatus();
                    }, 3 * 1000);
                    // setInterval(fetchStatus, 1000);
                },
                error: function() {
                    $('#live-status').text('Error').removeClass().addClass('badge bg-danger');
                }
            });
        }

        function AurenFetchStatus() {
            ///$('#buyer_aw').html('<div style="">Checking...</div>');
            state = $("select#state").val();
            // auren_state = $("select#auren_state").val();
            auren_zipcode = $("input#aauren_zipcode").val();
            // auren_phone = $("input#auren_phone").val();
            buyer_auren_state = $("select#buyer_auren_state").val();

            $.ajax({
                url: 'buyers_available_server_auren.php?buyer_auren_state=' + buyer_auren_state + '&state=' +
                    state + '&auren_state=' + auren_state + '&auren_zipcode=' + auren_zipcode + '&auren_phone=' +
                    auren_phone,
                method: 'GET',
                dataType: 'json',
                async: true,
                beforeSend: function() {
                    //$('#buyer_aw').html('<div style="">&nbsp;</div>');
                },
                success: function(response) {
                    //////////////////////////////////////////////
                    $('#buyers_available_server_auren').html(response.buyers_available_server_auren);
                    $('#buyers_available_server_auren').css('color', getRandomColor());

                    setTimeout(function() {
                        //ASBONfetchStatus();
                    }, 20 * 1000);
                    // setInterval(fetchStatus, 1000);
                },
                error: function() {
                    $('#live-status').text('Error').removeClass().addClass('badge bg-danger');
                }
            });
        }

        function AurenFetchStatus2() {
            ///$('#buyer_aw').html('<div style="">Checking...</div>');
            state = $("select#state").val();
            // auren_state = $("select#auren2_state").val();
            auren_zipcode = $("input#aauren2_zipcode").val();
            // auren_phone = $("input#auren2_phone").val();
            buyer_auren_state = $("select#buyer_auren2_state").val();

            $.ajax({
                url: 'buyers_available_server_auren_2.php?buyer_auren_state=' + buyer_auren_state + '&state=' +
                    state + '&auren_state=' + auren_state + '&auren_zipcode=' + auren_zipcode + '&auren_phone=' +
                    auren_phone,
                method: 'GET',
                dataType: 'json',
                async: true,
                beforeSend: function() {
                    //$('#buyer_aw').html('<div style="">&nbsp;</div>');
                },
                success: function(response) {
                    //////////////////////////////////////////////
                    $('#buyers_available_server_auren2').html(response.buyers_available_server_auren);
                    $('#buyers_available_server_auren2').css('color', getRandomColor());

                    setTimeout(function() {
                        //ASBONfetchStatus();
                    }, 20 * 1000);
                    // setInterval(fetchStatus, 1000);
                },
                error: function() {
                    $('#live-status').text('Error').removeClass().addClass('badge bg-danger');
                }
            });
        }

        function AurenFetchStatus3() {
            ///$('#buyer_aw').html('<div style="">Checking...</div>');
            state = $("select#state").val();
            // auren_state = $("select#auren3_state").val();
            auren_zipcode = $("input#aauren3_zipcode").val();
            // auren_phone = $("input#auren3_phone").val();
            buyer_auren_state = $("select#buyer_auren3_state").val();

            $.ajax({
                url: 'buyers_available_server_auren_3.php?buyer_auren_state=' + buyer_auren_state + '&state=' +
                    state + '&auren_state=' + auren_state + '&auren_zipcode=' + auren_zipcode + '&auren_phone=' +
                    auren_phone,
                method: 'GET',
                dataType: 'json',
                async: true,
                beforeSend: function() {
                    //$('#buyer_aw').html('<div style="">&nbsp;</div>');
                },
                success: function(response) {
                    //////////////////////////////////////////////
                    $('#buyers_available_server_auren3').html(response.buyers_available_server_auren);
                    $('#buyers_available_server_auren3').css('color', getRandomColor());

                    setTimeout(function() {
                        //ASBONfetchStatus();
                    }, 20 * 1000);
                    // setInterval(fetchStatus, 1000);
                },
                error: function() {
                    $('#live-status').text('Error').removeClass().addClass('badge bg-danger');
                }
            });
        }

        function AurenFetchStatus4() {
            ///$('#buyer_aw').html('<div style="">Checking...</div>');
            state = $("select#state").val();
            auren_state = $("select#auren4_state").val();
            auren_zipcode = $("input#aauren4_zipcode").val();
            auren_phone = $("input#auren4_phone").val();
            buyer_auren_state = $("select#buyer_auren4_state").val();

            $.ajax({
                url: 'buyers_available_server_auren_4.php?buyer_auren_state=' + buyer_auren_state + '&state=' +
                    state + '&auren_state=' + auren_state + '&auren_zipcode=' + auren_zipcode + '&auren_phone=' +
                    auren_phone,
                method: 'GET',
                dataType: 'json',
                async: true,
                beforeSend: function() {
                    //$('#buyer_aw').html('<div style="">&nbsp;</div>');
                },
                success: function(response) {
                    //////////////////////////////////////////////
                    $('#buyers_available_server_auren4').html(response.buyers_available_server_auren);
                    $('#buyers_available_server_auren4').css('color', getRandomColor());

                    setTimeout(function() {
                        //ASBONfetchStatus();
                    }, 20 * 1000);
                    // setInterval(fetchStatus, 1000);
                },
                error: function() {
                    $('#live-status').text('Error').removeClass().addClass('badge bg-danger');
                }
            });
        }

        function PMPLfetchStatus() {
            ///$('#buyer_aw').html('<div style="">Checking...</div>');
            // PMPL_state = $("select#PMPL_state").val();
            // PMPL_phone = $("input#PMPL_phone").val();
            $.ajax({
                url: 'buyers_available_server_pmpl.php?PMPL_state=' + PMPL_state + '&PMPL_phone=' + PMPL_phone,
                method: 'GET',
                dataType: 'json',
                async: true,
                beforeSend: function() {
                    //$('#buyer_aw').html('<div style="">&nbsp;</div>');
                },
                success: function(response) {
                    //////////////////////////////////////////////
                    $('#buyers_available_server_pmpl').html(response.buyers_available_server_pmpl);
                    //$('#buyers_available_server_pmpl').css('color', getRandomColor());

                    setTimeout(function() {
                        //ASBONfetchStatus();
                    }, 20 * 1000);
                    // setInterval(fetchStatus, 1000);
                },
                error: function() {
                    $('#live-status').text('Error').removeClass().addClass('badge bg-danger');
                }
            });
        }

        function M1fetchStatus() {
            ///$('#buyer_aw').html('<div style="">Checking...</div>');
            // PMPL_state = $("select#M1_state").val();
            // PMPL_phone = $("input#M1_phone").val();
            $.ajax({
                url: 'buyers_available_server_m1.php?M1_state=' + M1_state + '&M1_phone=' + M1_phone,
                method: 'GET',
                dataType: 'json',
                async: true,
                beforeSend: function() {
                    //$('#buyer_aw').html('<div style="">&nbsp;</div>');
                },
                success: function(response) {
                    //////////////////////////////////////////////
                    $('#buyers_available_server_m1').html(response.buyers_available_server_m1);
                    //$('#buyers_available_server_m1').css('color', getRandomColor());

                    setTimeout(function() {
                        //M1fetchStatus();
                    }, 20 * 1000);
                    //setInterval(fetchStatus, 1000);
                },
                error: function() {
                    $('#live-status').text('Error').removeClass().addClass('badge bg-danger');
                }
            });
        }

        function MMFetchStatus() {
            ///$('#buyer_aw').html('<div style="">Checking...</div>');
            // mm_state = $("select#mm_state").val();
            // mm_phone = $("input#mm_phone").val();
            $.ajax({
                url: 'buyers_available_server_mm.php?state=' + mm_state + '&phone=' + mm_phone,
                method: 'GET',
                dataType: 'json',
                async: true,
                beforeSend: function() {
                    //$('#buyer_aw').html('<div style="">&nbsp;</div>');
                },
                success: function(response) {
                    //////////////////////////////////////////////
                    $('#buyers_available_server_mm').html(response.buyers_available_server_mm);
                    $('#buyers_available_server_mm').css('color', getRandomColor());

                    setTimeout(function() {
                        //M1fetchStatus();
                    }, 20 * 1000);
                    //setInterval(fetchStatus, 1000);
                },
                error: function() {
                    $('#live-status').text('Error').removeClass().addClass('badge bg-danger');
                }
            });
        }

        function PCBFetchStatus() {
            ///$('#buyer_aw').html('<div style="">Checking...</div>');
            // pcb_state = $("select#pcb_state").val();
            $.ajax({
                url: 'buyers_available_server_pcb.php?state=' + pcb_state,
                method: 'GET',
                dataType: 'json',
                async: true,
                beforeSend: function() {
                    //$('#buyer_aw').html('<div style="">&nbsp;</div>');
                },
                success: function(response) {
                    //////////////////////////////////////////////
                    $('#buyers_available_server_pcb').html(response.buyers_available_server_pcb);
                    $('#buyers_available_server_pcb').css('color', getRandomColor());

                    setTimeout(function() {
                        //M1fetchStatus();
                    }, 20 * 1000);
                    //setInterval(fetchStatus, 1000);
                },
                error: function() {
                    $('#live-status').text('Error').removeClass().addClass('badge bg-danger');
                }
            });
        }
    </script>
</body>
</html>
