<?php

	namespace WP_Piwik\Widget;

	class Items extends \WP_Piwik\Widget {
	
		public $className = __CLASS__;

		protected function configure($prefix = '', $params = array()) {
            $timeSettings = $this->getTimeSettings();
            $this->title = $prefix.__('E-Commerce Items', 'wp-piwik');
			$this->method = 'Goals.getItemsName';
            $this->parameter = array(
                'idSite' => self::$wpPiwik->getPiwikSiteId($this->blogId),
                'period' => $timeSettings['period'],
                'date'  => $timeSettings['date']
            );
		}
		
		public function show() {
			$response = self::$wpPiwik->request($this->apiID[$this->method]);
			if (!empty($response['result']) && $response['result'] ='error')
				echo '<strong>'.__('Piwik error', 'wp-piwik').':</strong> '.htmlentities($response['message'], ENT_QUOTES, 'utf-8');
			else {
			    $tableHead = array(
                    __('Label', 'wp-piwik'),
                    __('Revenue', 'wp-piwik'),
                    __('Quantity', 'wp-piwik'),
                    __('Orders', 'wp-piwik'),
                    __('Avg. price', 'wp-piwik'),
                    __('Avg. quantity', 'wp-piwik'),
                    __('Conversion rate', 'wp-piwik'),
                );
			    $tableBody = array();
			    if (is_array($response))
                    foreach ($response as $data) {
                        array_push($tableBody, array(
                            $data['label'],
                            number_format($data['revenue'],2),
                            $data['quantity'],
                            $data['orders'],
                            number_format($data['avg_price'],2),
                            $data['avg_quantity'],
                            $data['conversion_rate']
                        ));
                    }
                $tableFoot = array();
                $this->table($tableHead, $tableBody, $tableFoot);
			}
		}
		
	}