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

/**
 * 系统设置相关
 */
class systemCompensation extends MY_Controller
{
    public $postData = array('frombg' => 1, 'lid' => 1, 'sid' => 1);
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('cookie');
        $this->load->library('curl');
        $this->load->library('pagination');
        $this->env = get_cookie($this->_wenenv);
    }

    public function index(){
        $tb = otable::SysCompenConfig();
        $ret= oo::commonOprDb('slavedb')->getAll("SELECT * FROM {$tb}",MYSQLI_ASSOC);
        $data['list'] = $ret;
        $data['langList'] = array_keys($this->_langList);
        $data['version']  = oo::commonOprModel('config')->versions;
        $data['device']  = oo::commonOprModel('config')->device;
        $data['country']  = oo::commonOprModel('config')->country;

        $this->load->view('systemCompensation/index', $data);
    }

    public function actionAdd(){
        $post = $this->input->post();
        if($post['reward']['spins'] > 200){
        	$data['state'] = 'error';
            $data['message'] = '添加失败，体力不能大于200';
            $data['refresh'] = false;
            die(json_encode($data));
        }
        $lang = array_keys($this->_langList);
        $content = [];
        foreach ($lang as $row){
            $content[strtoupper($row)] = str_replace("'",'',$post['content_'.$row]);
            unset($post['content_'.$row]);
        }
        $post['loginTime'] = explode(' - ',$post['loginTime']);

        $tb = otable::SysCompenConfig();
        $insert = [
            'id'        => NULL,
            'startTime' => strtotime($post['startTime']),
            'endTime'   => strtotime($post['endTime']),
            'loginTime' => $post['loginTimeSwitch'] ? strtotime($post['loginTime'][0]).",".strtotime($post['loginTime'][1]) : 0,
            'levelId'   => $post['levelIdSwitch'] ? $post['levelIdMin'].",".$post['levelIdMax'] : 0,
            'payNum'    => $post['paySwitch']  ? $post['payMin'].",".$post['payMax'] : 0,
            'device'    => $post['versionDevice'],
            'version'   => $post['versionMin'].",".$post['versionMax'],
            'country'   => $post['countrySwitch'] ? json_encode($post['country']) : "[]",
            'content'   => addslashes(json_encode($content)),
            'reward'    => json_encode($post['reward']),
            'remark'    => $post['remark'],
            'addTime'   => time()
        ];
        $key   = implode(',',array_keys($insert));
        $value = "'".implode("','",array_values($insert))."'";
        oo::commonOprDb('games')->query("INSERT INTO {$tb} ({$key}) VALUES({$value})");
        $ret= oo::commonOprDb('slavedb')->getOne("SELECT * FROM {$tb} ORDER BY id DESC ",MYSQLI_ASSOC);
        $insert['id'] = $ret['id'];
        $insert['content'] = json_encode($content);
        oo::commonOprRedis('games')->hSet(okeys::SysCompenConfig(),$ret['id'],json_encode($insert));
        $data['state'] = 'success';
        $data['message'] = '添加成功';
        $data['refresh'] = true;
        die(json_encode($data));
    }

    public function edit($id){
        $tb = otable::SysCompenConfig();
        $ret= oo::commonOprDb('slavedb')->getOne("SELECT * FROM {$tb} WHERE id={$id}",MYSQLI_ASSOC);
        $ret['reward']  = json_decode($ret['reward'],true);
        $ret['content'] = json_decode($ret['content'],true);
        $ret['country'] = json_decode($ret['country'],true);
        $ret['loginTime'] = $ret['loginTime']   == "0" ? 0 : explode(',',$ret['loginTime']);
        $ret['levelId']   = $ret['levelId']     == "0" ? 0 : explode(',',$ret['levelId']);
        $ret['payNum']    = $ret['payNum']      == "0" ? 0 : explode(',',$ret['payNum']);
        $ret['version']   = $ret['version']     == "0" ? 0 : explode(',',$ret['version']);
        $data['list'] = $ret;
        $data['id'] = $id;
        $data['langList'] = array_keys($this->_langList);
        $data['version']  = oo::commonOprModel('config')->versions;
        $data['device']  = oo::commonOprModel('config')->device;
        $data['country']  = oo::commonOprModel('config')->country;
        $this->load->view('systemCompensation/edit', $data);
    }

    public function actionEdit(){
        $post = $this->input->post();
        if($post['reward']['spins'] > 200){
        	$data['state'] = 'error';
            $data['message'] = '修改失败，体力不能大于200';
            $data['refresh'] = false;
            die(json_encode($data));
        }
        
        $lang = array_keys($this->_langList);
        $content = [];
        foreach ($lang as $row){
            $content[strtoupper($row)] = str_replace("'",'',$post['content_'.$row]);
            unset($post['content_'.$row]);
        }
        $post['content']  = $content;
        $post['loginTime'] = explode(' - ',$post['loginTime']);
        $insert = [
            'id'        => $post['id'],
            'startTime' => strtotime($post['startTime']),
            'endTime'   => strtotime($post['endTime']),
            'loginTime' => $post['loginTimeSwitch'] ? strtotime($post['loginTime'][0]).",".strtotime($post['loginTime'][1]) : 0,
            'levelId'   => $post['levelIdSwitch'] ? $post['levelIdMin'].",".$post['levelIdMax'] : 0,
            'payNum'    => $post['paySwitch']  ? $post['payMin'].",".$post['payMax'] : 0,
            'device'    => $post['versionDevice'],
            'version'   => $post['versionMin'].",".$post['versionMax'],
            'country'   => $post['countrySwitch'] ? json_encode($post['country']) : "[]",
            'content'   => addslashes(json_encode($content)),
            'reward'    => json_encode($post['reward']),
            'remark'    => $post['remark'],
        ];
        $update = "";
        foreach ($insert as $key=>$row){
            $update .= "{$key}='{$row}',";
        }
        $update = trim($update,',');
        $tb = otable::SysCompenConfig();
        oo::commonOprDb('games')->query("UPDATE {$tb} SET {$update} WHERE id={$post['id']}");
        $insert['content'] = json_encode($content);
        oo::commonOprRedis('games')->hSet(okeys::SysCompenConfig(),$post['id'],json_encode($insert));
        oo::commonOprRedis('games')->delete(okeys::UserSysCompen($post['id']));
        $data['state'] = 'success';
        $data['message'] = '添加成功';
        $data['refresh'] = true;
        die(json_encode($data));
    }

    public function actionDel(){
        $id = $this->input->post('id');
        $tb = otable::SysCompenConfig();
        oo::commonOprDb('games')->query("DELETE FROM {$tb} WHERE id={$id} LIMIT 1");
        oo::commonOprRedis('games')->delete(okeys::UserSysCompen($id));
        oo::commonOprRedis('games')->hDel(okeys::SysCompenConfig(),$id);
        $data['state'] = 'success';
        $data['message'] = '添加成功';
        $data['refresh'] = true;
        die(json_encode($data));
    }
}