<?php
header('Access-Control-Allow-Origin: *');
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(E_ALL);
include_once __DIR__ . '/dompdf/autoload.inc.php';
$path = __DIR__ . '/img/';

writeToLog($_POST, 'POST');
writeToLog($_FILES, 'FILES');
//writeToLog(count($_POST['cards']),'COUNT');

//for ($i = 0; $i < count($_POST['cards']); $i++) {
foreach ($_POST['cards'] as $i => $vv) {
    $img_1 = NULL;
    $img_2 = NULL;
    unset($img_gorizontal);
    $img_all = NULL;
	//unset($array_img);
	$array_img = array();
	$images = NULL;

    $name = $_POST['cards'][$i]['title'];
    $adress = $_POST['cards'][$i]['address'];
    $price = $_POST['cards'][$i]['fullPrice'];
	$price = intval(preg_replace('/[^0-9]+/', '', $price), 10);
    $text = $_POST['cards'][$i]['details'];
    $square = $_POST['cards'][$i]['square'];
    $sleep = $_POST['cards'][$i]['bedroom'];
    $floor = $_POST['cards'][$i]['floor'];
    $finish = $_POST['cards'][$i]['otc'];
    $id = $_POST['cards'][$i]['id'];
    $currency = $_POST['cards'][$i]['currency'];
    $images = $_POST['cards'][$i]['urlImages'];

    if (!empty($images)) {
        $newImages = explode(',;,', $images);
        foreach ($newImages as $index => $image) {
            $array_img_url = explode(',', $image);
            writeToLog($array_img_url);
            $from = $array_img_url[0];
            $to = $path .$_POST['cards'][$i]['title'].'_'. $index . '_img.png';
            copy($from, $to);
            $array_img[$index] = 'https://spa-bitrix.ru/masthev/create_presentation/img/' .$_POST['cards'][$i]['title'].'_'. $index . '_img.png';
            writeToLog($array_img, 'array_img');
            $size = getimagesize($path .$_POST['cards'][$i]['title'].'_'. $index . '_img.png');
            $height_img = $size[0]; // Высота картинки
            $width_img = $size[1];  // Ширина картинки
            if ($array_img_url[1] == 'true') {
                $img_1 = $array_img[$index];
                writeToLog($img_1, 'IMAGES_1');
            } else if ($array_img_url[2] == 'true') {
                $img_2 = $array_img[$index];
                writeToLog($img_1, 'IMAGES_2');
            } else {
                //$img_gorizontal[] = $array_img[$index];
                if ($height_img == $width_img) {
                    echo "Квадратная";
                } else if ($height_img > $width_img) {

                    $img_gorizontal[] = $array_img[$index];
                } else {
                    $img_vertical[] = $array_img[$index];
                }
              //  writeToLog($img_gorizontal, 'IMAGES_GORIZONT');
            }
        }
    }


    writeToLog($images, 'ALL');
    //$text = substr($text, 0, 1000);


    if ($sleep <> '') {
        $block_sleep = '<p style="margin-left:25px;font-size:15px;padding-top:20px"><img style="width:25px;height:24px;" src="http://94.198.219.190/img/sleep.png" alt="">  Спальни: ' . $sleep . '</p>';
    } else {
        $block_sleep = "";
    }
    if ($floor <> '') {
        $block_floor = '<p style="margin-left:25px;font-size:15px;padding-top:20px"><img style="width:25px;height:24px;" src="http://94.198.219.190/img/floor.png" alt="">  Этаж: ' . $floor . '</p>';
    } else {
        $block_floor = "";
    }
    if ($finish <> '') {
        $block_finish = '<p style="margin-left:25px;font-size:15px;padding-top:20px"><img style="width:25px;height:24px;" src="http://94.198.219.190/img/finish.png" alt="">  Отделка: ' . $finish . '</p>';
    } else {
        $block_finish = "";
    }
    if (!empty($_FILES['files']['tmp_name'][$i]['main'])) {
        copy($_FILES['files']['tmp_name'][$i]['main'], $path . $_FILES['files']['name'][$i]['main']);
        $img_1 = "https://spa-bitrix.ru/masthev/create_presentation/img/" . $_FILES['files']['name'][$i]['main'];
    }

    if (!empty($_FILES['files']['tmp_name'][$i]['second'])) {
        copy($_FILES['files']['tmp_name'][$i]['second'], $path . $_FILES['files']['name'][$i]['second']);
        $img_2 = "https://spa-bitrix.ru/masthev/create_presentation/img/" . $_FILES['files']['name'][$i]['second'];
    }


    foreach ($_FILES['files']['tmp_name'][$i] as $key => $value) {

        if (!(($key <> 'main') xor ($key <> 'second'))) {
            // writeToLog($key, 'key');
            copy($_FILES['files']['tmp_name'][$i][$key], $path . $_FILES['files']['name'][$i][$key]);
            $size = getimagesize($path . $_FILES['files']['name'][$i][$key]);
            $height_img = $size[0]; // Высота картинки
            $width_img = $size[1];  // Ширина картинки
            if ($height_img == $width_img) {
                echo "Квадратная";
            } else if ($height_img > $width_img) {

                $img_gorizontal[] = "https://spa-bitrix.ru/masthev/create_presentation/img/" . $_FILES['files']['name'][$i][$key];
            } else {
                $img_vertical[] = "https://spa-bitrix.ru/masthev/create_presentation/img/" . $_FILES['files']['name'][$i][$key];
            }

        }
    }

    $price_m2 = $price / $square;
    $price_m2 = round($price_m2);
    $cur = '$';
    if ($_POST['cards'][$i]["typeDeal"] == 'Продажа') {
        $price_temp = '<p style="padding-top:20px;color:white;font-size:22px;padding-left:25px">' . format_price($price, $cur) . ' / ' . format_price($price_m2, $cur) . ' за м2</p>';
    } else if ($_POST['cards'][$i]["typeDeal"] == 'Аренда') {
        $price_temp = '<p style="padding-top:20px;color:white;font-size:22px;padding-left:25px">' . format_price($price, $cur) . ' в мес</p>';
    }
    if (!empty($img_vertical)) {
        if (count($img_vertical) % 2 == 0) {
            for ($i = 0; $i < count($img_vertical); $i++) {
                $slide = '
            <div class="slid1">
            <div class="header">
                <div class="logo">
                    <img src="https://spa-bitrix.ru/masthev/create_presentation/img/logo.png" alt="logo">
                </div>
                <div class="info">
                    <h1>+7 (985) 222 47 47</h1>
                    <p style="padding: 5px 0 0 5px;">127051, Москва, Большой Каретный переулок, д. 24, стр. 2</p>
                    <p style="padding: 5px 0 0 5px;">Время работы: с 10:00 до 19:00</p>
                    <p style="padding: 10px 0 0 5px;">info@mhburo.ru</p>
                </div>
            </div>
            <div style="padding-top:130px;"class="content">
                <img style="width:400px!important;height:520px!important;padding-top:10px;padding-left:120px" class="img_other" src="' . $img_vertical[$i] . '" alt="">
                <img style="width:400px!important;height:520px!important;padding-top:10px;padding-left:80px" class="img_other" src="' . $img_vertical[$i + 1] . '" alt="">
            </div>
            </div>';
                $i++;
                $img_all = $img_all . $slide;
            }
        }
    }


    for ($i = 0; $i < count($img_gorizontal); $i++) {
        $slide = '
		<div class="slid1">
		<div class="header">
			<div class="logo">
				<img src="https://spa-bitrix.ru/masthev/create_presentation/img/logo.png" alt="logo">
			</div>
			<div class="info">
				<h1>+7 (985) 222 47 47</h1>
				<p style="padding: 5px 0 0 5px;">127051, Москва, Большой Каретный переулок, д. 24, стр. 2</p>
				<p style="padding: 5px 0 0 5px;">Время работы: с 10:00 до 19:00</p>
				<p style="padding: 10px 0 0 5px;">info@mhburo.ru</p>
			</div>
		</div>
		<div  class="content">
			<img style="width:1000px!important;height:640px!important;padding-top:10px;padding-left:60px" class="img_other" src="' . $img_gorizontal[$i] . '" alt="">
		</div>
		</div>';
        $img_all = $img_all . $slide;
    }
    $a_html = create_code($name, $adress, $price_temp, $text, $square, $block_sleep, $block_floor, $block_finish, $img_1, $img_2, $img_all, $id);
    $f_html = $f_html . $a_html;

}
//writeToLog($f_html, 'html');
print_r(create_presentation($f_html));

