<?php

class GoodsAction extends BaseAction {
	
	public function __construct()
	{
		parent::__construct("Goods",'Goods');
	}

	public function index()
	{
		parent::init_search(array('title'));
		$arr = array('new','hot','show','recommend');
		$this->_where = ($this->_where=='')?'1 ':$this->_where;
		
		$sear = array();
		if($_GET['show']!='')
		{
			$sear['is_show'] = $_GET['show'];
		}
		if($_GET['start']!='')
		{
			$sear['time'] = array('egt',$_GET['start']);
		}
		if($_GET['end']!='')
		{
			$sear['time'] = array('elt',$_GET['end']);
		}
		if($_GET['pid']!='')
		{
			$sear['pid'] = $_GET['pid'];
		}
		if($_GET['key']!='')
		{
			$sear['title'] = array('like','%二级%');
		}
		//已锁定商品
		$locking_order = M('orders')->where("`state` in (0,1,2)")->select();
		$pid_string = '';
		foreach ($locking_order as $v){
			$pid_string .= $v['id'].',';
		}
		$pid_string = rtrim($pid_string,',');
		$locking_list = M('order_goods')->where("`oid` in ($pid_string)")->select();
	
		//查询列表
		import('ORG.Util.Page');
		
		
		$sear['state'] = 0;
		$count      = D('goods') ->where($sear) ->count();
		$Page       = new Page($count,20);
		$Page->setConfig('theme','<span>%totalRow% %header% %nowPage%/%totalPage% 页</span>  %first%  %upPage% %linkPage%  %downPage% %end%');
		$show       = $Page->show();
		$list = D('goods')->where($sear)->relation('types')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		foreach($list as $key=>$value)
		{
			$list[$key]['son'] = M('goods_number')->where("`pid`=".$value['id'])->order('`order` desc,`id` desc')->select();
			$list[$key]['cangku_address'] = M('place')->where("`id`=".$value['cangku'])->find();
			$list[$key]['all_num'] = M('goods_number')->where("`pid`=".$value['id'])->sum('num');
			foreach($list[$key]['son'] as $key2=>$value2){
				$list[$key]['all_weight'] += $value2['num'] * $value2['weight'];
			}
			
			//已锁定商品
			foreach($list[$key]['son'] as $v=>$val){
				foreach($locking_list as $n){
					if(($val['id'] == $n['nid'])&&($val['pid'] == $n['pid'])){
						$list[$key]['son'][$v]['locking'] += $n['num'];	
					}
				}
				$list[$key]['all_locking_num'] += $list[$key]['son'][$v]['locking'];
				$list[$key]['all_locking_weight'] += $list[$key]['son'][$v]['locking'] * $val['weight'];
			}
		}
		
		
		$this->assign('list',$list);
		$this->assign('page',$show);
	
		//添加
		$param = array_filter($_GET);
		foreach ($param as $key=>$value) {
			$url_param .= $key.'/'.$value.'/';
		}
		$url_param = rtrim($url_param,'/');
		
		$category =  D('GoodsTypes')->get_select();
		unset($category[0]);
		$this->assign('category',$category);
		$this->assign('jump',$url_param);
		$this->display();
	}
	
	public function add()
	{	
		$place = M('place')->select();
		$zone = M('zone')->select();
		$yewu = M('salesman')->select();
		$this->assign('place',$place);
		$this->assign('zone',$zone);
		$this->assign('yewu',$yewu);
		$this->selected = intval($_REQUEST['pid']);
		parent::get_types('Goods');
		$this->display('edit');
	}
	
