<?php

require $_SERVER['DOCUMENT_ROOT'] . '/application/libraries/phpmailer/Exception.php';
require $_SERVER['DOCUMENT_ROOT'] . '/application/libraries/phpmailer/PHPMailer.php';
require $_SERVER['DOCUMENT_ROOT'] . '/application/libraries/phpmailer/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;

class Update extends Controller
{
    function __construct()
    {
        parent::__construct();

        $this->load->model('mupdate');
        $this->lang->load('site');
    }

    function index()
    {
        if ($this->uri->segment(2) != 'index')
            redirect('update/index');

        if ($this->uri->segment(3) == '' || !is_numeric($this->uri->segment(3))) {
            $this->load->view('update/404.php');
            return;
        }

        $updateData = array();
        $updateData['update_name'] = $_SESSION['update_name'];
        $updateData['update_email'] = $_SESSION['update_email'];
        $updateData['update_years'] = $_SESSION['update_years'];
        $updateData['update_info'] = $_SESSION['update_info'];
        $updateData['update_old_hl_number'] = $_SESSION['update_old_hl_number'];
        $updateData['update_year'] = $updateData['update_years'] != '' ? $updateData['update_years'] : '2';
        $updateData['error'] = $_SESSION['error'];

        $_SESSION['update_name'] = '';
        $_SESSION['update_email'] = '';
        $_SESSION['update_years'] = '';
        $_SESSION['update_info'] = '';
        $_SESSION['update_old_hl_number'] = '';
        $_SESSION['error'] = '';

        $productId = $this->uri->segment(3);

        $updateData['product'] = $this->mupdate->getProduct($productId);

        if (empty($updateData['product'])) {
            $this->load->view('update/404.php');
            return;
        }

        $updateData['normalPrice'] = $updateData['product']->price;

        if ($productId == 1282 || $productId == 1283)
            $updateData['product']->price *= $updateData['update_year'];

        $afa_key            = "27";
        $afa                = $afa_key / 100;
        $updateData['brut'] = $updateData['product']->price;

        $updateData['header'] = $updateData['product']->id == 1856 ? 'ARCHLine.XP 2025 BETA TESTER - REGISTRATION' : $updateData['product']->header;
        $updateData['title'] = $updateData['product']->id == 1856 ? 'ARCHLine.XP 2025 Beta Tester - Registration' : 'ARCHLine.XP PROFESSIONAL UPGRADE';
        $updateData['btn'] = $updateData['product']->id == 1856 ? 'Send Request' : 'Send Order';
        $updateData['star'] = $updateData['product']->id == 1856 ? '- ARCHLine.XP PROF 2022,2023,2024 serials accepted only' : '*';
        $updateData['informed'] = $updateData['product']->id == 1856 ? 'I understand that works produced using Beta version cannot be used for commercial purposes while these features remain in the beta stage.' : "I confirm I'm informed on the products";
        $updateData['purchase'] = $updateData['product']->id == 1856 ? 'Please accept' : 'Purchase confirmations';

        $this->load->view('update/index.php', $updateData);
    }

