<?php


namespace Home\Controller;

use Admin\Model\PromoteModel;
use Think\Controller;
use User\Api\PromoteApi;

/**
 * 游戏分成比例控制器
 */
class GameDivideController extends BaseController
{
    //首页
    public function index()
    {
        $parentId = getParentPromoteId(PID);//上级ID
        if ($parentId > 0) {
            $this->error('权限异常');
        }

        $securityCode = empty(session('game_divide_second_pwd')) ? '' : session('game_divide_second_pwd');//安全密码
        $model = new PromoteApi();
        $res = $model->verify_er_User(PID, $securityCode);//验证安全密码
        if ($res) {
            $map['tab_game.online_status'] = 1;//开发者游戏上线状态
            $map['tab_game.down_port'] = 1;//游戏端口 第三方接口不能申请
            $map['tab_game.game_status'] = 1;//游戏状态
            empty(I('relation_game_id')) || $map['relation_game_id'] = I('relation_game_id');
            empty(I('sdk_version')) || $map['sdk_version'] = I('sdk_version');
            empty(I('server_type')) || $map['server_type'] = I('server_type');

            $minRatio = floatval(I('min_ratio', 0));
            $maxRatio = floatval(I('max_ratio', 0));
            if (!empty($minRatio) && empty($maxRatio)) {
                $map['ratio'] = ['egt', $minRatio];
            } elseif (empty($minRatio) && !empty($maxRatio)) {
                $map['ratio'] = ['elt', $maxRatio];
            } elseif (!empty($minRatio) && !empty($maxRatio)) {
                $map['ratio'] = ['between', [$minRatio, $maxRatio]];
            }

            $page = intval(I('get.p', 0));
            $page = $page ? $page : 1; //默认显示第一页数据

            if (isset($_REQUEST['row'])) {
                $row = $_REQUEST['row'];
            } else {
                $row = 10;
            }

            $data = M('Game', 'tab_')
                ->field('id,icon,game_name,game_type_name,sdk_version,server_type,discount,ratio')
                ->where($map)
                ->order('sort desc')
                ->page($page, $row)
                ->select();
            $count = M('Game', 'tab_')
                ->where($map)
                ->count();

            //分页
            $parameter['p'] = I('get.p', 1);
            $parameter['row'] = I('get.row');
            empty(I('relation_game_id')) || $parameter['relation_game_id'] = I('relation_game_id');
            empty(I('sdk_version')) || $parameter['sdk_version'] = I('sdk_version');
            empty(I('server_type')) || $parameter['server_type'] = I('server_type');
            empty($minRatio) || $parameter['min_ratio'] = $minRatio;
            empty($maxRatio) || $parameter['max_ratio'] = $maxRatio;

            $page = set_pagination($count, $row, $parameter);
            if ($page) {
                $this->assign('_page', $page);
            }

            $this->assign('dataList', $data);
            $this->assign('count', $count);
            $this->assign('serverType', I('server_type', 0));
        }

        $this->meta_title = "分成比例";
        $this->display();
    }

    //验证安全密码
    public function verifyPassword()
    {
        $password = I('post.password');
        $model = new PromoteApi();
        $res = $model->verify_er_User(PID, $password);

        if ($res) {
            session('game_divide_second_pwd', $password);

            $data['status'] = 1;
            $data['msg'] = '验证成功';
        } else {
            $data['status'] = 0;
            $data['msg'] = '安全密码不正确';
        }

        $this->ajaxReturn($data);
    }
}