<?php

namespace auctionforge\ajax;

use stdClass;
use auctionforge\ajax\abs\AjaxHandler;
use auctionforge\inc\abs\ImageModel;
use auctionforge\inc\Debug;
use auctionforge\inc\helpers\LotHelper;
use auctionforge\inc\helpers\SettingsHelper;
use auctionforge\inc\InstanceDB;
use auctionforge\inc\model\Lot;
use auctionforge\inc\model\LotImage;
use WP_Post;

class LoadMore extends AJAXHandler
{
    function callback ()
    {
        $data = $_POST;
        $this->setAction($data["action"]);

        $limit = apply_filters('bp_posts_per_page_limit', get_option('posts_per_page', 10));
        $auctionId = sanitize_title($data['termId']);
        $count = intval($data['count']);

        $params = array(
            "slug" => $auctionId,
            "part" => (!empty($data["part"])) ? intval($data["part"]) : 0,
            Lot::NAME_PARAM => (!empty($data[Lot::NAME_PARAM])) ? $data[Lot::NAME_PARAM] : "",
            Lot::NAME_PARAM_PHRASE => (!empty($data[Lot::NAME_PARAM_PHRASE])) ? $data[Lot::NAME_PARAM_PHRASE] : "",
            Lot::NAME_PARAM_CATEGORY => (!empty($data[Lot::NAME_PARAM_CATEGORY])) ? $data[Lot::NAME_PARAM_CATEGORY] : "",
            Lot::NAME_PARAM_PER_PAGE => (!empty($data[Lot::NAME_PARAM_PER_PAGE])) ? $data[Lot::NAME_PARAM_PER_PAGE] : "",
            "lang" => (!empty($data["lang"])) ? $data["lang"] : "en",
        );

        $resultQuery = Lot::getLotsInAuction($auctionId, $limit, $count, $params);
        $items = $resultQuery->get_posts();
        $total = $resultQuery->found_posts;

        $displayStartPriceInCatalog = SettingsHelper::getTermOption($data['termId'], 'auction_displayStartPriceInCatalog');
        $showEstimatedPriceInItemsList = SettingsHelper::getTermOption($data['termId'], 'auction_showEstimatedPriceInItemsList');

        if (!empty($items)) {
            $lots = array();
            /** @var WP_Post $item */
            foreach ($items as $item) {
                $lot = new stdClass();
                $lotHelper = new LotHelper($item->ID);
                $itemIndex = $lotHelper->getOptionValue('lot_itemIndex', 0);
                $lot->post_title = LotHelper::printTranslateLot($itemIndex);
                $lot->guid = get_the_permalink($item->ID);
                $lot->guid = LotHelper::getLotLink($lot->guid, $data['lang']);

                $title = $lotHelper->getOptionValue('lot_name', '');
                $content = $item->post_content;
                $title = LotHelper::getLotTitle($title, $content);

                $lot->post_content = htmlspecialchars_decode($title);

                $lot->featureImage = $lotHelper->getFeatureImage();

                $lot->start_price = $lotHelper->getOptionValue('lot_start_price', 0);
                $lot->estimated_price = $lotHelper->getOptionValue('lot_estimated_price', 0);
                $lot->isRemovedItem = $lotHelper->getOptionValue('removedFromAuction', 0);
                $lot->itemIndex = $itemIndex;

                $itemData = new stdClass();
                $itemData->title = $title;
                $itemData->content = $content;
                $lot->itemData = urlencode(json_encode($itemData, ENT_QUOTES));

                $lots[] = apply_filters('auctionforge_item_do_load_more', $lot, $item, $itemData);
            }
            $lots = apply_filters('auctionforge_items_do_load_more', $lots);

            $response = array(
                'lots' => $lots,
                'total' => $total,
                'showEstimatedPriceInItemsList' => $showEstimatedPriceInItemsList,
                'displayStartPriceInCatalog' => $displayStartPriceInCatalog
            );
            wp_send_json_success($response);
        } else {
            wp_send_json_error(array(
                'lots' => array(),
                'total' => $total,
            ));
        }
    }
}
$gate = new LoadMore();
$gate->init('do_load_more');
