<?php
namespace app\index\controller;

use app\index\controller\Commone;
use \think\Db;
use \think\Cookie;
use \think\Session;
use \think\Cache;
class Land extends Commone
{
    public function list(){
         $this->AI();
         $this->INDEX_AI();
         $user_game_id = $this->get_user('game_id');
         if($user_game_id == 0){
            $user_game_id = config('game_id');
         }
         
         $game_list = Db::name('game_list')->where('id','in',$user_game_id)->where('game_status',1)->select();
         $this->assign('game_list',$game_list);
         return view(); 
    }
    
    public function land_list(){
        $this->AI();
        $this->INDEX_AI();
        $where = $this->getparam(['id','username','typec']);
        $where['userid'] = $this->get_user('id');
        $user_game_id = $this->get_user('game_id');
        if($user_game_id == 0){
             $user_game_id = config('game_id');
        }
        
        if(isset($where['username'])){
            $where['username'] = ['like','%'.$where['username'].'%'];
        }
        
       
        
        $list = Db::name("land")->where('typec','in',$user_game_id)->where($where)->order('id asc')->paginate([
            'list_rows'=> $this->request->param("limit")??10,
            'var_page' => 'page',
        ]);
        
        
        
         for ($i = 0; $i < count($list); $i++) {
             $user_where['id'] = $list[$i]['userid'];
             $game_where['id'] = $list[$i]['typec'];
             $orders_where['land_id'] = $list[$i]['id'];
             $orders_where['state'] = 2;
             $temp = $list[$i];
             $user = Db::name("user")->where($user_where)->find();
             $game_list = Db::name("game_list")->where($game_where)->find();
             $jr_money = Db::name("orders")->where($orders_where)->whereTime('pay_time', 'today')->sum('money');
             $zr_money = Db::name("orders")->where($orders_where)->whereTime('pay_time', 'yesterday')->sum('money');
             $z_money = Db::name("orders")->where($orders_where)->sum('money');
             $typec = $game_list['game_name'] ;
             $name = $user['name'] ;
             $json = json_decode($list[$i]['json'],true);
             
            $ck = "";
             
             if($game_list['game_type']=='1'){
					$s=$list[$i]['json'];
					preg_match_all('/ck(.+?)"/',$s,$r);
					
					$m=count($r[1]);
				
					for($k = 0;$k<$m;$k++){
					    if($r[1][$k]!=''){
					    $ck =  $ck.$r[1][$k].'元';
					       if($k<$m-1){$ck =  $ck.' / ';}
					    }
					    
					}
					
			 }else{
			     
					$ck = $ck.'---';
			 }
			
							    
             $temp['name'] = $name;
             $temp['typec_name'] = $typec;
             $temp['jr_pay'] = $jr_money;
             $temp['zr_pay'] = $zr_money;
             $temp['z_pay'] = $z_money;
             $temp['ck'] = $ck;
             $temp['alipay_status'] = $json['alipay_status'];
             $list[$i] = $temp;
             
        }
       
        
        $this->json_success("",$list);
	     
    }
    
     //修改账户轮询状态
    public function status_save(){
        $this->AI(); 
        $this->INDEX_AI();
        $post = $this->request->get();
        $post['bug'] = 0;
        $where['id'] = $post['id'];
        $where['userid'] = $this->get_user('id');
        $where_bug_log['user_id'] = $this->get_user('id');
        $where_bug_log['land_id'] = $post['id'];
        $r = Db::name("land")->where($where)->update($post);
        Db::name("bug_log")->where($where_bug_log)->delete();
        if($r){
            $this->success("操作成功");
        }else{
            $this->error("修改失败");
        }
    }
    
