<?php
namespace App\Controllers;

use App\Models\Employee;
use App\Models\Product;
use App\Models\Admin;
use App\Models\PriceList;
use App\Models\PresellSection;

class SeasonalPresell extends BaseController
{
	private $priceList = [];

	function __construct()
	{
		parent::__construct('seasonal_presell');
		helper('utils_helper');

		$PriceList = new PriceList();
		$this->priceList = $PriceList->get_all();
	}

	function index($promotion_id = null)
	{
		$Employee = new Employee();
		$Product = new Product();
		$Admin = new Admin();
		$PresellSection = new PresellSection();

		if (!$Employee->is_logged_in()) {
			return redirect()->to('/');
		}

		$user_info = $Employee->get_logged_in_employee_info();

		// Get all active presell sections
		$sections = $PresellSection->getActiveSections();

		// If no promotion_id specified, redirect to first active section
		if (empty($promotion_id) && !empty($sections)) {
			$promotion_id = $sections[0]['promotion_id'];
		}

		// Get current section info
		$current_section = null;
		foreach ($sections as $s) {
			if ($s['promotion_id'] === $promotion_id) {
				$current_section = $s;
				break;
			}
		}

		if (empty($current_section)) {
			$this->data['sections'] = $sections;
			$this->data['current_section'] = null;
			$this->data['products'] = [];
			$this->data['total_rows'] = 0;
			$this->data['total_page'] = 0;
			$this->data['from'] = 0;
			$this->data['to'] = 0;
			$this->data['promotion_id'] = '';
			echo view('v2/pages/seasonal_presell', $this->data);
			return;
		}

		$sort_key = $this->request->getGet('sort_key') ?? 9;
		$category_id = $this->request->getGet('category_id') ?? 0;
		$per_page = intval($this->request->getGet('per_page') ?? 50);
		$offset = intval($this->request->getGet('offset') ?? 0);
		$im_new = $this->request->getGet('im_new') ?? 0;
		$plan_profit = $this->request->getGet('plan_profit') ?? 0;
		$favorite = $this->request->getGet('favorite') ?? 0;
		$rrp = $this->request->getGet('rrp') ?? 0;
		$pmp = $this->request->getGet('pmp') ?? 0;
		$non_pmp = $this->request->getGet('non_pmp') ?? 0;
		$own_label = $this->request->getGet('own_label') ?? 0;
		$filter_brands = urldecode($this->request->getGet('filter_brands')) ?? '';
		$filter_brand_arr = json_decode($filter_brands, true) ?? [];
		$view_mode = $this->request->getGet('view_mode') ?? (session()->get('view_mode') ?? 'grid');
		$search0 = urldecode($this->request->getGet('search0')) ?? '';

		if ($this->request->getGet('view_mode') != null) session()->set('view_mode', $view_mode);

		$filter = [
			'search0' => $search0,
			'category_id' => $category_id,
			'im_new' => $im_new,
			'plan_profit' => $plan_profit,
			'own_label' => $own_label,
			'brand' => $filter_brands,
			'favorite' => $favorite,
			'rrp' => $rrp,
			'pmp' => $pmp,
			'non_pmp' => $non_pmp,
			'spresell' => 1,
			'promotion_id' => $promotion_id,
		];

		$this->data['brands'] = $Product->get_brands($user_info, $filter);

		$total_rows = $Product->total_search_num_rows_category($user_info, $filter);

		$products = $Product->search_category($user_info, array_merge($filter, [
			'limit' => $per_page,
			'offset' => $offset,
			'sort_key' => $sort_key,
		]));

		$this->data['products'] = [];
		foreach ($products->getResult() as $row) {
			Product::populate($row, $this->priceList, $user_info, false);
			$this->data['products'][] = $row;
		}

		// Decode ribbon JSON for display
		if (!empty($current_section['ribbon'])) {
			$ribbon = json_decode($current_section['ribbon'], true);
			if (empty($ribbon['bg_color'])) $ribbon['bg_color'] = 'transparent';
			if (empty($ribbon['txt_color'])) $ribbon['txt_color'] = 'white';
			$current_section['ribbon'] = $ribbon;
		}

		$this->data['sections'] = $sections;
		$this->data['current_section'] = $current_section;
		$this->data['promotion_id'] = $promotion_id;
		$this->data['user_info'] = $user_info;
		$this->data['controller_name'] = 'seasonal_presell';
		$this->data['total_rows'] = $total_rows;
		$this->data['total_page'] = floor($total_rows / $per_page) + 1;
		$this->data['from'] = $offset + 1;
		$this->data['to'] = $offset + count($this->data['products']);
		$this->data['sort_key'] = $sort_key;
		$this->data['curd_page'] = intval($offset / $per_page) + 1;
		$this->data['categories'] = $Product->get_all_categories($category_id);
		$this->data['category_id'] = $category_id;
		$this->data['im_new'] = $im_new;
		$this->data['plan_profit'] = $plan_profit;
		$this->data['favorite'] = $favorite;
		$this->data['rrp'] = $rrp;
		$this->data['pmp'] = $pmp;
		$this->data['non_pmp'] = $non_pmp;
		$this->data['own_label'] = $own_label;
		$this->data['view_mode'] = $view_mode;
		$this->data['filter_brands'] = $filter_brand_arr;
		$this->data['per_page'] = $per_page;
		$this->data['offset'] = $offset;
		$this->data['search0'] = $search0;
		$this->data['spresell'] = 1;
		$this->data['base_url'] = base_url("seasonal_presell/index/{$promotion_id}?"
			."&sort_key=".$sort_key
			."&category_id=".$category_id
			."&view_mode=".$view_mode
			."&im_new=".$im_new
			."&plan_profit=".$plan_profit
			."&own_label=".$own_label
			."&favorite=".$favorite
			."&rrp=".$rrp
			."&pmp=".$pmp
			."&non_pmp=".$non_pmp
		);

		$mobile = session()->get('is_mobile');

		echo view('v2/pages/seasonal_presell', $this->data);
	}

	function to_cart()
	{
		$Employee = new Employee();
		$Product = new Product();

		$prod_code = request()->getPost('prod_code');
		$spresell = request()->getPost('spresell');
		$mode = request()->getPost('mode');
		$quantity = request()->getPost('quantity');
		$type = request()->getPost('type');
		$user_info = $Employee->get_logged_in_employee_info();
		$incentive = intval(request()->getPost('incentive'));

		$quantity = $Product->to_cart($prod_code, $mode, $user_info->person_id, $quantity, $spresell, $type, $incentive);
		echo $quantity;
	}
}
