<?php
class Merchant_OrderUploadController extends Ec_Controller_Action{
	public function preDispatch ()
	{
		$this->tplDirectory = "merchant/views/order/";
		// $this->tplDirectory = "merchant/account_config/";
		$this->customerAuth = new Zend_Session_Namespace("customerAuth");
	}
	/**
	 * @author william-fan
	 * @todo 用于下载订单导入的模板
	 */
	public function downOrderTempleteAction(){
		$file = $this->_request->getParam('file','');
		switch($file){
			case 'orderupload':
				$fullPath = APPLICATION_PATH."/../data/file".'/orderbatch.xls';
				break;
		}
		//方法2
		$filename = basename($fullPath);

		header("Content-Type: APPLICATION/OCTET-STREAM");
		//Force the download
		$header="Content-Disposition: attachment; filename=".$filename.";";
		header($header );
		// 	header("Content-Transfer-Encoding: binary");
		// 	header("Content-Length: ".$len);
		echo file_get_contents($fullPath);
		exit;
	}
	/**
	 * @author william-fan
	 * @todo 用于订单的导入
	 */
	public function batchOrderAction(){
		$customer = $this->_customerAuth;
		$customerId = $customer['id'];
		//print_r($customer);
		$customerId = $customer['id'];
		echo Ec::renderTpl($this->tplDirectory . "order-batch.tpl",'layout');
	}
	/**
	 * @author william-fan
	 * @todo 的订单导入检查
	 */
	public function batchcheckAction(){
		//header('Content-type:text/html;charset=utf-8');
		//set_time_limit(300);
		$orderUpload = new Zend_Session_Namespace('orderUpload');
		$field = 'orderFile';
		$xlsData = Service_OrderUpload::getXLSData($_FILES, $field);
		//print_r($xlsData);
		//exit;
		if(isset($xlsData['ask']) && $xlsData['ask']=='0'){
			//echo 'sss';
			$this->view->uploadinfo = $xlsData;
			echo Ec::renderTpl($this->tplDirectory . "order-batch.tpl",'layout');
		}else{
			$orderRows = Service_OrderUpload::getDataArr($xlsData);
			
			$orderUpload->uploadData = $orderRows;
			
			//echo '<pre>';
			//print_r($orderRows);
			//echo '</pre>';
			$this->view->uploadData=$orderRows;
			
			echo Ec::renderTpl($this->tplDirectory . "order-upload-edit.tpl",'layout');
		}
	}
	/**
	 * @author william-fan
	 * @todo 用于订单导入
	 */
	public function importAction(){
		$orderUpload = new Zend_Session_Namespace('orderUpload');
		$sessionData = $orderUpload->uploadData;

		$ids = $this->_request->getParam('select');
		$process = new Service_OrderProcess();
		
		$success =0;
		$false =0;
		$successHtml = '';
		$failseHtml = '';
		$html = '';
		$index=1;

		foreach ($ids as $val){
			
			$status = 1;
			$orderRow = $sessionData[$val];
			$result = $process->createOrderTransaction($orderRow);
			
			if($result['ask']=='1'){
				$successHtml.="<p>第{$index}个订单".'创建成功'.'</p>';
				$success++;
			}else{
				$failseHtml.="<p>第{$index}个订单".$result['message'];
				if(isset($result['error'])){
					$error = $result['error'];
					if(!empty($error)){
						foreach($error as $key=>$err){
							$failseHtml.="<div class='error'>{$err}</div>";
						}
					}
				}
				$failseHtml.='</p>';
				$false++;
			}
			$index++;
		}
		$uploadResult = "<p>成功{$success}失败$false</p>";
		$html = $html.$successHtml.$failseHtml.$uploadResult;
		$this->view->uploadResult=$html;
		unset($orderUpload->uploadData);
		echo Ec::renderTpl($this->tplDirectory . "order-batch.tpl",'layout');
	}
	
	
}