<?php
namespace Active\Src;

use Dever;
use Active\Lib\Coreoff;

Class Off extends Coreoff
{
	#核销首页
	public function getHome()
	{
		$data['user'] = $this->user;
		$data['info'] = Dever::db('active/info')->getAll(array('off_id'=>$data['user']['id']));
		foreach ($data['info'] as $k => $v) {
			$data['info'][$k]['act_time'] = date('Y-m-d H:i',$v['act_start']).'~'.date('Y-m-d H:i',$v['act_end']);
			unset($data['info'][$k]['sign_end'],$data['info'][$k]['act_start'],$data['info'][$k]['act_end']);
		}
		return $data;
	}

	#获取核销信息
	public function getInfo(){
		$code = Dever::input('code');
		if (!$code) {
			Dever::alert('手机号或核销码不存在');
		}
		$where['active_id'] = Dever::input('active_id');
		if (!$where['active_id']) {
			Dever::alert('活动id不存在');
		}
		$count = strlen($code);
		$res = array();
		if ($code) {
			$data = Dever::db('active/code')->find(array('write_code'=>$code));
			if (!$data) {
				if (strlen($code) != 11){
					Dever::alert('手机号或核销码不正确');
				} else {
					$tell = preg_match("/^1[3456789]\d{9}$/",$code);
					if ($tell) {
						$user = Dever::db('active/user')->find(array('mobile'=>$code));
						if ($user) {
							$data = Dever::db('active/code')->find(array('join_uid'=>$user['id'],'status'=> 1));
							if (!$data) {
								Dever::alert('手机号或核销码不正确或已核销');
							}
						} else {
							Dever::alert('手机号或核销码不正确');
						}
					}
				}
			}
			if ($data && ($data['active_id'] != $where['active_id'])) {
					Dever::alert('该核销码不属于此活动');
			}
		}
		$active = Dever::db('active/info')->find($where['active_id']);
		if ($data) {
			$user = Dever::db('active/user')->find($data['join_uid']);
			$res['info']['code_id'] = $data['id'];
			$res['info']['write_code'] = $data['write_code'];
			$res['info']['active_name'] = $active['name'];
			$res['info']['name'] = $user['name'];
			$res['info']['mobile'] = $user['mobile'];
			$parent_user = Dever::db('active/user')->find($data['buy_uid']);
			$res['info']['parent_name'] = $parent_user['name'];
			$res['info']['parent_mobile'] = $parent_user['mobile'];
			$res['info']['role_name'] = '';
			$res['info']['title_name'] = '';
			$member = Dever::load('active/lib/user')->agent($user);
			if ($member) {
				$role = Dever::db('setting/role')->find($member['role']);
				$title = Dever::db('setting/title')->find($member['title_id']);
				$level = Dever::db('setting/level')->find($member['level_id']);
				if ($role) {
    				if (isset($level) && $level && isset($level['name']) && $level['name']){
    					$res['info']['role_name'] = $role['name'] .'('.$level['name'].')';
    				} else {
    					$res['info']['role_name'] = $role['name'];
    				}
    			}
    			if ($title  && $title['name']) {
    				$res['info']['title_name'] = $title['name'];
    			}
    			if ($member['avatar']) {
    				$res['info']['avatar'] = $member['avatar'];
    			}
			}
			if ($user['avatar']) {
				$res['info']['avatar'] = $user['avatar'];
			} else {
				$config = Dever::db('deploy/base-one');
				$res['info']['avatar'] = $config['avatar'];
			}
		} else {
			Dever::alert('手机号不正确或核销码已核销');
		}
		return $res;
	}
	#确认核销
	public function confirm()
	{
		$code_id = Dever::input('code_id');
		if (!$code_id) {
			Dever::alert('核销码的id不能为空');
		}
		$data = Dever::db('active/code')->find($code_id);
		if ($data) {
			$active = Dever::db('active/info')->find($data['active_id']);
			// if (time() < $active['act_start']){
			// 	Dever::alert('活动未开始');
			// }
			if ($active['act_status'] == 5 || time() >= $active['act_end']) {
				Dever::alert('活动已结束');
			}
			if ($data['status'] == 2) {
				Dever::alert('当前核销码已核销');
			}
			$where['where_id'] = $code_id;
			$where['status'] = 2;
			$where['time'] = time();
			$where['top_admin'] = $this->uid;
			$ids = Dever::db('active/code')->update($where);
			if ($ids) {
				return $data;
			} else {
				Dever::alert('核销失败');
			}
		} else {
			Dever::alert('核销码的信息不存在');
		}
	}
}