<!--Adding Template Specific Style -->
<?php
wp_enqueue_style( 'qcld_express_basic_style', QCLD_EXPRESS_PLUGIN_DIR_URL ."/templates/$qcld_express_template/style.css");
?>

<?php

$temp_top ="";
if($qcld_express_cart_mode=='top'){
        $qcld_express_options           = get_option('qcld_express_options');
        $qcld_express_lang_cart_summary = $qcld_express_options['qcld_express_lang_cart_summary'];
        $qcld_express_lang_edit_on_cart = $qcld_express_options['qcld_express_lang_edit_on_cart'];
        $qcld_express_lang_checkout     = $qcld_express_options['qcld_express_lang_checkout'];
        $qcld_express_lang_no_products  = $qcld_express_options['qcld_express_lang_no_products'];

    $temp_top.='<div class="qcld-express-shop-cart" id="qcld-express-cart-container">
        <div class="qcld-express-show-cart-wrapper">
            <div class="qcld-express-cart-heading">

                <h2>'. esc_html($qcld_express_lang_cart_summary, 'express-shop') .'</h2><button type="button"  id="show-top-cart-close" class="fa fa-times"></button>
            </div>
            <div class="qcld-express-show-cart-data">';
    global $woocommerce;

    $items = array();
//    if ( ! is_admin() ){
      $items = $woocommerce->cart->get_cart();
    //}
    $itemCount = count($items);
    if ($itemCount >= 1) {
        $temp_top.='<div class ="qcld-express-table">
                        <div class="qcld-express-row">
                            <div class="qcld-express-cell">'.esc_html( 'Title', 'express-shop' ).'</div>
                            <div class="qcld-express-cell">'.esc_html( 'Price', 'express-shop' ).'</div>
                        </div>';

        foreach ($items as $item => $values) {
            $_product = apply_filters('woocommerce_cart_item_product', $values['data'], $values, $item);
            $getProductDetail = wc_get_product($values['product_id']);
            $price = get_post_meta($values['product_id'], '_price', true);

            $temp_top.='<div class="qcld-express-row">
                                <div class="qcld-express-cell qcld-express-title-cell"> <h3 class="qcld-express-title">';
                                                       
                              $product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $values ) : '', $values, $item );
                              $product_name =  apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_title() ), $values, $item );

                              $temp_top .= '<p>'.$product_name.'</p>';

                              $attributes = '';

                              //Variation
                              $attributes .= $_product->is_type('variable') || $_product->is_type('variation') ? wc_get_formatted_variation($_product) : '';
                             

                              if($attributes){
                                 $temp_top .= $attributes;
                              }

                             $temp_top .= '</h3></div>';
            $temp_top.='<div class="qcld-express-cell qcld-express-price-cell"><span>'.apply_filters('woocommerce_cart_item_price', WC()->cart->get_product_price($_product), $values, $item).'</span></div></div>'; 

        }
        $temp_top.='<div class="qcld-express-row">
                            <div class="qcld-express-cell"><strong>Total</strong></div>
                            <div class="qcld-express-cell"><strong>'.$woocommerce->cart->get_cart_total().'</strong></div>
                        </div>
                    </div>';
    } else{
        $temp_top.='<div class="qcld_no_cartprods">'.esc_html($qcld_express_lang_no_products, 'express-shop').'</div>';
    }
    $temp_top.='</div>
            <div class="qcld-express-cart-footer">
                <a href="'.esc_url(get_site_url(), 'express-shop').'/cart">'.esc_html($qcld_express_lang_edit_on_cart, 'express-shop').'</a>
                <a class="qcld-express-checkout-link" href="'.esc_url(get_site_url(), 'express-shop').'/checkout">'.esc_html($qcld_express_lang_checkout, 'express-shop').'</a>
            </div>
        </div>
    </div>';
}
echo $temp_top;
?>

<div class="express-shop-wrapper">
<?php
  //Sku show or not
