<?php

include "../../root.class.php";
$db = new db_safeguard();
$email = new email();

// var_dump($_POST);
$index = 1;
$date = date('Y-m-d H:i', strtotime("+2 Hours"));
while ($index <= count($_POST)) {
    echo $_POST['unit_id_' . $index];
    if ($_POST['return_amount_' . $index] > 0) {
        echo "added";
        $unit_id = !empty($_POST['unit_id_' . $index]) ? $_POST['unit_id_' . $index] : 0;
        $db->query("order_trans", "INSERT INTO `order_trans`(`stock_id`, `non_stock_id`, `amount`, `date_time`, `order_id`, `user_id`, `job_card_no`, `units_stock_id`, `signature`, `received`) VALUES ({$_POST['stock_id_' . $index]},0,{$_POST['return_amount_' . $index]},'$date',0,{$_SESSION['user_id']},'{$_GET['jobcard_number']}','$unit_id','{$_POST['signature']}','{$_POST['received']}')");
        // get stock desctiprion
        $stock_res = $db->query("stock", "SELECT * FROM stock WHERE record_id = {$_POST['stock_id_' . $index]}");
        $stock = $stock_res->fetch_assoc();
        $stock_return_array[] = "<tr><td>{$stock['description']}</td><td>{$_POST['return_amount_' . $index]}</td></tr>";

    }
    $index++;

}

//get responsable user for jobcard (PROJECT)???/
$emails = [];
$names = [];
$jobcard_res = $db->query("job_cards", "SELECT * FROM job_cards WHERE jc_number = '{$_GET['jobcard_number']}'");
if ($jobcard_res->num_rows == 0) {
    // check if jobcard is under technical
    $jobcard_res = $db->query("job_cards_tech", "SELECT * FROM job_cards_tech WHERE jc_number = '{$_GET['jobcard_number']}'");
    if ($jobcard_res->num_rows == 0) {
        // for accounts
        $jobcard_res = $db->query("job_card_accounts", "SELECT * FROM job_card_accounts WHERE `name` = '{$_GET['jobcard_number']}'");
        $jobcard = $jobcard_res->fetch_assoc();
        $user_res = $db->query("users", "SELECT * FROM users WHERE record_id = {$jobcard['account_manager_id']}");
        $user = $user_res->fetch_assoc();
        $emails[] = $user['email'];
        $names[] = $user['username'];
        $project = "<p>Account Name: {$jobcard['description']} </p>";
    } else {
        $jobcard = $jobcard_res->fetch_assoc();
        $user_res = $db->query("users", "SELECT * FROM users WHERE record_id = {$jobcard['user_id']}");
        $user = $user_res->fetch_assoc();
        $emails[] = $user['email'];
        $names[] = $user['username'];
        $project = "<p>Project Name: {$jobcard['project_name']} </p>";

    }
} else {
    $jobcard = $jobcard_res->fetch_assoc();
    $user_res = $db->query("users", "SELECT * FROM users WHERE record_id = {$jobcard['account_manager_id']}");
    $user = $user_res->fetch_assoc();
    $emails[] = $user['email'];
    $names[] = $user['username'];
    $project = "<p>Client Name: {$jobcard['company_name']} </p>";

}

// check if job card is on project job card:
$job_card_project_res = $db->query("job_cards", "SELECT * FROM job_cards WHERE jc_number = '{$_GET['jobcard_number']}'");
$job_card_tech_res = $db->query("job_cards_tech", "SELECT * FROM job_cards_tech WHERE jc_number ='{$_GET['jobcard_number']}'");
$job_card_acc_res = $db->query("job_card_accounts", "SELECT * FROM job_card_accounts WHERE `name` = '{$_GET['jobcard_number']}'");
if ($job_card_project_res->num_rows > 0) {
    $job_card = $job_card_project_res->fetch_assoc();
    $job_card_name = $job_card["project_name"];
    $jc_number_text = "<p> Job Card Number: " . $_GET['jobcard_number'] . "</p>" . "<p> Name : " . $job_card['project_name'] . "</p>";
} else if ($job_card_tech_res->num_rows > 0) {
    $job_card = $job_card_tech_res->fetch_assoc();
    $job_card_name = $job_card["company_name"];
    $jc_number_text = "<p> Job Card Number:" . $_GET['jobcard_number'] . "</p><p>  Name : " . $job_card['company_name'] . "</p>";
} else if ($job_card_acc_res->num_rows > 0) {
    $job_card = $job_card_acc_res->fetch_assoc();
    $job_card_name = $job_card["description"];
    $jc_number_text = "<p> Job Account:" . $job_card['name'] . "</p><p>  Description : " . $job_card['description'] . "</p>";
} else {
    $job_card_name = "N/A";
    $account_manager = "N/A";
}


$image_data = file_get_contents("signatures/" . $_POST['signature']);
$call = new call_functions();

$email_html = "
            <html>
                <body>
                  <h2>RETURNED STOCK</h2>
                    <p>Collected by: " . $call->get_username($_SESSION['user_id']) . "</p>
                    $jc_number_text
                    <p>Recieved By: {$_POST['received']}</p>
                    <img src='data:image/png;base64," . base64_encode($image_data) . "' width='200' height='100' />
                  <table style='width:100%; text-align:center;border:1px solid black'>
                    <tr style='border:1px solid black'>
                        <th style='width:60%'>Item</th>
                        <th style='width:20%'>Amount</th>
                    </tr>
                    " . implode($stock_return_array) . "
                  </table>
                </body>
            </html>
            ";


$emails[] = "anton@midrandac.co.za";
$names[] = "Anton";
$emails[] = "alex@midrandac.co.za";
$names[] = "Alex";
$emails[] = "chris@midrandac.co.za";
$names[] = "chris EWG";
$subject = "Return Stock for jobcard number {$_GET['jobcard_number']}";



$call->send_for_email($emails, $names, $subject, $email_html);

echo "<script>location.href='returns.php';</script>";