    function order_update()
    {
        $years = $_POST['years-select'];
        $email = $_POST['email'];
        $name = $_POST['name'];
        $info = $_POST['info'];
        $productId = $_POST['productId'];
        $oldSerial = $_POST['old_hl_number'];
        $hlNum = substr($oldSerial, 0, 6);

        $program = $this->db->query("SELECT * FROM cl_hlusers.h_programs WHERE prPass = '{$oldSerial}'")->row();

        if (empty($program)) {
            $_SESSION['update_years'] = $years;
            $_SESSION['update_email'] = $email;
            $_SESSION['update_name'] = $name;
            $_SESSION['update_info'] = $info;
            $_SESSION['error'] = 'Wrong serial number';

            redirect('update/index/' . $productId);
            return;
        }

        $product = $this->mupdate->getProduct($productId);

        $currentVer = $product->type == 'L' ? 2 : 1;

        if ($productId == 1856)
            $version = $this->db->query("SELECT * FROM cl_hlusers.p_versions WHERE verID = 25")->row();
        else
            $version = $this->db->query("SELECT * FROM cl_hlusers.p_versions WHERE currentVer = 0 OR currentVer = {$currentVer}")->row();

        $checkUpdate = $this->db->query("SELECT * FROM cl_hlusers.h_programs WHERE prHlNum = '{$hlNum}' AND prVerID = {$version->verID}")->row();

        if (!empty($checkUpdate)) {
            $_SESSION['update_years'] = $years;
            $_SESSION['update_email'] = $email;
            $_SESSION['update_name'] = $name;
            $_SESSION['update_info'] = $info;
            $_SESSION['error'] = 'You already have the latest version';

            redirect('update/index/' . $productId);
            return;
        }

        if ($product->cont_id != 0) {
            $year = $product->termek;
            $productName = $product->termek;
        } else {
            $year = 'ARCHLine.XP order: Upgrade for ' . $years . ' years';
            $productName = $product->termek . " Fix price for {$years} years";
        }

        if ($product->o_type == 7 && substr($oldSerial, 7, 2) != $version->verPassword8and9 - 1) {
            $_SESSION['update_years'] = $years;
            $_SESSION['update_email'] = $email;
            $_SESSION['update_name'] = $name;
            $_SESSION['update_info'] = $info;
            $_SESSION['error'] = 'Wrong serial number';

            redirect('update/index/' . $productId);
            return;
        }

        if ($product->o_type == 8 && substr($oldSerial, 7, 2) == $version->verPassword8and9 - 1) {
            $_SESSION['update_years'] = $years;
            $_SESSION['update_email'] = $email;
            $_SESSION['update_name'] = $name;
            $_SESSION['update_info'] = $info;
            $_SESSION['error'] = 'Wrong serial number';

            redirect('update/index/' . $productId);
            return;
        }

        if ($product->o_type == 26 && substr($oldSerial, 7, 2) != $version->verPassword8and9 - 2) {
            $_SESSION['update_years'] = $years;
            $_SESSION['update_email'] = $email;
            $_SESSION['update_name'] = $name;
            $_SESSION['update_info'] = $info;
            $_SESSION['error'] = 'Wrong serial number';

            redirect('update/index/' . $productId);
            return;
        }

        if ($product->o_type == 27 && substr($oldSerial, 7, 2) > $version->verPassword8and9 - 3) {
            $_SESSION['update_years'] = $years;
            $_SESSION['update_email'] = $email;
            $_SESSION['update_name'] = $name;
            $_SESSION['update_info'] = $info;
            $_SESSION['error'] = 'Wrong serial number';

            redirect('update/index/' . $productId);
            return;
        }

        if ($product->o_type == 6 && substr($oldSerial, 7, 2) < 38) {
            $_SESSION['update_years'] = $years;
            $_SESSION['update_email'] = $email;
            $_SESSION['update_name'] = $name;
            $_SESSION['update_info'] = $info;
            $_SESSION['error'] = 'Wrong serial number';

            redirect('update/index/' . $productId);
            return;
        }

        if (substr($oldSerial, 6, 1) != $product->type) {
            if ($product->type == '8' && substr($oldSerial, 6, 1) != '7') {
                $_SESSION['update_years'] = $years;
                $_SESSION['update_email'] = $email;
                $_SESSION['update_name'] = $name;
                $_SESSION['update_info'] = $info;
                $_SESSION['error'] = 'Wrong serial number';

                redirect('update/index/' . $productId);
                return;
            }
        }

        if ($productId == 1856 && substr($oldSerial, 7, 2) < $version->verPassword8and9 - 3) {
            $_SESSION['update_years'] = $years;
            $_SESSION['update_email'] = $email;
            $_SESSION['update_name'] = $name;
            $_SESSION['update_info'] = $info;
            $_SESSION['error'] = 'Wrong serial number';

            redirect('update/index/' . $productId);
            return;
        }

        if ($productId == 1282 || $productId == 1283)
            $product->price *= $years;

        $afa_key            = "27";
        $afa                = $afa_key / 100;
        $brut               = $product->price;

        $key = $this->db->query("SELECT * FROM cl_hlusers.hardlock WHERE hlNum = '{$hlNum}'")->row();

        $serial = $this->mupdate->createPassword($hlNum, $version->verCode, $product->type, $key->hlLan);
        $orderNumber = $this->mupdate->getOrderNumber();

        $this->mupdate->createLicence($serial, $version->verID, $product->type, $orderNumber, $key->hlLan, $brut, $productId);
        $this->mupdate->createOrder($name, $email, $product->termek, $orderNumber, $oldSerial, $serial, $brut, $info);

        $template = $this->mupdate->getEmailTemplate($year, $email, $name, $info, $productName, $serial, $brut, $orderNumber, $version->year, $productId);

        $mail = new PHPMailer();

        $mail->IsSMTP();

        $mail->CharSet  = 'UTF-8';
        $mail->SMTPAuth = true;

        $mail->SMTPOptions = [
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            ]
        ];

        $mail->From     = 'info@archlinexp.com';
        $mail->FromName = 'info@archlinexp.com';
        $mail->AddAddress($email);

        $mail->addCC('office@cadline.hu');
        $mail->addBCC('mate.nagy@cadline.hu');

        $mail->isHTML(true);
        $mail->Subject  = $template->subject;
        $mail->Body     = $template->body;

        $mail->send();

        if ($product->id == 1282) {
            if ($years == 2)
                $product->barion_url = 'https://secure.shareit.com/shareit/cart.html?DELIVERY[300158265]=EML&PRODUCT[301037670]=1&language=English&js=-1';
            else
                $product->barion_url = 'https://secure.shareit.com/shareit/cart.html?DELIVERY[300158265]=EML&PRODUCT[301037673]=1&language=English&js=-1';
        }

        if ($product->id == 1283) {
            if ($years == 2)
                $product->barion_url = 'https://secure.shareit.com/shareit/cart.html?DELIVERY[300158265]=EML&PRODUCT[301037669]=1&language=English&js=-1';
            else
                $product->barion_url = 'https://secure.shareit.com/shareit/cart.html?DELIVERY[300158265]=EML&PRODUCT[301037672]=1&language=English&js=-1';
        }

        if ($product->barion_url == '') {
            redirect('update/success');
            die();
        }

        return header('Location: ' . $product->barion_url);
    }

    function success()
    {
        $this->load->view('update/success.php');
    }
}