if(isset($qcld_express_filter) && $qcld_express_filter=='on' ){
 if(isset($qcld_express_sticky) && $qcld_express_sticky =='on'){
     $is_stickyed = ' is_stickyed';
  }else{
    $is_stickyed = '';
  }
?>
    <div class="category-carousel <?php echo esc_attr($is_stickyed, 'express-shop'); ?>">
    <?php 
    $all_selected='';
    $select_category='';
    $single_selected='';
    if(!isset($_GET['category']) || isset($_GET['category']) == 'all'){
        $all_selected='qcld-express-active-category qcld_express_c_search';
    }else{

        $category       = isset($_GET['category']) ? sanitize_text_field($_GET['category']) : '';
        $category_slug  = explode('_',$category);
        $select_category= $category_slug[1];
        $all_selected   = 'qcld-express-active-category qcld_express_c_search';
    }
    ?>
    <div><a class="<?php echo esc_attr($all_selected, 'express-shop'); ?> active" c-id="all" data-offset="<?php echo esc_attr($offset, 'express-shop'); ?>" author="<?php echo $dokan_get_current_user_id; ?>" href="#"> <?php esc_html_e( 'All', 'express-shop' ); ?></a></div>
    <?php
    foreach ($filter_categories as $key => $f_cat){
    ?>
      <div><a class="<?php echo esc_attr($all_selected, 'express-shop'); ?>" data-offset="<?php echo esc_attr($offset, 'express-shop'); ?>" c-id="<?php echo esc_attr($f_cat['cat_id'], 'express-shop'); ?>" author="<?php echo $dokan_get_current_user_id; ?>" href="#"><?php echo esc_html($f_cat['category_name'], 'express-shop'); ?></a></div>
    <?php
    }
    ?>
   </div>
<?php
}

