<?php

/* ====================
  [BEGIN_COT_EXT]
  Hooks=ds.ajax.send.first
  [END_COT_EXT]
  ==================== */

defined('COT_CODE') or die('Wrong URL');

require_once cot_incfile('market', 'module');

if($message_type == 'offer_custom_prd') { // Индивидульная услуга лайт версия
  $mtype = 2;
  $mdata = array(
    'type' => 'offer_custom_prd',
    'pid' => 0,
    'name' => cot_import('prd_name', 'P', 'TXT'),
    'desc' => cot_import('prd_desc', 'P', 'TXT'),
    'duration' => cot_import('prd_duration', 'P', 'INT'),
    'price' => cot_import('prd_price', 'P', 'NUM'),
    'count' => 1,
    'decline' => 0,
    'orderId' => 0
  );
  if(!$mdata['duration']) $mdata['duration'] = 0;
  if(!$mdata['price']) $mdata['price'] = 500;
} elseif($message_type == 'offer_prd') {
  $mid = cot_import('prd_id', 'P', 'INT');
  $mitem = $db->query("SELECT * FROM $db_market WHERE item_id=".$mid.' AND item_userid='.$usr['id'].' AND item_state=0 LIMIT 1')->fetch();
  if($mitem['item_id'] > 0) {
    $item_catcostplans = (!empty($mitem['item_catcostplans']) ? json_decode($mitem['item_catcostplans'], 1) : array());
    $mtype = 3;
    $mdata = array(
      'type' => 'offer_prd',
      'pid' => $mid,
      'name' => cot_import('prd_name', 'P', 'TXT'),
      'desc' => cot_import('prd_desc', 'P', 'TXT'),
      'duration' => 0,
      'price' => cot_import('prd_price', 'P', 'NUM'),
      'count' => cot_import('prd_count', 'P', 'INT'),
      'item_cache' => $mitem,
      'package_type' => cot_import('prd_package_type', 'P', 'TXT'),
      'gextras' => cot_import('gextras', 'P', 'ARR'),
      'gextras_count' => array(),
      'gextras_custom' => array(),
      'gextras_selected' => array(), /* для package */
      'decline' => 0,
      'orderId' => 0
    );
    if(!$mdata['count']) $mdata['count'] = 1;
    if(empty($mdata['name'])) $mdata['name'] = $mitem['item_title'];
    if(empty($mdata['desc'])) $mdata['desc'] = $mitem['item_text'];

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

    if($item_catcostplans['bundle_type'] == 'package') {
      $cpdata = cot_get_costplan_bydata($item_catcostplans, $mdata['package_type']);

      $mdata['gextras_selected'] = $cpdata['bundle'];
      $mdata['price'] = $cpdata['price'];
      $mdata['duration'] = $cpdata['days'];
    } else {
      $mdata['price'] = $mitem['item_cost']*$mdata['count'];
      $mdata['duration'] = $item_catcostplans['bundles']['category']['time']*$mdata['count'];
    }

    if($item_catcostplans['bundle_type'] == 'single' && count($mdata['gextras']) > 0) {
      foreach($mdata['gextras'] as $eid) {
        if(is_array($item_catcostplans['bundles']['custom'][$eid])) {
          $mdata['gextras_count'][$eid] = cot_import('extra_count'.$eid, 'P', 'INT');
          if(!$mdata['gextras_count'][$eid]) {
            $mdata['gextras_count'][$eid] = 0;
          } else {
            $mdata['price'] += $costplans_prices[$item_catcostplans['bundles']['custom'][$eid]['price']]*$mdata['gextras_count'][$eid];
            $mdata['duration'] += $item_catcostplans['bundles']['custom'][$eid]['duration']*$mdata['gextras_count'][$eid];
          }
        }
      }
    }

    $customExtraName = cot_import('customExtraName', 'P', 'ARR');
    $customExtraTime = cot_import('customExtraTime', 'P', 'ARR');
    $customExtraPrice = cot_import('customExtraPrice', 'P', 'ARR');

    foreach($customExtraName as $cid => $cname) {
      if(!empty($cname)) $mdata['gextras_custom'][] = array(
        'name' => $cname,
        'time' => $customExtraTime[$cid],
        'price' => $costplans_prices[$customExtraPrice[$cid]],
        'priceId' => $customExtraPrice[$cid],
      );
      $mdata['price'] += $costplans_prices[$customExtraPrice[$cid]];
      $mdata['duration'] += $customExtraTime[$cid];
    }
  }
}