function create_code($name, $adress, $price_temp, $text, $square, $block_sleep, $block_floor, $block_finish, $img_1, $img_2, $img_all, $id)
{
//	
    $html = '
    <div class="slid1">
        <div class="header">
            <div class="logo">
                <img src="https://spa-bitrix.ru/masthev/create_presentation/img/logo.png" alt="logo">
            </div>
            <div class="info">
                <h1>+7 (985) 222 47 47</h1>
                <p style="padding: 5px 0 0 5px;">127051, Москва, Большой Каретный переулок, д. 24, стр. 2</p>
                <p style="padding: 5px 0 0 5px;">Время работы: с 10:00 до 19:00</p>
                <p style="padding: 10px 0 0 5px;">info@mhburo.ru</p>
            </div>
        </div>
        <div class="content">
        <img style="padding-top:40px;padding-left:60px" class="img_other" src="' . $img_1 . '" alt="">
        </div>
        <div class="box">
            <p style="padding-top: 15px;color:white;font-size:21px;padding-left:25px" >' . $name . '</p>
            <p style="padding-top:5px;color:white;font-size:16px;padding-left:25px">' . $adress . '</p>
            ' . $price_temp . '
        </div>
        
    </div>
    <div class="slid1">
        <div class="header">
            <div class="logo">
                <img src="https://spa-bitrix.ru/masthev/create_presentation/img/logo.png" alt="logo">
            </div>
            <div class="info">
                <h1>+7 (985) 222 47 47</h1>
                <p style="padding: 5px 0 0 5px;">127051, Москва, Большой Каретный переулок, д. 24, стр. 2</p>
                <p style="padding: 5px 0 0 5px;">Время работы: с 10:00 до 19:00</p>
                <p style="padding: 10px 0 0 5px;">info@mhburo.ru</p>
            </div>
        </div>
        <div class="content_2">
        <table cellspacing="0" cellpadding="0" id="maket"  style="padding-top:25px;">
        <tr> 
         <td id="leftcol"><p style="margin-left: 25px;color:white;font-size:13px;width:500px;">' . $text . '
         </p></td>
            <td>
                <img style="position:absolute;top: 200px;left: 625px;width: 400px; height:250px;"  src="' . $img_2 . '" alt="">
            </td>
        </tr>
        <tr>
            <td></td>
            <td><div style="position:absolute;top: 475px;left: 625px;width:400px;background-color:white;height:200px;">
            <p style="margin-left:25px;font-size:15px;padding-top:25px"><img style="width:25px;height:24px;" src="https://spa-bitrix.ru/masthev/create_presentation/img/square.png" alt="">  Площадь: ' . $square . ' м2</p>
            ' . $block_sleep . '
            ' . $block_floor . '
            ' . $block_finish . '
            </div></td>
        </tr>
       </table>
        </div>
        
    </div>
' . $img_all;
    return $html;
}


