<?php
/**
 * 会员等级管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoli <1094186793@sina.cn>
 */

namespace Admin\Controller;
class PackageController extends BaseController{
	
	public function _initialize()
    {
        parent::_initialize();
    }
	public function index(){
		$name = M('Goods');
		$map = array();
		$map['goods_type'] = 6;
        $postdata = I('param.');
		$keyword = $postdata['keyword'];
        if(!empty($keyword)){
            $map['name']=array('like','%'.$keyword.'%');
		}
		if(!empty($postdata['reg_from']))$map['addtime'][]=array('EGT',strtotime($postdata['reg_from']));
		if(!empty($postdata['reg_to']))$map['addtime'][]=array('elt',strtotime($postdata['reg_to']));
        $list = $this->lists($name, $map);
		foreach($list as $k=>$v){
			$list[$k]['contain_service'] = json_decode($v['contain_service'],true);
		}
        $this->assign('list', $list);
        $this->assign($postdata);
        $this->display();
	}
	public function add(){
		if(IS_POST){
			$model = M('Goods');
			$postdata=I('param.');
			if($postdata['is_integral']!=1){
				$postdata['is_integral']=0;
			}
			foreach($postdata['service'] as $k=>$v){
				$postdata['contain_service'][$v] = $postdata['nums'][$v]; 
			}unset($postdata['service'],$postdata['nums']);
			if($postdata['shop_ids'])$postdata['shop_ids'] = ','.implode(',',$postdata['shop_ids']).',';
			$postdata['contain_service'] = json_encode($postdata['contain_service']);
			$postdata['continued'] = intval($postdata['continued']);
			$postdata['shop_price'] = $postdata['market_price'];
			$postdata['addtime'] = NOW_TIME;
			$postdata['publish_wait'] = 1;
			$postdata['status'] = 1;
			$postdata['goods_type'] = 6;
			$map['goods_name'] = $postdata['goods_name'];
			$map['goods_type'] = 6;
			$count = $model->where($map)->count();
			if($count){
				$this->error('套餐名称重复');
			}
			$id = $model->add($postdata);
			$content['goods_id'] = $id;
			$content['content'] = $postdata['content'];
			M('GoodsContent')->add($content);
			if($id){
				$jumpUrl = U(CONTROLLER_NAME. '/index');
				$this->assign('jumpUrl', $jumpUrl);
				$this->success(L('ADD_OK'));
			}else{
				$this->error('添加失败');
			}
		}else{
			$this->getService();
			$this->getShop();
			$this->display('edit');
		}
		
	}
	public function edit(){
		$id = I('get.id');
        $info = '';
        if (!$id) $this->error(L('PARAM_ERR'));
        $info = M('Goods')->field(true)->find($id);
		$info['contain_service'] = json_decode($info['contain_service'],true);
		$info['contain'] = $info['contain_service'];
		foreach($info['contain_service'] as $k=>$v){
			$info['contain_service'][$k] = idtoname($k,'Goods','goods_name')."(".idtoname($k,'Goods','shop_price').")(".$v."次)";
		}unset($k,$v);
		if($info['shop_ids']){
			$shopids = array_filter(explode(',', $info['shop_ids']));
			foreach($shopids as $k=>$v){
				$info['shop_arr'][$v] = idtoname($v,'Shops','name');
			}
			$info['shop_check'] = implode(',',array_keys($info['shop_arr']));
		}
		$info['check'] =implode(',',array_keys($info['contain_service']));
		$info['content'] = idtoname($id,'GoodsContent','content','goods_id',false);
		$this->assign($info);
		$this->getService();
		$this->getShop();
		$this->display();
	}
	public function update(){
		$model = M('Goods');
		$postdata=I('param.');
		if($postdata['is_integral']!=1){
			$postdata['is_integral']=0;
		}
		foreach($postdata['service'] as $k=>$v){
			$postdata['contain_service'][$v] = $postdata['nums'][$v]; 
		}unset($postdata['service'],$postdata['nums']);
		$postdata['contain_service'] = json_encode($postdata['contain_service']);
		if($postdata['shop_ids'])$postdata['shop_ids'] = ','.implode(',',$postdata['shop_ids']).',';
		$postdata['shop_price'] = $postdata['market_price'];
		$postdata['continued'] = intval($postdata['continued']);
		$postdata['updatetime'] = NOW_TIME;
		$postdata['status'] = 1;
		$postdata['publish_wait'] = 1;
		$map['goods_name'] = $postdata['goods_name'];
		$map['id'] = array('neq',$id);
		$count = $model->where($map)->count();
		if($count){
			$this->error('套餐名称重复');
		}
		$id = $model->save($postdata);
		$content['content'] = $postdata['content'];
		$where['goods_id'] = $postdata['id'];
		M('GoodsContent')->where($where)->save($content);
		if($id){
			$jumpUrl = U(CONTROLLER_NAME. '/index');
			$this->assign('jumpUrl', $jumpUrl);
			$this->success(L('EDIT_OK'));
		}else{
			$this->error('编辑失败');
		}
	}
	public function care(){
		$this->display();
	}
	public function careadd(){
		$this->display();
	}
	/**
     * 服务查询
     */
	private function getService(){
		$map = array();
		$map['goods_type'] = 4;
		$map['status'] = 1;
		$map['is_delete'] = 0;
		$serviceData = M('Goods')->where($map)->field('id,goods_name,shop_price')->select();
		$this->assign('serviceData',$serviceData);
	}
	//店铺查询
	private function getShop(){
		$map = array();
		$map['status'] = 1;
		$shopData = M('Shops')->where($map)->field('id,name')->select();
		$this->assign('shopData',$shopData);
	}

}
?>