<?php
// +----------------------------------------------------------------------
// | WindSNS 国内领先企业级社交开源SNS框架系统
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2028 http://windsns.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 常州微诺信息科技有限公司 http://www.weinuo.net
// +----------------------------------------------------------------------
// | E-mail: open@weinuo.net
// +----------------------------------------------------------------------
namespace app\index\controller;
use app\common\controller\Frontend;
use think\Config;
use think\Hook;
use think\Db;
use think\Request;
use think\Session;
class Windsnsuseractivities extends Frontend
{
    protected $layout = 'default';
    protected $noNeedLogin = ['login', 'register', 'third'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $auth = $this->auth;

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

        //监听注册登录注销的事件
        Hook::add('user_login_successed', function ($user) use ($auth) {
            $expire = input('post.keeplogin') ? 30 * 86400 : 0;
            Cookie::set('uid', $user->id, $expire);
            Cookie::set('token', $auth->getToken(), $expire);
        });
        Hook::add('user_register_successed', function ($user) use ($auth) {
            Cookie::set('uid', $user->id);
            Cookie::set('token', $auth->getToken());
        });
        Hook::add('user_delete_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
        Hook::add('user_logout_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
    }

    /**
     * 空的请求
     * @param $name
     * @return mixed
     */
    public function _empty($name)
    {
        $data = Hook::listen("user_request_empty", $name);
        foreach ($data as $index => $datum) {
        $this->view->assign($datum);
        }
        return $this->view->fetch('user/' . $name);
    }

	/**
	 * 协议
	 * @author   Devil
	 * @blog     http://www.weinuo.net
	 * @version  0.0.1
	 * @datetime 2020-03-04
	 */
    public function index()
    {
    $this->view->assign('title', __('活动-用户中心'));
    $windsns_userid = $this->auth->getUserinfo()['user_id'];
    
    
	$windsnspaylist = Db::name('windsnsactivity_paylist')->
	alias('a')->join('ws_windsnsactivity_types w','a.windsns_hdfenlei = w.activitytypeid') ->
	where(['user_id'=>$windsns_userid]) -> order('windsnsactivitypayid', 'desc') -> paginate(20); 
	
	
	$windsnspaysum  = Db::name('windsnsactivity_paylist')->where(['user_id' => $windsns_userid])->count();
	$page = $windsnspaylist->render();
	$this->assign('windsnspaylist', $windsnspaylist);
	
	$this->assign('page', $page);
	
	
	 if($windsnspaysum > 0 or $windsnspaysum == 1){
        	return $this->view->fetch('windsns_activitiespaylist');
        }else{
        return $this->view->fetch('errors_windsnsnoinfos');
        }

    }


      public function windsnshdtuichu($windsnsactivitypayid)
    {
        $urlpayid  =  input('get.windsnsactivitypayid'); //获取这个活动订单的ID
        $sqlwindsns = Db::name('windsnsactivity_paylist')->where(['windsnsactivitypayid'=>$urlpayid])->find();
        $sqlhdzhuangtai = $sqlwindsns['windsns_hdzhuangtai'];//获取这个活动订单的活动状态ID
        $sqlhdjiage = $sqlwindsns['windsns_hdjiage'];//获取这个活动订单的活动的价格
        $sqlhduserid = $sqlwindsns['user_id'];//获取这个活动订单的用户id
         
        if($sqlhdzhuangtai == 1){ //如果活动还在进行中，则执行退出操作
           //1、修改这个记录的报名状态，状态：1、成功、2退回、3失败',
            $data['windsns_hdbaomingzhuangtai'] =  2; 
	        $xiugaizhuangtai = Db::name('windsnsactivity_paylist')->where('windsnsactivitypayid', $urlpayid)->update($data);// 
        	//2、返回报名费
        	$sqlusermoney = Db::name('user')->where('id',$sqlhduserid)->setInc('money', $sqlhdjiage);//报名费返还
        	return $this->view->fetch('errors_windsnsout');//退出成功
        }elseif($sqlhdzhuangtai == 2){
        	return $this->view->fetch('errors_windsnsoutno');//退出失败！
        }else{
        	return $this->view->fetch('errors_windsnsoutno');//退出失败！
        }

        
   }  



}
