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

class Dashboard extends CI_Controller {

	/**
	 * Class constructor and validate authenticate user
	 *
	 * @return  void
	 */
	public function __construct() {
		parent::__construct();
		//$this->output->enable_profiler(TRUE);
		if (  ! $this->session->user_id ) {
			redirect( '', 'refresh' );
		}
		$this->privileges = $this->MUser_privileges->get_by_ref_user( $this->session->user_id );
	}

	/**
	 * Dashboard landing
	 *
	 * @return  void
	 */
	public function index() {
		// Generate Bar Chart data
		$sales        = $this->MSales_details->get_total_price_last_days( 7 );
		$purchase     = $this->MPurchase_details->get_total_price_last_days( 7 );
		$bar_labels   = '';
		$bar_sales    = '';
		$bar_purchase = '';
		foreach ( $sales as $key => $value ) {
			$bar_labels .= "'" . date_to_ui( $value['sales_date'] ) . "'";
			$bar_sales .= $value['total_price'];
			$bar_purchase .= isset($purchase[$key]) ? $purchase[$key]['total_price'] : 0;
			if ( next( $sales ) ) {
				$bar_labels .= ', ';
				$bar_sales .= ', ';
				$bar_purchase .= ', ';
			}
		}
		$data['bar_labels']   = $bar_labels;
		$data['bar_sales']    = $bar_sales;
		$data['bar_purchase'] = $bar_purchase;

		// Generate Pie Chart data
		$top_sales  = $this->MSales_details->get_total_quantity_top_product( 7 );
		$color      = ['#f56954', '#00a65a', '#f39c12', '#00c0ef', '#3c8dbc'];
		$pie_labels = '';
		$pie_sales  = '';
		$pie_color  = '';
		foreach ( $top_sales as $key => $value ) {
			$pie_labels .= "'" . $value['item_name'] . "'";
			$pie_sales .= $value['total_qty'];
			$pie_color .= "'" . (isset($color[$key]) ? $color[$key] : '#999999') . "'";
			if ( next( $top_sales ) ) {
				$pie_labels .= ', ';
				$pie_sales .= ', ';
				$pie_color .= ', ';
			}
		}
		$data['pie_labels'] = $pie_labels;
		$data['pie_sales']  = $pie_sales;
		$data['pie_color']  = $pie_color;

		$data['title']                 = 'Dashboard - ERP Online';
		$data['menu']                  = 'dashboard';
		$data['content']               = 'admin/dashboard';
		$data['total_sales']           = $this->MSales_details->get_total_price();
		$data['total_sales_return']    = $this->MSales_return_details->get_total_price();
		$data['total_purchase']        = $this->MPurchase_details->get_total_price();
		$data['total_purchase_return'] = $this->MPurchase_return_details->get_total_price();
		$data['privileges']            = $this->privileges;
		$this->load->view( 'admin/template', $data );
	}

	/** Test Function Start */
	public function xml_write() {
		$myFile = "thumbnail.xml";
		$fh     = fopen( $myFile, 'w' ) or die( "can't open file" );

		$data = $this->MUsers->get_all(); //get data from database
		//starting tag for xml
		$stringData = '<?xml version="1.0" encoding="utf-8" ?>' . "\n";
		fwrite( $fh, $stringData );
		$stringData = '<thumbnails>' . "\n";
		fwrite( $fh, $stringData );

		//loop through each data
		foreach ( $data as $key => $value ) {
			$stringData = '<thumbnail filename="' . $value['fname'] . '" email="' . $value['email'] . '" />' . "\n";
			fwrite( $fh, $stringData );
		}

		//closing tag for xml
		$stringData = '</thumbnails>';
		fwrite( $fh, $stringData );

		fclose( $fh );
	}

	public function csv_import() {
		$this->load->library( 'csvreader' );
		$filePath = './database/stock_18_01_12.csv';
		$csvData  = $this->csvreader->parse_file( $filePath );
		//print_r($csvData);

		foreach ( $csvData as $key => $value ) {
			$this->MPurchase_details->create_by_csv( $value );
		}
	}

	/** Test Function End */

}