	public function edit()
	{
		$place = M('place')->select();
		$zone = M('zone')->select();
		$yewu = M('salesman')->select();
		$this->assign('place',$place);
		$this->assign('zone',$zone);
		$this->assign('yewu',$yewu);
		parent::init_get('number');
		$this->selected = $this->info['pid'];
		parent::get_types('Goods');
		
		if($_POST){
			if($_POST['id']){
				$data = array();
				$id = $_POST['id'];
				$data['pid'] = $_POST['pid'];
				$data['title']     = $_POST['title'];
				$data['price']   = $_POST['price'];
				$data['guige']       = $_POST['guige'];
				$data['caizhi']  = $_POST['caizhi'];
				$data['gangchang']  = $_POST['gangchang'];
				$data['cangku']  = $_POST['cangku'];
				$data['diqu']  = $_POST['diqu'];
				$data['yewuyuan']  = $_POST['yewuyuan'];
				$data['houdu_low']  = $_POST['houdu_low'];
				$data['houdu_high']  = $_POST['houdu_high'];
				$data['kuandu_low']  = $_POST['kuandu_low'];
				$data['kuandu_high']  = $_POST['kuandu_high'];
				if($_POST['is_show'] != 1){
					$data['is_show']  = 0;
				}else{
					$data['is_show']  = $_POST['is_show'];
				}
				$status=M('goods')->where(" `id` = $id ")->save($data);
				if($status !== false){
					$this->ajaxReturn(0,$id,true);
				}
				else
				{
					$this->ajaxReturn(2,$id,false);
				}
			}else{
				$data = array();
				$data['pid'] = $_POST['pid'];
				$data['title']     = $_POST['title'];
				$data['price']   = $_POST['price'];
				$data['guige']       = $_POST['guige'];
				$data['caizhi']  = $_POST['caizhi'];
				$data['gangchang']  = $_POST['gangchang'];
				$data['cangku']  = $_POST['cangku'];
				$data['diqu']  = $_POST['diqu'];
				$data['yewuyuan']  = $_POST['yewuyuan'];
				$data['houdu_low']  = $_POST['houdu_low'];
				$data['houdu_high']  = $_POST['houdu_high'];
				$data['kuandu_low']  = $_POST['kuandu_low'];
				$data['kuandu_high']  = $_POST['kuandu_high'];
				if($_POST['is_show'] != 1){
					$data['is_show']  = 0;
				}else{
					$data['is_show']  = $_POST['is_show'];
				}
				$status=M('goods')->add($data);
				if($status){
					$this->ajaxReturn(0,$status,true);
				}
				else
				{
					$this->ajaxReturn(2,$status,false);
				}
			}
		}
		
		$this->display();
	}
	
	public function edit_number()
	{
		
		if($_POST){
			$postData = array();
			$data_db = M('goods_number');
			$postData['pid'] = $_POST['pid'];
			$postData['num'] = $_POST['num'];
			$postData['weight'] = $_POST['weight'];
			
			if($_POST['id']){
				$id = $_POST['id'];
				$status = M('goods_number')->where(" `id` = $id ")->save($postData);
			}else{
				$status = M('goods_number')->add($postData);
			}
			if($status !== false){
				$this->ajaxReturn(0,'添加成功',true);
			}
			else
			{
				$this->ajaxReturn(2,'添加失败',false);
			}
		}else{}
	}
	public function edit_delete()
	{
		$id = $_POST['id'];
		$status = M('goods_number')->where(array('id'=>$id))->delete();
		if($status){
			$this->ajaxReturn('','删除成功',true);
		}else{
			$this->ajaxReturn('','删除失败',false);
		}
	}
	
	public function upImages()
	{
		$img_name = $this->UploadFile(NULL,true);
		$name = explode('.',$img_name[0]);
		$id = $name[0];
		$img = $img_name[0];
		$url = __APP__.'/'.C('UPLOAD_DIR').'/thumb/_'.$img_name[0];
		$show_thumb_width = 'idwidth';
		$show_thumb_height = 'idheight';
		
		echo $url.'|'.$img_name[0].'|'.$img.'|'.$id.'|'.'list'.'|'.'_'.$show_thumb_width.'_'.$show_thumb_height;
	}
	
	public function price()
	{
		$ids = $_REQUEST['id'];
		$this->assign('ids',$ids);
		
		if($_POST){
			$id    = $_POST['id'];
			$data['price'] = $_POST['price'];
			$where = array('id'=> array('in', $id));
			$resdeal = M('goods')->where($where)->save($data);
			if($resdeal)
			{
				echo '<script type="text/javascript">parent.change_page();parent.refush();</script>';
			}
			else
			{
				echo '<script type="text/javascript">parent.change_page();alert("保存失败");</script>';
			}
			exit;
		}
		
		$this->display();
	}
	
	public function ajax()
	{
		$id = $_REQUEST['id'];
		$data['state'] = 1;
		M('goods')->where(array('id'=>$id))->save($data);
		M('goods_number')->where(array('pid'=>$id))->delete();
		M('carts')->where(array('pid'=>$id))->delete();
		$this->ajaxReturn('','删除成功',true);
	}
	
	public function setValue()
	{
		$arr = array('is_new','is_hot','is_show','is_recommend','order');
		$field = $this->_param(3);
		if(in_array($field,$arr))
		{
			$data = array();
			$id = (int)$this->_param(5);
			$data[$field] = (int)$this->_param(4);
			$res = $this->_model->setValue($data,$id);
			if($res)
			{
				echo 'ok';
			}
			else
			{
				echo '数据库操作出错！';
			}
		}
		else
		{
			echo '请求错误!';
		}
	}
}