<?php
defined('COT_CODE') or die('Wrong URL');

$return['content'] = array(
  'prd' => array(
    'id' => '',
    'gdesc' => '',
    'gwork' => '',
    'ginst' => '',
    'is_package' => 0
  ),
  'reviews' => array(
    'html' => '<br><div class="pb20">Пока нет</div>'
  ),
  'track_order' => array(
    'html' => '',
    /*
    'orderData' => array(
      "OID" => "2698211",
      "PID" => "402502",
      "payerId" => "474969",
      "count" => "1",
      "prdspayerprice" => "6000.00",
      "prdsworkerprice" => "4800.00",
      "duration" => "432000",
      "deadline" => null,
      "stime" => "1534499623",
      "crt" => "4800.00",
      "totalprice" => "6000.00",
      "gtitle" => "TEST",
      "totaldays" => "5",
      "photo" => "86\/402502-1.jpg",
      "price" => "500.00",
      "owner" => "612929",
      "url" => "URL",
      "lang" => "ru",
      "owner_live_date" => "1534499559",
      "ownerpp" => "noprofilepicture.gif",
      "days" => "3",
      "ginst" => "TEST2",
      "username" => "evgenya23021982",
      "sellername" => "",
      "seo" => "website-development",
      "buyer" => "textuser",
      "buyername" => "testuser2",
      "buyer_live_date" => "1535283113",
      "buyerpp" => "474969-4.jpg",
      "late" => "0",
      "currency_id" => "643",
      "currency_rate" => "1.00",
      "catId" => "37"
    )
    */
  ),
  'files' => ''
);
$return['status'] = 'error';

$orderId = $_REQUEST['orderId'];

