<?php
/**
 * 系统设置控制器
 * @Copyright (C) 2017 汉朝 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author dongziyi <2066207038@qq.com>
 */
namespace Admin\Controller;
class ConfigController extends BaseController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->model = D('Config');
        $this->device='';
    }

    /**
     * 添加新设置
     */
    public function add()
    {
        if (IS_POST) {
            $data = $this->model->create();
            if ($data) {
                if ($this->model->add()) {
                    S('DB_CONFIG_DATA', null);
                    $this->success('新增成功', U('index'));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($this->model->getError());
            }
        } else {
            $grouplist = strtoarray(C('CONFIG_GROUP_LIST'));
            $devicelist = strtoarray(C('DEVICE_GROUP_LIST'));
            $grouplist[0] = '隐藏';
            $this->assign('TYPE_LIST', strtoarray(C('CONFIG_TYPE_LIST')));
            $this->assign('GROUP_LIST', $grouplist);
            $this->assign('DEVICE_LIST', $devicelist);
            $this->assign('info', null);
            $this->display('edit');
        }
    }

    /**
     * 编辑配置
     */
    public function edit($id = 0)
    {
        if (IS_POST) {
            $data = $this->model->create();
            if ($data) {
                if ($this->model->save()) {
                    S('DB_CONFIG_DATA', null);
                    //记录行为
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error($this->model->getError());
            }
        } else {
            $info = array();
            /* 获取数据 */
            $info = $this->model->field(true)->find($id);

            if (false === $info) {
                $this->error('获取配置信息错误');
            }
            $grouplist = strtoarray(C('CONFIG_GROUP_LIST'));
            $devicelist = strtoarray(C('DEVICE_GROUP_LIST'));
           // print_r($devicelist);
            $grouplist[0] = '隐藏';
            $this->assign('TYPE_LIST', strtoarray(C('CONFIG_TYPE_LIST')));
            $this->assign('GROUP_LIST', $grouplist);
            $this->assign('DEVICE_LIST', $devicelist);
            $this->assign($info);
            $this->meta_title = '编辑配置';
            $this->display();
        }
    }

    /**
     * 批量保存配置
     */
    public function save($config)
    {
        if ($config && is_array($config)) {
            $vi=array();
            foreach ($config as $name => $value) {
                $map = array('name' => $name);
                if (str_exists($name, 'UPLOAD_') || str_exists($name, 'THUMB_')) $configupload[$name] = $value;
                if(is_array($value)){
                    foreach($value as $key=>$val){
                         $vi[]=is_array($val)?implode(":",$val):$val;
                    }
                    $value = implode(",",$vi);
                }
                $this->model->where($map)->setField('value', $value);

                //1204新加
                // if($name=='SENSITIVE_WORDS'){
                //     $im= new \Common\Vendor\Webim;
                //     $usersig=$im->getusersig(C('USERSIG'));//获取用户sig
                //     $im->set_user_sig($usersig['info']);//设置当前用户sig
                //     $info = $im->openim_dirty_words($value);
                // }
                //end
            }
            if ($configupload) extentionconfig($configupload);
        }
        if ($config['MEMBER_UC'] && $config['MEMBER_UCONFIG']) {//更新ucenter配置
            $con = "<?php\n" . $config['MEMBER_UCONFIG'] . ";\n?>";
            oaofile(APP_PATH . 'Home/Conf/uc.php', $con);
        }
        S('DB_CONFIG_DATA', null);
        $this->success('保存成功！');
    }

    public function index()
    {
        /* 查询条件初始化 */
        $map = array();
        $getdata = I('param.');
        //$map  = array('status' => 1);
        if (!empty($getdata['group'])) $map['group'] = $getdata['group'];
        if (!empty($getdata['device'])) $map['device'] = $getdata['device'];
        if (!empty($getdata['name'])) {
            $whereor['name'] = array('like', '%' . $getdata['name'] . '%');
            $whereor['title'] = array('like', '%' . $getdata['name'] . '%');
            $whereor['value'] = array('like', '%' . $getdata['name'] . '%');
            $whereor['extra'] = array('like', '%' . $getdata['name'] . '%');
            $whereor['remark'] = array('like', '%' . $getdata['name'] . '%');
            $whereor['_logic'] = 'or';
            $map['_complex'] = $whereor;
        }
        $list = $this->lists('Config', $map);
        $this->assign('group_id', I('get.group', 0));
        $this->assign('list', $list);
        $this->assign($getdata);
        $this->display();

    }

    /**
     * 获取某个标签的配置参数
     */
    public function group()
    {
        if($this->device>0){
            $divicelist = strtoarray(C('DEVICE_GROUP_LIST'));
            $grouplist[1] = $divicelist[$this->device];
        }else{
            $grouplist = strtoarray(C('CONFIG_GROUP_LIST'));
        }
		
		$map = array();
		$map['status'] = 1;
		$tpl_list = M('TplReceipt')->where($map)->field(true)->select();
		$this->assign('tpl_list',$tpl_list);
		
        $map=array();
        $map['status']=1;
        $map['device']=$this->device;
        $map['group']=array("gt",0);
        $list = $this->model->where($map)->field(true)->order('sort')->select();
        if ($list) {
            $sonlist=array();
            foreach($list as $k=> $v){
                if(!empty($v['pid'])){$sonlist[$v['pid']][]=$v;unset($list[$k]);}
            }
            $this->assign('sonlist', $sonlist);
            $this->assign('list', $list);
        }
		
		// echo "<pre>";print_r($list);exit;
        $this->assign('grouplist', $grouplist);
		$this->assign('type', I('type'));
        $this->display("group");
    }
    public function _empty(){
        $this->device=intval(str_replace('sitting','',ACTION_NAME));
        $this->group();
    }
	public function tpl_receipt(){
		$id = I('id');
		if(empty($id)){
			$id = 1;
		}
		$map = array();
		$map['status'] = 1;
		$list = M('TplReceipt')->where($map)->field(true)->select();
		$this->assign('list',$list);
		
		$map = array();
		$map['id'] = $id;
		$map['status'] = 1;
		$info = M('TplReceipt')->where($map)->field(true)->find();
		$set = json_decode($info['set_info'],true);
		if(empty($set['shopname'])){
			$set['shopname'] = '汉潮小店';
		}
		if(empty($set['type_id'])){
			$set['type_id'] = 1;
		}
		if(empty($set['temp'])){
			$set['temp'] = '欢迎下次光临！';
		}
        if(empty($set['description'])){
            $set['description'] = '扫码关注微信';
        }
		if(C('WEIXIN_PUBLIC_NUMBER_URL')){
            $url=C('WEIXIN_PUBLIC_NUMBER_URL');
            $qrcode_url=U('Home/Qrcode/index',array('data'=>urlencode($url)),true,true);
        }else{
            /*$url='http://weixin.qq.com/r/a0xyan-EniLhrTsJ9xl5';
            $qrcode_url=U('Home/Qrcode/index',array('data'=>urlencode($url)),true,true);*/
            $wx = new \Common\Vendor\Weixinopen;
            $result=$wx->get_authorizer_info();
            //echo "<pre>";
            if(!empty($result['authorizer_info']['user_name'])){
                $qrcode=$wx->get_wxqrcode($result['authorizer_info']['user_name']);
                if(!empty($qrcode) && str_exists($qrcode,'weixin.qq.com'))$url=$qrcode;
                $qrcode_url=U('Home/Qrcode/index',array('data'=>urlencode($url)),true,true);
                if(C('WEIXIN_PUBLIC_NUMBER_URL') != $url && strlen($url)<=80){
                    $map = $save = array();
                    $map['name'] = 'WEIXIN_PUBLIC_NUMBER_URL';
                    $save['value'] = $url;
                    M('Config')->where($map)->save($save);
                }
            }
        }
		$this->assign('is_select',$info['is_select']);
        $this->assign('qrcode_url',$qrcode_url);
		$this->assign('set',$set);
		$this->assign('id',$id);
		$this->display();
    }
	public function sub_tpl(){
		$return = array();
		$postdata = I('param.');
		$id = $postdata['id'];
		
		$data = $set = array();$set_info = '';
		if(isset($id)){
			if(isset($postdata['is_show']) && $postdata['is_show'] == 1){
				$set['is_show'] = 1;
			}else{
				$set['is_show'] = 0;
			}
			if(empty($postdata['shopname'])){
				$set['shopname'] = '汉潮小店';
			}else{
				$set['shopname'] = $postdata['shopname'];
			}
			if(empty($postdata['type_id'])){
				$set['type_id'] = 1;
			}else{
				$set['type_id'] = $postdata['type_id'];
			}
			if(empty($postdata['temp'])){
				$set['temp'] = '欢迎下次光临！';
			}else{
				$set['temp'] = $postdata['temp'];
			}
            if(isset($postdata['is_yue']) && $postdata['is_yue'] == 1){
                $set['is_yue'] = 1;
            }else{
                $set['is_yue'] = 0;
            }
            if(empty($postdata['description'])) {
                $set['description'] = '扫码关注微信';
            }else{
                $set['description'] = $postdata['description'];
            }
			$set_info = json_encode($set);
			
			$data['set_info'] = $set_info;
			$data['status'] = 1;
			
			$map = array();
			$map['id'] = $id;
			$map['status'] = 1;
			$save = M('TplReceipt')->where($map)->save($data);
			
			if($save > 0){
				$return['status'] = 1;
				$return['info'] = '编辑成功';
			}else{
				$return['status'] = 0;
				$return['info'] = '编辑失败或没有任何修改';
			}
		}else{
			$return['status'] = 0;
            $return['info'] = '参数获取错误！';
		}
		$this->ajaxReturn($return);
	}
	public function select_tpl(){
		$return = array();
		$id = I('id');
		
		if($id){
			$data = array();
			$data['is_select'] = 0;
			
			$map = array();
			$map['id'] = array('neq',$id);
			$map['status'] = 1;
			$is_save = M('TplReceipt')->where($map)->save($data);
			
			$data = array();
			$data['is_select'] = 1;
			
			$map = array();
			$map['id'] = $id;
			$map['status'] = 1;
			$save = M('TplReceipt')->where($map)->save($data);
			
			if($is_save && $save){
				$return['status'] = 1;
				$return['info'] = '操作成功';
			}else{
				$return['status'] = 0;
				$return['info'] = '操作失败或没有任何修改';
			}
		}else{
			$return['status'] = 0;
            $return['info'] = '参数获取错误！';
		}
		$this->ajaxReturn($return);
	}
}

?>