<?php

class goodsAction extends frontendAction {

    public function _initialize() {
        parent::_initialize();
		//访问者控制
        if (!$this->visitor->is_login) {
            IS_AJAX && $this->ajaxReturn(0, L('login_please'));
            $this->redirect('user/login');
        }
		$this->_mod = D('items');
		$this->cid = $_SERVER['HTTP_HOST'];
        $this->_cate_mod = D('items_cate');
		$this->assign('nav_curr', 'index');
    }


	public function goods_add() {
		$orig_list = M('items_orig')->where(array('pass'=>1))->select();
        $this->assign('orig_list',$orig_list);
		$this->_config_seo(array(
            'title' => L('goods_add') . '	-	' . C('ftx_site_name'),
        ));
		$this->display();
	}

	public function mygoods() {
		$item_mod = M('items');
		$cate_mod = M('items_cate');
		$page_size = 20;
        $p = $this->_get('p', 'intval', 1); //页码
        $start = $page_size * ($p - 1) ;

        $res = $cate_mod->field('id,name')->select();
        $cate_list = array();
        foreach ($res as $val) {
            $cate_list[$val['id']] = $val['name'];
        }
        $this->assign('cate_list', $cate_list);
		$type = $this->_get('type', 'trim', 'all'); //排序
		$order = 'ordid asc ';
		$map['uid'] = $this->visitor->info['id'];
		switch ($type) {
            case 'all':
                break;
            case 'pass':
                $map['pass'] = 1;   
                break;
			case 'wait':
				$map['pass'] = 0;
				$map['status'] = 'underway';
                break;
			case 'fail':
                $map['pass'] = 0; 
				$map['status'] = 'fail';
                break;
        }
		$goods_list = $item_mod->where($map)->limit($start . ',' . $page_size)->select();
		$this->assign('goods_list', $goods_list);
		$count = $item_mod->where($map)->count('id');
       // $pager = $this->_pager($count, 20);
		//$this->assign('page_bar', $pager->fshow());

		$pager = $this->_pager($count, $page_size);
        $this->assign('page_bar', $pager->fshow());


		$this->assign('type', $type);
		$this->_config_seo(array(
            'title' => L('my_goods') . '	-	' . C('ftx_site_name'),
        ));
		$this->display();
	}


    /**
     * AJAX获取宝贝
     */
    public function ajaxgetid() {
		$url = $this->_get('url', 'trim');
        $url == '' && $this->ajaxReturn(0, L('please_input') . L('correct_itemurl'));
		!$this->get_id($url) && $this->ajaxReturn(0, L('please_input') . L('correct_itemurl'));
		$iid=$this->get_id($url);
		$items = M('items')->where(array('num_iid' => $iid))->find();
		$items && $this->ajaxReturn(0, L('item_exist'));

		$ftxia_https = new ftxia_https();
		$ftxia_https->gethtml('http://bbs.8mob.com/plugin.php?id=ftxia:getvip&action=ftxiataobao',$iid);
		$content = $ftxia_https->results;
		//exit($content);

		if('error' == $content){
			$this->ajaxReturn(0,'请输入正确的商品链接');
		}else{
			$items=array();
			$iid								= get_word($content,'iid','\/iid');
			$items['item']['num_iid']			= get_word($content,'iid','\/iid');
			$items['item']['detail_url']		= 'http://item.taobao.com/item.htm?id='.$iid;
			$items['item']['title']				= get_word($content,'title','\/title');
			$items['item']['price']				= get_word($content,'price','\/price');
			$items['item']['tmall']				= get_word($content,'tmall','\/tmall');
			$items['item']['pic_url']			= get_word($content,'pic_url','\/pic_url');
			$items['item']['ems']				= get_word($content,'ems','\/ems');
			$items['item']['volume']			= get_word($content,'volume','\/volume');
			$items['item']['quantity']			= get_word($content,'quantity','\/quantity');
			$items['item']['nick']				= get_word($content,'nick','\/nick');
			$items['item']['coupon_price']		= get_word($content,'coupon_price','\/coupon_price');
			$items['item']['coupon_start_time']	= get_word($content,'coupon_start_time','\/coupon_start_time');
			$items['item']['coupon_end_time']	= get_word($content,'coupon_end_time','\/coupon_end_time');
			$items['msg']						= get_word($content,'msg','\/msg');
			$this->ajaxReturn(1,'',$items);
		}

/*
		$itemcollect = new itemcollect();
        $itemcollect->url_parse($url);
		if($item = $itemcollect->fetch_tb()){
			$this->ajaxReturn(1, '', $item);
		}
		$this->ajaxReturn(0, L('item_not_excute'));
		*/
    }

	/**
     * AJAX提交
     */
	public function ajaxadd(){
		if(IS_POST){
			$items_mod		= M('items');
			$num_iid		= $this->_post('iid', 'trim');
			if($num_iid == ''){
				$this->ajaxReturn(1005, '商品IID不能为空，请输入宝贝地址获取');
			}
			$cate_id		= $this->_post('cate_id', 'trim');
			$title			= $this->_post('title', 'trim');
			!$title && $this->ajaxReturn(1005, '商品名称不能为空');
			$nick			= $this->_post('nick', 'trim');
			!$nick && $this->ajaxReturn(1005, '掌柜名称不能为空');
			$price			= $this->_post('price', 'trim');
			$coupon_price	= $this->_post('good_price', 'trim');
			$inventory		= $this->_post('good_inventory', 'trim');
			$pic_url		= $this->_post('pic_url', 'trim');
			$shop_type		= $this->_post('shop_type', 'trim');
			$coupon_start_time			= $this->_post('coupon_start_time', 'trim');
			$coupon_end_time			= $this->_post('coupon_end_time', 'trim');
			$intro			= $this->_post('intro', 'trim');

			$items = $items_mod->where(array('num_iid' => $num_iid))->find();
			$items && $this->ajaxReturn(1005, L('item_exist'));

			$data['num_iid']		= $num_iid;
			$data['cate_id']		= $cate_id;
			$data['title']			= $title;
			$data['nick']			= $nick;
			$data['price']			= $price;
			$data['coupon_price']	= $coupon_price;
			$data['inventory']		= $inventory;
			$data['pic_url']		= $pic_url;
			$data['intro']			= $intro;
			$data['coupon_start_time']			= strtotime($coupon_start_time);
			$data['coupon_end_time']			= strtotime($coupon_end_time);
			$data['shop_type']		= $shop_type;
			$data['add_time']		= time();
			$data['pass']			= 0;
			$data['uid']			= $this->visitor->info['id'];
			$data['uname']			= $this->visitor->info['username'];

			$items_mod->create($data);
			if($items_mod->add()){
				$resp = $this->fetch('dialog:goods_add_success');
				$this->ajaxReturn(1, '', $resp);
			}else{
				$this->ajaxReturn(0, '数据错误，请检查！');
			}
		}
	}



	public function get_id($url) {
        $id = 0;
        $parse = parse_url($url);
        if (isset($parse['query'])) {
            parse_str($parse['query'], $params);
            if (isset($params['id'])) {
                $id = $params['id'];
            } elseif (isset($params['item_id'])) {
                $id = $params['item_id'];
            } elseif (isset($params['default_item_id'])) {
                $id = $params['default_item_id'];
            }
        }
        return $id;
    }


}