<?php

/**
 * 签到配置相关
 */
class SignIn extends MY_Controller
{
    /**
     * @var array Post 数据
     */
    private $postData;

    private $type;

    const WEEKLY = 'weekly';
    const PROGRESS = 'progress';

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

        $this->postData = $this->input->post();
    }

    //region 主题

    /**
     * 主题配置列表
     */
    public function themeRes()
    {
        $data['dirList'] = json_encode(array_column(oo::commonOprModel('config')->getActiveImgNameConfig("signIn", true)['content'], "type"));
        $data['selDir'] = oo::commonOprModel('config')->getSysCon('Game', "signInDir");
        $this->load->view('signIn/themeRes', $data);
    }

    /**
     * 主题更新
     */
    public function themeResUpdate()
    {
        $ret = oo::commonOprModel('config')->setSysCon('Game', "signInDir", $this->postData['selDir']);
        if (!$ret) {
            return $this->response(1, '操作失败');
        } else {
            return $this->response(0, '操作成功', $this->postData);
        }
    }

    //endregion

    //region 七天签到

    /**
     * 配置列表
     */
    public function weeklySignIn()
    {
        $this->type = self::WEEKLY;

        $this->showView(__FUNCTION__);
    }


    /**
     * 配置文件上传
     */
    public function weeklySignInImport()
    {
        $this->type = self::WEEKLY;

        $this->importConfigExcelFile();
    }

    //endregion

    //region 累积签到

    /**
     * 配置列表
     */
    public function countSignIn()
    {
        $this->type = self::PROGRESS;

        $this->showView(__FUNCTION__);
    }

    //endregion

    //region 配置更新
    public function configUpdate()
    {
        $ret = oo::commonOprModel('SignIn')->updateSignInConfig($this->postData);

        if ($ret == -1) {
            $msg = '奖励配置出错';
        } else {
            $msg = '操作成功';
        }

        return $this->response($ret, $msg);
    }

    public function countSignInImport()
    {
        $this->type = self::PROGRESS;

        $this->importConfigExcelFile();
    }

    //endregion

    /**
     * 列表通用展示
     */
    private function showView($view)
    {
        $data = [];

        //数数分组
        $data['ssGroupList'] = array_column(oo::commonOprModel('config')->getUserGroupConfigByCid(5), 'name', 'id');

        //配置列表
        $data["configList"] = oo::commonOprModel('SignIn')->configList($this->type);

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

    /**
     * 配置文件上传
     */
    private function importConfigExcelFile()
    {
        $this->load->library('uploader', [
            'savePath' => 'Excel/uploads',
            'maxSize' => 1024 * 50,
            'allowFiles' => array('.xls', '.xlsx', '.json', '.png', '.jpg')]);

        $fileContent = $this->uploader->getFileInfo();

        $response = oo::commonOprModel('SignIn')->importConfig($fileContent, $this->type);
        if ($response[0] === -1) {
            return $this->response($response[0], $response[1]);
        }

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