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

/**
 * 新手收集活动配置
 */
class Collect extends MY_Controller
{
    /**
     * @var array Post 数据
     */
    private $postData;

    /**
     * @var string key名
     */
    private $key;

    /**
     * @var string 表名
     */
    private $tb;


    public function __construct()
    {
        parent::__construct();

        $this->postData = $this->input->post();
        $this->key = okeys::NewPlayerCollect();
        $this->tb = otable::NewUserCollectConf();
    }

    /**
     * 配置表单
     */
    public function index()
    {
        $conf = oo::commonOprModel('config')->getCommonConf($this->key);

        $data = [];
        $data['debug'] = $this->config->item('oos_debug') ? 1 : 0;
        $data['conf'] = $conf ?? ["switch" => 0, "lvStart" => 0, "cid" => 0];
        $data['imgNames'] = oo::commonOprModel('config')->getActiveImgNameConfig()['super_collect'];
        $data['collectType'] = [2 => "特殊收集", 3 => "特殊收集+偷", 4 => "特殊收集+打", 8 => "偷特殊", 9 => "打特殊"];

        $list = oo::commonOprDb('slavedb')->getAll("SELECT `id`,`tag_name`,`create_ts`,`update_ts` FROM {$this->tb}");
        foreach ($list as $item) {
            $data['list'][] = [
                "id" => $item['id'],
                "name" => $item['tag_name'],
                "createTime" => date('Y-m-d H:i:s', $item['create_ts']),
                "updateTime" => date('Y-m-d H:i:s', $item['update_ts']),
            ];
        }

        $this->load->view("collect/index", $data);
    }

    /**
     * 触发奖励配置变动
     */
    public function updateConfig()
    {
        $formData = $this->postData['conf'];

        $res = oo::commonOprModel('config')->saveCommonConf($this->key, $formData, "新手收集活动配置");
        if (!$res) $this->response(__LINE__ * -1, '保存失败');

        oo::commonOprRedis('games')->set($this->key, json_encode($formData, JSON_NUMERIC_CHECK));


        $sql = "UPDATE {$this->tb} SET `status`= 1 WHERE `id`= {$formData['cid']}";

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

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

    /**
     * 详细奖励配置信息
     */
    public function detail($id)
    {
        $data = oo::commonOprDb('slavedb')->getOne("SELECT * FROM {$this->tb} Where `id` = {$id}");

        $data['basic_conf'] = json_decode($data['basic_conf'], true);
        $data['reward'] = json_decode($data['reward'], true);

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

    /**
     * 详细奖励配置更新
     */
    public function updateDetail()
    {
        $formData = $this->postData;

        foreach ($formData['reward'] as &$item) {
            $item['prize'] = json_decode($item['prize'] ?? '[]', true);
        }

        foreach ($formData['basic_conf'] as $item2 => $val) {
            if ($item2 == "steal_success") {
                $formData['basic_conf']['steal_break'] = $val;
            }
            if ($item2 == "attack_success") {
                $formData['basic_conf']['attack_break'] = $val;
            }
        }
        $formData['update_ts'] = time();

        $sql = funs::db_updateSQL($this->tb, $formData, "id = " . $formData['id']);
        $ret = oo::commonOprDb('games')->query($sql);
        if (!$ret) {
            return $this->response(1, '操作失败');
        } else {
            $cache = [
                "act_skin" => $formData['act_skin'],
                "basic_conf" => $formData['basic_conf'],
                "reward" => $formData['reward'],
            ];
            oo::commonOprRedis('games')->set(okeys::NewPlayerCollectReward($formData['id']), json_encode($cache, JSON_NUMERIC_CHECK));
            return $this->response(0, '操作成功');
        }
    }


}
