<?php
namespace app\index\controller;

use app\index\controller\Commone;
use \think\Db;
use \think\Cookie;
use \think\Session;
use \think\Cache;
class Orders 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();
        $where['userid'] = $this->get_user('id');
        $where['state'] = 2;
        
        $tj_data['jr'] = Db::name('orders')->where('payc','in',$user_game_id)->where($where)->whereTime('pay_time', 'today')->sum('money');
        $tj_data['zr'] = Db::name('orders')->where('payc','in',$user_game_id)->where($where)->whereTime('pay_time', 'yesterday')->sum('money');
        $tj_data['yz'] = Db::name('orders')->where('payc','in',$user_game_id)->where($where)->whereTime('pay_time', 'week')->sum('money');
        $tj_data['yy'] = Db::name('orders')->where('payc','in',$user_game_id)->where($where)->whereTime('pay_time', 'month')->sum('money');
        
        $order_Set = config_set('order_set');
        $this->assign('order_Set',$order_Set['value']);
        $this->assign('tj_data',$tj_data);
        $this->assign('game_list',$game_list);
        $this->assign('cate',config('cate'));
        return view();
    }
    
    public function log(){
        $this->AI();
        $cate = $this->get_user('cate_id');
        if(config('cate')!='1') {
            
              exit($this->fetch('/error'));
            
        }
        $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 orders_list(){
        $this->AI();
        $this->INDEX_AI();
        $where = $this->getparam(['num','remark','startDate','endDate','state','payc']);
        
        if(isset($where['startDate']) && isset($where['endDate'])){
            $where['order_time'] = ['between', [strtotime($where['startDate'] . ' 00:00:00'),strtotime($where['endDate'] . ' 00:00:00')]];
            
        }
        
        if(isset($where['num'])){
            $where['num'] = ['like','%'.$where['num'].'%'];
        }
        
        unset($where['startDate']);  
        unset($where['endDate']);  
        
        $user_game_id = $this->get_user('game_id');
        if($user_game_id == 0){
            if(isset($where['payc'])){
                $user_game_id = $where['payc'];
            }else{
                $user_game_id = config('game_id');
            }
        }
        unset($where['payc']); 
        $where['userid'] = $this->get_user('id');
        
        $list = Db::name("orders")->where($where)->where('payc','in',$user_game_id)->order('id desc')->paginate([
            'list_rows'=> $this->request->param("limit")??10,
            'var_page' => 'page',
        ]);
        
        for ($i = 0; $i < count($list); $i++) {
             
             $land_where['id'] = $list[$i]['land_id'];
             $game_where['id'] = $list[$i]['payc'];
             $user_where['id'] = $list[$i]['userid'];
             $temp = $list[$i];
             
             $land = Db::name("land")->where($land_where)->find();
             $game = Db::name("game_list")->where($game_where)->find();
             $user = Db::name("user")->where($user_where)->find();
             if($list[$i]['land_name']!=''){
             $temp['land_id'] = $list[$i]['land_name'];    
             }else{
             $temp['land_id'] = $land['username'];    
             }
             $temp['land_type'] = $game['game_name'];
                
             $list[$i] = $temp;
        }
        
        
        
        $this->json_success("",$list);
    }
    
    public function log_list(){
        $this->AI();
        $this->INDEX_AI();
        $where = $this->getparam(['num','payc','time']);
        $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['num'])){
            $where['num'] = ['like','%'.$where['num'].'%'];
        }
        
        $list = Db::name("log")->where($where)->where('payc','in',$user_game_id)->order('id desc')->paginate([
            'list_rows'=> $this->request->param("limit")??10,
            'var_page' => 'page',
        ]);
        
        for ($i = 0; $i < count($list); $i++) {
             
             $land_where['id'] = $list[$i]['land'];
             $game_where['id'] = $list[$i]['payc'];
             $user_where['id'] = $list[$i]['userid'];
             $temp = $list[$i];
             
             $land = Db::name("land")->where($land_where)->find();
             $game = Db::name("game_list")->where($game_where)->find();
             $user = Db::name("user")->where($user_where)->find();
             
             if($list[$i]['land_name']!=''){
             $temp['land_id'] = $list[$i]['land_name'];    
             }else{
             $temp['land_id'] = $land['username'];    
             }
             $temp['land_type'] = $game['game_name'];
             $temp['name'] = $user['name'].'('.$list[$i]['userid'].')';    
             $list[$i] = $temp;
        }
        
        
        
        $this->json_success("",$list);
    }
    
    
    public function orders_del_type(){
        $this->AI();
        $this->INDEX_AI();
        $type = $this->request->param('type');
        $userid = $this->get_user('id');
        if($type==1){
           $nowTime = strtotime(date("Y-m-d",time()) . ' 00:00:00');
           $xqTime = $nowTime-(86400*7); //7天的时间
           $where['order_time'] = ['<',$xqTime];
           $where['userid'] = $userid;
           
        }else if($type==2){
           $nowTime = strtotime(date("Y-m-d",time()) . ' 00:00:00');
           $xqTime = $nowTime-(86400*14); //7天的时间
           $where['order_time'] = ['<',$xqTime];
           $where['userid'] = $userid;
        }else if($type==3){
           
           $where['state'] = 3;
           $where['userid'] = $userid;
        }else if($type==4){
 
           $where['userid'] = $userid;
        }
        
        $del_status = Db::name('orders')->where($where)->delete();
        
        if($del_status){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
        
        
    }
    
    public function orders_del_type_pl(){
        $this->AI();
        $this->INDEX_AI();
        $type = $_POST['type'];
        $userid = $this->get_user('id');
        $where['userid'] = $userid;
        $where['id'] = ['in',$type];
        $del_status = Db::name('orders')->where($where)->delete();
        
        if($del_status){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
        
        
    }
    
    public function log_del_type_pl(){
        $this->AI();
        $this->INDEX_AI();
        $type = $_POST['type'];
        $userid = $this->get_user('id');
        $where['id'] = ['in',$type];
        $where['userid'] = $userid;
        $del_status = Db::name('log')->where($where)->delete();
        
        if($del_status){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
        
        
    }
    
    public function reback(){
        $this->AI();
        $this->INDEX_AI();
        $userid = $this->get_user('id');
        $order_id = $this->request->param('id');
        $order_where['userid'] = $userid;
        $order_where['id'] = $order_id;
        $order_list = Db::name('orders')->where($order_where)->find();
        //开始变更订单
        $order_update['state'] = 2;
        $order_update['pay_time'] = time();
        $order_update = Db::name('orders')->where($order_where)->update($order_update);
        if($order_update){
            $user = Db::name('user')->where('id',$userid)->find();
            api_request($order_list['id'],$user);
            //变更订单状态为手动补单
            $update = Db::name('orders')->where('id',$order_list['id'])->update(['reback'=>1]);
            if($update){
               $this->success("手动补单成功");  
            }else{
                $this->error("手动补单失败");
            }
        }else{
            $this->error("订单处理异常");
        }
    }
    
    
}
