<?php

    $json = file_get_contents('php://input');   
    $obj = json_decode($json);
    $id = $obj->flag;
    
    // Returns a Json
    header('Content-Type: application/json');
    
    $first_pass = $obj->pass_anterior;
    $old_password;
    $passwd = $obj->passwd;
    $passwdConfirm = $obj->passwdConfirm;
    
    $conn = new mysqli("directoriofitness.com.mx", "direct18", "L4LL4v3M@3str@","$obj->bd");
    $conn -> set_charset("utf8");
    
    $query = "SELECT socios.Contrasena FROM socios WHERE socios.idSocio = '$obj->id_cliente'";
    $resultado = $conn->query($query);

    while($row = $resultado->fetch_assoc()){

        $old_password = $row['Contrasena'];
    }

    if(password_verify($first_pass, $old_password)){
        if($passwd == $passwdConfirm){
            if(strlen($passwdConfirm) >= 6){
                $passHash = password_hash($passwdConfirm, PASSWORD_BCRYPT);
                $queryUpdate = "UPDATE socios SET socios.Contrasena = '$passHash' WHERE socios.idSocio = '$obj->id_cliente'";
                if($conn->query($queryUpdate) == TRUE){
                    echo 'success';
                }else{
                    echo 'Ocurrio un error';
                }
            }else{
                echo 'Tu contrasena nueva debe contener al menos 6 caracteres';
            }
                
        }else{
            echo 'Las contrasenas no coinciden';
        }
    }else{
        echo 'Tu contrasena actual no coincide, intenta de nuevo.';
    }
    
    $conn->close(); 
?>