<?php
/**
 * 拖拽编辑模板管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <570035208@qq.com>
 */
namespace Admin\Controller;
class DrageditController extends BaseController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->name = CONTROLLER_NAME;
        $this->currentid = intval(I('id'));
        $this->codelist = array(
            array('code'=>'red','value'=>'rgb(201, 24, 27)'),
            array('code'=>'yellow','value'=>'rgb(255, 147, 0)'),
            array('code'=>'green','value'=>'rgb(9, 187, 7)'),
            array('code'=>'blue','value'=>'rgb(74, 144, 226)'),
        );
        $is_auth=check_use_auth("WEBSHOP");
        if($is_auth['status']==0){
            $this->error('该版本没有此权限',$is_auth['url']);return;
        }
    }
    /**
     * 编辑首页
     */
    public function index(){
        $map=array();
        $map['is_wxsmall']=0;
        $list = M($this->name)->where($map)->field(true)->select();
        $default = array();
        foreach($list as $k=>$v){
            if($v['is_default']==1){
                $default = $v;break;
            }
        }
        $jumpurl = geturl('Mobile/Index/index',array(),true,true);$jumpurl=str_replace('index.php?s=/','',$jumpurl);
        $jumpurl=str_replace('index/index.html','',$jumpurl);
//        var_dump($default);
        $this->assign('default', $default);
        $this->assign('jumpqrcode',geturl('Home/Qrcode/index',array('data'=>urlencode($jumpurl))));
        $this->assign('jumpurl', $jumpurl);
        $this->assign('list', $list);
        $this->assign('currentcode', idtoname('TEMPLATE_THEME_COLOR','Config','value','name',false));
        $this->assign('codelist', $this->codelist);
        //判断权限
        $waimai_auth=checksiteauth('WaiMai');
        $this->assign('waimai_auth',$waimai_auth);
        $this->display();
    }
    /**
     * 编辑内容页
     */
    public function vindex(){
        if($this->currentid<1)$this->error('参数错误');
        $urlrules = geturlrules(3);
        $add = array('id'=>'url','name'=>'自定义URL');
        array_unshift($urlrules, $add);
        foreach($urlrules as $key=>$val){
            if($val['id']==26){
                unset($urlrules[$key]);
            }
        }
        //获取筛选条件
        $HCorderlist = D('Common/Dragedit')->getorderlist();
        if($this->currentid>0){
            $map['id'] = $this->currentid;
            $content = M($this->name)->where($map)->getField('value');
            if($content){
                $this->assign('HCinitlist', $content);
            }else{
                $this->assign('HCinitlist', json_encode(array()));
            }
        }
        $this->assign('jumpUrl', geturl('Mobile/Index/index',array('id'=>$this->currentid,'key'=>base64_encode(C('HANCHAO_KEY').$this->currentid)),true,true));
        $this->assign('HCorderlist', json_encode($HCorderlist));
        $this->assign('urlrules', json_encode($urlrules));
        $this->display();
    }
    /**
     * 商品搜索
     */
    public function get_goods_list(){
        $return = array('status'=>0,'msg'=>'未获取到数据');
        $name = 'Goods';
        $postdata = I('param.');
        $postdata['showpage'] = 1;//显示分页模板
        $list = D('Common/Dragedit')->get_goods_list($name,$postdata);
        if($list['status']){
            $return = $list;
        }
        $this->ajaxReturn($return);
    }
    
    public function add(){
        $return = array('status'=>0,'msg'=>'保存失败');
        $postdata = I('post.');
        if(empty($postdata))$this->ajaxReturn($return);
        $list = D('Common/Dragedit')->formatdiydata($postdata['content']);
        $data['add_time'] = NOW_TIME;
        $data['update_time'] = NOW_TIME;
        $data['value'] = json_encode($list);
        $result = M($this->name)->add($data);
        if($result)$return['status'] = 1;
        $this->ajaxReturn($return);
    }

    public function update(){
        $return = array('status'=>0,'msg'=>'保存失败');
        $postdata = I('post.');
        if(empty($postdata))$this->ajaxReturn($return);
        $list = D('Common/Dragedit')->formatdiydata($postdata['content']);
        $data['update_time'] = NOW_TIME;
        $data['value'] = json_encode($list);
        $map['id'] = $this->currentid;
        $result = M($this->name)->where($map)->save($data);
        if($result)$return['status'] = 1;
        $this->ajaxReturn($return);
    }
    //设置默认模板
    public function setting(){
        if($this->currentid<1)$this->error('参数错误');
        $map['id'] = array('gt',0);
        $map['is_wxsmall'] = 0;
        M($this->name)->where($map)->setField('is_default',0);
        $map['id'] = $this->currentid;
        $result = M($this->name)->where($map)->setField('is_default',1);
        if($result !==false){
            $this->success('设置成功');
        }else{
            $this->error('设置失败');
        }
    }
    //添加修改模板
    public function edit(){
        if(IS_POST){
            $param = I('post.');
            $data['name'] = $param['name'];
            $data['thumb_img'] = $param['thumb_img'];
            $data['code'] = $param['code'];
            if($this->currentid>0){
                $map['id'] = $this->currentid;
                $result = M($this->name)->where($map)->save($data);
            }else{
                $data['add_time'] = $data['update_time'] = NOW_TIME;
                $result = M($this->name)->add($data);
            }
            if($result !==false){
                $this->success('保存成功',geturl('Dragedit/index'));
            }else{
                $this->error('保存失败');
            }
        }else{
            $list = array();
            if($this->currentid>0){
                $map['id'] = $this->currentid;
                $info = M($this->name)->where($map)->field('id,name,thumb_img,code')->find();
            }
            $this->assign('codelist', $this->codelist);
            $this->assign('info', $info);
            $this->display();
        }
    }
    public function tab_color(){
        $code=I('code');
        $return = array('status'=>0,'msg'=>'切换失败');
        if(!empty($code)){
            $map['name']='TEMPLATE_THEME_COLOR';
            $result=M('config')->where($map)->setField('value',$code);
            if($result !==false){
                $return['status'] = 1;
                $return['msg'] = '切换成功';
            }
        }
        $this->ajaxReturn($return);
    }
	public function list_index(){
        if(!checksiteauth('WaiMai')){ exit;}
		$module='Mobile';
		$indexlist=getmodtpl('shops_index_','Takeout','',$module);
		$list = array();
		foreach($indexlist as $k=>$v){
			unset($list[0]);
			$list[$k]['id'] = strstr($v,'.',true);
			$list[$k]['thumb_img'] = APP_PATH.$module.'/View/'.get_default_theme($module).'/'.$pathdir.'Takeout'.'/'.str_replace('shops_index_',"",strstr($v,'.',true)).'.jpg';
			$list[$k]['name'] = str_replace('shops_index_',"",strstr($v,'.',true)).'模板';
		}
		$this->assign('list',$list);
		$list_tpl = C('LIST_TPL');
		if(empty($list_tpl)) $list_tpl = "shop_index_list";
		$this->assign('tplimg',APP_PATH.$module.'/View/'.get_default_theme($module).'/'.$pathdir.'Takeout'.'/'.str_replace('shops_index_',"",$list_tpl).'.jpg');
		$list_tpll = str_replace('shops_index_',"",$list_tpl).'模板';
		if($list_tpl =="shop_index_list") $list_tpll='默认模板';
		$this->assign('list_tpl',$list_tpl);
		$this->assign('list_tpll',$list_tpll);
		$jumpurl = geturl('Mobile/takeout/shops_index',array('id'=>1),true,true);$jumpurl=str_replace('index.php?s=/','',$jumpurl);
		$this->assign('jumpqrcode',geturl('Home/Qrcode/index',array('data'=>urlencode($jumpurl))));
		$this->assign('jumpurl', $jumpurl);
		$postdata=I('get.');
		if($postdata) $config['LIST_TPL']= $postdata['id'];
		if ($config && is_array($config)) {
				$vi=array();
				$this->model = D('Config');
				foreach ($config as $name => $value) {
					$map = array('name' => $name);
					if(is_array($value)){
						foreach($value as $key=>$val){
							$vi[]=implode(":",$val);
						}$value = implode(",",$vi);
					 }
					 $this->model->where($map)->setField('value', $value);
				}
			S('DB_CONFIG_DATA', null);
			$this->success('应用成功', U('list_index'));exit;
		}
        $this->display();
	}
}