<?php

session_start();

include "../db.php";



if (isset($_POST['uStatus']) && isset($_POST['userId'])) {

    $uStatus = mysqli_real_escape_string($con, $_POST['uStatus']);

    $userId = mysqli_real_escape_string($con, $_POST['userId']);



    $user_sql = mysqli_query($con, "SELECT * FROM users WHERE uid = '$userId' ");

    if($user_row = mysqli_fetch_assoc($user_sql)){

        $ucontact = $user_row["ucontact"];

        $uname = $user_row["uname"];

        $uemail = $user_row["uemail"];

    }



    // Fetch data_subCategory and data_category for the user

    $checkSql = "SELECT * FROM data WHERE data_up_for = '$userId' AND data_subCategory = 'KYC' AND data_catagory = 'Documents'"; 

    $checkResult = mysqli_query($con, $checkSql);



    if ($uStatus == 1) {

        if ($checkResult && mysqli_num_rows($checkResult) > 0) {

            // Update uStatus to Active

            $updateSql = "UPDATE users SET uStatus = '$uStatus' WHERE uid = '$userId'";

            if (mysqli_query($con, $updateSql)) {

                // Send SMS 

                $api_key = '45899D6EF8D236';

                $entity = '1501681760000015777';

                $template_id = '1407173554566676394';

                $contacts = $ucontact;

                $message = "Your account is verified and activeted.";

                $from = 'DXTECH';

                $sms_text = urlencode(''.$uname.' Password to Recover your Account is '.$message.' Do not share with anyone. DIGI X');



                //Submit to server

                $ch = curl_init();

                curl_setopt($ch,CURLOPT_URL, "http://manage.sambsms.com/app/smsapi/index.php");

                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

                curl_setopt($ch, CURLOPT_POST, 1);

                curl_setopt($ch, CURLOPT_POSTFIELDS, "key=".$api_key."&entity=".$entity."&tempid=1407173554566676394&campaign=0&routeid=7&type=text&contacts=".$contacts."&senderid=".$from."&msg=".$sms_text);



                $smsResponse = curl_exec($ch);

                curl_close($ch);



                echo json_encode(['status' => 'success']);

            } else {

                echo json_encode(['status' => 'error', 'message' => mysqli_error($con)]);

            }

        } else {

            // Redirect to KYC page

            echo json_encode(['redirect' => "kyc.php?user=$userId"]);

        }

    } elseif ($uStatus == 0) {

        // Update uStatus to Inactive

        $updateSql = "UPDATE users SET uStatus = '$uStatus' WHERE uid = '$userId'";

        if (mysqli_query($con, $updateSql)) {

            // Send SMS 

            $api_key = '45899D6EF8D236';

            $entity = '1501681760000015777';

            $template_id = '1407173554566676394';

            $contacts = $ucontact;

            $message = "Your account is temporarily suspended.";

            $from = 'DXTECH';

            $sms_text = urlencode(''.$uname.' Password to Recover your Account is '.$message.' Do not share with anyone. DIGI X');



            //Submit to server

            $ch = curl_init();

            curl_setopt($ch,CURLOPT_URL, "http://manage.sambsms.com/app/smsapi/index.php");

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

            curl_setopt($ch, CURLOPT_POST, 1);

            curl_setopt($ch, CURLOPT_POSTFIELDS, "key=".$api_key."&entity=".$entity."&tempid=1407173554566676394&campaign=0&routeid=7&type=text&contacts=".$contacts."&senderid=".$from."&msg=".$sms_text);



            $smsResponse = curl_exec($ch);

            curl_close($ch);



            echo json_encode(['status' => 'success']);

        } else {

            echo json_encode(['status' => 'error', 'message' => mysqli_error($con)]);

        }

    }

} else {

    echo json_encode(['status' => 'error', 'message' => 'Invalid request']);

}