function create_presentation($html)
{
    $mainHtml = $basic = '<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style type="text/css">
    
            *,*{
                margin: 0px;
                padding: 0px
            }
    
            body {
                font-family: dejavu serif;
                background-color: #d4d4d4;
                
            }
    
            .slid1 {
                width: 1120px;
                height: 791px;
                background: #FFFFFF;
                border: 1 solid #a5a5a5;
            }
    
            .header{
                height: 93px;
                background: #E7E7E9;
                padding: 15px 70px;
            }
    
            h1 {
                font-size: 11px;
            }
    
            p{
                font-size: 9px;
            }
    
            .info{
                text-align: right;
                position: absolute;
                width:165px;
                height: 81px;
                left: 885px;
                top: 20px;
    
            }
    
            img {
                width: 103px;
                height: 66px;
            }
    
            .photo1 {
                width: 578px;
                height: 375px;
            }
    
            .block1{
                float: right;
            }
    
            .photo2 {
                position: absolute;
                width: 360px;
                height: 235px;
                /* left: 500px; */
            }
    
            .photo {
                padding: 20px 60px;
            }
    
            .description {
                width: 360px;
                height: 95px;
                font-size: 10px;
                color: #878787;
            }
            .fio {
                color: #878787;
                font-size: 10px;
            }
    
            .fio_text{
                bottom: 20px;
                float: right;
                position: relative;
                margin-right:10px;
            }
            .text-content {
                
                position: relative;
                top: 230px;
                
            }
    
            .block {
                width: 578px;
                height: 165px;
                background: #294651;
                margin-left:60px;
                float: left;
            }
            .info-block{
                color: #FFFFFF;
                padding: 10px 0 0 15px;
            }
    
            .info-block > h1 {
                font-size: 21px;
                margin: 0;
                padding-bottom: 10px;
            }
    
            .info-block > p {
                font-size: 12px;
                margin: 0;
                padding-bottom: 20px;
            }
    
            .info-block > h2 {
                font-size: 21px;
                margin: 0;
                padding: 15px 0 10px 0;
            }
        
            .slid2 {
                width: 842px;
                height: 595px;
                background: #FFFFFF;
                margin-top: 40px;
            }
            .logo {
                padding-top:15px;
            }
            .img_other {
                width: 1000px;
                height: 578px;
            }
            .box {
                background-color: #294651;
                width: 480px;
                height: 175px;
                position: absolute;
                left: 60px;
                top: 520px;
            }
            .content_2 {
                background-color: #294651;
                width: 1000px;
                height: 578px;
                margin-left:60px;
                margin-top: 40px;
            }
            </style>
    </head><body style="background-color:#ffffff;">' . $html . '
    </body>
    </html>
    ';
    writeToLog($mainHtml, 'html');
    $dompdf = new Dompdf\Dompdf();
    $dompdf->set_option('isRemoteEnabled', TRUE);
    $dompdf->setPaper('A4', 'landscape');
    $dompdf->loadHtml($mainHtml, 'UTF-8');
    $dompdf->render();
    $pdf = $dompdf->output();
    file_put_contents(__DIR__ . '/presentation/1.pdf', $pdf);
    $url = 'https://spa-bitrix.ru/masthev/create_presentation/presentation/1.pdf';
    //writeToLog($url, 'URL');
    return $url;
}


function format_price($value, $unit)
{
    if ($value > 0) {
        $value = number_format($value, 2, ',', ' ');
        $value = str_replace(',00', '', $value);

        if (!empty($unit)) {
            $value .= ' ' . $unit;
        }
    }

    return $value;
}

function writeToLog($data, $title = '')
{
    $log = "\n------------------------\n";
    $log .= date("Y.m.d G:i:s") . "\n";
    $log .= (strlen($title) > 0 ? $title : 'DEBUG') . "\n";
    $log .= print_r($data, 1);
    $log .= "\n------------------------\n";
    file_put_contents(getcwd() . '/log.log', $log, FILE_APPEND);
    return true;
}

?>