<?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 Windsnsusermall 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   扣扣小子
	 * @blog     http://www.weinuo.net
	 * @version  0.0.1
	 * @datetime 2020-12-20
	 */
    public function index()
    {
    $this->view->assign('title', __('我的购物-用户中心'));

    $windsns_userid = $this->auth->getUserinfo()['user_id'];
    
	$windsnspaylist = Db::name('windsnsmall_shoppaylist') -> where(['windsns_userid'=>$windsns_userid]) ->  order('windsns_goodspayid', 'desc') -> paginate(20); 
	$windsnspaysum  = Db::name('windsnsmall_shoppaylist')->where(['windsns_userid' => $windsns_userid])->count();
	$page = $windsnspaylist->render();
	$this->assign('windsnspaylist', $windsnspaylist);
	$this->assign('page', $page);
	 if($windsnspaysum > 1 or $windsnspaysum == 1){
        return $this->view->fetch('windsns_mallshopindex');
        }else{
         return $this->view->fetch('windsns_goodsnull');
        }

    }


        public function windsnsgoodstruedo($windsnsgoodsid)
    {
         $urlwindsnsgoodsid  =  input('get.windsnsgoodsid'); 
        
         
          $sqgoodspayid   = Db::name('windsnsmall_shoppaylist')-> where(['windsns_goodspayid'=>$urlwindsnsgoodsid])->find();
          $goodspaytype   =  $sqgoodspayid['windsns_goodspaytype'];
          $goodsshouhuotype   =  $sqgoodspayid['windsns_goodsdingdantype'];
         if($goodspaytype == 2){
         	return $this->view->fetch('windsns_errorstypea'); 
         }elseif($goodsshouhuotype == 1){
         	
         	return $this->view->fetch('windsns_errorstypeb');
         	
         }elseif($goodsshouhuotype == 4){
         	
         	return $this->view->fetch('windsns_errorstypec');
         	
         }elseif($goodsshouhuotype == 5){
         	return $this->view->fetch('windsns_errorstyped');
         }else{
         	 $data['windsns_goodsdingdantype'] =  4; 
             $xiugaizhuangtai = Db::name('windsnsmall_shoppaylist')->where('windsns_goodspayid', $urlwindsnsgoodsid)->update($data);// 
         	return $this->view->fetch('windsns_okdoa');
        
         }

   }  
   
        public function windsnsgoodstuido($windsnsgoodsid)
    {
         $urlwindsnsgoodsid  =  input('get.windsnsgoodsid'); 

          $sqgoodspayid   = Db::name('windsnsmall_shoppaylist')-> where(['windsns_goodspayid'=>$urlwindsnsgoodsid])->find();
          $goodspaytype   =  $sqgoodspayid['windsns_goodspaytype'];
          $goodsshouhuotype   =  $sqgoodspayid['windsns_goodsdingdantype'];
         if($goodspaytype == 2){
         	
         	return $this->view->fetch('windsns_errorstypea'); 
         }elseif($goodsshouhuotype == 1){
         	return $this->view->fetch('windsns_errorstypeb');
         }elseif($goodsshouhuotype == 4){
         	return $this->view->fetch('windsns_errorstypec');
         }elseif($goodsshouhuotype == 5){
         	 return $this->view->fetch('windsns_errorstyped');
         }else{
         	 $data['windsns_goodsdingdantype'] =  3; 
             $xiugaizhuangtai = Db::name('windsnsmall_shoppaylist')->where('windsns_goodspayid', $urlwindsnsgoodsid)->update($data);// 
         	return $this->view->fetch('windsns_okdob');
         }

   }  


        public function windsnsgoodstuidook($windsnsgoodsid)
    {
          $urlwindsnsgoodsid  =  input('get.windsnsgoodsid'); 
          $sqgoodspayid   = Db::name('windsnsmall_shoppaylist')-> where(['windsns_goodspayid'=>$urlwindsnsgoodsid])->find();
          $goodspaytype   =  $sqgoodspayid['windsns_goodspaytype'];
          $goodsshouhuotype   =  $sqgoodspayid['windsns_goodsdingdantype'];
         if($goodspaytype == 2){
         	
         	return $this->view->fetch('windsns_errorstypea'); 
         }elseif($goodspaytype == 1){
         	return $this->view->fetch('windsns_errorstypef');
         }
         elseif($goodsshouhuotype == 1){
         	return $this->view->fetch('windsns_errorstypeb');
         }elseif($goodsshouhuotype == 4){
         	return $this->view->fetch('windsns_errorstypec');
         }elseif($goodsshouhuotype == 5){
         	return $this->view->fetch('windsns_errorstyped');
         }else{
         	 $data['windsns_goodsdingdantype'] =  5; 
             $xiugaizhuangtai = Db::name('windsnsmall_shoppaylist')->where('windsns_goodspayid', $urlwindsnsgoodsid)->update($data);// 
         	 return $this->view->fetch('windsns_okdob');
         }

   }  



        public function windsnsgoodspaydo($windsnsgoodsid)
    {
          $urlwindsnsgoodsid  =  input('get.windsnsgoodsid'); 
		  $userid = $this->auth->getUserinfo()['user_id'];
	      $user_money = Db::name('user')->where('id', $userid)->find(); 
	      $usermoneys =  $user_money['money'];	
         
          $sqgoodspayid   = Db::name('windsnsmall_shoppaylist')-> where(['windsns_goodspayid'=>$urlwindsnsgoodsid])->find();
          $goodspaytype   =  $sqgoodspayid['windsns_goodspaytype'];
          $goodspriceall  =   $sqgoodspayid['windsns_goodspayallprice'];
          $goodsshouhuotype   =  $sqgoodspayid['windsns_goodsdingdantype'];
         if($goodspaytype == 1){
         	return $this->view->fetch('windsns_errorstypee');
         }elseif($goodsshouhuotype == 5){
         	 return $this->view->fetch('windsns_errorstyped');
         }else{
         	if ($usermoneys >= $goodspriceall) {

				        $xiaofei = Db::name('user')->where('id', $userid)->setDec('money', $goodspriceall);
				        $shopdingdansumadd = Db::name('windsnsmall_shoplist')-> where(['windsnsmallshopsid'=>$urlwindsnsgoodsid])->setInc('windsnsmallshopdingdansum', 1);
			            $data['windsns_goodspaytype'] =  1; 
			            $data['windsns_goodsdingdantype'] =  1;
                        $xiugaizhuangtai = Db::name('windsnsmall_shoppaylist')->where('windsns_goodspayid', $urlwindsnsgoodsid)->update($data);// 
			              return $this->view->fetch('windsns_okpaydo');

				       } else {
				        	 return $this->view->fetch('windsns_errorsmoneynull');
				        }

         	
         }

   }  





}
