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

Class User extends Core
{

    #再次发起支付
    public function rpay(){
        $id = Dever::input('id');#订单的id
        $order = Dever::db('active/order')->find($id);

        if (!$order) {
            Dever::alert('订单信息错误');
        }
        if ($order['status'] > 1) {
            Dever::alert('订单已支付或已取消');
        }
        $time = time();
        if ($time-$order['cdate']>900){
            $w['where_id'] = $id;
            $w['status'] = 3;
            Dever::db('active/order')->update($w);
            Dever::alert('订单已关闭');
        }
        $info = Dever::db('active/order')->getNum(array('uid'=>$this->uid,'active_id'=>$order['active_id']));
        $active = Dever::db('active/info')->find($order['active_id']);
        if ($info['total'] >= $active['num']) {
            Dever::alert('您已超过限购数量');
        }
        $jump = Dever::input('jump');
        if ($jump == 1) {
            Dever::location($order['url']);
        }
        return $order;
    }
	 #领取
    // public function recive()
    // {
    // 	$user = $this->user;
    //     $active_id = Dever::input('active_id');
    //     $order_id = Dever::input('id');
    //     $info = Dever::db('active/info')->find($active_id);     
    //     if ($info['status'] == 2) {
    //         $member = Dever::load('active/lib/user')->agent($user);
    //         if (!$member) {
    //             Dever::alert('仅限代理商领取');
    //         }
    //     }
    //     $code = Dever::db('active/code')->state(array('order_id'=>$order_id));
    //     $data = Dever::db('active/order')->find($order_id);
    //     if ($data['status'] < 2) {
    //         Dever::alert('订单还没有支付');
    //     }
    //     if ($data['num'] <= count($code)) {
    //         Dever::alert('已领完');
    //     } else {
    //         $where['active_id'] = $data['active_id'];
    //         $where['buy_uid'] = $data['uid'];
    //         $where['join_uid'] = $this->uid;
    //         $where['order_id'] = $order_id;
    //         $where['act_status'] = $info['act_status'];
    //         $code = Dever::db('active/code')->find($where);
    //         if ($code) {
    //             Dever::alert('您已领取过了，不可重复领取');
    //         }
    //         $where['write_code'] = substr(base_convert(md5(uniqid(md5(microtime(true)),true)), 16, 10), 0, 6);
    //         $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) {
    //             $data = Dever::db('active/code')->find($ids);
    //             return $data;
    //         }
    //     }
    // }
    #查看核销码
    public function getSeeCode()
    {
    	$where['join_uid'] = $this->uid;
    	$where['order_id'] =  Dever::input('id');
    	$where['active_id'] = Dever::input('active_id');
    	$data = Dever::db('active/code')->find($where);
    	if ($data) {
    		$string = $data['write_code'];
    		$pic = Dever::load('qrcode/api.qrcode', $string, $local = true, $logo = false, $uid = false);
    		$where['where_id'] = $data['id'];
    		$where['pic'] = Dever::url($pic['url']);
    		$ids = Dever::db('active/code')->update($where);
    		$data = Dever::db('active/code')->find($ids);
    		return $data;
    	} else {
    		Dever::alert('您还没有生成核销码');
    	}
    }
    #我的核销码
    public function getMyCode()
    {
    	$uid = $this->uid;
    	$data['info'] = Dever::db('active/code')->getAll(array('join_uid'=>$uid));
    	foreach ($data['info'] as $k => $v) {
    		$active = Dever::db('active/info')->find($v['active_id']);
    		$data['info'][$k]['active_name'] = $active['name'];
    		$data['info'][$k]['cdate'] = date('Y-m-d H:i',$v['cdate']);
    		if ($v['status'] == 1) {
    			$data['info'][$k]['status_name'] = '待核销';
    		} else {
    			$data['info'][$k]['status_name'] = '已核销';
    		}
            if ($v['type'] == 1) {
                if ($v['join_uid'] == $v['buy_uid']) {
                    $data['info'][$k]['type_name'] = '来自自行购买订单';
                } else {
                    $data['info'][$k]['type_name'] = '来自好友分享领取订单';
                }
            } elseif ($v['type'] == 2) {
                $data['info'][$k]['type_name'] = '来自手动发放';
            }
    	}
    	return $data;
    }
    #我的订单列表
	public function getList()
	{
		$data['list'] = Dever::db('active/order')->getAll(array('uid'=>$this->uid,'status'=>3));
		foreach($data['list'] as $k => $v){
			$active = Dever::db('active/info')->find($v['active_id']);
			$data['list'][$k]['active_name'] = $active['name'];
			
			$config = Dever::db('active/order')->config['config_status'];
			$data['list'][$k]['status_name'] = Dever::status($config,$v['status']);
			$data['list'][$k]['total_price'] = $v['num'] * $v['price'];
			$data['list'][$k]['pay_time'] = '';
            if ($active['mobile']) {
                $data['list'][$k]['mobile'] = $active['mobile'];
            } else {
                $config = Dever::db('deploy/base-one');
                $data['list'][$k]['mobile'] = $config['phone'];
            }
            
            $data['list'][$k]['time'] = '';
            if ($v['status'] == 1) {
                $time = time() - $v['cdate'];
                if ($time >= 900){
                    $data['list'][$k]['status'] = 3;
                    Dever::db('active/order')->update(array('where_id'=>$v['id'],'status'=>3));
                    $data['list'][$k]['time'] = 0;
                } else{
                    $data['list'][$k]['time'] = 900 - $time;
                }
            }
            $data['list'][$k]['cdate'] = date('Y.m.d H:i',$v['cdate']);
		}
		return $data;
	}
    #订单详情页
    public function getDesc()
	{
		$id = Dever::input('id');
		if (!$id) {
			Dever::alert('订单id不能为空');
		}
		$user = $this->user;
		$data['user']['name'] = $user['name'];
		$data['user']['mobile'] = $user['mobile']; 
		$data['user']['idcard'] = $user['idcard'];
		$data['info'] = Dever::db('active/order')->find(array('id' => $id, 'uid' => $this->uid));
		if ($data['info']) {
			$active = Dever::db('active/info')->find($data['info']['active_id']);
			$data['info']['active_name'] = $active['name'];
			$data['info']['cash'] = $active['price'];#单价
            $data['info']['time'] = '';
            if ($data['info']['status'] == 1) {
                $time = time() - $data['info']['cdate'];
                if ($time >= 900){
                    $data['info']['status'] = 3;
                    Dever::db('active/order')->update(array('where_id'=>$id,'status'=>3));
                    $data['info']['time'] = 0;
                } else{
                    $data['info']['time'] = 900 - $time;
                }
            }
			$data['info']['cdate'] = date('Y-m-d H:i',$data['info']['cdate']);
			$config = Dever::db('active/order')->config['config_status'];
			$data['info']['status_name'] = Dever::status($config,$data['info']['status']);
			$data['info']['refund_cash'] = '';#全额退款金额
			if ($data['info']['status'] == 6) {
				$data['info']['refund_cash'] = $data['info']['price'];
			}
            if ($data['info']['rdate']) {
                $data['info']['rdate'] = date('Y-m-d H:i',$data['info']['rdate']);
            }
			$data['info']['share_name'] = '';
            $u_name = '';
			if ($active['share_name']) {
                if (strstr($active['share_name'],'{member}')){
                    $u_name = str_replace('{member}',$user['name'],$active['share_name']);   
                }
                if ($u_name && strstr($u_name,'{active}')) {
                    $u_name = str_replace('{active}',$active['name'],$u_name);
                }
                if (!$u_name && strstr($active['share_name'],'{active}')) {
                    $u_name = str_replace('{active}',$active['name'],$active['share_name']);
                }
                if (!$u_name && $active['share_name']) {
                    $u_name = $active['share_name'];
                }
				$data['info']['share_name'] = $u_name;
			}
            if ($active['mobile']) {
                $data['info']['mobile'] = $active['mobile'];
            } else {
                $config = Dever::db('deploy/base-one');
                $data['info']['mobile'] = $config['phone'];
            }
			
            $data['info']['idcard'] = $data['user']['idcard'];
            $data['info']['field'] = array();
            if ($data['info']['path']) {
                $data['info']['field'] = json_decode($data['info']['path']);
                foreach ($data['info']['field'] as $k => $v) {
                    $data['info']['field'][$k] = (array)$v;
                }
                foreach ($data['info']['field'] as $k => $v) {
                   $field = Dever::db('active/info_field')->find($v['id']);
                   if ($field) {
                        if ($field['type'] == 1){
                            $data['info']['field'][$k]['name'] = $field['name'];
                        } elseif($field['type'] == 2) {
                            $data['info']['field'][$k]['name'] = $field['name'] ;
                            $field_value = Dever::db('active/info_field_value')->find($v['value']);
                            if ($field_value) {
                                $data['info']['field'][$k]['value'] = $field_value['name'];
                            }
                        }
                   }
                } 
            }
			#使用核销码
			$code = Dever::db('active/code')->getCode(array('order_id'=>$id,'status'=>'1,2'));
            if (!$data['info']['refund_num']){
                $data['info']['refund_num'] = 0;
            }
			$no_code = $data['info']['num']-count($code)-$data['info']['refund_num'];
			if ($data['info']['status'] == 2) {
				$data['info']['use_code'] = count($code);
				$data['info']['no_code'] = $no_code;
				if ($code) {
					foreach ($code as $k => $v) {
						if ($v['status'] == 1) {
                            $data['info']['code'][$k]['status'] = 1;
							$data['info']['code'][$k]['status_name'] = '待核销';
						} elseif ($v['status'] == 2) {
                            $data['info']['code'][$k]['status'] = 2;
							$data['info']['code'][$k]['status_name'] = '已核销';
						} else {
                            $data['info']['code'][$k]['status'] = 3;
                            $data['info']['code'][$k]['status_name'] = '已作废';
                        }
						$user = Dever::db('active/user')->find($v['join_uid']);
						$data['info']['code'][$k]['name'] = $user['name'];
						$data['info']['code'][$k]['mobile'] = substr($user['mobile'], 0, 3).'****'.substr($user['mobile'], 7);
					}
				}
			} else {
				$data['info']['use_code'] = '';
				$data['info']['no_code'] = '';
			}
			
		} else {
			Dever::alert('订单不存在');
		}
		return $data;
	}
    #我的页面
    public function getMy()
    {
    	$data['user'] = $this->user;
    	if ($data['user']) {
    		$data['user']['role_name'] = '';
    		$data['user']['title_name'] = '';
            if ($data['user']['birthday']) {
                $data['user']['birthday'] = date('Y-m-d',$data['user']['birthday']);
            }
            $data['user']['mid'] = -1;
            $member = Dever::load('active/lib/user')->agent($data['user']);
            $config = Dever::db('deploy/base-one');
    		if ($member) {
    			$data['user']['mid'] = $member['id'];
    			$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']) {
    					$data['user']['role_name'] = $role['name'] .'('.$level['name'].')';
    				} else {
    					$data['user']['role_name'] = $role['name'];
    				}
    			}
    			if ($title  && $title['name']) {
    				$data['user']['title_name'] = $title['name'];
    			}

    			if (!$data['user']['avatar']) {
    				$data['user']['avatar'] = $member['avatar'];
    			} else {
                    $data['user']['avatar'] = $config['avatar'];
                }
    		}
    		
    	} else {
    		$data['user']['name'] = '匿名用户';
            $data['user']['avatar'] = $config['avatar'];
    	}
    	$data['config']['phone'] = $config['phone'];
    	return $data;
    }
    #修改密码
    public function setPass()
    {
    	$pass = Dever::input('pass');
        if (!$pass) {
            Dever::alert('请输入密码');
        }

        $where['where_id'] = $this->uid;
        $where['pass'] = $pass;

        Dever::db('active/user')->update($where);

        return Dever::db('active/user')->find(array('id' => $this->uid, 'clear' => true));

    }
    #修改资料
    public function setInfo()
    {
    	$where = array();
        $avatar = Dever::input('avatar');
        if ($avatar) {
            $where['avatar'] = $avatar;
        }
        $username = Dever::input('username');
        if ($username) {
            $where['username'] = $username;
        }
        $birthday = Dever::input('birthday');
        if ($birthday) {
            $where['birthday'] = Dever::maketime($birthday);
        }

        if ($where) {
            $where['where_id'] = $this->uid;
            Dever::db('active/user')->update($where);
            return Dever::db('active/user')->find(array('id' => $this->uid, 'clear' => true));
        }
    }

    #取消订单
	public function cancel()
	{
		$order_id = Dever::input('id');
		$data = Dever::db('active/order')->find(array('uid'=>$this->uid,'id'=>$order_id));
		if ($data) {
			$where['where_id'] = $data['id'];
			$where['status'] = 3;
			$ids = Dever::db('active/order')->update($where);
			if ($ids) {
				
				return 'ok';
			} else {
				Dever::alert('取消失败');
			}
		} else {
			Dever::alert('此订单不存在');
		}
	}
}