<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\api\controller\fenxiao;

use app\api\controller\Base;
use think\Db;
use app\common\model\bbfxshop\User;
use app\common\model\bbfxshop\FenxiaoLevel;
use app\common\model\bbfxshop\FenxiaoTongji;


class Index extends Base
{
    protected $noNeedLogin = ['getLevel'];
    protected $noNeedRight = ['*'];
    
    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Fenxiao;
    }

    public function index(){
        global $_W;
        $set = $_W['base'];
        
        if($set['fenxiao']['is_becomedue'] != 1){
            if($this->user['is_distributor'] != 1){
                __error(104,'您暂无权限访问！');
            }
        }
        else{
            if($this->user['is_distributor'] != 1 && $this->user['distributor_status'] == 1){
                // __error(104,'您的分销商身份已到期，到期后将无法获得佣金收益！');
            }
        }
        
        if($this->user['distributor_status'] != 1){
            if($set['fenxiao']['become_agent'] == 4){
                $apply = Db::name("bbfx_fenxiao_apply")->where("user_id",$this->user['id'])->find();
                if(!empty($apply)){
                    if($apply['status'] == 2){
                        __error(104,'审核未通过！');
                    }
                }
                else{
                    __error(104,'您暂无权限访问！');
                }
            }
            
            __error(105,'您暂无权限访问！');
        }
        
        $alldown = \app\common\model\bbfxshop\FenxiaoUser::getAllDown($this->user['id']);

        $info = FenxiaoTongji::getCommissionStatistics($this->user,$alldown);
        $info['texts'] = $set['fenxiao']['texts'];
        $info['leveldata'] = FenxiaoLevel::getLevel($this->user['level']);
        $info['user'] = $this->user;
        $info['freeze_commission'] = round($info['user']['freeze_money'] + $info['user']['freeze_commission'],2);
        $info['max_strikeout_money'] = \app\common\model\bbfxshop\Strikeout::getMaxStrikeoutMoney($this->user);
        $info['becomedue_tips'] = '您的分销商身份已到期，到期后将无法获得佣金收益！';
        
        $user = $this->user;
        $navItems = FenxiaoTongji::getRewardList($user,false,$alldown,$info['fenxiao_datas']);
        $info['navItems'] = $navItems;
        unset($info['fenxiao_datas']);

        // 计算出局倍数
        $info['strikeout_multiple_total'] = 0;
        $info['strikeout_multiple_ok'] = 0;
        $info['strikeout_multiple_surplus'] = 0;

        if($set['fenxiao']['is_strikeout'] == 1){
            
            $result = \app\common\model\bbfxshop\Strikeout::getStrikeoutMultipleData($user);

            $info['strikeout_multiple_total'] = $result['strikeout_multiple_total'];
            $info['strikeout_multiple_ok'] = $result['strikeout_multiple_ok'];
            $info['strikeout_multiple_surplus'] = $result['strikeout_multiple_surplus'];
        }
        
        __success("操作成功",$info);
    }
    
    public function apply(){
        global $_W;
        $set = $_W['base'];
        
        
        $params = $this->paramPost(['realname/s','mobile/s','level/d']);
        if(empty($params['realname'])){
            __error("请填写姓名");
        }
        if(empty($params['mobile'])){
            __error("请填写手机号");
        }
        $level = intval($params['level']);
        $levels = FenxiaoLevel::getLevels(1);
        if($level == -1){
            $nextLevel = $this->user['nextLevel'];
            if(empty($nextLevel)){
                __error("未开启申请");
            }
            
            if($nextLevel['id'] != 0){
                if($nextLevel['apply'] != 1){
                    __error("该等级暂不支持申请");
                }
            }
            else{
                if($set['fenxiao']['become_agent'] != 4){
                    __error("未开启申请");
                }
            }
            $level = $nextLevel['id'];
        }
        else{
            
            if(empty($levels[$level])){
                __error("申请等级错误");
            }
            
            if($level != 0){
                if($levels[$level]['conditions']['apply'] != 1){
                    __error("该等级暂不支持申请");
                }
            }
            else{
                if($set['fenxiao']['become_agent'] != 4){
                    __error("未开启申请");
                }
            }
        }
        
        if(empty($levels[$level])){
            __error("申请等级错误");
        }
        
        if($this->user['is_distributor'] = 1 && $this->user['distributor_status'] == 1){
            if($levels[$this->user['level']]['level'] >= $levels[$level]['level']){
                __error("不可申请低等级");
            }
        }
        
        
        $apply = Db::name("bbfx_fenxiao_apply")->where(["user_id"=>$this->user['id'],'level'=>$level])->find();
        if(!empty($apply)){
            if($apply['status'] == 0){
                __error("您已提交申请，请等待审核");
            }
        }
        
        $data = array(
            'uniacid'=>UNIACID,
            'user_id'=>$this->user['id'],
            'realname'=>$params['realname'],
            'mobile'=>$params['mobile'],
            'status'=>0,
            'level'=>$level,
            'createtime'=>time()
        );
        
        if(!empty($apply)){
            $res = Db::name("bbfx_fenxiao_apply")->where(['id'=>$apply['id']])->update($data);
        }
        else{
            $res = Db::name("bbfx_fenxiao_apply")->insert($data);
        }
        
        if($res){
            __success("提交成功");
        }
        else{
            __error("提交失败");
        }
    }
    
    public function getLevel(){
        $id = $this->paramGet("id/d");
        
        $level = FenxiaoLevel::getLevel($id);
        
        if(empty($level)){
            __error("等级错误");
        }
        
        __success("操作成功",$level);
    }
}