<?php
    define('HOST',"localhost:3306");
    define('USER',"root");
    define('PASS',"mYsqlroo2023p@SS");
    define('DB',"starindia_prod");
    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
    $referrer = $_POST['referrer'];
    $applicantsName = $_POST['applicantsName'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $city = $_POST['city'];
    $country = $_POST['country'];
    $resume = '';
    $linkedin = '';
    $portfolio = '';
    $pnote = $_POST['pnote'];
    $experienceLetter = '';
    $addressProofImg = '';
    $idProofImg = '';
    if($_FILES['resume']['name']){
        $key = '';$pattern = '123456789abcdefg';$counter = strlen($pattern) - 1;for ($i = 0; $i < 10; $i++) {$key .= $pattern[rand(0, $counter)];}$imageName = $key;
        move_uploaded_file($_FILES['resume']['tmp_name'], "resume/".$imageName.$_FILES['resume']['name']);
        $resume="https://apply.stararabgroup.com/admin/resume/".$imageName.$_FILES['resume']['name'];
    }
    if($_FILES['portfolio']['name']){
        $key = '';$pattern = '123456789abcdefg';$counter = strlen($pattern) - 1;for ($i = 0; $i < 10; $i++) {$key .= $pattern[rand(0, $counter)];}$imageName = $key;
        move_uploaded_file($_FILES['portfolio']['tmp_name'], "portfolio/".$imageName.$_FILES['portfolio']['name']);
        $portfolio="https://apply.stararabgroup.com/admin/portfolio/".$imageName.$_FILES['portfolio']['name'];
    }
    if($_FILES['experienceLetter']['name']){
        $key = '';$pattern = '123456789abcdefg';$counter = strlen($pattern) - 1;for ($i = 0; $i < 10; $i++) {$key .= $pattern[rand(0, $counter)];}$imageName = $key;
        move_uploaded_file($_FILES['experienceLetter']['tmp_name'], "experienceLetter/".$imageName.$_FILES['experienceLetter']['name']);
        $experienceLetter="https://apply.stararabgroup.com/admin/experienceLetter/".$imageName.$_FILES['experienceLetter']['name'];
    }
    if($_FILES['addressProofImg']['name']){
        $key = '';$pattern = '123456789abcdefg';$counter = strlen($pattern) - 1;for ($i = 0; $i < 10; $i++) {$key .= $pattern[rand(0, $counter)];}$imageName = $key;
        move_uploaded_file($_FILES['addressProofImg']['tmp_name'], "addressProofImg/".$imageName.$_FILES['addressProofImg']['name']);
        $addressProofImg="https://apply.stararabgroup.com/admin/addressProofImg/".$imageName.$_FILES['addressProofImg']['name'];
    }
    if($_FILES['idProofImg']['name']){
        $key = '';$pattern = '123456789abcdefg';$counter = strlen($pattern) - 1;for ($i = 0; $i < 10; $i++) {$key .= $pattern[rand(0, $counter)];}$imageName = $key;
        move_uploaded_file($_FILES['idProofImg']['tmp_name'], "idProofImg/".$imageName.$_FILES['idProofImg']['name']);
        $idProofImg="https://apply.stararabgroup.com/admin/idProofImg/".$imageName.$_FILES['idProofImg']['name'];
    }
    $password = preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '', $applicantsName))."".rand(1000,9999);
    $sql_query_add = "INSERT INTO `om_submissions`(`password`,`referrer`,`applicantsName`,`email`,`phone`,`city`,`country`,`resume`,`linkedin`,`portfolio`,`pnote`,`experienceLetter`,`addressProofImg`,`idProofImg`) VALUES ('$password',$referrer,'$applicantsName','$email','$phone','$city','$country','$resume','$linkedin','$portfolio','$pnote','$experienceLetter','$addressProofImg','$idProofImg')";
    if (mysqli_query($con, $sql_query_add)) {
        header("Location: /thank_you.html");
        exit;
    }
    else {
        echo mysqli_error($con);
        exit;
    }
    header("Location: /");
    exit;
?>