<?php

defined('ABSPATH') || die('Access Denied');

wp_enqueue_script('wde_' . $this->_layout);
wp_enqueue_style('wde_' . $this->_layout);

$options = $this->options;
$decimals = $options->option_show_decimals == 1 ? 2 : 0;
$products_data = $this->products_data;
$pager_data = $this->pager_data;

$theme = $this->theme;
$row_default_currency = $this->row_default_currency;
?>
<div class="container">
  <div class="row">
    <div class="col-sm-12">
      <!-- panel -->
      <div class="wd_shop_panel_user_data panel panel-default">
        <div class="panel-body">
          <div class="row">
            <div class="col-sm-12">
              <h2 class="wd_shop_header">
                <?php _e('Products data', 'wde'); ?>
              </h2>
              <!-- product data form -->
              <form name="wd_shop_main_form" action="" method="POST">
                <ul class="wd_shop_product_containers_list" data-shipping-calculation="<?php echo $options->option_order_shipping_type; ?>">
                  <?php
                  if (is_array($products_data)) {
                    foreach ($products_data as $product_data) {
                      $order_product_id = $product_data->order_product_id;
                      $selectable_parameter_array = array();
                      foreach ($product_data->selectable_parameters_data as $selectable_parameter) {
                        if ($selectable_parameter->type_id != 1) {
                          $selectable_parameter_array[$selectable_parameter->name] = 0;
                        }
                      }
                      $selectable_parameter_json = json_encode($selectable_parameter_array);
                        ?>
                        <!-- product data container -->
                    <li class="wd_shop_product_data_container"
                        count_is_unlimited="<?php echo $product_data->unlimited; ?>"
                        count_available="<?php echo $product_data->amount_in_stock; ?>"
                        parameters_price='<?php echo $selectable_parameter_json; ?>'
                        product_price = "<?php echo $product_data->price_without_t_d; ?>"
                        tax_rate="<?php echo $product_data->tax_rate; ?>"
                        discount_rate="<?php echo $product_data->discount_rate; ?>"
                        >
                      <div class="row">
                        <!-- name -->
                        <div class="col-sm-12">
                          <h4 class="wd_shop_header_sm wd_shop_product_name_all">
                            <?php echo $product_data->name; ?>
                          </h4>
                        </div>
                      </div>
                      <div class="row">
                        <div class="col-sm-4">
                          <div class="row">
                            <!-- count -->
                            <div class="form-group col-sm-12">
                              <?php
                              if ($product_data->is_available == true) {
                                ?>
                                <label
                                    for="wd_shop_product_<?php echo $product_data->id; ?>_data_quantity"
                                    class="control-label">
                                    <?php _e('Quantity', 'wde') ?>:
                                </label>
                                <input type="number"
                                 name="product_count_<?php echo $product_data->id . '_' . $order_product_id; ?>"
                                 id="wd_shop_product_<?php echo $product_data->id  . '_' . $order_product_id; ?>_data_quantity"
                                 class="wd_shop_product_data_count form-control wd-input-xs"
                                 value="<?php echo $product_data->count; ?>" min="1">
                                <?php
                              }
                              ?>
                              <div>
                                <small><?php echo $product_data->available_msg; ?></small>
                              </div>
                            </div>
                            <br />
                            <!-- parameters -->
                            <?php
                            if (is_array($product_data->selectable_parameters_data)) {
                              foreach ($product_data->selectable_parameters_data as $selectable_parameter_data) {
                                $parameter_id = $selectable_parameter_data->id;
                                $name = $selectable_parameter_data->name;
                                $values_list = $selectable_parameter_data->values_list;
                                $value = $selectable_parameter_data->value;
                                $type_id = $selectable_parameter_data->type_id;
                                  ?>
                                  <div class="form-group col-sm-12">
                                    <label for="wd_shop_product_data_parameter_<?php echo $name; ?>">
                                      <?php echo $name; ?>:
                                    </label>
                                    <div>
                                      <?php
                                    switch ($type_id) {
                                      // Input field
                                      case 1:
                                        ?>
                                      <input type="text" name='product_parameter_<?php echo $product_data->id . '_' . $parameter_id; ?>' id=""
                                             class="wd_shop_parameter_input form-control wd-input-xs"
                                             value="<?php echo $value; ?>" />
                                        <?php
                                        break;
                                      // Select
                                      case 3:
                                        $default_value = array();
                                        $default_value['value'] = 0;
                                        $default_value['text'] = __('Select', 'wde');
                                        $default_value['type_id'] = 0;
                                        $default_value['parameter_price'] = '';
                                        array_unshift($values_list, $default_value);
                                        ?>
                                      <select id="wd_shop_selectable_parameter_<?php echo $name  . '_' . $order_product_id; ?>"
                                              name="product_parameter_<?php echo $product_data->id . '_' . $parameter_id  . '_' . $order_product_id; ?>"
                                              data-type="<?php echo $type_id; ?>"
                                              class="wd_shop_parameter_select form-control wd-input-xs">
                                        <?php
                                        if (is_array($values_list)) {
                                          foreach ($values_list as $value_list) {
                                            ?>
                                          <option value="<?php echo $value_list['value']; ?>"
                                                  data-price="<?php echo $value_list['parameter_price']; ?>"
                                                  <?php if ($value_list['value'] === $value) {
                                                  ?>selected="SELECTED" <?php }?>>
                                            <?php echo $value_list['text']; ?>
                                          </option>
                                            <?php
                                          }
                                        }
                                        ?>
                                      </select>
                                        <?php
                                        break;
                                      // Radio
                                      case 4:
                                        if (is_array($values_list)) {
                                          foreach ($values_list as $value_list) {
                                            ?>
                                        <input type="radio"
                                               id="wd_shop_checkbox_parameter_<?php echo $name . $value_list['value'] . '_' . $order_product_id; ?>"
                                               name='product_parameter_<?php echo $product_data->id . '_' . $parameter_id . '_' . $order_product_id; ?>'
                                               data-type="<?php echo $type_id; ?>"
                                               value="<?php echo $value_list['value'] ?>"
                                               data-price="<?php echo $value_list['parameter_price']; ?>"
                                               class="parameters_input wd_shop_parameter_radio"
                                               <?php echo ($value_list['value'] == $value) ? 'checked="checked"' : '';?> />
                                        <label
                                          for="wd_shop_checkbox_parameter_<?php echo $name . $value_list['value'] . '_' . $order_product_id; ?>"
                                          id="wd_shop_checkbox_parameter_<?php echo $name; ?>"
                                          class="parameters_label">
                                          <?php echo $value_list['text'] ?>
                                        </label>
                                            <?php
                                          }
                                        }
                                        break;
                                      // Checkbox
                                      case 5:
                                        if (is_array($values_list)) {
                                          foreach ($values_list as $value_list) {
                                            ?>
                                        <input type="checkbox"
                                               id="wd_shop_checkbox_parameter_<?php echo $name . $value_list['value'] . '_' . $order_product_id; ?>"
                                               name='product_parameter_<?php echo $product_data->id . '_' . $parameter_id . '_' . $order_product_id; ?>[]'
                                               data-type="<?php echo $type_id; ?>"
                                               value="<?php echo $value_list['value'] ?>"
                                               data-price="<?php echo $value_list['parameter_price']; ?>"
                                               class="parameters_input wd_shop_parameter_checkbox"
                                               <?php
                                               if (gettype($value) != 'string') {
                                                 echo in_array($value_list['value'], $value) ? 'checked="checked"' : '';
                                               }
                                              ?> />
                                        <label
                                          for="wd_shop_checkbox_parameter_<?php echo $name . $value_list['value'] . '_' . $order_product_id; ?>"
                                          id="label_<?php echo $name; ?>"
                                          class="parameters_label">
                                          <?php echo $value_list['text']; ?>
                                        </label>
                                            <?php
                                          }
                                        }
                                        break;
                                    }
                                    ?>
                                    </div>
                                  </div>
                                <?php
                              }
                            }
                            ?>
                          </div>
                        </div>
                        <?php
                        if (($product_data->enable_shipping == 1 or ($product_data->enable_shipping == 2 /*&& $options->checkout_enable_shipping == 1*/)) && ($product_data->country_specified == true)) {
                          ?>
                        <!-- shipping methods -->
                          <div class="col-sm-4">
                            <div class="row">
                              <div class="form-group col-sm-12">
                                <?php
                                if (empty($product_data->shipping_method_rows) == false) {
                                  ?>
                                  <label><?php _e('Shipping method', 'wde'); ?></label>
                                  <?php
                                  if (is_array($product_data->shipping_method_rows)) {
                                    foreach ($product_data->shipping_method_rows as $shipping_method) {
                                      $checked = $shipping_method->checked == true ? 'checked="checked"' : '';
                                      ?>
                                      <div class="radio">
                                        <label>
                                          <input type="radio"
                                                 name="product_shipping_method_id_<?php echo $product_data->id . '_' . $product_data->order_product_id; ?>"
                                                 class="wd_shop_product_data_shipping_method_id"
                                                 value="<?php echo $shipping_method->id; ?>"
                                                 data-price="<?php echo WDFText::wde_number_format($shipping_method->price, $decimals); ?>"
                                                 data-free-shipping="<?php echo $shipping_method->free_shipping; ?>"
                                                 data-free-shipping-start-price="<?php echo WDFText::wde_number_format($shipping_method->free_shipping_start_price, $decimals); ?>"
                                                 data-shipping-type="<?php echo $shipping_method->shipping_type; ?>"
                                                 <?php echo $checked; ?> />
                                          <?php
                                          echo $shipping_method->label;
                                          ?>
                                        </label>
                                      </div>
                                      <?php
                                      $checked = '';
                                    }
                                  }
                                }
                                else {
                                  ?>
                                  <div class="alert alert-danger">
                                    <?php _e('This item will not ship to your country', 'wde'); ?>
                                  </div>
                                  <?php
                                }
                                ?>
                              </div>
                            </div>
                          </div>
                          <?php
                        }                        
                        if ($theme->product_view_show_price == 1 && $product_data->price_text) {
                          ?>
                            <div class="col-sm-4">
                              <div class="row">
                                <div class="form-group col-sm-12">
                                  <label class="control-label">
                                      <?php _e('Price', 'wde'); ?>:
                                  </label>
                                  <?php echo $product_data->price_text; ?>
                                </div>
                              </div>
                              <?php
                              if ($product_data->discount_rate) {
                              ?>                                
                              <div class="row">
                                <div class="form-group col-sm-12">
                                  <label class="control-label">
                                      <?php _e('Discount', 'wde'); ?>:
                                  </label>
                                  <?php echo $product_data->discount_rate . '%'; ?>
                                </div>
                              </div>
                              <?php
                              }
                              ?>
                              <?php
                              if ($product_data->tax_rate) {
                              ?>
                              <div class="row">                                
                                <div class="form-group col-sm-12">
                                  <label class="control-label">
                                      <?php _e('Tax', 'wde'); ?>:
                                  </label>
                                  <?php echo $product_data->tax_rate . '%'; ?>
                                </div>
                              </div>
                              <?php
                              }
                              ?>
                            </div>
                          <?php
                        }
                        ?>
                      </div>
                      <?php
                      if ($theme->product_view_show_price == 1 && $product_data->price_text) {
                        ?>
                        <div class="col-sm-12 text-right">
                          <!-- subtotal -->
                          <p>
                            <span class="wd_shop_order_product_subtotal_title wd_shop_product_price_all">
                              <?php _e('Subtotal', 'wde'); ?>:
                            </span>
                            <span class="wd_shop_product_price_text wd_shop_product_price_all wd_shop_order_product_subtotal">
                              <?php echo $product_data->price_text; ?>
                            </span>
                          </p>
                        </div>
                        <?php
                      }
                      ?>
                    </li>
                      <?php
                    }
                    ?>
                    <?php
                    if (($product_data->enable_shipping == 1 or ($product_data->enable_shipping == 2 /*&& $options->checkout_enable_shipping == 1*/)) && ($product_data->country_specified == true)) {
                      if ($options->option_order_shipping_type == 'per_order') {
                        $shipping_method_price_text = '';
                      ?>
                      <li class="wd_shop_shipping_method_container">
                          <?php
                            if ($theme->product_view_show_price == 1/*  && $product_data->price_text */) {
                              ?>
                              <div class="col-sm-12 text-right">
                                <!-- subtotal -->
                                <p>
                                  <span class="wd_shop_order_product_subtotal_title wd_shop_product_price">
                                    <?php _e('Shipping', 'wde'); ?>:
                                  </span>
                                  <span class="wd_shop_product_price_text wd_shop_product_price wd_shop_order_product_subtotal">
                                    <?php echo $shipping_method_price_text; ?>
                                  </span>
                                </p>
                              </div>
                              <?php
                            }
                        ?>
                      </li>
                      <?php
                      }
                    }
                  }
                  ?>
                </ul>
                <input type="hidden" name="data" value="products_data">
              </form>
              <!-- alert -->
              <div class="wd_shop_checkout_alert_incorrect_data alert alert-danger hidden">
                <p><?php _e('This field is required', 'wde'); ?></p>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- pager -->
      <div>
        <ul class="pager">
          <?php
          $btn_cancel_checkout_data = $pager_data['btn_cancel_checkout_data'];
          ?>
          <li class="previous">
            <a href="<?php echo $btn_cancel_checkout_data['url']; ?>">
              <span><?php _e('Cancel checkout', 'wde'); ?></span>
            </a>
          </li>
          <?php
          if (isset($pager_data['btn_prev_page_data'])) {
            $btn_prev_page_data = $pager_data['btn_prev_page_data'];
            ?>
          <li class="previous">
            <a href="<?php echo $btn_prev_page_data['action']; ?>"
               onclick="onWDShop_pagerBtnClick(event, this); return false;">
              <span class="glyphicon glyphicon-arrow-left"></span>&nbsp;
              <span><?php echo $btn_prev_page_data['text']; ?></span>
            </a>
          </li>
            <?php
          }
          if (isset($pager_data['btn_next_page_data']) ) {
            $btn_next_page_data = $pager_data['btn_next_page_data'];
            ?>
          <li class="next">
            <a href="<?php echo $btn_next_page_data['action']; ?>"
               onclick="onWDShop_pagerBtnClick(event, this); return false;">
              <span><?php echo $btn_next_page_data['text']; ?></span>&nbsp;
              <span class="glyphicon glyphicon-arrow-right"></span>
            </a>
          </li>
            <?php
          }
          ?>
        </ul>
      </div>
    </div>
  </div>
</div>
<script>
  var wdShop_currency_code = "<?php echo $row_default_currency->sign; ?>";
  var wdShop_currency_position = "<?php echo $row_default_currency->sign_position; ?>";
  var decimals = "<?php echo $options->option_show_decimals == 1 ? 2 : 0; ?>";
</script>
<?php  ?>
