<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Whatsnew extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	var $menu;

	public function __construct() {
		parent::__construct();
		$this->load->model('Section', 'section_model');
		$this->menu = $this->section_model->buildTopMenuItem();
		$this->breadcrumbs = [];
		$this->load->model('WhatsnewContent', 'whatsnew_model');
		$this->load->library('pagination');
		$this->load->model('ContactDetails', 'contactdetails_model');

	}

	public function index($offset = '') {

		//paging
		//配置分页信息
		$config['base_url'] = site_url('Whatsnew/index');
		//获取数据库总条数
		$config['total_rows'] = $this->whatsnew_model->getWhatsnewcontentsAllcount();
		// $config['total_rows'] = 12;
		//每页显示条数
		$config['per_page'] = 6;
		//页码参数位置
		$config['uri_segment'] = 3;

		//自定义分页连接
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['prev_link'] = '上一页';
		$config['next_link'] = '下一页';

		// $config['full_tag_open'] = '<div class="pagination"><ul>';
		// $config['full_tag_close'] = '</ul></div>';

		// $config['first_link'] = '« First';
		// $config['first_tag_open'] = '<li class="prev page">';
		// $config['first_tag_close'] = '</li>';

		// $config['last_link'] = 'Last »';
		// $config['last_tag_open'] = '<li class="next page">';
		// $config['last_tag_close'] = '</li>';

		// $config['next_link'] = 'Next →';
		// $config['next_tag_open'] = '<li class="next page">';
		// $config['next_tag_close'] = '</li>';

		// $config['prev_link'] = '← Previous';
		// $config['prev_tag_open'] = '<li class="prev page">';
		// $config['prev_tag_close'] = '</li>';

		// $config['cur_tag_open'] = '<li class="active"><a href="">';
		// $config['cur_tag_close'] = '</a></li>';

		// $config['num_tag_open'] = '<li class="page">';
		// $config['num_tag_close'] = '</li>';

		//初始化分类页
		$this->pagination->initialize($config);

		$currentpage = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		//生成分页信息
		//可以输出查看结果是一串html字符串
		// $data['pageinfo'] = $this->pagination->create_links();
		$pageinfo = $this->pagination->create_links();
		// $limit = $config['per_page'];
		// $data['goodstypes'] = $this->goodstype_model->list_goodstype($limit,$offset);
		$whatsnewlist = $this->whatsnew_model->list_whatsnew($config["per_page"], $currentpage);
		// $this->load->view('goods_type_list.html',$data);

		//end paging

		// $whatsnewlist=$this->whatsnew_model->getWhatsnewcontentsAll();
		//$this->load->view('welcome_message');

		$contactusdata = $this->contactdetails_model->getContactus();
		$this->breadcrumbs[] = array(
			'label' => '最新消息',
			'link' => '',
		);
		$this->twig->display('main', array(
			'path' => array(
				'vendor' => $this->config->item('vendor_path'),
				'script' => $this->config->item('script_path'),
				'css' => $this->config->item('css_path'),
				'image' => $this->config->item('image_path'),
			),
			'breadcrumbs' => $this->breadcrumbs,
			'menus' => $this->menu,
			'session' => 'whatsnew',
			'whatsnewlist' => $whatsnewlist,
			'pageinfo' => $pageinfo,
			'currentpage' => $currentpage,
			'page' => 'index',
			'per_page' => $config['per_page'],
			'total_rows' => $config['total_rows'],
			'contactusdata' => $contactusdata,
			'base_url' => $config['base_url'],
		));
	}

	public function details() {
		if (!$this->uri->segment(3)) {
			redirect('/whatsnew', 'refresh');
		}

		$param = urldecode($this->uri->segment(3));
		$whatsnewdetails = $this->whatsnew_model->getWhatsnewcontents($param);
		$topTenWhatsnew = $this->whatsnew_model->getTopTenWhatsnew();
		$contactusdata = $this->contactdetails_model->getContactus();
		//$this->load->view('welcome_message');
		$this->breadcrumbs[] = array(
			'label' => '最新消息',
			'link' => '/whatsnew',
		);
		$this->breadcrumbs[] = array(
			'label' => $param,
			'link' => '',
		);
		$this->twig->display('main', array(
			'path' => array(
				'vendor' => $this->config->item('vendor_path'),
				'script' => $this->config->item('script_path'),
				'css' => $this->config->item('css_path'),
				'image' => $this->config->item('image_path'),
			),
			'breadcrumbs' => $this->breadcrumbs,
			'menus' => $this->menu,
			'session' => 'whatsnew',
			'whatsnewdetails' => $whatsnewdetails,
			'topTenWhatsnew' => $topTenWhatsnew,
			'contactusdata' => $contactusdata,
			'page' => 'details',
		));
	}

}
