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

class Index extends CI_Controller {
	
	public function __construct() {
        parent::__construct();
		$this->load->model('ordersfrom_model');
	}
	
	public function index(){
		$data = array();
		$data['startime'] = $this->input->get_post('startime');
		$data['endtime'] = $this->input->get_post('endtime');
		$data['webcode'] = $this->input->get_post('webcode');
		$data['total_nums'] = 0;
		
		if($data['startime'] && $data['endtime'] && $data['webcode']){
			$all_orders = $this->ordersfrom_model->get_all_orders($data['startime'],$data['endtime'],$data['webcode']);
			$data['total_nums'] = count($all_orders);
			$data['orders'] = $this->group_order_arr($all_orders);
		}else{
			$data['orders'] = null;
		}
		$this->load->view('index',$data);
	}
	
	function group_order_arr($all_orders){
        $data = array();
        $num = 0;
        foreach ($all_orders as $item){
            preg_match('/(https|http):[\/]{2}[a-z]+[.]{1}[a-z\d\-]+[.a-z\d-\/.htm]*/',$item->COLI_OrderDetailText,$matches);
            if(!empty($matches[0])){
                if(isset($data[$matches[0]])){
                    $data[$matches[0]]['num'] +=1;
                    array_push($data[$matches[0]]['order'],$item->coli_id); 
                }else{
                    $data[$matches[0]] = array();
                    $data[$matches[0]]['num'] = 1;
                    $data[$matches[0]]['order'] = array();
                    array_push($data[$matches[0]]['order'],$item->coli_id); 
                }
            }
        }
		
		return $this->array_sort($data,'num','desc');
    }
	
	function array_sort($arr,$keys,$type='asc'){ 
		$keysvalue = $new_array = array();
		foreach ($arr as $k=>$v){
			$keysvalue[$k] = $v[$keys];
		}
		if($type == 'asc'){
			asort($keysvalue);
		}else{
			arsort($keysvalue);
		}
		reset($keysvalue);
		foreach ($keysvalue as $k=>$v){
			$new_array[$k] = $arr[$k];
		}
		return $new_array; 
	}

	public function count_ah_orders(){
		$data = array();
		$ah_product_obj = $this->ordersfrom_model->ah_productions();
		foreach($ah_product_obj as $item){
			$data[$item->cli_no] = array();
			$orders_arr = $this->ordersfrom_model->get_orders($item->cli_no);
			$data[$item->cli_no] = $orders_arr;
		}
		$mdata['orders'] = $data;
		$this->load->view('ah_orders',$mdata);
	}	
}


?>