if($orderId > 0 && $usr['id'] > 0) {
  require_once cot_incfile('projects', 'module');

  $offer = $db->query("SELECT * FROM $db_projects_offers as o LEFT JOIN $db_projects as p ON o.offer_pid=p.item_id WHERE offer_id=".$orderId.($usr['isadmin'] ? '' : " AND (item_userid=".$usr['id']." OR offer_userid=".$usr['id'].")")." LIMIT 1")->fetch();

  if($offer['offer_id'] > 0) {
    $mdata = (!empty($offer['offer_json']) ? json_decode($offer['offer_json'],1) : array());
    if(!is_array($mdata)) $mdata = array();

    $item_cache = (!is_array($mdata['item_cache']) ? (!empty($mdata['item_cache']) ? json_decode($mdata['item_cache'],1) : array()) : $mdata['item_cache']);
    if(!is_array($item_cache)) $item_cache = array();
    if(!$item_cache['item_id'] && $mdata['pid'] > 0) {
      require_once cot_incfile('market', 'module');
      $item_cache = $db->query("SELECT * FROM $db_market WHERE item_id=".$mdata['pid'].' AND item_state=0 LIMIT 1')->fetch();
    }

    $item_catcostplans = (!empty($item_cache['item_catcostplans']) ? json_decode($item_cache['item_catcostplans'],1) : array());
    if(!is_array($item_catcostplans)) $item_catcostplans = array();

    require_once cot_incfile('catcostplans', 'plug');

    $return['content']['prd'] = array(
      'id' => ($item_cache['item_id'] > 0 ? $item_cache['item_id'] : ''),
      'gdesc' => cot_html_removelines($item_cache['item_text']),
      'gwork' => '',
      'ginst' => cot_html_removelines($item_cache['item_instruction']),
      'is_package' => 0
    );

    if($item_catcostplans['bundle_type'] == 'package') {
      $return['content']['prd']['is_package'] = 1;

      $return['content']['track_order']['html'] = '<table class="order-info" style="width:100%;border-spacing:0;table-layout:fixed;">
        <thead style="background-color:#f0f0f0;color:#000">
          <tr>
            <td style="padding:10px 20px;width:54%;" class="font-OpenSansSemi">Услуги</td>
            <td class="ta-center nowrap"style="width:14%;padding:10px 10px;">Кол-во</td>
            <td class="ta-center nowrap" style="width:14%;padding:10px 10px;">Срок</td>
            <td class="ta-center" style="width:18%;padding:10px 10px;">Стоимость</td>
          </tr>
        </thead>
        <tbody>
          <tr class="font-OpenSans prd">
            <td style="padding:10px 20px;width: 100%;" class="order-info_services">
              <a class="f15 track-description-spoiler font-OpenSans break-word" style="cursor:pointer;" onclick="showDodjiDescription($(this));">
                Вариант '.cot_sitename('padeg', 'r').' "Стандарт". '.$item_cache['item_title'].'<img src="/images/arrow_right_blue.png" width="9" alt=""/>
              </a>
            </td>
            <td class="nowrap" style="text-align:center;padding:10px 20px">1</td>
            <td class="nowrap" style="text-align:right;padding:10px 20px">2 дня</td>
            <td class="nowrap" style="text-align:right;padding:10px 20px;color:#009900;">5 000&nbsp;<span class="rouble">Р</span></td>
          </tr>
          <tr class="track-order_description" style="display: none;">
            <td colspan="4" style="padding:10px 20px;" class="p10">
              <div class="shortened-url-track">
                '.$item_cache['item_text'].'
              </div>
              <div class="mt10" onclick="hideDodjiDescription($(this));"><a class="dib mt5 m-hidden" style="cursor:pointer;">Свернуть<img src="/images/arrow_right_blue.png" class="rotate180" width="9" alt=""/></a></div>
              <div style="margin:5px -5px -5px;background-color:#EAEAEA;height:1px;"></div>
            </td>
          </tr>';

        $packtype = $mdata['package_type'];
        $checkcat = (!empty($item_cache['item_catattr']) ? $item_cache['item_catattr'] : $item_cache['item_cat']);

        if(!empty($checkcat)) {
          $catsql = $db->query("SELECT * FROM $db_structure WHERE structure_area='market' AND structure_code='".$checkcat."' LIMIT 1")->fetch();

          if($catsql['structure_id'] > 0) {
            $plan = $db->query("SELECT * FROM $db_catcostplans WHERE p_area='market' AND p_catid=".$catsql['structure_id']." LIMIT 1")->fetch();
            $jj=0;
            while($jj < 5 && !$plan['p_id'] && $structure['market'][$catsql['structure_code']]['parent'] != $catsql['structure_code']) {
              $catsql = $db->query("SELECT * FROM $db_structure WHERE structure_area='market' AND structure_code='".$structure['market'][$catsql['structure_code']]['parent']."' LIMIT 1")->fetch();
              if($catsql['structure_id'] > 0) {
                $plan = $db->query("SELECT * FROM $db_catcostplans WHERE p_area='market' AND p_catid=".$catsql['structure_id']." LIMIT 1")->fetch();
              }
              $jj++;
            }

            if($plan['p_id'] > 0) {
              $options = $db->query("SELECT * FROM $db_catcostplans_attrs WHERE at_planid=".$plan['p_id']." ORDER BY at_id ASC")->fetchAll();

              if($plan['p_onlyprice'] == 1 && $structure['market'][$catsql['structure_code']]['parent'] != $catsql['structure_code']) {
                $parent = $db->query("SELECT * FROM $db_structure WHERE structure_area='market' AND structure_code='" . $structure['market'][$catsql['structure_code']]['parent'] . "' LIMIT 1")->fetch();
                if($parent['structure_id'] > 0 && $parent['structure_id'] != $catsql['structure_id']) $options = get_catcostplans_parent_attrs($parent, 'market', $plan['p_bl_attrs'], $options);
              }

              if(count($mdata['gextras_selected']['category']) > 0) foreach($options as $otp) {
                foreach($mdata['gextras_selected']['category'] as $popt) {
                  if($popt['id'] == $otp['at_id']) {
                    $return['content']['prd']['gwork'] .= '<tr class="option font-OpenSans">
                        <td style="padding:10px 20px;width: 100%;" class="font-OpenSans break-word"><img class="dib mr10" src="themes/itbirja/images/ico-ok.png" alt="ОК"/>'.$otp['at_title'].'</td>
                        <td class="nowrap" style="text-align:center;padding:10px 20px">'.$popt['val'].'</td>
                        <td class="nowrap" style="text-align:right;padding:10px 20px"></td>
                        <td class="" style="text-align:right;padding:10px 20px;color:#009900;"></td>
                      </tr>';
                    break;
                  }
                }
              }

              if(count($item_catcostplans['epackages']) > 0) foreach($item_catcostplans['epackages'] as $otp) {
                $popval = $mdata['gextras_selected']['custom_val'][$otp['id']];
                if(!empty($mdata['gextras_selected']['custom_val'][$otp['id']])) {
                  $return['content']['prd']['gwork'] .= '<tr class="option font-OpenSans">
                    <td style="padding:10px 20px;width: 100%;" class="font-OpenSans break-word"><img class="dib mr10" src="themes/itbirja/images/ico-ok.png" alt="ОК"/>'.$otp['at_title'].'</td>
                    <td class="nowrap" style="text-align:center;padding:10px 20px">'.$mdata['gextras_selected']['custom_val'][$otp['id']].'</td>
                    <td class="nowrap" style="text-align:right;padding:10px 20px"></td>
                    <td class="" style="text-align:right;padding:10px 20px;color:#009900;"></td>
                  </tr>';
                }
              }
            }
          }
        }

        if(is_array($mdata['gextras_custom'])) foreach($mdata['gextras_custom'] as $val) {
          $return['content']['track_order']['html'] .= '<tr class="option font-OpenSans">
            <td style="padding:10px 20px;width: 100%;" class="font-OpenSans break-word">'.$val['name'].'</td>
            <td class="nowrap" style="text-align:center;padding:10px 20px">1</td>
            <td class="nowrap" style="text-align:right;padding:10px 20px">'.cot_declension($val['time'], 'день,дня,дней').'</td>
            <td class="" style="text-align:right;padding:10px 20px;color:#009900;">'.($val['price'] > 0 ? number_format($val['price'], 0, '.', '').'&nbsp;<span class="rouble">Р</span>' : '').'</td></td>
          </tr>';
        }

        $return['content']['prd']['gwork'] .= '<tr class="m-hidden">
            <td colspan="4" style="margin:0px;padding:0px;">
              <div style="margin:5px 15px;background-color:#EAEAEA;height:1px;"></div>
            </td>
          </tr>
          <tr class="m-text-center">
            <td></td>
            <td class="nowrap f14 font-OpenSansSemi p10 m-text-cente t-align-r" style="padding:10px 20px;">Итого:</td>
            <td class="nowrap f14 font-OpenSansSemi p10 m-text-center t-align-r" style="padding:10px 20px">'.cot_declension($mdata['duration'], 'день,дня,дней').'</td>
            <td class="nowrap f14 font-OpenSansSemi p10 m-text-center t-align-c" style="padding:10px 0"><span class="f20" style="color:#009900;">'.number_format($mdata['price'], 0, '.', '').'&nbsp;<span class="rouble">Р</span></span></td>
          </tr>
        </tbody>
      </table>';
    } else {
      $return['content']['prd']['gwork'] = $item_catcostplans['bundles']['category']['title'];

      $return['content']['track_order']['html'] = '<table class="order-info" style="width:100%;border-spacing:0;table-layout:fixed;">
        <thead style="background-color:#f0f0f0;color:#000">
          <tr>
            <td style="padding:10px 20px;width:47%;" class="font-OpenSansSemi">Услуги</td>
            <td class="ta-center nowrap"style="width:14%;padding:10px 10px;">Кол-во</td>
            <td class="ta-center nowrap" style="width:14%;padding:10px 10px;">Срок</td>
            <td class="ta-center" style="width:18%;padding:10px 10px;">Стоимость</td>
          </tr>
        </thead>
        <tbody>
          <tr class="font-OpenSans prd">
            <td style="padding:10px 20px;width: 100%;" class="order-info_services">
              <span class="semibold break-word">'.$mdata['name'].'</span>
            </td>
            <td class="ta-center nowrap" style="padding:10px 20px">'.$mdata['count'].'</td>
            <td class="nowrap" style="text-align:right;padding:10px 20px">'.cot_declension($mdata['duration'], 'день,дня,дней').'</td>
            <td class="nowrap" style="text-align:right;padding:10px 20px;color:#009900;">'.($item_cache['item_cost'] > 0 ? number_format($item_cache['item_cost'], 0, '.', '').'&nbsp;<span class="rouble">Р</span>' : '').'</td>
          </tr>
          <tr class="track-order_description" style="display: none;">
            <td colspan="4" style="padding:10px 20px;" class="p10">
              <div class="shortened-url-track">
                <span class="bold db">Описание '.cot_sitename('padeg', 'r').'</span>
                <p>'.'todo'.'</p>
              </div>
              <br>
              <div><span class="bold">Объем услуги при заказе одного '.cot_sitename('padeg', 'r').': </span> 1 модуль интернет-магазин</div>
              <div class="mt10" onclick="hideDodjiDescription($(this));"><a class="dib mt5 m-hidden" style="cursor:pointer;">Свернуть<img src="/images/arrow_right_blue.png" class="rotate180" width="9" alt=""/></a></div>
            </td>
          </tr>';

        foreach($item_catcostplans['bundles']['custom'] as $key => $val) {
          if(in_array($key, $mdata['gextras']) && $mdata['gextras_count'][$key] > 0) {
            $val['price'] = $costplans_prices[$val['price']];
            $val['count'] = $mdata['gextras_count'][$key];
            $return['content']['track_order']['html'] .= '<tr class="option font-OpenSans">
              <td style="padding:10px 20px;width: 100%;" class="font-OpenSans break-word">'.$val['name'].'</td>
              <td class="nowrap" style="text-align:center;padding:10px 20px">'.$val['count'].'</td>
              <td class="nowrap" style="text-align:right;padding:10px 20px">'.cot_declension($val['duration'], 'день,дня,дней').'</td>
              <td class="" style="text-align:right;padding:10px 20px;color:#009900;">'.($val['price'] > 0 ? number_format($val['price'], 0, '.', '').'&nbsp;<span class="rouble">Р</span>' : '').'</td></td>
            </tr>';
          }
        }
        if(is_array($mdata['gextras_custom'])) foreach($mdata['gextras_custom'] as $val) {
          $return['content']['track_order']['html'] .= '<tr class="option font-OpenSans">
            <td style="padding:10px 20px;width: 100%;" class="font-OpenSans break-word">'.$val['name'].'</td>
            <td class="nowrap" style="text-align:center;padding:10px 20px">1</td>
            <td class="nowrap" style="text-align:right;padding:10px 20px">'.cot_declension($val['time'], 'день,дня,дней').'</td>
            <td class="" style="text-align:right;padding:10px 20px;color:#009900;">'.($val['price'] > 0 ? number_format($val['price'], 0, '.', '').'&nbsp;<span class="rouble">Р</span>' : '').'</td></td>
          </tr>';
        }
        $return['content']['track_order']['html'] .= '<tr class="m-hidden">
            <td colspan="4" style="margin:0px;padding:0px;">
              <div style="margin:5px 15px;background-color:#EAEAEA;height:1px;"></div>
            </td>
          </tr>
          <tr class="m-text-center">
            <td></td>
            <td class="nowrap f14 font-OpenSansSemi p10 m-text-cente t-align-r" style="padding:10px 20px;">Итого:</td>
            <td class="nowrap f14 font-OpenSansSemi p10 m-text-center t-align-r" style="padding:10px 20px">'.cot_declension($mdata['duration'], 'день,дня,дней').'</td>
            <td class="nowrap f14 font-OpenSansSemi p10 m-text-center t-align-c" style="padding:10px 0"><span class="f20" style="color:#009900;">'.number_format($mdata['price'], 0, '.', '').'&nbsp;<span class="rouble">Р</span></span></td>
          </tr>
        </tbody>
      </table>';
    }

    /* === Hook === */
    foreach (cot_getextplugins('api.project_get_Details') as $pl)
    {
    	include $pl;
    }
    /* ===== */

    $return['content']['track_order']['html'] = cot_html_removelines($return['content']['track_order']['html']);

    $return['content']['reviews']['html'] = '<div class="reviews_order">
      <div class="reviews_order_type">
        <div id="pos" data-count="1" data-type="positive" class="reviews-tab__item active" ><span>Положительные</span> 1</div>
        <div id="neg" data-count="0" data-type="negative" class="reviews-tab__item" style="cursor: default; color: #000; border-bottom: 1px solid #eaeaea;">
          <span style="background-position: -1px -2px;">Отрицательные</span> 0
          <div class="reviews_order_preloader-js preloader__ico preloader__ico--analytics hidden" ></div>
        </div>
      </div>
      <div class="reviews_order_block" style="display:block;" >
        <ul class="gig-reviews-list mb20">
          <li class="clearfix">
            <div itemprop="review" itemscope itemtype="http://schema.org/Review" style="display: none;">
              <span itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">
                <meta itemprop="bestRating" content="5" >
                <meta itemprop="worstRating" content="1" >
                <meta itemprop="ratingValue" content="5" >
              </span>
              <span itemprop="name">Установка интернет-магазина 1С Битрикс</span><span itemprop="author" itemscope itemtype="http://schema.org/Person"><span itemprop="name">thules</span></span>
              <meta itemprop="datePublished" content="2018-12-07">
              <div itemprop="reviewBody">Все быстро, все отлично! Был бы я на связи чаще, то работа была бы закончена еще быстрее, чем обсуждалось по срокам. Спасибо! </div>
            </div>
            <span class="review-avatar">
              <div class="user-avatar">
                <div class="user-avatar__default">t</div>
              </div>
            </span>
            <span class="comment ">
              <span class="comment-meta"><a href="https://prd.ru/user/thules">thules</a></span><span class="f13 color-gray font-OpenSans db"><i class="ico-green-circle smile dib v-align-m"></i><span class="dib v-align-m ml5">1 месяц  назад</span></span><span class="comment-comment">Все быстро, все отлично! Был бы я на связи чаще, то работа была бы закончена еще быстрее, чем обсуждалось по срокам. Спасибо! </span>
              <span>
                <span>
                  <span class="review-avatar">
                    <div class="user-avatar"><img class="user-avatar__picture rounded" src="https://prd.ru/files/avatar/medium/25/562788-1.jpg" srcset="https://prd.ru/files/avatar/medium/25/562788-1.jpg 1x, https://prd.ru/files/avatar/medium_r/25/562788-1.jpg 2x" alt="s"></div>
                  </span>
                  <span class="f14 comment"><span class="comment-meta"><a href="https://prd.ru/user/seo-max24">seo-max24</a></span><span class="color-gray font-OpenSans db"><span class="dib v-align-m f13">1 месяц  назад</span></span><span class="comment-comment">Спасибо за отзыв! Всегда рад помочь! </span></span>
                </span>
              </span>
            </span>
          </li>
        </ul>
      </div>
    </div>
    <div class="m-text-center">
      <div class="more-btn-blue more-btn-reviews mb10 pr0" data-offset="" style="display:none;">
        <span id="more-text" class="more-btn__text" style="cursor: pointer;">Показать ещё</span><span id="arrow-down"></span>
        <div class="reviews_order_preloader-js preloader__ico preloader__ico--analytics hidden" ></div>
      </div>
    </div>';
    $return['content']['reviews']['html'] = '';//cot_html_removelines($return['content']['reviews']['html']);

    $return['content']['files'] = cot_html_removelines($return['content']['files']);

    $return['mdata'] = $mdata; /** del **/
    $return['item_catcostplans'] = $item_catcostplans; /** del **/

    $return['status'] = 'success';
    $return['success'] = true;
  }
}

?>