<?php
include("../includes/db_connection.php");
include("../includes/auth.php");

$id = $_POST['id'];
$name = $_POST['name'];
$org_name = $_POST['org_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$country = $_POST['country'];
$pin_zip = $_POST['pin_zip'];
$address = $_POST['address'];
$tax_id = $_POST['tax_id'];
$status = $_POST['status'];
$password = $_POST['password'];

if($id){

    if(!empty($password)){
        $hash = password_hash($password,PASSWORD_DEFAULT);
        $stmt = $conn->prepare("UPDATE associates SET name=?,org_name=?,email=?,phone=?,country=?,pin_zip=?,address=?,tax_id=?,status=?,password=? WHERE id=?");
        $stmt->bind_param("ssssssssssi",$name,$org_name,$email,$phone,$country,$pin_zip,$address,$tax_id,$status,$hash,$id);
    } else {
        $stmt = $conn->prepare("UPDATE associates SET name=?,org_name=?,email=?,phone=?,country=?,pin_zip=?,address=?,tax_id=?,status=? WHERE id=?");
        $stmt->bind_param("sssssssssi",$name,$org_name,$email,$phone,$country,$pin_zip,$address,$tax_id,$status,$id);
    }

} else {

    $hash = password_hash("123456",PASSWORD_DEFAULT); // default password
    $stmt = $conn->prepare("INSERT INTO associates (name,org_name,email,phone,country,pin_zip,address,tax_id,status,password,email_verified) VALUES (?,?,?,?,?,?,?,?,?, ?,1)");
    $stmt->bind_param("ssssssssss",$name,$org_name,$email,$phone,$country,$pin_zip,$address,$tax_id,$status,$hash);
}

$stmt->execute();

header("Location: associates.php");
exit;
?>