<?php
namespace Active\Src;

use Dever;
use Agent\Lib\Core;

Class Api extends Core {
	#活动详情
	public function index(){
		$id = Dever::input('id');
		$data = Dever::db('active/info')->find($id);
		$data['banner'] = explode(',', $data['banner']);
		$member = Dever::db('agent/member')->find($this->uid);
		$data['member_name'] = $member['name'];
		$data['member_mobile'] = $member['mobile'];
		return $data;
	}
	#确认信息
	public function confirm(){
		$id = Dever::input('id');
		if(!$id){
			Dever::alert('活动id不能为空');
		}
		$num = Dever::input('num');
		if(!$num){
			Dever::alert('购买数量不能为空');
		}
		$remark = Dever::input('remark');
		$data = Dever::db('active/info')->find($id);
		$insert['active_id'] = $id;
		$insert['mid'] = $this->uid;
		$insert['num'] = $num;
		$insert['price'] = $num * $data['price'];
		if($remark){
			$insert['remark'] = $remark;
		}
		$data = Dever::db('active/order')->getNum(array('mid'=>$this->uid,'active_id'=>$id));
		// if($data && $data['total'] && $data['tatal'] > $data['num']){
		// 	Dever::alert('您已超过限购数量');
		// }
		$insert['order_num'] = $this->getOrderId();
		$ids = Dever::db('active/order')->insert($insert);

		$url = Dever::url();
		$refer = 'https://www.churenyiliao.com/';
		if (strstr($url, 'uat.')) {
			$refer = 'http://guanli.uat.churenyiliao.com/';
		}
		$refer .= 'data/payback/active.html?order_id=' . $ids;
		if($ids){
			$info = Dever::db('active/order')->find($ids);
			$active = Dever::db('active/info')->find($info['active_id']);
			$param = array
	        (
	            'project_id' => 6,
	            'channel_id' => 1,
	            'system_source' => 1,
	            'account_id' => 8,
	            'uid' => $this->uid,
	            'name' => $active['name'],
	            //'openid' => $data['openid'],
	            'cash' => $info['price'],
	            'product_id' => $ids,
	            'order_id' => $info['order_num'],
	            'h5' => 1,
	            'refer' => $refer,
	        );

	        $result = Dever::load('pay/api.pay', $param);
	        if($result['order']){
	        	Dever::db('active/order')->update(array('where_id'=>$ids,'url'=>$result['order']));
	        }
	        $result['order_num'] = $info['order_num'];
	        $result['order_id'] = $info['id'];
	        $jump = Dever::input('jump');
	        if ($jump == 1) {
	        	Dever::location($result['order']);
	        }

	        return $result;
		}else{
			Dever::alert('提交失败');
		}
	}

	 # 支付成功回调 安全加密 设置token
    public function success_secure_api_token()
    {
        $project_id = Dever::input('pay_project_id');
        $info = Dever::db('pay/project')->one($project_id);
        if ($info) {
            return $info['key'];
        }
        return 'agent_buy_dever_2020';
    }

    # 支付成功回调 安全加密
    public function success_secure_api($param = array())
    {
        $this->success($param);
    }

    # 支付成功回调
    public function success($param = array())
    {
        $send = $param ? $param : Dever::preInput('pay_');
        $product_id = $send['pay_product_id'];
        $order_id = $send['pay_order_id'];
        $status = $send['pay_status'];
        $cash = $send['pay_cash'];
        $msg = $send['pay_msg'];

        $order = Dever::db('active/order')->one(array('id' => $product_id, 'clear' => true));
        if ($order && $status == 2) {
            $update['status'] = 2;
            $update['where_id'] = $order['id'];
            Dever::db('active/order')->update($update);
        }

        return 'ok';
    }
    #再次发起支付
    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']>300){
    		Dever::alert('订单已关闭');
    	}
        $jump = Dever::input('jump');
        if ($jump == 1) {
        	Dever::location($order['url']);
        }
        return $order;
    }
    
	#门票列表
	public function getList(){
		$data['list'] = Dever::db('active/order')->getAll(array('mid'=>$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'];
			$data['list'][$k]['cdate'] = date('Y-m-d H:i',$v['cdate']);
			$config = Dever::db('active/order')->config['config_status'];
			$data['list'][$k]['status_name'] = Dever::status($config,$v['status']);
		}
		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['info'] = Dever::db('active/order')->find($id);
		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']['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'];
			}
			$data['info']['mobile'] = $active['mobile'];
		} else {
			Dever::alert('订单不存在');
		}
		return $data;
	}

	#生成订单号
    public function getOrderId()
    {
        $where['order_num'] = Dever::order('D');
        $where['clear'] = true;
        $state = Dever::db('active/order')->one($where);
        if (!$state) {
            return $where['order_num'];
        } else {
            return $this->getOrderId();
        }
    }
}