<?php
    include '../db/db.php';
    session_start();
    $log=htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
    if (empty($log)) {
         
      $redirectUrl = "index.php";
      echo "<script type=\"text/javascript\">";
      echo "window.location.href = '$redirectUrl'";
      echo "</script>";
         
    }else{

      $sqlu="SELECT * FROM details_of_users WHERE user='$log' and val='1'";
      $resultu=mysqli_query($conn, $sqlu);
      $countu=mysqli_num_rows($resultu);
      
    if($countu==1){

      $sqlc="SELECT * FROM details_of_users where user='$log'";
      $resultc=mysqli_query($conn, $sqlc);
      while ($rowc = mysqli_fetch_array($resultc)){
        $name=$rowc['id'];
      }

      if (array_key_exists('del_cus', $_POST)) {
        /*echo $_POST['uname'];*/
        $id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');

        $sql2 = mysqli_query($conn, "DELETE FROM details_of_users WHERE id = $id");
        $sql3 = mysqli_query($conn, "DELETE FROM billing_address WHERE u_id = $id");
        $sql4 = mysqli_query($conn, "DELETE FROM shipping_address WHERE u_id = $id");
        
        if ($sql2) {
          $response['status'] = 'ok';
        }else{
          $response['status'] = 'error';
        }

        echo json_encode($response);
        //echo "In";
      }

      if (array_key_exists('load_data', $_POST)) {
        $id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');
        $sql = "SELECT
shipping_address.id,
shipping_address.u_id AS s_u_id,
shipping_address.f_name AS s_f_name,
shipping_address.l_name AS s_l_name,
shipping_address.comp_name AS s_comp_name,
shipping_address.s_address AS s_s_address,
shipping_address.app_address AS s_app_address,
shipping_address.town AS s_town,
shipping_address.state,
shipping_address.zip_no AS s_zip_no,
shipping_address.email AS s_email,
shipping_address.phone AS s_phone,
billing_address.id,
billing_address.u_id,
billing_address.f_name,
billing_address.l_name,
billing_address.comp_name,
billing_address.s_address,
billing_address.app_address,
billing_address.town,
billing_address.state,
billing_address.zip_no,
billing_address.email,
billing_address.phone
FROM
details_of_users
Inner Join billing_address ON details_of_users.id = billing_address.u_id
Inner Join shipping_address ON details_of_users.id = shipping_address.u_id
WHERE
details_of_users.id =  '$id'
";
      //echo $sql;
        $result = mysqli_query($conn, $sql);
        $row = mysqli_fetch_assoc($result);

        echo json_encode($row);
      }

  } else{

    $redirectUrl = "../index.php";
    echo "<script type=\"text/javascript\">";
    echo "window.location.href = '$redirectUrl'";
    echo "</script>";

  }
}
?>