<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 红黑大战
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/5 0005
 * Time: 17:14
 */
class Redblack extends MY_Controller
{
    public $postdata = array('frombg' => 1, 'mod' => '', 'act' => '', 'lid' => 1, 'sid' => 1);
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('cookie');
        $this->load->library('curl');

        $this->env = get_cookie($this->_wenenv);
    }

    /**
     * 加载大喇叭列表页面
     */
    public function index()
    {
        $this->load->view('redblack/index');
    }

    /**
     * 请求红黑赔率配置
     */
    public function config()
    {
        $url = ($this->env == 2) ? $this->config->item('localUrl') : $this->config->item('officialUrl');
        $postData = $this->postdata;
        $postData['mod'] = 'Redblack';
        $postData['act'] = 'getOdds';
        ($this->env == 2) && $postData['demo'] = 1;
        $res = $this->curl->req($url, $postData);
        $data = json_decode($res, 1);
        $this->load->view('redblack/config', $data);

    }

    /**
     * 编辑红黑赔率
     */
    public function editConfigOdds()
    {
        $url = ($this->env == 2) ? $this->config->item('localUrl') : $this->config->item('officialUrl');
        $postData = $this->postdata;
        for($i=1; $i <= 18; $i++) {
            $postData['pokertype_'.$i] = $this->input->post('pokertype_'.$i) * 100;
        }
        $postData['max_bet_num'] = $this->input->post('max_bet_num');
        $postData['mod'] = 'Redblack';
        $postData['act'] = 'editOdds';
        ($this->env == 2) && $postData['demo'] = 1;

        $res = $this->curl->req($url, $postData);
        die($res);
    }


 /**
     * 请求红黑配置
     */
    public function indexData()
    {
        $url = ($this->env == 2) ? $this->config->item('localUrl') : $this->config->item('officialUrl');
        $postData = $this->postdata;
        $postData['mod'] = 'Redblack';
        $postData['act'] = 'getRedblackConfig';
        ($this->env == 2) && $postData['demo'] = 1;
        $res = $this->curl->req($url, $postData);
        $retData = json_decode($res, 1);
        if(empty($retData)) {
            die(json_encode($this->withNodata()));
        }
        $list = [];
        $i = 0;
        foreach ($retData as $k => $v) {
            $total = $i+1;
            $list[$i]['id'] = $total;
            $list[$i]['money'] = $v;
            $i++;
        }
        $data['data'] = $list;
        $data['recordsTotal'] = $total;
        $data['recordsFiltered'] = $total;
        die(json_encode($data));

    }


    public function statistics()
    {
        $data = ['dayLilst'=>[], 'info'=>''];

        //时间查询
        $dateArr = [];
        if( $stime = get_cookie('redblack_stime') ){
            $data['stime'] = $stime;
            $dateArr['stime'] = strtotime($stime.'0:0:1');
        }else{
            $dateArr['stime'] = strtotime('-7 days 0:0:1');//默认开始时间
            $data['stime'] = date('Y-m-d', $dateArr['stime']);
        }

        if( $etime = get_cookie('redblack_etime') ){
            $data['etime'] = $etime;
            $dateArr['etime'] = strtotime($etime.'23:59:59');
        }else{
            $dateArr['etime'] = strtotime('-1 days 23:59:59');//默认结束时间
            $data['etime'] = date('Y-m-d', $dateArr['etime']);
        }

        $url = ($this->env == 2) ? $this->config->item('localUrl') : $this->config->item('officialUrl');
        $postData = array_merge($this->postdata, $dateArr);
        $postData['mod'] = 'Redblack';
        $postData['act'] = 'statistics';
        ($this->env == 2) && $postData['demo'] = 1;
        $res = $this->curl->req($url, $postData);
        $retData = json_decode($res, 1);
        if(empty($retData)) {
            $this->load->view('redblack/statistics', $data);
        }
        $dayList = $retData['dayList'];
        $typeConfig = $retData['typeConfig'];
        $list = $retData['list'];
        $playNumArr = $winListArr = $betListArr = $totalArr = $betTotalArr = $winTotalArr = $betSubWinTotalArr =  [];
        $avgExpendList = $avgNumList = [];
        $redblackNumList = $retData['redblackNumList'] ?? [];
        foreach ($dayList as $day) {
            $redblack = $list[$day] ?? [];
            if(!empty($redblack)) {
                $betTotal = $redblack['betTotal'] ?? 0;
                $winTotal = $redblack['winTotal'] ?? 0;
                $betSubWinTotal = $betTotal - $winTotal;
                array_push($playNumArr, $redblack['playNum'] ?? 0);//红黑局数
                array_push($betTotalArr, $betTotal);//玩家下注筹码
                array_push($winTotalArr, $winTotal);//玩家赢得的筹码
                array_push($betSubWinTotalArr, $betSubWinTotal);//净筹码
                array_push($totalArr, $betTotal + $winTotal);//玩家赢得的筹码
                $this->getValue($typeConfig, $redblack['betList'], $betListArr);//获取玩家下注的筹码
                $this->getValue($typeConfig, $redblack['winList'], $winListArr);//获取玩家赢得的筹码
                $avgExpendList[$day] = empty($redblackNumList[$day]) ? 0 : round($betTotal / $redblackNumList[$day], 2);
                $avgNumList[$day] = empty( $redblackNumList[$day]) ? 0 : round($redblack['playNum'] / $redblackNumList[$day], 2);
            }else {
                array_push($playNumArr, 0);//红黑局数
                array_push($betTotalArr, 0);//玩家下注筹码
                array_push($winTotalArr, 0);//玩家赢得的筹码
                array_push($betSubWinTotalArr, 0);//净筹码
                array_push($totalArr, 0);//玩家赢得的筹码
                $this->getValue($typeConfig, [], $betListArr);
                $this->getValue($typeConfig, [], $winListArr);
                $avgExpendList[$day] = 0;
                $avgNumList[$day] = 0;
            }
        }

        $totalPlayerStr = "{name:'局数',data: [".implode(',',$playNumArr).']}';
        $totalPlayerStr .= ",{name:'人数',data: [".implode(',',$redblackNumList).']}';
        $totalPlayerStr .= ",{name:'人均玩牌局数',data: [".implode(',',$avgNumList).']}';
        $totalPlayerStr .= ",{name:'消耗', visible:false, data: [".implode(',',$betTotalArr).']}';
        $totalPlayerStr .= ",{name:'人均消耗', visible:false,data: [".implode(',',$avgExpendList).']}';

        $betTotalStr = "{name:'【消耗】总筹码',data: [".implode(',',$betTotalArr).']}';
        $winTotalStr = "{name:'【发放】总筹码',data: [".implode(',',$winTotalArr).']}';
        $betSubwinTotalStr = "{name:'净消耗筹码【消耗-发放】',data: [".implode(',',$betSubWinTotalArr).']}';
        $betStr = $winStr = $betSubWinStr = '';
        foreach ($typeConfig as $k => $v) {
            $betStr .= "{name:'【消耗】".$v."', visible:false ,data: [".implode(',',$betListArr[$k]).']},'."{name:'【发放】".$v."' ,visible:false ,data: [".implode(',',$winListArr[$k]).']},';
        }
        $dispalyStr = $betStr;
        $dispalyStr =  rtrim($dispalyStr, ',');
//        $data['totalStr'] = $totalStr;
        $data['displayStr'] = $betTotalStr.','.$winTotalStr.','.$betSubwinTotalStr.','.$dispalyStr;
        $data['dayList'] = $dayList;
        $data['totalPlayerStr'] = $totalPlayerStr;
        $this->load->view('redblack/statistics', $data);
    }




    /**
     * 获取红黑类型数据
     * @param $levelKey
     * @param $loseLevel
     * @param $loseLevelValue
     * @return mixed
     */
    private function getValue($key, $list, &$Value)
    {
        foreach ($key as $kl=>$v) {
            $Value[$kl] = isset($Value[$kl]) ? $Value[$kl] : [];
            array_push($Value[$kl], $list[$kl] ?? 0);
        }
        return $Value;
    }
    /**
     * 添加红黑筹码
     */
    public function addMoney()
    {
        $money= $this->input->post('money');

        if(empty($money)) {
            $this->response(1, '参数错误');
        }

        $url = ($this->env == 2) ? $this->config->item('localUrl') : $this->config->item('officialUrl');
        $postData = $this->postdata;

        $postData['money'] = $money;
        $postData['mod'] = 'Redblack';
        $postData['act'] = 'addRedblackMoney';
        ($this->env == 2) && $postData['demo'] = 1;
        $res = $this->curl->req($url, $postData);
        die($res);
    }

    /**
     * 删除红黑筹码
     */
    public function delMoney()
    {
        $money= $this->input->post('money');

        if(empty($money)) {
            $this->response(1, '参数错误');
        }

        $url = ($this->env == 2) ? $this->config->item('localUrl') : $this->config->item('officialUrl');
        $postData = $this->postdata;

        $postData['money'] = $money;
        $postData['mod'] = 'Redblack';
        $postData['act'] = 'deleteRedblackMoney';
        ($this->env == 2) && $postData['demo'] = 1;
        $res = $this->curl->req($url, $postData);
        die($res);
    }
}