<?php

require_once 'includes/provider_auth.php';
require_once '../config/constants.php';

$distribution_id = $_GET['id'] ?? 0;

/*
-----------------------------------
CALL LEADS API
-----------------------------------
*/

$api = API_URL . "providers/leads.php";

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => $api,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'X-API-KEY: ' . API_KEY,
        'Authorization: Bearer ' . $_SESSION['provider_token']
    ]
]);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);

/*
-----------------------------------
FIND THE LEAD
-----------------------------------
*/

$lead = [];

if (isset($data['data']['data'])) {
    foreach ($data['data']['data'] as $row) {
        if ($row['distribution_id'] == $distribution_id) {
            $lead = $row;
            break;
        }
    }
}

$phone = htmlspecialchars($lead['phone'] ?? '');

?>

<?php include '../admin/partials/layouts/layoutTop.php' ?>

<div class="dashboard-main-body">

    <div class="card h-100">

        <div class="card-body p-24">

            <div class="row">

                <div class="col-sm-6">
                    <label>Customer Name</label>
                    <input class="form-control" value="<?php echo htmlspecialchars($lead['name'] ?? ''); ?>" readonly>
                </div>

                <div class="col-sm-6">
                    <label>Phone</label>
                    <input class="form-control" value="<?php echo $phone; ?>" readonly>
                </div>

                <div class="col-sm-6">
                    <label>Category</label>
                    <input class="form-control" value="<?php echo htmlspecialchars($lead['category'] ?? ''); ?>"
                        readonly>
                </div>

                <div class="col-sm-6">
                    <label>Sub Category</label>
                    <input class="form-control" value="<?php echo htmlspecialchars($lead['sub_category'] ?? ''); ?>"
                        readonly>
                </div>

                <div class="col-sm-6">
                    <label>Pincode</label>
                    <input class="form-control" value="<?php echo htmlspecialchars($lead['pincode'] ?? ''); ?>"
                        readonly>
                </div>

                <div class="col-sm-6">
                    <label>Address</label>
                    <input class="form-control" value="<?php echo htmlspecialchars($lead['address'] ?? ''); ?>"
                        readonly>
                </div>

                <div class="col-sm-12">
                    <label>Description</label>
                    <textarea class="form-control" rows="4" readonly>
<?php echo htmlspecialchars($lead['description'] ?? ''); ?>
</textarea>
                </div>

            </div>


            <!-- ACTION BUTTONS -->

            <div style="display:flex;flex-wrap:wrap;gap:12px;margin-top:25px">

                <a href="tel:<?php echo $phone ?>" class="lead-action-btn" data-type="call"
                    style="flex:1 1 45%;background:#28a745;color:#fff;padding:12px;text-align:center;border-radius:8px;text-decoration:none">

                    Call

                </a>


                <a href="https://wa.me/<?php echo $phone ?>" class="lead-action-btn" data-type="whatsapp"
                    style="flex:1 1 45%;background:#0d6efd;color:#fff;padding:12px;text-align:center;border-radius:8px;text-decoration:none">

                    WhatsApp

                </a>


                <a href="leads.php"
                    style="flex:1 1 45%;border:1px solid #dc3545;color:#dc3545;padding:12px;text-align:center;border-radius:8px;text-decoration:none">

                    Back

                </a>

            </div>

        </div>

    </div>

</div>


<script>

    document.querySelectorAll(".lead-action-btn").forEach(function (btn) {

        btn.addEventListener("click", function (e) {

            e.preventDefault();

            let actionType = this.dataset.type;
            let link = this.href;

            fetch("<?php echo BASE_URL ?>api/providers/track-lead-contact.php", {

                method: "POST",

                headers: {
                    "Content-Type": "application/x-www-form-urlencoded",
                    "X-API-KEY": "<?php echo API_KEY ?>",
                    "Authorization": "Bearer <?php echo $_SESSION['provider_token'] ?>"
                },

                body: new URLSearchParams({
                    distribution_id: "<?php echo $distribution_id ?>",
                    provider_id: "<?php echo $_SESSION['provider_id'] ?>",
                    action_type: actionType
                })

            })
                .then(response => response.json())
                .then(data => {

                    console.log("Tracking Response:", data);

                    window.location.href = link;

                })
                .catch(error => {

                    console.log("Tracking Error:", error);

                    window.location.href = link;

                });

        });

    });

</script>

<?php include '../admin/partials/layouts/layoutBottom.php' ?>