?>


  <div class="cart" id="qcld-express-cart-container">
  </div>
  <form method="get" action="<?php echo esc_url(get_the_permalink(), 'express-shop'); ?>">
  <div class="qcld-express-table qcld-express-product-container">
    <div class="qcld-express-row header">
      <?php
      //Sku show or not
      if(isset($qcld_express_sku) && $qcld_express_sku=='on' ){
       ?>
      <div class="qcld-express-cell qcld-express-sku-cell">
        <p class="qcld-express-sku-heading">  
          <a id="sku-sort"  href="<?php echo esc_url(get_the_permalink(), 'express-shop'); ?>?product_list_order"> 
          <?php 
        if($qcld_express_lang_sku == ''){
          esc_html_e( 'SKU', 'express-shop' );
        }else{
          echo esc_html($qcld_express_lang_sku, 'express-shop');
        }
      ?>
         </a> 
       </p>
          <?php if(!isset($_GET['category'] )){?>
          <input type="text" name="sku" placeholder="Sku">
          <?php } ?>
      </div>
        <?php }
        //Image thumnail show or not
        if(isset($qcld_express_thumbnail) && $qcld_express_thumbnail=='on' ){
        ?>
      <div class="qcld-express-cell qcld-express-thumb-cell">
          <p class="qcld-express-thumbnail-heading">
             <?php 
              if($qcld_express_lang_thumbnail == ''){
                esc_html_e( 'Thumbnail', 'express-shop' );
              }else{
                echo esc_html($qcld_express_lang_thumbnail, 'express-shop');
              }
            ?>
            
          </p>
      </div>
      <?php }
      //Title show or not
      if(isset($qcld_express_title) && $qcld_express_title=='on' ){
      ?>
      <div class="qcld-express-cell qcld-express-title-cell">
          <p class="qcld-express-title-heading"> <a id="title-sort" href="<?php echo esc_url(get_the_permalink(), 'express-shop'); ?>?product_list_order">
             <?php 
              if($qcld_express_lang_title == ''){
                esc_html_e( 'Title', 'express-shop' );
              }else{
                echo esc_html($qcld_express_lang_title, 'express-shop');
              }
            ?>
          </a> </p>
          <?php if(!isset($_GET['category'] )){
          //if(isset($qcld_express_hide_search_option) && $qcld_express_hide_search_option != 'on'){
            ?>

              <div class="qcld-express-search-form-wrapper">
                <form method="get" class="qcld_express_searchform" action="<?php echo esc_url( home_url( '/'  ) ) ?>" role="search">
                    <div class="qcld_express_search_products">
                        <input type="text" value="" name="title" id="qcld_title" class="search-field" author="<?php echo $dokan_get_current_user_id; ?>" placeholder="Search" autocomplete="off" />
                    </div>
                </form>
              </div>
          <?php } ?>
      </div>
      <?php }
      //Description show or not
      if(isset($qcld_express_description) && $qcld_express_description=='on' ){
      ?>
      <div class="qcld-express-cell qcld-express-desc-cell">
          <p class="qcld-express-desc-heading"> 
             <?php 
              if($qcld_express_lang_desc == ''){
                esc_html_e( 'Description', 'express-shop' );
              }else{
                echo esc_html($qcld_express_lang_desc, 'express-shop');
              }
            ?>
          </p>
      </div>
      <?php }
      //Availability show or not
      if(isset($qcld_express_stock) && $qcld_express_stock=='on' ){
      ?>
	  <div class="qcld-express-cell qcld-express-avail-cell">
          <p class="qcld-express-avail-heading">
             <?php 
              if($qcld_express_lang_availability == ''){
                esc_html_e( 'Availability', 'express-shop' );
              }else{
                echo esc_html($qcld_express_lang_availability, 'express-shop');
              }
            ?>
          </p>
      </div>
      <?php }
      //Price show or not
      if(isset($qcld_express_price) && $qcld_express_price=='on' ){
      ?>
	  <div class="qcld-express-cell qcld-express-price-cell">
          <p class="qcld-express-price-heading"><a id="price-sort" href="<?php echo esc_url(get_the_permalink(), 'express-shop'); ?>?product_list_order">
             <?php 
              if($qcld_express_lang_price == ''){
                esc_html_e( 'Price', 'express-shop' );
              }else{
                echo $qcld_express_lang_price;
              }
            ?>
          </a> </p>
          <?php if(!isset($_GET['category'] )){?>
   
          <?php } ?>
      </div>
      <?php }
      // Action show or not
      if(isset($qcld_express_action) && $qcld_express_action=='on' ){
      ?>
	  <div class="qcld-express-cell qcld-express-action-cell">
          <p class="qcld-express-action-heading"> </p>
          <?php if(!isset($_GET['category'] )){?>
          <button type="submit" id="submit_search">
             <?php 
              if($qcld_express_lang_search == ''){
              //  echo 'Search';
                esc_html_e( 'Search', 'express-shop' );
              }else{
                echo $qcld_express_lang_search;
              }
            ?>
          </button>
          <?php } ?>
      </div>
      <?php } ?>
    </div>

    <?php
    //Creating WooCommerce product factory.
    $wc_pf = new WC_Product_Factory();
    //repeating the products
    if ($product_query->have_posts()) :
     //foreach($products as $P){
    while ($product_query->have_posts()) : $product_query->the_post();
	     $product = $wc_pf->get_product(get_the_ID());

      //var_dump($product);
         //Checking for image thumb.
         $qcld_thumb = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'shop_thumbnail' );
         if(!empty($qcld_thumb)){
             $image =$qcld_thumb;
         }else{
             $image[0] =QCLD_EXPRESS_IMG_URL.'/placeholder.png';
         }
         ?>
    <div class="qcld-express-row  qcld-express-body-content">
     <?php
    //Sku show or not
     if(isset($qcld_express_sku) && $qcld_express_sku=='on' ){
     ?>
      <div class="qcld-express-cell qcld-express-sku-cell">
          <p class="qcld-express-sku"> <?php echo esc_html($product->get_sku(), 'express-shop'); ?></p>
      </div>
     <?php }
     //Image thumnail show or not
     if(isset($qcld_express_thumbnail) && $qcld_express_thumbnail=='on' ){
     ?>
	  <div class="qcld-express-cell qcld-express-thumb-cell">
          <p class="qcld-express-thumbnail">
              <img class="qlcd-express-list-thumb" src="<?php  echo esc_url($image[0]); ?>"  alt=" <?php  echo esc_attr(get_the_title(get_the_ID()), 'express-shop'); ?>"/>
              </p>
      </div>
     <?php }
     //Title show or not
     if(isset($qcld_express_title) && $qcld_express_title=='on' ){
     ?>
	  <div class="qcld-express-cell qcld-express-title-cell">
          <h3 class="qcld-express-title"><a href="<?php echo esc_url(get_permalink(get_the_ID())); ?>">
              <?php  echo esc_html(get_the_title(get_the_ID()), 'express-shop'); ?>
              </a> </h3>
      </div>
     <?php }
     //Description show or not
     if(isset($qcld_express_description) && $qcld_express_description=='on' ){
     ?>
	  <div class="qcld-express-cell qcld-express-desc-cell">
          <p class="qcld-express-desc"><?php echo strip_tags(substr(get_the_excerpt(get_the_ID()), 0, 100)) . ' ... '; ?></p>
      </div>
     <?php }
     //Availability show or not
     if(isset($qcld_express_stock) && $qcld_express_stock=='on' ){
     ?>
	  <div class="qcld-express-cell qcld-express-avail-cell">
          <p class="qcld-express-avail">
                <?php
                  $stock = get_post_meta( get_the_ID(), '_stock', true );
                  $stock_status = get_post_meta( get_the_ID(), '_stock_status', true );
                  if( $stock > 0 ){ ?>
                  <span class="qcld-express-avail-in"> In Stock (<?php  echo $product->get_stock_quantity(); ?>)</span>
                <?php } else if( $stock == "" ){ ?>
                    <?php if( $stock_status == "instock" ){ ?>
                      <span class="qcld-express-avail-out"> <?php esc_html_e( 'In Stock', 'express-shop' ); ?> </span>
                    <?php } else if( $stock_status == "outofstock" ){ ?>
                      <span class="qcld-express-avail-outs"> <?php esc_html_e( 'Out of Stock', 'express-shop' ); ?> </span>
                    <?php } ?>
                <?php }else if( $stock < 1 ){ ?>
                      <span class="qcld-express-avail-out"> <?php esc_html_e( 'Out of Stock', 'express-shop' ); ?> </span>
                <?php } ?>
          </p>
      </div>
     <?php }
     //Price show or not
     if(isset($qcld_express_price) && $qcld_express_price=='on' ){
     ?>
      <div class="qcld-express-cell qcld-express-price-cell">
          <p class="qcld-express-price"><?php echo $product->get_price_html() ?></p>
      </div>
     <?php }
     // Action show or not
     if(isset($qcld_express_action) && $qcld_express_action=='on' ){
     ?>
      <div class="qcld-express-cell qcld-express-action-cell">
          <ul class="qcld-express-action">
         <?php if($product->get_type()=='variable' ){?>
             <li><a href="#" class="qcld_express_v_p qcld_express_config" p-id="<?php echo esc_attr(get_the_ID(), 'express-shop') ?>"><i class="fa fa-shopping-cart"></i></a></li>
          <?php }else{ 

              $stock = get_post_meta( get_the_ID(), '_stock', true );
              $stock_status = get_post_meta( get_the_ID(), '_stock_status', true );

              if( $stock > 0 ){

                if (isset($qcld_express_cart_mode) && $qcld_express_cart_mode == 'wooinstants') {       

                $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                    $response_min_max = qcld_woo_minmax_express_add_rules_text(get_the_ID(),0);

                    $min_qty  = $response_min_max['min'];
                    $max_qty  = $response_min_max['max'];
                    $step_qty = $response_min_max['step'];
          ?>

            <li><input type="number" name="qcld_quantity" class="qcld-express-simple-p-qnty"  value="<?php echo ($min_qty ? $min_qty : 1); ?>" step="<?php echo ($step_qty ? $step_qty : ''); ?>" min="<?php echo ($min_qty ? $min_qty : ''); ?>" max="<?php echo ($max_qty ? $max_qty : ''); ?>"></li>
        <?php }else{ ?>
          <li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"  min="1"></li>
        <?php } 

           $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
          if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

              $response_min_max = qcld_woo_minmax_express_add_rules_text(get_the_ID(),0);
              $min_qty  = $response_min_max['min'];
            }else{
              $min_qty  = 1;

            }
        ?>

             <li><a  class="qcld_express_s_p ajax_add_to_cart add_to_cart_button product_type_simple"  data-quantity="<?php echo ($min_qty ? $min_qty : 1); ?>" data-product_id="<?php echo esc_attr(get_the_ID(), 'express-shop'); ?>" p-id="<?php echo esc_attr(get_the_ID(), 'express-shop'); ?>" p-price="<?php echo esc_attr($product->get_price(), 'express-shop'); ?>"><i class="fa fa-shopping-cart"></i></a></li>
             <?php }else{ ?>
              <li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"  min="1"></li>
              <li><a href="#" class="qcld_express_s_p" p-id="<?php echo esc_attr(get_the_ID(), 'express-shop'); ?>" p-price="<?php echo esc_attr($product->get_price(), 'express-shop'); ?>" ><i class="fa fa-shopping-cart"></i></a></li>
              <?php } ?>
          <?php 
              } else if( $stock == "" ){
               if( $stock_status == "instock" ){ 

                if (isset($qcld_express_cart_mode) && $qcld_express_cart_mode == 'wooinstants') {

                $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                    $response_min_max = qcld_woo_minmax_express_add_rules_text(get_the_ID(),0);

                    $min_qty  = $response_min_max['min'];
                    $max_qty  = $response_min_max['max'];
                    $step_qty = $response_min_max['step'];
          ?>
             <li><input type="number" name="qcld_quantity" class="qcld-express-simple-p-qnty"  value="<?php echo ($min_qty ? $min_qty : 1); ?>" step="<?php echo ($step_qty ? $step_qty : ''); ?>" min="<?php echo ($min_qty ? $min_qty : ''); ?>" max="<?php echo ($max_qty ? $max_qty : ''); ?>"></li>
        <?php }else{ ?>
           <li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"  min="1"></li>
        <?php }  ?>

             
              <?php

                 $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                    $response_min_max = qcld_woo_minmax_express_add_rules_text(get_the_ID(),0);
                    $min_qty  = $response_min_max['min'];
                  }else{
                    $min_qty  = 1;

                  } 
                  ?>
             <li><a href="#" class="qcld_express_s_p ajax_add_to_cart add_to_cart_button product_type_simple"  data-quantity="<?php echo ($min_qty ? $min_qty : 1); ?>" data-product_id="<?php echo esc_attr(get_the_ID(), 'express-shop'); ?>" p-id="<?php echo esc_attr(get_the_ID(), 'express-shop'); ?>" p-price="<?php echo esc_attr($product->get_price(), 'express-shop'); ?>"><i class="fa fa-shopping-cart"></i></a></li>
             <?php }else{ ?>
              <li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"  min="1"></li>
              <li><a href="#" class="qcld_express_s_p" p-id="<?php echo esc_attr(get_the_ID(), 'express-shop'); ?>" p-price="<?php echo esc_attr($product->get_price(), 'express-shop'); ?>" ><i class="fa fa-shopping-cart"></i></a></li>
              <?php } ?>
          <?php } else if( $stock_status == "outofstock" ){  ?>
              <li><a href="#" class="qcld_express_out_of_pro" p-id="<?php echo esc_attr(get_the_ID(), 'express-shop'); ?>" p-price="<?php echo esc_attr($product->get_price(), 'express-shop'); ?>" > <i class="fa fa-shopping-cart"></i> </a></li>
          <?php }}else if( $stock < 1 ){  

                if (isset($qcld_express_cart_mode) && $qcld_express_cart_mode == 'wooinstants') { 
            
                $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                    $response_min_max = qcld_woo_minmax_express_add_rules_text(get_the_ID(),0);

                    $min_qty  = $response_min_max['min'];
                    $max_qty  = $response_min_max['max'];
                    $step_qty = $response_min_max['step'];
          ?>

           <li><input type="number" name="qcld_quantity" class="qcld-express-simple-p-qnty" value="<?php echo ($min_qty ? $min_qty : 1); ?>" step="<?php echo ($step_qty ? $step_qty : ''); ?>" min="<?php echo ($min_qty ? $min_qty : ''); ?>" max="<?php echo ($max_qty ? $max_qty : ''); ?>"></li>
        <?php }else{ ?>
           <li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"  min="1"></li>
        <?php }  ?>

              <?php

                 $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                    $response_min_max = qcld_woo_minmax_express_add_rules_text(get_the_ID(),0);
                    $min_qty  = $response_min_max['min'];
                  }else{
                    $min_qty  = 1;

                  } 
                  ?>
              <li><a  class="qcld_express_s_p ajax_add_to_cart add_to_cart_button product_type_simple"  data-quantity="<?php echo ($min_qty ? $min_qty : 1); ?>" data-product_id="<?php echo esc_attr(get_the_ID(), 'express-shop'); ?>" p-id="<?php echo esc_attr(get_the_ID(), 'express-shop'); ?>" p-price="<?php echo esc_attr($product->get_price(), 'express-shop'); ?>"><i class="fa fa-shopping-cart"></i></a></li>
             <?php }else{ ?>
              <li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"  min="1"></li>
              <li><a href="#" class="qcld_express_s_p" p-id="<?php echo esc_attr(get_the_ID(), 'express-shop'); ?>" p-price="<?php echo esc_attr($product->get_price(), 'express-shop'); ?>" ><i class="fa fa-shopping-cart"></i></a></li>
              <?php } ?>
          <?php } ?>
          <?php } ?>

          </ul>

          <?php

            $qcld_woo_minmax_dynamic_pricing_setting = get_option('qcld_woo_minmax_dynamic_pricing_setting');

            if(function_exists('qcld_woo_minmax_install') && (isset($qcld_woo_minmax_dynamic_pricing_setting['qcld_woo_minmax_price_table_on_off']) && $qcld_woo_minmax_dynamic_pricing_setting['qcld_woo_minmax_price_table_on_off'] == 'enable') ){

              $result_show = qcld_woo_minmax_express_dynamic_discount_price_table_show(get_the_ID());
              echo $result_show;
            }

              ?>

      </div>
     <?php }//End of show or not ?>
    </div>
    <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
    <?php else: ?>
        <p class="qcld-express-body-content" style="text-align:center ">
            <?php esc_html_e('No Products'); ?>
        </p>
    <?php endif; ?>
  </div>
  </form>
</div>