<?php

$INC_PATH = getenv("DOCUMENT_ROOT");

require($INC_PATH . "_x_local_subs/site_all.php");

$ID = get("id");
$PID = get("pid");

$ID_ARRAY = preg_split("/-/", $ID);
$ID = check_isDefault($ID_ARRAY[0]);

//print "$ID $PID";
//exit;

$CONTENT = "";

if (empty($ID)) {

    $RUBS_CONTENT = "";

    $query = "SELECT id, name FROM $MY_RUBS WHERE id=pid";

    $sth = $dbh->prepare($query);
    //$sth->bindValue(':id', $GOODS_PID, PDO::PARAM_STR);
    $sth->execute();
    $rows = $sth->fetchAll(PDO::FETCH_ASSOC);

    if (sizeof($rows) > 0) {

        foreach ($rows as $loc_key => $row) {

            $rub_id = $row["id"];
            $rub_name = $row["name"];


            $PICS_ARR = array();
            $P_LIST = "";

            $query = "SELECT $MY_GOODS_PICS.id, $MY_GOODS_PICS.pic FROM $MY_GOODS_PICS, $MY_GOODS WHERE $MY_GOODS.pid=:pid AND $MY_GOODS_PICS.pid = $MY_GOODS.id AND $MY_GOODS.vis = '1'";

            $sth = $dbh->prepare($query);
            $sth->bindValue(':pid', $rub_id, PDO::PARAM_STR);
            $sth->execute();
            $rows2 = $sth->fetchAll(PDO::FETCH_ASSOC);

            if (sizeof($rows2) > 0) {
                $i = 0;
                foreach ($rows2 as $loc_key2 => $row2) {

                    $loc_pic = $row2["pic"];

                    $P_LIST .= "$loc_pic<br>";

                    $PICS_ARR[$i] = $loc_pic;
                    //print "$i - $loc_pic<br>";
                    $i++;
                }
            }

            $rnd = rand(0, $i-1);
            //print "rubid=$rub_id rnd = $rnd<br>";

            $filename_pic = "";
            if (isset($PICS_ARR[$rnd])) {
                $filename_pic = $PICS_ARR[$rnd];
            }

/*            $RUBS_CONTENT .= <<<ENDOF
$loc_id $loc_name - $filename_pic (rnd = $rnd)<br>
<i>$P_LIST</i><br>
ENDOF;*/
            $RUB_PIC = "<img src='/images/def-pic.jpg' border='0'>";
            if (!empty($filename_pic)) {
                $RUB_PIC = <<<ENDOF
<img src="/images/pm/$filename_pic" border="0">
ENDOF;

            }

            $SELECTED = "";
            if ($rub_id == $PID) {
                $SELECTED = " rubs_item_selected";
            }

            $RUBS_CONTENT .= <<<ENDOF
<div class="rubs_item$SELECTED">
<a href="/catalog/$rub_id/">
<div class="rubs_item_content">
<div class="rubs_item_pic">$RUB_PIC</div>
<div class="rubs_item_name">$rub_name</div>
</div>
</a>
</div>
ENDOF;


        }
    }

    $IS_GOODS = 0;

    $PICS_ARR = array();


    $ADD = "";
    if (!empty($PID)) {
        $ADD = " AND pid=:pid";
    }

    $GOODS_ADD = "";

    $query = "SELECT id, pid, name, articul FROM $MY_GOODS WHERE vis=1" . "$ADD" . " LIMIT 50";

    $sth = $dbh->prepare($query);
    if (!empty($PID)) {
        $sth->bindValue(':pid', $PID, PDO::PARAM_STR);
    }
    $sth->execute();
    $rows = $sth->fetchAll(PDO::FETCH_ASSOC);

    if (sizeof($rows) > 0) {

        foreach ($rows as $loc_key => $row) {

            $loc_id = $row["id"];

            if (!empty($GOODS_ADD)) {
                $GOODS_ADD .= " OR ";

            }
            $GOODS_ADD .= "pid='$loc_id'";

            $IS_GOODS = 1;
        }
    }

    if ($IS_GOODS) {

        if (!empty($GOODS_ADD)) {
            $GOODS_ADD = " WHERE $GOODS_ADD";
        }

        $query = "SELECT pid, pic FROM $MY_GOODS_PICS$GOODS_ADD";

        $sth = $dbh->prepare($query);
        if (!empty($PID)) {
            $sth->bindValue(':pid', $PID, PDO::PARAM_STR);
        }
        $sth->execute();
        $rows2 = $sth->fetchAll(PDO::FETCH_ASSOC);

        if (sizeof($rows2) > 0) {

            foreach ($rows2 as $loc_key2 => $row2) {

                $loc_pid = $row2["pid"];
                $loc_pic = $row2["pic"];


                $PICS_ARR[$loc_pid] = $loc_pic;
//print "$loc_pid - $loc_pic<br>";
            }
        }


        $query = "SELECT id, pid, name, articul FROM $MY_GOODS WHERE vis=1" . "$ADD" . " LIMIT 50";

        $sth = $dbh->prepare($query);
        if (!empty($PID)) {
            $sth->bindValue(':pid', $PID, PDO::PARAM_STR);
        }
        $sth->execute();
        $rows = $sth->fetchAll(PDO::FETCH_ASSOC);

        if (sizeof($rows) > 0) {

            foreach ($rows as $loc_key => $row) {
                $loc_id = $row["id"];
                $loc_pid = $row["pid"];
                $loc_name = $row["name"];
                $loc_articul = $row["articul"];

                $loc_name_lat = trans($loc_name);

                $link = "/catalog/$loc_pid/$loc_id" . "-" . "$loc_name_lat";


                $THIS_PIC = "/images/pic_list_default.jpg";
                if (isset($PICS_ARR[$loc_id]) && !empty($PICS_ARR[$loc_id])) {
                    $filename_pic = $PICS_ARR[$loc_id];
                    $THIS_PIC = "/images/p/$filename_pic";
                }

                $CONTENT .= <<<ENDOF
<div class="goods_list_item">
<a href="$link"><div class="goods_list_item_content">
<div class="goods_list_image"><img src="$THIS_PIC"></div>
<div class="goods_list_name">$loc_name</div>
<div class="goods_list_articul">Артикул: $loc_articul</div>
</div></a>
</div>
ENDOF;

            }
        }
    } else {
        $CONTENT = <<<ENDOF
<div class="goods_list_empty">Товаров не найдено. Попробуйте выбрать другую рубрику каталога.</div>
ENDOF;

    }


} else {

//    print "$ID $PID";
//exit;

    $IS_FOUND = 0;

    $query = "SELECT pid, name, articul, descr, content FROM $MY_GOODS WHERE id=:id";

    $sth = $dbh->prepare($query);
    $sth->bindValue(':id', $ID, PDO::PARAM_STR);
    $sth->execute();
    $rows = $sth->fetchAll(PDO::FETCH_ASSOC);

    if (sizeof($rows) > 0) {

        foreach ($rows as $loc_key => $row) {

            $GOODS_PID = $row["pid"];
            $GOODS_NAME = $row["name"];
            $GOODS_ARTICUL = $row["articul"];
            $GOODS_DESCR = $row["descr"];
            $GOODS_CONTENT = $row["content"];

            $IS_FOUND = 1;

        }
    }


    if ($IS_FOUND) {

        $query = "SELECT pid, name, content_title, content, pic  FROM $MY_RUBS WHERE id=:id";

        $sth = $dbh->prepare($query);
        $sth->bindValue(':id', $GOODS_PID, PDO::PARAM_STR);
        $sth->execute();
        $rows = $sth->fetchAll(PDO::FETCH_ASSOC);

        if (sizeof($rows) > 0) {

            foreach ($rows as $loc_key => $row) {

                $RUB_PID = $row["pid"];
                $RUB_NAME = $row["name"];
                $RUB_CONTENT_TITLE = $row["content_title"];
                $RUB_CONTENT = $row["content"];
                $RUB_PIC = $row["pic"];


            }
        }

        if ($RUB_PID != $GOODS_PID) {

            $query = "SELECT pid, name FROM $MY_RUBS WHERE id=:id";

            $sth = $dbh->prepare($query);
            $sth->bindValue(':id', $RUB_PID, PDO::PARAM_STR);
            $sth->execute();
            $rows = $sth->fetchAll(PDO::FETCH_ASSOC);

            if (sizeof($rows) > 0) {

                foreach ($rows as $loc_key => $row) {

                    $PARENT_RUB_ID = $RUB_PID;
                    $PARENT_RUB_NAME = $row["name"];


                }
            }

        }


        $IS_STORAGE_FOUND = 0;

        $query = "SELECT id, name, timestamp FROM $MY_GOODS_STORAGE WHERE pid=:id LIMIT 1";

        $sth = $dbh->prepare($query);
        $sth->bindValue(':id', $ID, PDO::PARAM_STR);
        $sth->execute();
        $rows = $sth->fetchAll(PDO::FETCH_ASSOC);

        if (sizeof($rows) > 0) {

            foreach ($rows as $loc_key => $row) {
                $loc_id = $row["id"];

                $loc_name = $row["name"];
                $loc_tim = $row["timestamp"];

                $IS_STORAGE_FOUND = 1;

            }
        }
        $STORAGE_CONTENT = "";
        if ($IS_STORAGE_FOUND) {

            $STORAGE_FILENAME = $GOODS_STORAGE_FOLDER . $loc_name . "?$loc_tim";

            $STORAGE_CONTENT = <<<ENDOF
<div class="goods_storage"><a href="$STORAGE_FILENAME" target="_blank">Скачать документацию</a></div>
ENDOF;

        }


        $IS_PIC_FOUND = 0;

        $query = "SELECT id, pic, ts FROM $MY_GOODS_PICS WHERE pid=:id LIMIT 1";

        $sth = $dbh->prepare($query);
        $sth->bindValue(':id', $ID, PDO::PARAM_STR);
        $sth->execute();
        $rows = $sth->fetchAll(PDO::FETCH_ASSOC);

        if (sizeof($rows) > 0) {

            foreach ($rows as $loc_key => $row) {
                $loc_id = $row["id"];

                $loc_pic = $row["pic"];
                $loc_ts = $row["ts"];

                $IS_PIC_FOUND = 1;

            }
        }

        $PICS_CONTENT = "";
        if ($IS_PIC_FOUND) {
            $PICS_CONTENT = <<<ENDOF
<a href="/images/pb/$loc_pic?$loc_ts" class="image-popup-no-margins"><img src="/images/p/$loc_pic?$loc_ts" width="482" border="0"></a>
ENDOF;

        } else {
            $PICS_CONTENT = <<<ENDOF
            <img src="/images/pic_default.jpg" width="482" border="0">
ENDOF;

        }

        if (empty($GOODS_DESCR)) {

            $query = "SELECT content FROM $MY_CONTENT_DEFAULT WHERE typ='goods'";

            $sth = $dbh->prepare($query);
            //$sth->bindValue(':id', $ID, PDO::PARAM_STR);
            $sth->execute();
            $rows = $sth->fetchAll(PDO::FETCH_ASSOC);

            if (sizeof($rows) > 0) {

                foreach ($rows as $loc_key => $row) {
                    $loc_content = $row["content"];

                    $GOODS_DESCR = $loc_content;

                }
            }



        }

        $CONTENT .= <<<ENDOF
<div class="goods_card_content">
<div class="goods_card_content_imagearea">
<div class="goods_card_content_image">$PICS_CONTENT</div>
</div>
<div class="goods_card_content_namearea">
<div class="goods_card_content_namearea_inner">
<div class="goods_card_content_name">$GOODS_NAME</div>
<div class="goods_card_content_articul">Артикул: $GOODS_ARTICUL</div>
</div>
<div class="goods_card_content_namearea_form">
<a href="/request/?id=$ID"><img src="/images/request_button.jpg" border="0"></a>
</div>
</div>
</div>

<p>&nbsp;</p>
<div class="w3-border">
<div class="w3-bar w3-black">
  <button class="w3-bar-item w3-button testbtn" onclick="openCity(event, 'goods_descr')" id="myLink">Описание</button>
  <button class="w3-bar-item w3-button testbtn" onclick="openCity(event, 'goods_tech')">Технические характеристики</button>
</div>


<div id="goods_descr" class="w3-container city w3-animate-opacity">
$GOODS_DESCR
</div>

<div id="goods_tech" class="w3-container city w3-animate-opacity" style="display:none">
$GOODS_CONTENT
</div>

</div>


<script>

function openCityOld(cityName) {
  var i;
  var x = document.getElementsByClassName("city");
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";
  }
  document.getElementById(cityName).style.display = "block";
    
  
}


function openCity(evt, cityName) {
  var i;
  var x = document.getElementsByClassName("city");
  for (i = 0; i < x.length; i++) {
     x[i].style.display = "none";
  }
  var activebtn = document.getElementsByClassName("testbtn");
  for (i = 0; i < x.length; i++) {
    activebtn[i].className = activebtn[i].className.replace(" w3-dark-grey", "");
  }
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " w3-dark-grey";
}
var mybtn = document.getElementById("myLink");
mybtn.click();

function openc(cityn) {
    var i;
    var x = document.getElementsByClassName("cityclose");
    for (i = 0; i < x.length; i++) {
       x[i].style.display = "none";  
    }
    document.getElementById(cityn).style.display = "block";  
}


</script>

ENDOF;

        $CONTENT .= <<<ENDOF
<!-- Magnific Popup core CSS file -->
<link rel="stylesheet" href="/dist/magnific-popup.css">

<!-- jQuery 1.7.2+ or Zepto.js 1.0+ -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<!-- Magnific Popup core JS file -->
<script src="/dist/jquery.magnific-popup.min.js?v=1.0.0"></script>

<style>
/* padding-bottom and top for image */
.mfp-no-margins img.mfp-img {
	padding: 0;
}
/* position of shadow behind the image */
.mfp-no-margins .mfp-figure:after {
	top: 0;
	bottom: 0;
}
/* padding for main container */
.mfp-no-margins .mfp-container {
	padding: 0;
}


/*

for zoom animation
uncomment this part if you haven't added this code anywhere else

*/
/*

.mfp-with-zoom .mfp-container,
.mfp-with-zoom.mfp-bg {
	opacity: 0;
	-webkit-backface-visibility: hidden;
	-webkit-transition: all 0.3s ease-out;
	-moz-transition: all 0.3s ease-out;
	-o-transition: all 0.3s ease-out;
	transition: all 0.3s ease-out;
}

.mfp-with-zoom.mfp-ready .mfp-container {
		opacity: 1;
}
.mfp-with-zoom.mfp-ready.mfp-bg {
		opacity: 0.8;
}

.mfp-with-zoom.mfp-removing .mfp-container,
.mfp-with-zoom.mfp-removing.mfp-bg {
	opacity: 0;
}
*/
</style>
<script>
$(document).ready(function() {

	$('.image-popup-vertical-fit').magnificPopup({
		type: 'image',
		closeOnContentClick: true,
		mainClass: 'mfp-img-mobile',
		image: {
			verticalFit: true
		}

	});

	$('.image-popup-fit-width').magnificPopup({
		type: 'image',
		closeOnContentClick: true,
		image: {
			verticalFit: false
		}
	});

	$('.image-popup-no-margins').magnificPopup({
		type: 'image',
		closeOnContentClick: true,
		closeBtnInside: false,
		fixedContentPos: true,
		mainClass: 'mfp-no-margins mfp-with-zoom', // class to remove default margin from left and right side
		image: {
			verticalFit: true
		},
		zoom: {
			enabled: true,
			duration: 300 // don't foget to change the duration also in CSS
		}
	});

});
</script>
ENDOF;

        $CONTENT .= <<<ENDOF
$STORAGE_CONTENT
ENDOF;





        $RUB_PIC_FILENAME = "";
        if (!empty($RUB_PIC)) {
            $RUB_PIC_FILENAME = "/images/us/$RUB_PIC";
        }

        if (!empty($RUB_CONTENT)) {
            $CONTENT .= <<<ENDOF
<div class="useful_area">
<div class="useful_title">Полезная информация</div>
<div class="useful_content_flex">
    <div class="useful_pic_content"><img src="$RUB_PIC_FILENAME"></div>
    <div class="useful_text_content">
        <div class="useful_content_title">$RUB_CONTENT_TITLE</div>
        <div class="useful_content">$RUB_CONTENT</div>
    </div>
</div>
</div>
ENDOF;
        }




    } else {
        $CONTENT .= <<<ENDOF
<p>товар не найден</p>
ENDOF;

    }

}


