<?php
class Merchant_CustomerAddressController extends Ec_Controller_Action{
	public function preDispatch()
	{
		$this->tplDirectory = "merchant/views/customerAddress/";
		$params = self::getFrontController()->getRequest()->getParams();
		$this->view->ac = $params['action'];
		$this->customerAuth = new Zend_Session_Namespace("customerAuth");
	}
	/**
	 * @author william-fan
	 * @todo用于客户的地址列表
	 */
	public function listAction(){
		$param = $this->_request->getParams();
		
		$customer = $this->_customerAuth;
		$customerId = $customer['id'];
		
		$page = $this->_request->getParam('page', 1);
		$pageSize = $this->_request->getParam('pageSize', 20);
		
		$condition = array();
		/* 以下为提交数据整理 */
		
		$cab_type = trim($this->_request->getParam("cab_type",""));
		$condition["cab_type"] = $cab_type;
		
		
		$condition["customer_id"] = $customerId;
		
		
		$cab_code = $this->_request->getParam("cab_code","");
		$condition["cab_code"] = $cab_code;
		
		
		$cab_lastname = $this->_request->getParam("cab_lastname","");

		$condition["cab_lastname"] = $cab_lastname;
		
		$count = Service_CustomerAddressBook::getByCondition($condition,'count(*)');
		
		$rows = Service_CustomerAddressBook::getByCondition($condition,'*');
		foreach ($rows as $key=>$value){
			$cab_country_name = '';
			if(!empty($value) && $value['cab_country_id']>0){
				$cabCountryInfo = Service_Country::getByField($value['cab_country_id']);
				if($cabCountryInfo){
					$cab_country_name = $cabCountryInfo['country_name'];
				}
			}
			$rows[$key]['cab_country_name'] = $cab_country_name;
		}
		
		
		/* $return['data'] = $rows;
		$return['ask'] = 1;
		$return['message'] = ""; */
		
		$this->view->count = $count;
		$this->view->result = $rows;
		$this->view->params = $condition;
		$this->view->page = $page;
		$this->view->pageSize = $pageSize;
		
		echo Ec::renderTpl($this->tplDirectory . "address-list.tpl", 'noleftlayout');
	}
	/**
	 * @author william-fan
	 * @todo 用于编辑客户地址
	 */
	public function createAction(){
		$customer = $this->_customerAuth;
		$customerId = $customer['id'];
		$customer_code = $customer['code'];
		$cab_type = trim($this->_request->getParam('cab_type'));
		if($this->_request->isPost()){
			$param = $this->_request->getParams();

			
			$cab_type 			= trim($this->_request->getParam('cab_type',''));
			$cab_code 			= trim($this->_request->getParam('cab_code',''));
			$cab_lastname		= trim($this->_request->getParam('cab_lastname',''));
			$cab_firstname		= trim($this->_request->getParam('cab_firstname',''));
			$cab_company		= trim($this->_request->getParam('cab_company',''));
			
			$country_id			= trim($this->_request->getParam('country_id',''));
			$cab_state			= trim($this->_request->getParam('cab_state',''));
			$cab_city			= trim($this->_request->getParam('cab_city',''));
			$cab_street_address1= trim($this->_request->getParam('cab_street_address1',''));
			$cab_street_address2= trim($this->_request->getParam('cab_street_address2',''));
			$cab_postcode		= trim($this->_request->getParam('cab_postcode',''));
			$cab_phone			= trim($this->_request->getParam('cab_phone',''));
			$cab_cell_phone		= trim($this->_request->getParam('cab_cell_phone',''));
			$cab_fax			= trim($this->_request->getParam('cab_fax',''));
			$cab_email			= trim($this->_request->getParam('cab_email',''));
			$cab_note			= trim($this->_request->getParam('cab_note',''));
			
			$cab_id				= $this->_request->getParam('cab_id','');
			
			$row = array(
					'customer_id'=>$customerId,
					'customer_code'=>$customer_code,
					'cab_type'=>$cab_type,
					'cab_code'=>$cab_code,
					'cab_firstname'=>$cab_firstname,
					'cab_lastname'=>$cab_lastname,
					'cab_company'=>$cab_company,
					'cab_country_id'=>$country_id,
					'cab_state'=>$cab_state,
					'cab_city'=>$cab_city,
					'cab_street_address1'=>$cab_street_address1,
					'oab_street_address2'=>$cab_street_address2,
					'cab_postcode'=>$cab_postcode,
					'cab_cell_phone'=>$cab_cell_phone,
					'cab_fax'=>$cab_fax,
					'cab_email'=>$cab_email,
					'cab_note'=>$cab_note
			);
		

			
			$cab_id = $this->_request->getParam('cab_id','');
			//var_dump($ordersCode);
			$process = new Service_CustomerAddressProcess();
			if($cab_id){
				$result = array("ask"=>0,"message"=>"更改信息失败",'error'=>array());
				$customerAddress = Service_CustomerAddressBook::getByField($cab_id);
				if(empty($customerAddress)){
					$result['message'] = '客户地址无效';
					die(Zend_Json::encode($result));
				}
				if($customerAddress['customer_id']!=$customerId){
					$result['message'] = '无权操作';
					die(Zend_Json::encode($result));
				}
				$row['cab_id'] = $cab_id;
				$result = $process->updateCustomerTransaction($row,$customerId);
				//Common_DataCache::getOrderElseInfo($ordersCode,1);
			}else{
				$result = $process->creataCustomerTransaction($row,$customerId);
			}
			//print_r($result);
			//exit;
			/* if(!empty($result['error'])){
				foreach($result['error'] as $k=>$v){
					$result['error'][$k] = preg_replace('/^([0-9]+)/', '', $v);
				}
			} */
		
			die(Zend_Json::encode($result));
		}else{
			$cab_id				= $this->_request->getParam('cab_id','');
			if($cab_id){
				$customerAddress = Service_CustomerAddressBook::getByField($cab_id);
				$cab_type = $customerAddress['cab_type'];
				if($customerAddress){
					$this->view->customerAddress = $customerAddress;
					$this->view->modify ='1';
				}
			}
			if($cab_type!='1' && $cab_type!='2'){
				$this->_redirect('/error/error');
			}
		}
		$this->view->cab_id = $cab_id;
		
		$countryArr = Common_DataCache::getCountry();
		//print_r($countryArr);
		/* foreach($countryArr as $key=>$value){
			foreach($value['ship_type'] as $k2=>$v2){
				if($v2['sm_code']!=""){
					$smRow = Service_ShippingMethod::getByField($v2['sm_code'],'sm_code',"sm_name_cn");
					$countryArr[$key]['ship_type'][$k2]['sm_name_cn'] = $smRow['sm_name_cn'];
				}
			}
		} */
		
		$this->view->country = $countryArr;
		$currencyAll = Common_DataCache::getCurrency();
		$this->view->currencyAll = $currencyAll;
		$tpl = 'addressbook-send.tpl';
		if($cab_type=='1'){
			$tpl = 'addressbook-receive.tpl';
		}else{
			//发件人$cab_type==2
			$province_array = Service_Region::getByCondition(array('parent_id'=>1));
			$this->view->province_array = $province_array;
			$this->view->city_array = array();
			if(isset($customerAddress)&&$customerAddress['cab_state_id']>0){
				//print_r(Service_Region::getByCondition(array('parent_id'=>$orderInfo['oab_state_id'])));
				$this->view->city_array = Service_Region::getByCondition(array('parent_id'=>$customerAddress['cab_state_id']));;
			}
		}	
		$action = '新增';
		if($cab_id){
			//$actionLabel="add";
			$action = Ec_Lang::getInstance()->getTranslate('OrderModify');;
		}
		//$this->view->actionLabel = $actionLabel;
		$this->view->action = $action;
		$this->view->cab_type = $cab_type;
		$this->view->countryJs = Zend_Json::encode($countryArr);
		//$this->view->cityJs = Zend_Json::encode($cityArr);
		/* $ecg_category = Common_DataCache::getProductCategory();
		$this->view->ecg_category = $ecg_category;
		$this->view->orderType = Service_OrderProcess::getOrderTypeArr(); */

		echo Ec::renderTpl($this->tplDirectory . $tpl,'noleftlayout');
	}
	
}