<?php
namespace Active\Src;
use Dever;
use Active\Lib\Core;

Class Home extends Core
{
	protected $check = false;
	#活动首页
	public function get()
	{
		$data['info'] = Dever::db('active/info')->getAll(array('state'=>1));
		$config = Dever::db('active/info')->config['config_act_status'];
		foreach($data['info'] as $k=>$v){
			if (isset($v['cover']) && $v['cover']) {
				$data['info'][$k]['banner'] = $v['cover'];
			} else {
				$data['info'][$k]['banner'] = $v['banner'];
			}
			$data['info'][$k]['act_status_name'] = Dever::status($config,$v['act_status']);
			if ($v['sign_start']) {
                $ids = Dever::load('active/lib/cron')->start($v['cdate'],$v['act_start'],$v['act_end'],$v['sign_start'],$v['sign_end']);
            } else {
               $ids = Dever::load('active/lib/cron')->start($v['cdate'],$v['act_start'],$v['act_end']);
            }
            if (isset($ids) && $ids) {
            	$where['where_id'] = $v['id'];
                $where['act_status'] = $ids['act_status'];
                Dever::db('active/info')->update($where);
            }
			if ($v['act_status'] == 2) {
				if ($v['sign_end']) {
					$day = floor(($v['sign_end'] - strtotime(date('Y-m-d').'23:59:59'))/86400);
					if ($day < 0){
						$data['info'][$k]['day'] = '';
					} else {
						$day +=1;
						$data['info'][$k]['day'] = '剩余'.$day.'天';
					}
					
				} else {
					$day = floor(($v['act_start'] - strtotime(date('Y-m-d').'23:59:59'))/86400);
					if ($day > 0) {
						$data['info'][$k]['day'] = '剩余'.$day.'天';
					} else {
						$data['info'][$k]['day'] = '';
					}
					
				}
			}else {
				$data['info'][$k]['day'] = '';
			}
		}
		return $data;
	}

	# 活动报名获取信息
	public function getSign()
	{
		$uid = $this->uid;
		$id = Dever::input('id');
		$this->data['info'] = Dever::db('active/info')->find($id);
		$this->data['info']['banner'] = explode(',', $this->data['info']['banner']);
		$this->data['info']['field'] = Dever::db('active/info_field')->getList(array('active_id'=>$id,'state'=>1));
		foreach($this->data['info']['field'] as $k => $v){
			if ($v['is_must'] == 1) {
				$this->data['info']['field'][$k]['is_must_name'] = '必填项';
			} else {
				$this->data['info']['field'][$k]['is_must_name'] = '选填项';
			}
			if($v['type'] == 2) {
				$this->data['info']['field'][$k]['value_name']= Dever::db('active/info_field_value')->getList(array('active_id'=>$id,'field_id'=>$v['id']));
				
			}else{
				$this->data['info']['field'][$k]['value_name'] = '';
			}
		}
		$this->data['user'] = $this->user;
		$member = Dever::load('active/lib/user')->agent($this->data['user']);
		$this->data['user']['mid'] = -1;
		if ($member) {
			$this->data['user']['mid'] = $member['id'];
		}
		$this->data['user']['content'] = '';
		if ($uid && $uid > 0) {
			if (time() < $this->data['info']['act_end']){
				$count = Dever::db('active/order')->getNum(array('active_id'=>$this->data['info']['id'],'uid'=>$uid,'status'=>'1,2,4,5'));
				$code = Dever::db('active/code')->getNum(array('active_id'=>$this->data['info']['id'],'buy_uid'=>$uid,'status'=>'1,2'));
				if ($count['total']  && $count['total'] > 0){
					
					$no_num = $count['total'] - $code;
					if ($no_num < 0) {
						$no_num = 0;					
					}
					$this->data['user']['content'] = '已购核销码'.$count['total'].'张，未使用'.$no_num.'张';
				}
			}
			
		}
		return $this->data;
	}

	# 活动报名页
	public function upSign_commit()
	{
		$data['name'] = Dever::input('name');
		$data['mobile'] = Dever::input('mobile');
		$data['idcard'] = Dever::input('idcard');

		$uid = $this->uid;
		if ($this->uid <= 0) {
			if (!$data['mobile']) {
				Dever::alert('手机号不存在');
			}
			if (!$data['name']) {
				Dever::alert('姓名不存在');
			}
			Dever::load('active/lib/manage')->checkMcode($data['mobile']);
			$user = Dever::db('active/user')->find(array('mobile'=>$data['mobile']));
			if ($user) {
				$uid = $user['id'];
			} else {
				# 注册
				$uid = Dever::load('active/lib/user')->upUser($data['mobile'],$data);
			}
		}
		if ($uid) {
			$user = Dever::db('active/user')->find($uid);
			$member = Dever::load('active/lib/user')->agent($user);
			$where['where_id'] = $uid;
			if ($member) {
				if ($data['name'] != $member['name']) {
					$where['name'] = $member['name'];
				} else {
					$where['name'] = $user['name'];
				}
			} else {
				$where['name'] = $data['name'];
			}
			
			if (!$user['idcard'] && $data['idcard']) {
				$where['idcard'] = $data['idcard'];
			}
			if ($where['name'] || $where['idcard']) {
				Dever::db('active/user')->update($where); 
			}
		}
		$result = array();
		$result = Dever::load('passport/reg')->getSign($uid);

		$id = Dever::input('id');
		if (!$id) {
			Dever::alert('活动id不能为空');
		}
		$num = Dever::input('num');
		if (!$num) {
			Dever::alert('购买数量不能为空');
		}
		$remark = Dever::input('remark');
		$path = Dever::input('path');
		if (!$data['idcard']) {
			$data['idcard'] = '';
		}
		if ($path) {
			$p = Dever::json_decode($path);
			$pa = array();
			if (isset($p) && $p){
				foreach ($p as $k => $v){
					$pa[$v['id']] = $v; 
				}
			}
		}
		$field = Dever::db('active/info_field')->getList(array('active_id'=>$id,'state'=>1));
		if ($field) {
			foreach ($field as $k => $v) {
				if ($v['is_must'] == 1) {
					if (!isset($pa[$v['id']])) {
						Dever::alert($v['name'].'不能为空');
					} elseif (isset($pa[$v['id']]) && !$pa[$v['id']]) {
						Dever::alert($v['name'].'不能为空');
					}
				}
			}
		}
		$result += Dever::load('active/lib/active')->action($uid, $id, $num, $path, $remark,$data['idcard']);
		return $result;
	}

	public function recive_commit()
    {
    	$w['name'] = Dever::input('name');
    	if (!$w['name']){
    		Dever::alert('姓名不存在');
    	}
    	$w['mobile'] = Dever::input('mobile');
    	if (!$w['mobile']){
    		Dever::alert('手机号不存在');
    	}
    	Dever::load('active/lib/manage')->checkMcode($w['mobile']);
    	$user = Dever::db('active/user')->find(array('mobile'=>$w['mobile']));
    	if ($user) {
			$uid = $user['id'];
		} else {
			# 注册
			$uid = Dever::load('active/lib/user')->upUser($w['mobile'],$w);
		}
    	$active_id = Dever::input('active_id');
    	if (!$active_id){
    		Dever::alert('活动id不存在');
    	}
        $order_id = Dever::input('id');
        if (!$order_id){
    		Dever::alert('订单id不存在');
    	}
    	$data['user'] = Dever::load('passport/reg')->getSign($uid);
    	$data['user']['mobile'] = $w['mobile'];#领取人的手机号
		$user = Dever::db('active/user')->find($uid);
       	
        $info = Dever::db('active/info')->find($active_id);
        if ($info['act_status'] == 5 || time() >= $info['act_end']) {
        	Dever::alert('活动已结束');
        }     
        if ($info['status'] == 2) {
            $member = Dever::load('active/lib/user')->agent($user);
            if (!$member) {
                Dever::alert('仅限代理商领取');
            }
        }
        // $code = Dever::db('active/code')->find(array('order_id'=>$order_id,'active_id'=>$active_id,'join_uid'=>$uid));
        // if ($code) {
        // 	Dever::alert('您已领取过了，不可重复领取');
        // }
        $code = Dever::db('active/code')->state(array('order_id'=>$order_id));
        $data['info'] = Dever::db('active/order')->find($order_id);
        if ($data['info']['status'] < 2) {
            Dever::alert('订单还没有支付');
        }
        if ($data['info']['num'] <= count($code)) {
            Dever::alert('已领完');
        } else {
           
            $code = Dever::db('active/code')->getOne(array('active_id'=>$active_id,'join_uid'=>$uid,'status'=>'1,2'));
            if ($code) {
                Dever::alert('您已领取过了，不可重复领取');
            }
            $where['active_id'] = $active_id;
            $where['join_uid'] = $uid;
            $where['buy_uid'] = $data['info']['uid'];
            $where['order_id'] = $order_id;
            $where['act_status'] = $info['act_status'];
            $where['write_code'] = $this->getCode();
            
            // $code = Dever::db('active/code')->find(array('write_code'=>$where['write_code']));
            // if ($code) {
            //     $where['write_code'] = substr(base_convert(md5(uniqid(md5(microtime(true)),true)), 16, 10), 0, 6);
            // }
            $ids = Dever::db('active/code')->insert($where);
            if ($ids) {
            	Dever::load('active/lib/order')->upNum($order_id);
                $data['code'] = Dever::db('active/code')->find($ids);
                return $data;
            }
        }
    }
    #活动核销码
    public function getCode()
    {
        $where['write_code'] = Dever::rand(6, 0);
        $state = Dever::db('active/code')->one($where);
        if (!$state) {
            return $where['write_code'];
        } else {
            return $this->getCode();
        }
    }

    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('当前核销码已核销');
			}
			$admin = Dever::load('manage/auth.info');
			$where['where_id'] = $code_id;
			$where['status'] = 2;
			$where['time'] = time();
			$where['audit_admin'] = $admin['id'];
			$ids = Dever::db('active/code')->update($where);
			if ($ids) {
				$data['msg'] = '核销成功';
				return $data;#'核销成功';
			} else {
				Dever::alert('核销失败');
			}
		} else {
			Dever::alert('核销码的信息不存在');
		}
	}
}