<?php
include "include/db.php";
include "include/auth.php";
if($user_type == 'admin'){
    $active_user_id = "admin_id = $admin_id,";
}elseif($user_type == 'staff'){
    $active_user_id = "employee_id = $employee_id,";
}

if(isset($_REQUEST['vendor_name_add']) && isset($_REQUEST['vendor_alias_add'])){
    if(strlen($_REQUEST['vendor_name_add']) >0){
        $time = date("Y-m-d H:i:s");
        $vendor_alias_new = $_REQUEST['vendor_alias_add'];
        $vendor_alias_new = trim($vendor_alias_new);
        $vendor_alias_new = mysqli_real_escape_string($conn, $vendor_alias_new);

        $vendor_name_new = $_REQUEST['vendor_name_add'];
        $vendor_name_new = trim($vendor_name_new);
        $vendor_name_new = mysqli_real_escape_string($conn, $vendor_name_new);
        $vendor_alias_set = 'notset';

        if(strlen($vendor_alias_new) > 1){
            $vendor_alias_set = 'set';
        }

        if($vendor_alias_set == 'notset'){
            $set_vendor_name_queryx = "SELECT * FROM vendor WHERE vendor_name = '$vendor_name_new' AND vendor_is_delete IS NULL";
            $set_vendor_name_runx = mysqli_query($conn, $set_vendor_name_queryx);
            if(mysqli_num_rows($set_vendor_name_runx) == 1){
                $arr = array('status' => 'exist');
                echo json_encode($arr);
                http_response_code(200);
                exit;  
            }
        }elseif($vendor_alias_set == 'set'){
            $set_vendor_name_queryx = "SELECT * FROM vendor WHERE vendor_name = '$vendor_name_new' AND vendor_company = '$vendor_alias_new' AND vendor_is_delete IS NULL";
            $set_vendor_name_runx = mysqli_query($conn, $set_vendor_name_queryx);
            if(mysqli_num_rows($set_vendor_name_runx) == 1){
                $arr = array('status' => 'exist');
                echo json_encode($arr);
                http_response_code(200);
                exit;  
            }
        }else{
            http_response_code(400);
            exit;
        }        

        if($vendor_alias_set == 'notset'){
            $set_vendor_name_queryn = "INSERT INTO vendor SET $active_user_id vendor_name = '$vendor_name_new',vendor_time = '$time'";
            $set_vendor_name_runn = mysqli_query($conn, $set_vendor_name_queryn);
            if(mysqli_affected_rows($conn) == 1){
                $vendor_id_new = mysqli_insert_id($conn);
                $arr = array('status' => 'added','vendor_name' => $vendor_name_new,'vendor_id' => $vendor_id_new);
                echo json_encode($arr);
                http_response_code(200);
                exit;  
            }
        }elseif($vendor_alias_set == 'set'){
            $set_vendor_name_queryn = "INSERT INTO vendor SET $active_user_id vendor_name = '$vendor_name_new',vendor_company = '$vendor_alias_new', vendor_time = '$time'";
            $set_vendor_name_runn = mysqli_query($conn, $set_vendor_name_queryn);
            if(mysqli_affected_rows($conn) == 1){
                $vendor_id_new = mysqli_insert_id($conn);
                $arr = array('status' => 'added','vendor_name' => $vendor_name_new,'vendor_id' => $vendor_id_new);
                echo json_encode($arr);
                http_response_code(200);
                exit;   
            }
        }else{
            http_response_code(400);
            exit;
        }

    }else{
        http_response_code(400);
        exit;
    }    
}else{
   
    http_response_code(400);
    exit;
}
?>

