<?php 

include "../config/connect.php";

// try {
//     $sqlData = "SELECT id, email FROM pelanggan WHERE id_data_server = '12' AND email = 'mikrotikbackupdnynet@gmail.com'";
//     $stmtData = $pdo->query($sqlData);
//     $no = 1;

//     while ($dataambil = $stmtData->fetch(PDO::FETCH_ASSOC)) {
//         $num = str_pad($no, 4, '0', STR_PAD_LEFT);
//         $email = "dnynet" . $num . "@gmail.com";

//         // Update data pelanggan
//         // $stmt = $pdo->prepare("UPDATE pelanggan SET email = :email WHERE id = :id AND email = :old_email");
//         // $stmt->bindParam(':email', $email);
//         // $stmt->bindParam(':id', $dataambil['id']);
//         // $stmt->bindParam(':old_email', $dataambil['email']);
//         // $stmt->execute();

//         echo $dataambil['id'] . " => " . $dataambil['email'] . " Jadi => " . $email . "<br>";
//         $no++;
//     }
// } catch (PDOException $e) {
//     error_log('Database Error: ' . $e->getMessage());
//     echo "Database error occurred: " . $e->getMessage();
// } catch (Exception $e) {
//     error_log('General Error: ' . $e->getMessage());
//     echo "An error occurred: " . $e->getMessage();
// }
try {
    // Menggunakan SUBSTRING dengan indeks yang benar untuk SQL
    $sqlData = "SELECT id, email, nama FROM v_tagihan WHERE id_data_server = '12' AND status = 'buat' AND SUBSTRING(email, 1, 6) = 'dnynet'";
    $stmtData = $pdo->query($sqlData);
    $no = 1;

    while ($dataambil = $stmtData->fetch(PDO::FETCH_ASSOC)) {
        $num = str_pad($no, 4, '0', STR_PAD_LEFT);
        $email = "dnynet" . $num . "@gmail.com";

        // Update kolom menjadi NULL
        $stmt = $pdo->prepare("UPDATE tagihan SET transaction_id = NULL, transaction_link = NULL, transaction_status = NULL WHERE id = :idtag");
        $stmt->bindParam(':idtag', $dataambil['id']);
        $stmt->execute();

        echo $dataambil['id'] . " Email => " . $dataambil['email'] . " Nama => " . $dataambil['nama'] . "<br>";
        $no++;
    }
} catch (PDOException $e) {
    error_log('Database Error: ' . $e->getMessage());
    echo "Database error occurred: " . $e->getMessage();
} catch (Exception $e) {
    error_log('General Error: ' . $e->getMessage());
    echo "An error occurred: " . $e->getMessage();
}


?>