hd("", "catalog", "");


print <<<ENDOF
<div class="nav_crumbs">
<div class="nav_crumbs_item"><a href="/">Главная</a></div>
<div class="nav_crumbs_item_diver"> / </div>
<div class="nav_crumbs_item"><a href="/catalog/">Продукция</a></div>

ENDOF;

if (!empty($ID)) {

    if (isset($PARENT_RUB_PID) && !empty($PARENT_RUB_PID)) {

        print <<<ENDOF
        <div class="nav_crumbs_item_diver"> / </div>
<div class="nav_crumbs_item"><a href="/catalog/$PARENT_RUB_PID/">$PARENT_RUB_NAME</a></div>
ENDOF;

    }

    if (isset($RUB_PID) && !empty($RUB_PID)) {

        print <<<ENDOF
        <div class="nav_crumbs_item_diver"> / </div>
<div class="nav_crumbs_item"><a href="/catalog/$RUB_PID/">$RUB_NAME</a></div>
ENDOF;

    }


}

print <<<ENDOF
</div>
ENDOF;


if (empty($ID)) {

    print <<<ENDOF
<div class="rubs_list">
$RUBS_CONTENT
</div>
ENDOF;

    print <<<ENDOF
<div class="catalog_content">
<p>Волоконно-оптический приемопередающий модуль  — это автономный компонент, способный как передавать, так и принимать сигнал. Обычно оптические приемопередатчики устанавливаются в такие устройства, как маршрутизаторы или сетевые карты, которые имеют один или несколько слотов для приемопередающих модулей (например,  QSFP+ ,  QSFP-DD , QSFP28, SFP28, SFP+, SFP).
 
</p>

<p>
Например,  QSFP28 , QSFP+,  SFP28 , QSFP-DD, трансивер  SFP+  (SFP Plus), трансивер X2, трансивер XENPAK, трансивер XFP, трансивер SFP (Mini GBIC), трансивер GBIC,  трансивер CWDM/DWDM ,  40G QSFP+  и CFP, SFP для видео 3G-SDI, двунаправленный трансивер WDM и трансивер PON. Fibermart также предлагает  активные оптические кабели  AOC для  100G QSFP28 ,  40G QSFP+ , 25G SFP28,  400G QSFP-DD . Все  оптические трансиверы,  волоконно-оптические трансиверы, активные оптические кабели и  кабели прямого подключения Fibermart  на 100% совместимы с ведущими брендами, такими как  Cisco , HP, Juniper,  Finisar , Nortel, Force10, D-link, 3Com. На них распространяется пожизненная гарантия, поэтому вы можете покупать с уверенностью. Мы также можем изготовить на заказ оптические волоконно-оптические трансиверы, соответствующие вашим конкретным требованиям.
</p>
<p>&nbsp;</p>
</div>
ENDOF;

}

if (empty($ID)) {


    print <<<ENDOF
<div class="goods_list">
$CONTENT
</div>
ENDOF;

} else {
    print <<<ENDOF
<div class="goods_card">
$CONTENT
</div>
ENDOF;
}


fd();



