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

    /**
     * 签到
     */
    public function index(){
		$map = array();$map['name'] = 'SIGN_TYPE';
		$sign_type = M('Config')->where($map)->getField('value');
		$map = array();$map['name'] = 'LOGIN_GAIN_SELL_INTEGRAL';
		$every_day = M('Config')->where($map)->getField('value');
		$map = array();$map['name'] = 'SIGN_INFO';
		$sign_info = M('Config')->where($map)->getField('value');
		$sign_info = json_decode($sign_info,true);
		$map = array();$map['name'] = 'SIGN_RULE';
		$sign_rule = M('Config')->where($map)->getField('value');
		$this->assign('sign_info',$sign_info);
		$this->assign('sign_type',$sign_type);
		$this->assign('every_day',$every_day);
		$this->assign('sign_rule',$sign_rule);
		$this->get_coupons();
		$this->display();
    }
	/* 得到所属组 */
    private function get_coupons(){
		$c_map = array();$new_time = NOW_TIME;
		$c_map['coupons_type'] = 1;
		$c_map['buy_type'] = 0;
		$c_map['status'] = 1;
		$c_map['quantity'] = array('GT',0);
		$c_map['_string'] = "end_timestamp >".$new_time." OR end_timestamp = '0'";
		//卡券
		$coupons = M('Coupons')->where($c_map)->field(true)->select();
        $this->assign('coupons', $coupons);
    }
    /**
     * 编辑签到
     */
    public function edit(){
        if (IS_POST) {
			$post = I('param.');
            if ($post) {
				if(C('SIGN_TYPE') != $post['sign_type']){
					$map = array();$map['name'] = 'SIGN_TYPE';
					$save = array();$save['value'] = $post['sign_type'];
					$ret = M('Config')->where($map)->save($save);
				}
				
				$map = array();$map['name'] = 'LOGIN_GAIN_SELL_INTEGRAL';
				$save = array();$save['value'] = $post['every_day'];
				$result = M('Config')->where($map)->save($save);
				
				$data = array();
				$data['gift_day'] = $post['gift_day'];
				$data['gift_type'] = $post['gift_type'];
				if($post['gift_type'] == 4){$data['gift_num'] = $post['q_id'];}
				else{$data['gift_num'] = $post['gift_num'];}
				$map = array();$map['name'] = 'SIGN_INFO';
				$save = array();$save['value'] = json_encode($data);
				$return = M('Config')->where($map)->save($save);
				
				
				$map = array();$map['name'] = 'SIGN_RULE';
				$save = array();$save['value'] = $post['sign_rule'];
				$res = M('Config')->where($map)->save($save);
				S('DB_CONFIG_DATA',null);
				$url = U('Admin/Sign/index');
				
				$this->success('编辑成功',$url);
            } else {
                $this->error('数据不能为空！');
            }
        }
    }
}

?>