<?php
defined('BASEPATH') or exit('No direct script access allowed');

/**
 * 副老虎机相关配置
 */
class AssistantSlots extends MY_Controller
{
    private $rewardBasicTitle = ['等级', '金币', '骰子'];
    private $weightTitle = ['等级', '金币<br/>(权重)', '骰子<br/>(权重)', '能量<br/>(权重)', 'WILD<br/>(权重)', 'JACKPOT<br/>(权重)', '收集物<br/>(权重)'];

    /***
     * 基础配置
     */
    public function basicConf()
    {
        $data = oo::commonOprModel('config')->getCommonConf(okeys::AssistantSlotsBasicCfg());

        $data['themeList'] = oo::commonOprModel('config')->getActiveImgNameConfig("assistant_slots")['content'] ?? [];

        $this->load->view('assistantSlots/basicConf', $data);
    }

    /***
     * 更新基础配置
     */
    public function updateBasicConf()
    {
        $postData = $this->input->post();

        $key = okeys::AssistantSlotsBasicCfg();
        oo::commonOprModel('config')->saveCommonConf($key, $postData, "副老虎机基础配置");
        oo::commonOprRedis('games')->set($key, json_encode($postData, JSON_NUMERIC_CHECK));

        $this->response(1, '保存成功');
    }

    /***
     * 奖励倍率配置
     */
    public function rewardRateConf()
    {
        $data = oo::commonOprModel('config')->getCommonConf(okeys::AssistantSlotsRewardRateCfg());

        $this->load->view('assistantSlots/rewardRateConf', $data);
    }

    /***
     * 更新奖励倍率配置
     */
    public function updateRewardRateConf()
    {
        $postData = $this->input->post();

        $key = okeys::AssistantSlotsRewardRateCfg();
        oo::commonOprModel('config')->saveCommonConf($key, $postData, "副老虎机奖励倍率配置");
        oo::commonOprRedis('games')->set($key, json_encode($postData, JSON_NUMERIC_CHECK));

        $this->response(1, '保存成功');
    }


    /***
     * 金币能量基准配置
     */
    public function rewardBasicConf()
    {
        $data['title'] = $this->rewardBasicTitle;

        $tb = otable::assistantSlotRewardBasicCfg();
        $data['list'] = oo::commonOprDb('slavedb')->getAll("select * from {$tb}");

        error_log(json_encode($data));
        $this->load->view('assistantSlots/rewardBasicConf', $data);
    }

    /***
     * 更新金币能量基准配置
     */
    public function updateRewardBasicConf($basicConf = [])
    {
        $basicConf = $basicConf ?: $this->input->post('data');

        $tb = otable::assistantSlotRewardBasicCfg();
        foreach ($basicConf as $row) {
            $lv = $row['lv'];
            $coins = $row['coins'];
            $spins = $row['spins'];
            $sql = "REPLACE INTO {$tb} (`lv`,`coins`,`spins`) VALUES ('$lv','$coins','$spins')";
            $retData = oo::commonOprDb('games')->query($sql);

            if (!$retData) {
                $this->response(1, '操作失败');
            }
        }

        return $this->response(0, '操作成功');
    }

    /***
     * 导入金币能量基准配置
     */
    public function importRewardBasicConf()
    {
        $config = array(
            'savePath' => 'Excel/uploads',             //存储文件夹
            'maxSize' => 1024 * 50,                   //允许的文件最大尺寸，单位KB
            'allowFiles' => array('.xls', '.xlsx', '.json', '.png', '.jpg')  //允许的文件格式
        );
        $this->load->library('uploader', $config);
        $this->load->library('PHPExcel');
        $this->load->library('PHPExcel/PHPExcel/IOFactory');
        $dataArray = $this->getExcelSheetsArray();

        $confArray = $dataArray[0] ?? [];

        unset($confArray[0]);
        $basicConf = [];
        foreach ($confArray as $index => $item) {
            list($lv, $coins, $spins) = $item;
            $basicConf[] = ['lv' => $lv, 'coins' => $coins, "spins" => $spins];
        }

        return $this->updateRewardBasicConf($basicConf);
    }

    /***
     * 权重配置
     */
    public function weightConf($type = 1)
    {
        $tb = otable::assistantSlotWeightCfg();
        $where = " where type='{$type}' ";
        $sql = "SELECT * from {$tb} {$where}";
        $rs = oo::commonOprDb('slavedb')->getAll($sql);
        $list = [];
        foreach ($rs as $k => $v) {
            $list[$v['lv']] = json_decode($v['slotpro'], true);
        }

        $data['title'] = $this->weightTitle;
        $data['list'] = $list;
        $data['type'] = $type;
        $this->load->view('assistantSlots/weightConf', $data);
    }

    /**
     * 更新权重配置
     */
    public function updateWeightConf($type)
    {
        $data = $this->input->post('data');

        foreach ($data as $row) {
            $lv = $row[0];
            unset($row[0]);

            $conf = [];
            foreach ($row as $id => $v) {
                $conf[] = [
                    "id" => $id,
                    "v" => $v
                ];
            }

            $this->saveRealSlotWeightCfg($lv, $type, $conf);
        }

        return $this->response(0, '操作成功');
    }

    /***
     * 导入权重配置
     */
    public function importWeightConf()
    {
        $config = array(
            'savePath' => 'Excel/uploads',             //存储文件夹
            'maxSize' => 1024 * 50,                   //允许的文件最大尺寸，单位KB
            'allowFiles' => array('.xls', '.xlsx', '.json', '.png', '.jpg')  //允许的文件格式
        );
        $this->load->library('uploader', $config);
        $this->load->library('PHPExcel');
        $this->load->library('PHPExcel/PHPExcel/IOFactory');
        $dataArray = $this->getExcelSheetsArray();

        foreach ($dataArray as $idx => $weightConf) {
            if (empty($weightConf)) {
                continue;
            }
            unset($weightConf[0]);
            foreach ($weightConf as $item) {
                // 等级	金币	骰子	能量	WILD	JACKPOT	收集物
                $lv = $item[0];
                unset($item[0]);
                $conf = [];
                foreach ($item as $id => $v) {
                    $conf[] = [
                        "id" => $id,
                        "v" => $v
                    ];
                }

                $type = $idx + 1;

                if (!$this->saveRealSlotWeightCfg($lv, $type, $conf)) {
                    return $this->response(1, '操作失败');
                }
            }
        }

        return $this->response(0, '操作成功');
    }

    /**
     * 保存老虎机权重配置
     */
    private function saveRealSlotWeightCfg(int $lv, int $type, array $conf)
    {
        $slotpro = json_encode($conf, JSON_NUMERIC_CHECK);
        $tb = otable::assistantSlotWeightCfg();

        $sql = "REPLACE INTO {$tb} (`lv`,`type`,`slotpro`) VALUES ('$lv','$type','$slotpro')";

        $rs = oo::commonOprDb('games')->query($sql);

        // todo 删除缓存
//        oo::commonOprRedis('games')->hset(okeys::AssistantSlotsWeightConfig($lv), $type, $slotpro);

        return $rs;
    }
}