     //删除账户
    public function del_save(){
        $this->AI();
        $this->INDEX_AI();
        $post = $this->request->get();
        $where['id'] = $post['id'];
        $where['userid'] = $this->get_user('id');
        $land = $this->get_land($post['id']);
        $r = Db::name("land")->where($where)->delete();
        
        $where_orders['land_id'] = $post['id'];
        $where_orders['userid'] = $this->get_user('id');
        $where_log['land'] = $post['id'];
        $where_log['userid'] = $this->get_user('id');
        
        $update['land_name'] = $land['username'];
        DB::name("orders")->where($where_orders)->update($update);
        DB::name("log")->where($where_log)->update($update);
        
        if($r){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }
    
     //删除异常账户
    public function yc_del_save(){
        $this->AI();
        $this->INDEX_AI();
        $post = $this->request->get();
        $where['id'] = $post['id'];
        $where['userid'] = $this->get_user('id');
        $land = $this->get_land($post['id']);
        $r = Db::name("land")->where($where)->delete();
        
        $where_orders['land_id'] = $post['id'];
        $where_orders['userid'] = $this->get_user('id');
        $where_log['land'] = $post['id'];
        $where_log['userid'] = $this->get_user('id');
        $update['land_name'] = $land['username'];
        DB::name("orders")->where($where_orders)>update($update);
        DB::name("log")->where($where_log)->update($update);
        Db::name("bug_log")->where('id',$post['bug_id'])->update($update);
        if($r){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }
    
  
    
     //修改账号
    public function land_save(){
        
        $this->AI();
        $this->INDEX_AI();
        $post = $this->request->post();
        $json = encode(json_encode(array(
            'method'=>$post['method'],
            'data'=>'key=[key]&money=[money]&amount=[amount]&order=[order]&record=[record]&remark=[remark]&sign=[sign]',
            'header'=>'',
            'cookie'=>''
        )));

        $user = Db::name('land')->where('id',$post['land_id'])->find();
        
        if($user['username']!=$post['username']){
            $user_1 = Db::name('land')->where('username',$post['username'])->find();
            if($user_1){
            $this->error("账号备注已存在");
            }
        }
        
        $post['reback']=$json;
        $where['id'] = $post['land_id'];
        $where['userid'] = $this->get_user('id');
        
        unset($post['method']);  
        unset($post['land_id']);  
        unset($post['game_id']);  
        
        $post1 = $post;
        unset($post1['username']); 
        unset($post1['r_money']); 
        unset($post1['z_money']); 
        unset($post1['reback']);
        
        foreach($post as $k=>$v)
        {
          if($post[$k] == $post1[$k]){
              unset($post[$k]);
          }
        }
        
        $post['json'] = json_encode($post1);
       
        if(!isset($post['json'])){
           unset($post['json']);
        }
        
        if($user['typec'] == 4){//修改不修改当面付，过滤当面付
           unset($post['json']); 
        }
        
        $r = Db::name("land")->where($where)->update($post);
        if($r){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
    
    public function land_edit(){
        $this->AI();
        $this->INDEX_AI();
        $id = $this->request->get('id');
        $land = $this->get_land($id);
        $where['id'] = $land['typec'];
        $user_game_id = $this->get_user('game_id');
        if($user_game_id == 0){
             $user_game_id = config('game_id');
        }
        
        $game = Db::name('game_list')->where('id','in',$user_game_id)->where($where)->find();
        $json = json_decode(encode($land['reback'],2));
        $dy_json = json_decode($land['json'],true);
        $this->assign('game_id','');
        $this->assign('land_id',$land['id']);
        $this->assign('username',$land['username']);
        $this->assign('r_money',$land['r_money']);
        $this->assign('typec',$land['typec']);
        $this->assign('z_money',$land['z_money']);
        $this->assign('json',json_decode($land['json'],true));   
        $this->assign('method',$json->method);
        if($game['game_type']=='1'){
           return view('game/game_view/qq');
        }else if($game['game_type']=='4'){
           return view('game/game_view/qq_ry');
        }else{
           return view('game/game_view/'.$game['game_dm']);
        }
        
        
    }
    
    public function land_add(){
        $this->AI();
        $this->INDEX_AI();
        $id = $this->request->get('id');
        $where['id'] = $id;
        $user_game_id = $this->get_user('game_id');
        if($user_game_id == 0){
            $user_game_id = config('game_id');
        }
        $game = Db::name('game_list')->where('id','in',$user_game_id)->where($where)->find();
        $this->assign('game_id',$game['id']);
        $this->assign('land_id','');
        $this->assign('username','');
        $this->assign('r_money','');
        $this->assign('z_money','');
        $this->assign('method','');
        $this->assign('typec',$game['id']);
         if($game['game_type']=='1'){
           return view('game/game_view/qq');
        }else if($game['game_type']=='4'){
           return view('game/game_view/qq_ry');
        }else{
           return view('game/game_view/'.$game['game_dm']);
        }
        
        
    }
    
    public function alipay_dmf(){
        $this->AI();
        $this->INDEX_AI();
        $id = intval($this->request->post('id'));
        $where_query['id'] = $id;
        $where_query['userid'] = $this->get_user('id'); 
        $query = Db::name('land')->where($where_query)->find();
    
        if($this->get_user('pay_sxf')!='0'){
		if ($this->get_user('balance') <= 0) {
		    $this->error("您的账号余额为0.00元,请充值后在进行授权登录");
		}
        }
		//当面付开始
		if ($query){
           
			  $images="https://wenhairu.com/static/api/qr/?size=150&text=https://openauth.alipay.com/oauth2/appToAppAuth.htm?app_id=2021001139666829%26redirect_uri=".urlencode('http://api.fssy.cc/pay/alipay/redirect_uri.html')."?url=".base64_encode(config('url').'|'.$id);
			  $json['code'] = 1;
			  $json['image'] = $images;
              exit(json_encode($json));
				
            
        }else{
             $this->error("请求授权失败");
        }
	}
	
	//当面付授权状态
    function land_sq_status(){
        $this->AI();
        $this->INDEX_AI();
        $id = intval($this->request->post('id'));
        $where['id'] = $id;
        $where['userid'] = $this->get_user('id'); 
        $land = Db::name('land')->where($where)->find();
        if ($land){
                $jsonst = json_decode($land['json']);
                $json['code'] = 1;
			    $json['alipay_status'] = $jsonst->alipay_status;
                exit(json_encode($json));
          
        }else{
            $this->error("查询失败");
        }
    }
    
    public function bug_set(){
        $this->AI();
        $this->INDEX_AI();
        $data['bug_num'] = $this->get_user('bug_set');
        $data['bug_type'] = $this->get_user('bug_type');
        exit(json_encode($data));
    }
    
    public function bug_save(){
        $this->AI();
        $this->INDEX_AI();
        $bug_num = intval($this->request->post('bug_num'));
        $bug_type = intval($this->request->post('bug_type'));
        $where['id'] = $this->get_user('id'); 
        $update['bug_set'] = $bug_num;
        $update['bug_type'] = $bug_type;
        $land = Db::name('user')->where($where)->update($update);
        if ($land){
            $this->success("设置成功");
        }else{
            $this->error("设置失败");
        }
    }
    
    public function pay_demo(){
        
        $this->AI();
        $this->INDEX_AI();
        $user_name = $this->get_user('name');
        $web_config = Db::name('config')->where('id',1)->find();
        $this->assign('name',$user_name);
        if($this->request->isPost()) {
            
            $post = $this->request->post();
            $api_id = $this->get_user('api_id');
            $key = $this->get_user('api_key');
            $url = $web_config['pay_url'];
            $data = [
                    'api_id' => $api_id,
                    'record' => '支付测试发起',
                    'money' => sprintf("%.2f",$post['money']),
            ];

            ksort($data);
                $str1 = '';
                foreach ($data as $k => $v) {
                    $str1 .= '&' . $k . "=" . $v;
            }

            $sign = md5(trim($str1) . $key);

            $data['refer']=url('index/index/member');

            $data['notify_url']=url('index/index/member');

            $data['sign'] = $sign;
            
            $data['mid'] = $post['mid'];
            
            $htmls = "<form id='ckpay' name='ckpay' action='" . $url . "' method='post'>";
            foreach ($data as $key => $val) {
                 $htmls .= "<input type='hidden' name='" . $key . "' value='" . $val . "'/>";
            }
            $htmls .= "</form>";
            $htmls .= "<script>document.forms['ckpay'].submit();</script>";
            return $this->success($htmls);
            }
        return view();
    
    }
    
    public function bug_list(){
         $this->AI();
         $this->INDEX_AI();
         return view(); 
    }
    
    public function bug_land_list(){
        $this->AI();
        $this->INDEX_AI();
        $land_where = $this->getparam(['username']);
        if($land_where){
        $land_d = Db::name('land')->where($land_where)->find();
        $where['land_id'] = $land_d['id'];
        }
        $where['user_id'] = $this->get_user('id');
        $user_game_id = $this->get_user('game_id');
        if($user_game_id == 0){
             $user_game_id = config('game_id');
        }
        
        $list = Db::name("bug_log")->where('land_type','in',$user_game_id)->where($where)->order('id asc')->paginate([
            'list_rows'=> $this->request->param("limit")??10,
            'var_page' => 'page',
        ]);
        
        
        
        for ($i = 0; $i < count($list); $i++) {
            
             $temp = $list[$i];
             $land = $this->get_land($list[$i]['land_id']);
             $game = $this->get_game($list[$i]['land_type']);
             $temp['mid'] = $land['mid'];
             $temp['username'] = $land['username'];
             $temp['typec_name'] = $game['game_name'];
             $temp['land_lx'] = $land['land_lx'];
             $list[$i] = $temp;
             
        }
       
        
        $this->json_success("",$list);
	     
    }
    
    public function ds_save(){
        $this->AI();
        $this->INDEX_AI();
        $ds_num = intval($this->request->post('ds_num'));
        $ds_time = intval($this->request->post('ds_time'));
        $ds_type = intval($this->request->post('ds_type'));
        $where['userid'] = $this->get_user('id'); 
        $update['ds_num'] = $ds_num;
        $update['ds_time'] = $ds_time;
     
        if($ds_num!=0 && $ds_time!=0){
          $update['ds_status_time'] = time();
          $update['ds_type'] = $ds_type;
        }else{
          $update['ds_status_time'] = '0'; 
          $update['ds_status'] = '0';  
          $update['ds_type'] = '0';
        }
        $land = Db::name('land')->where($where)->where('id','in',$_POST['ds_id'])->update($update);
        if ($land){
            $this->success("设置成功");
        }else{
            $this->error("设置失败");
        }
    }
    
     
    
}