<?php
include "../../root.class.php";
$email = new email();
$call = new call_functions();
$db = new db_safeguard();
// var_dump($_POST);
$user_id = $_POST['user_id'];
$jc_number = $_POST['jc_number'];
$signature = $_POST['signature'];
$receiving = $_POST['receiving'];
$contractor = $_POST['contractor'];
$tech = 0;
$account = 0;
$project = 0;
echo "<br>";
var_dump($_POST);

if ($_POST['stock_id_1'] == '') {
    echo "EMPTY
    <script> alert('STOCK ITEMS NOT CAPUTED'); </script>
    ";
    // exit();
}


echo "<br>";
echo "<br>";
// get manager of project job card 
$jobcard_res = $db->query("job_cards", "SELECT * FROM job_cards WHERE jc_number = '$jc_number'");
if ($jobcard_res->num_rows == 0) {
    $jobcard_res = $db->query("job_cards_tech", "SELECT * FROM job_cards_tech WHERE jc_number = '$jc_number'");
    if ($jobcard_res->num_rows == 0) {
        $jobcard_res = $db->query("job_card_accounts", "SELECT * FROM job_card_accounts WHERE `name` = '$jc_number'");
        $account = 1;

        echo "TYPE ACCOUNT";

    } else {

        $tech = 1;
        echo "TYPE TECH";
    }
} else {
    echo "TYPE PROJECT";
    echo "<br>";
    $project = 1;
}
echo "<br>";
$jobcard = $jobcard_res->fetch_assoc();
if ($tech == 1) {
    $manager_id = $jobcard['user_id'];
} else {
    $manager_id = $jobcard['account_manager_id'];
}
echo "manager id: " . $manager_id;
$date_time = date("Y-m-d H:i", strtotime("+2 Hours"));
$index = 1;
$error = 0;
$items_for_email = "";
while ($index <= 200) {

    echo "<br>" . $stock_id = $_POST['stock_id_' . $index];
    echo "<br>";

    $amount = $_POST['amount_' . $index];
    $stock_name = explode("|", $_POST['stock_id_' . $index]);
    $stock_name = $stock_name[0];
    if (strlen($stock_id) > 0) {

        echo "<br>R-- " . $stock_record_id = substr($stock_id, strpos($stock_id, "R:") + 2, strpos($stock_id, ":R") - strpos($stock_id, "R:") - 2);
        echo "<br>";


        if (strpos($stock_id, "S:") != true) {
            $serial_number = "";
            $rec_id = $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`,`contractor`) VALUES ($stock_record_id,0,'-$amount','$date_time',0,$user_id,'$jc_number',0,'$signature','$receiving','$contractor')");
            $items_for_email .= "<tr style='border:1px solid black'><td>$stock_name</td><td>$amount</td><td>$serial_number</td></tr>";
            if ($rec_id < 1) {
                $error = 1;
                echo "INSERT INTO order_trans (`stock_id`, `non_stock_id`, `amount`, `date_time`, `order_id`, `user_id`, `job_card_no`, `units_stock_id`,`signature`,`received`,`contractor`) VALUES ($stock_record_id,0,'-$amount','$date_time',0,$user_id,'$jc_number',0,'$signature','$receiving','$contractor')";
            }

        } else {
            echo "<br>S-- " . $serial_number = substr($stock_id, strpos($stock_id, "S:") + 2, strpos($stock_id, ":S") - strpos($stock_id, "S:") - 2);
            $res = $db->query("units_stock", "SELECT * FROM units_stock WHERE serial_number = '$serial_number'");
            $unit = $res->fetch_assoc();
            $unit_record_id = $unit['record_id'];
            $rec_id = 0;
            $rec_id = $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`,`contractor`) VALUES ($stock_record_id,0,'-$amount','$date_time',0,$user_id,'$jc_number',$unit_record_id,'$signature','$receiving','$contractor')");
            $items_for_email .= "<tr style='border:1px solid black'><td>$stock_name</td><td>$amount</td><td>$serial_number</td></tr>";
            if ($rec_id < 1) {
                $error = 1;
                echo "INSERT INTO order_trans (`stock_id`, `non_stock_id`, `amount`, `date_time`, `order_id`, `user_id`, `job_card_no`, `units_stock_id`,`signature`,`received`,`contractor`) VALUES ($stock_record_id,0,'-$amount','$date_time',0,$user_id,'$jc_number',$unit_record_id,'$signature','$receiving','$contractor')";
            }

        }

        echo "<br>";

    }
    $index++;

}

$image_data = file_get_contents("signatures/" . $signature);

$jobcard_type = "";

if ($project == 1) {
    $jobcard_type = "<p>Project Name: " . $jobcard['project_name'] . "</p>";
    $email_name = $jobcard['project_name'];

} else if ($tech == 1) {
    $jobcard_type = "<p> Name: " . $jobcard['company_name'] . "</p>";
    $email_name = $jobcard['company_name'];

} else {
    $jobcard_type = "<p>Account Name: " . $jobcard['description'] . "</p>";
    $email_name = $jobcard['description'];
}

$disp = "";

if ($contractor != '') {
    $contractor = "<p>Contractor: " . $contractor . "</p>";
}
if ($contractor == '') {
    $image = "<img src='data:image/png;base64," . base64_encode($image_data) . "' width='200' height='100' />";
} else {
    $image = "<br> SIGNATURE: __________________________________";
    $disp = "To Site";
}
$email_html = "
            <html>
                <head>
                    <title>Dispatch</title>
                </head>
                <body>
                    <h2>Dispatch $disp</h2>
                    <p>Dispatched by: " . $call->get_username($user_id) . "</p>
                    <p>Job Card Number: " . $jc_number . "</p>
                    $jobcard_type
                    <p>Received By: " . $receiving . "</p>
                    $contractor
                    $image
                  <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>
                        <th style='width:20%'>Serial Number</th>
                    </tr>
                    " . $items_for_email . "
                  </table>
                </body>
            </html>
            ";

$emails = ['stores@midrandac.co.za', 'alex@midrandac.co.za', 'anton@midrandac.co.za'];
$names = ['Stores', 'Alex', 'Anton'];
echo $manager_id;
$user_res = $db->query("users", "SELECT * FROM users WHERE record_id = $manager_id");
$user = $user_res->fetch_assoc();
$emails[] = $user['email'];
$names[] = $user['username'];

var_dump($emails);



$email_html = mb_convert_encoding($email_html, 'HTML-ENTITIES', 'UTF-8');
$call->send_for_email($emails, $names, $jc_number . " - " . $email_name, $email_html);

// $email->subject("Dispatch");
// $email->body($email_html);
// $email->send();
// echo $email_html;

if ($disp != "") {
    ?>
    <script>
        // window.location.href = "dispatch_to_site.php?disptach=1";
    </script>

    <?php
} else {
    ?>

    <script>
        // window.location.href = "dispatch.php?disptach=1";
    </script>

<?php } ?>