<?php
if (!defined('IN_59MIAO'))
{
	exit('Access Defined');
}
//导航条控制器
class SellerlistAction extends Action {		
	public function __construct() {
		parent::__construct();			
	}	
	public function index() {
		parent::page();		
		$Alllist=$this->_model->findAll();
		foreach ($Alllist as $key=>$value){
			$site_logo = $value->site_logo;
			$net_logo=$value->net_logo;
			if(file_exists($site_logo)){
				$value->logo=$site_logo;
			}
			else{
				$value->logo=$net_logo;
			}	
		}		
		$this->_tpl->assign('Alllist',$Alllist);
		$this->_tpl->display(SMARTY_ADMIN.'seller/list/show.tpl');
	}
	public function addb2cdata(){						
					$page_size=20;					
					if(isset($_GET['page'])){
						$page=$_GET['page'];	
					}
					else{
						$page=1;
					}					
					//调取数据入库
					$data=$this->_model->ShopListGet($page,$page_size);
					if(isset($data[0])){
						foreach ($data as $k=>$v){
							//$cid=$value['cid'];
							$sid=$v['sid'];
							$name=$v['name'];			
							$net_logo=$v['logo'];
							//如果不是二维数组，转换为二维数组
							if(Tool::IsTwoArray($v['sellercats']['sellercat'])){
								$v['sellercats']['sellercat']=array($v['sellercats']['sellercat']);
							}	
							$cid='';
							foreach ($v['sellercats']['sellercat'] as $value){							
									$cid.= $value['cid'].',';				
							}
							$cid= substr($cid, 0, -1);	
							//echo $cid.'<br>';
							$sitetype=$cid;
							$category_id=1;   //默认调取女装信息
							$click_url=$v['click_url'];
							$sort=10;
							$desc=$v['desc'];
							if(Validate::isArray($desc)){
								$desc=$name;
							}						
							$state=1;
							//是否免费送货 
							if($v['freeshipment']=='False'){
								$freeshipment=0; 
							}
							else{
								$freeshipment=1;
							}
							//是否支持分期付款 
							if($v['installment']=='False'){
								$installment=0; 
							}
							else{
								$installment=1;
							}
							//是否有发票
							if($v['has_invoice']=='False'){
								$has_invoice=0; 
							}
							else{
								$has_invoice=1;
							}
							$_updateData=array(
								'sid'=>$sid,							
								'name'=>$name,
								'net_logo'=>$net_logo,
								'sitetype'=>$sitetype,
								'click_url'=>$click_url,
								'category_id'=>$category_id,								
								'state'=>$state,
								'sort'=>$sort,							
								'description'=>$desc,
								'freeshipment'=>$freeshipment,
								'installment'=>$installment,
								'has_invoice'=>$has_invoice								
							);
							$_where = array("sid=$sid");	
							$data='';
							$data=$this->_model->isOne($_where);					
							if($data==1){
								$this->_model->b2cupdate($_where, $_updateData);				
							}
							else{				
								$this->_model->b2cadd($_updateData);				
							}
						}	
						
						$page++;	

						$this->_tpl->assign('page',$page);
						$this->_tpl->display(SMARTY_ADMIN.'public/categoryload.tpl');
					
					}
					else{
						$this->_redirect->succ('?a=sellerlist', '恭喜您同步成功！');//数据同步完成
					}	
				
	}
	public function add() {
		if (isset($_POST['send'])) $this->_model->add() ? $this->_redirect->succ('?a=sellerlist', '商家新增成功！') : $this->_redirect->error('商家新增失败！');
		$this->_tpl->assign('sitetype', Tool::setFormItem($this->_model->GetSellerCat(),'cid','name'));
		$this->_tpl->assign('category',Tool::setFormApiItem($this->_model->GetItemCats(),'cid','name'));  //调取api数据		
		$this->_tpl->display(SMARTY_ADMIN.'seller/list/add.tpl');
	}	
	public function update(){
		if (isset($_POST['send'])) $this->_model->update() ? $this->_redirect->succ($_POST['pre_page'], '商家修改成功！') : $this->_redirect->error('商家修改失败！');
		if (isset($_GET['id'])){			
			$Onelist=$this->_model->findOne();	
			switch ($Onelist[0]->recommend){
				case 1 :
					$Onelist[0]->recommend = '<option value="1" selected="selected">推荐</option>						
						<option value="0">不推荐</option>	';
					break;
				case 0 :
					$Onelist[0]->recommend = '<option value="1">推荐</option>						
						<option value="0" selected="selected">不推荐</option>';
					break;
			
			}	
			switch ($Onelist[0]->state){
				case 1 :
					$Onelist[0]->state = '<option value="1" selected="selected">显示</option>						
						<option value="0">隐藏</option>	';
					break;
				case 0 :
					$Onelist[0]->state = '<option value="1">显示</option>						
						<option value="0" selected="selected">隐藏</option>';
					break;
			
			}	
			$site_logo=$Onelist[0]->site_logo;
			
			if(file_exists($site_logo)){
				$Onelist[0]->logo=$site_logo;
			}
			else{
				$Onelist[0]->logo=$Onelist[0]->net_logo;
			}
			//echo $Onelist[0]->sitetype.'<br>';		
			
			$selectedSitetype=explode(",",$Onelist[0]->sitetype);		
			
			$this->_tpl->assign('sitetype', Tool::setFormItem($this->_model->GetSellerCat(),'cid','name'));
			$this->_tpl->assign('selectedSitetype',$selectedSitetype);
			
			//未知原因，无法获取上一页信息，分配到模板中
			$this->_tpl->assign('pre_page',Tool::getPrevPage());
			$this->_tpl->assign('sitetype', Tool::setFormItem($this->_model->GetSellerCat(),'cid','name'));
			$this->_tpl->assign('category',Tool::setFormApiItem($this->_model->GetItemCats(),'cid','name'));  //调取api数据			
			$this->_tpl->assign('Onelist',$Onelist[0]);		
			$this->_tpl->display(SMARTY_ADMIN.'seller/list/update.tpl');
		}
	}	
	public function delete() {
		if (isset($_GET['id'])) $this->_model->delete() ? $this->_redirect->succ(Tool::getPrevPage(), '商家删除成功！') : $this->_redirect->error('商家删除失败！');
	}
	
	public function sort() {
		if (isset($_POST['send'])) $this->_model->sort() ? $this->_redirect->succ(Tool::getPrevPage()) : $this->_redirect->error('排序失败！');
	}
	
}
?>