<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>

$(document).on("click", ".addbiz", function () {
    event.preventDefault();  
    // Close action sheet first
    $("#addbizModal").modal("show");
    

    // Wait until it's closed, then open product modal
    setTimeout(function () {
        $("#Admin_PanelRight").modal("hide");
    }, 300);

});


$("#secondregisterBtn").click(function () {

    $("#formLoader1").show();        // ðŸ”„ Show loader
    $("#secondregisterBtn").prop("disabled", true); // Prevent double click

    $.ajax({
        url: "../includes/second_biz_register.php",
        type: "POST",
        data: $("#secondregisterForm").serialize(),
        dataType: "json",

            success: function (response) {
            
                $("#formLoader1").hide();
                $("#secondregisterBtn").prop("disabled", false);
            
                if (response.status === "success") {
                    alert("Business created");
                    $("#addbizModal").modal("hide");
                } else {
                    alert(response.message || response.type || "Unknown error");
                }
            },

        error: function () {
            $("#formLoader1").hide();
            $("#secondregisterBtn").prop("disabled", false);
            alert("Server error. Try again.");
        }
    });

});


function loadcompanies(reset = false) {
    
    let comp_id = $('#sel_co').val();

    // ✅ Reset mode (when store changes)
    if (reset) {
        acc_offset = 0;
        acc_finished = false;
        acc_loading = false;
        $("#accounts").html("");
       
    }

   // if (!storeid) {
   //     console.warn("storeid not ready");
   //     return;
   // }

    if (acc_loading || acc_finished) {
        return;
    }

    acc_loading = true;
   // showLoader6();

    console.log("Loading...", { storeid, acc_offset });

    $.ajax({
        url: "fetch_scripts/fetch_company_list.php",
        type: "GET",
        data: {
            acc_limit: acc_limit,
            acc_offset: acc_offset,
            //storeid: storeid,
            comp_id: comp_id
        },
        dataType: "json",

        success: function (response) {

            //hideLoader6();

            if (!response || response.length === 0) {

                acc_finished = true;

                if (acc_offset === 0) {
                      // ❌ No data at all → hide whole section
                        $("#accountsLoader").hide();
                        $(".accounts-balances").hide();
                        
                    $("#accounts").html(`
                        <tr>
                            <td colspan="4" class="text-center text-muted p-3">
                                No Transactions Found
                            </td>
                        </tr>
                    `);
                } else {
                    $(".accounts-balances").show();
                    $("#accounts").append(`
                        <tr>
                            <td colspan="4" class="text-center text-muted p-3">
                                No more Transactions
                            </td>
                        </tr>
                    `);
                }

                return;
            }

            let rows = "";

            response.forEach(function (acc) {

                    //let status = (acc.acc_type || "").toLowerCase();
                  //  let t_type = (acc.acc_type || "").toLowerCase();
                
                    let dateStyle = '';
                    let rowclass = '';
                     let icon = '';
                     icon = '<ion-icon name="cash-outline"></ion-icon>';

                
                   // let total = Number(acc.balance || 0).toLocaleString('en-UG');
                
                  rows += `
                    <tr data-toggle="modal" class="stt-run" data-id="${acc.id}"
                        data-loc_id="${acc.id}"
                        data-accountno="${acc.org_name}"
                        data-accname="${acc.phone}"
                        data-stt_type="${acc.country}"
                        data-type="${acc.city}" 
                        data-type="${acc.Code}"data-target=" ">
                        
                        
                        <td class="${dateStyle}">${acc.org_name || ''}</td>
                        <td class="${dateStyle}">${acc.phone || ''}</td>
                        <td class="${dateStyle}">${acc.country || ''}</td>
                        <td class="${dateStyle}">${acc.city || ''}</td>
                        <td class="${dateStyle}">${acc.Code || ''}</td>
                    
                    </tr>
                    `;
                });
                
            $("#accountsLoader").hide();
            $(".accounts-balances").show();
            $("#accounts").append(rows);

            acc_offset += acc_limit;
            acc_loading = false;
        },

        error: function () {
            console.error("Failed to load accounts");
            acc_loading = false; // ✅ prevent freeze
           // hideLoader6();
        }
    });
}

</script>