<?php


defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 配置相关
 */
class Ad extends MY_Controller
{
    public function __construct()
    {
//        date_default_timezone_set("Asia/Shanghai");
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('cookie');
        $this->load->library('curl');
        $this->load->library('pagination');
        $this->env = get_cookie($this->_wenenv);
        $this->_url = $this->config->item('officialUrl');
        //判断model是否存在
        if (file_exists(APPPATH.'models/Common.php'))
        {
            $this->load->model('common');
        }
        $levelTiele = oo::cfg('lang')['lang_level'];
        $this->level_title = array_merge(["关卡"],array_values($levelTiele),['图标ID','奖励','奖励配置']);
    }
    public function list()
    {
        $tb = otable::adConfig();
        $ret = oo::commonOprDb('slavedb')->getAll("SELECT * FROM {$tb}", MYSQLI_ASSOC);
        $this->load->view('ad/list', ['list' => $ret]);
    }

    public function edit($id)
    {
        $tb = otable::adConfig();
        $ret = oo::commonOprDb('slavedb')->getOne("SELECT * FROM {$tb} WHERE id={$id}", MYSQLI_ASSOC);
        $ret['groups'] = json_decode($ret['groups'],true);
        $shushuGroups = array_column(oo::commonOprModel('config')->getUserGroupConfigByCid(5),'remark','id');
        $configSSGroupShow = oo::commonOprRedis('statistics')->hGetAll(okeys::configSSGroupShow());
        $hideSSgroups = array_keys($configSSGroupShow);
        foreach ($shushuGroups as $key => $row) {
            if (in_array($key, $hideSSgroups)) continue;
            if($key==0){
                $shushuGroups[$key]="数数-默认分组";
            }
        }
        $this->load->view('ad/edit', ['info' => $ret, 'shushuGroups' => $shushuGroups]);
    }

    public function action()
    {
        $postData = $this->input->post();
        $tb = otable::adConfig();
        $postData['ext'] = $postData['ext']?$postData['ext']:"{}";
        $postData['groups'] = $postData['groups'] ? json_encode($postData['groups']) : '[]';
        if($postData['id']){
            $w = " id={$postData['id']}";
            unset($postData['id']);
            $sql = funs::db_updateSQL($tb,$postData,$w);
        }else{
            $sql = funs::db_insertSQL($tb,$postData);
        }
        $ret = oo::commonOprDb('games')->query($sql);
        if(!$ret){
            $postData['sql'] = $sql;
            return $this->response(1, '操作失败',$postData);
        }
        oo::commonOprRedis('games')->delete("ads:config:list");
        oo::logs()->adminlog($this->router->fetch_method());

        return $this->response(0, '操作成功',$postData);
    }
    // 弃用
    public function resaveJson(){
        $tb = otable::adConfig();
        $ret = oo::commonOprDb('slavedb')->getAll("SELECT * FROM {$tb}", MYSQLI_ASSOC);
        foreach ($ret as $k=>$item){
            $item['groups'] = json_decode($item['groups'],true);
            $item['reward'] = json_decode($item['reward'],true);
            $item['ext'] = json_decode($item['ext'],true);
            $ret[$k] = $item;
        }
//        $jsonInfo = json_encode($ret);
        $filename = 'AdConfig' . time() . '.json';
        oo::commonOprModel("json")->SaveJson($ret, $filename);
        oo::commonOprModel("json")->setStaticConfig("AdConfig", $filename);
        echo '<a type="button" class="btn btn-outline btn-success"  href="#ad/list/">刷新成功，点击返回</a>';
    }
    // 体力不足广告转盘
    public function spinSlot(){
        $key = okeys::adSpinSlotConf();
        $data = oo::commonOprModel('config')->getCommonConf($key);
        $this->load->view('ad/spinSlot', $data);
    }
    public function actionSpinSlot(){
        $postData = $this->input->post();
//        $confArr = json_decode($postData['list'],true);
//        return $this->response(0, '操作成功',$postData);
        $key = okeys::adSpinSlotConf();
        $ret = oo::commonOprModel('config')->saveCommonConf($key,$postData,"体力不足广告转盘");
        oo::commonOprRedis('games')->delete($key);
        if($ret){
            return $this->response(0, '操作成功',$postData);
        }else{
            return $this->response(1, '操作失败',$postData);
        }
    }

    // 奖励转盘 广告 配置表
    public function adSlotGroupRewards(){
        $configs = AdModel::getAdSlotGroupRewards();
        $totalGroups = array_column(oo::commonOprModel('config')->getUserGroupConfigByCid(5),'name','id');
        $ssGroups = oo::commonOprModel('config')->hideSSGroup($totalGroups);
        $this->load->view('ad/adSlotGroupRewards', ['list' => $configs, 'ssGroups' => $ssGroups]);
    }
}