<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 任务配置相关
*/
class Task extends MY_Controller
{
	public $postData = array('frombg' => 1, 'mod' => '', 'act' => '', 'lid' => 1, 'sid' => 1);
	public $data = array('referer' =>'', 'refresh' => 'false', 'state' => 'error', 'message' => '');

	protected $allSidDau = [];


	function __construct()
	{
		parent::__construct();
		$this->load->helper('cookie');
		$this->load->library('curl');
        $this->load->library('pagination');

		$this->env = get_cookie($this->_wenenv);
        //请求地址
        $this->_url = ($this->env == 2) ? $this->config->item('localUrl') : $this->config->item('officialUrl');
	}


	/**
    *   展示配置文件
    */
    public function index()
    {
        $data = array('list'=>[], 'total'=>0);
        if($this->env == 1){
            $url = $this->config->item('officialUrl');
            $baseUrl = $this->config->item('officialBaseUrl');
        }else{
            $url = $this->config->item('localUrl');
            $baseUrl = $this->config->item('localBaseUrl');
            $this->postData['demo'] = 1;
        }

        $this->postData['mod'] = 'Task';
        $this->postData['act'] = 'getConfig';

        $retData = $this->curl->req($url, $this->postData);
        $list = json_decode($retData, TRUE);
        if ($list) {
            $data['list'] = $list;
            $data['total'] = count($list);
        }
        $data['baseUrl'] = $baseUrl;
        $this->load->view('task/index', $data);
    }

    /**
	*	成就展示
	*/
	public function indexcj()
	{
		$data = array('list');
		if($this->env == 1){
            $url = $this->config->item('officialUrl');
            $baseUrl = $this->config->item('officialBaseUrl');
        }else{
            $url = $this->config->item('localUrl');
            $baseUrl = $this->config->item('localBaseUrl');
            $this->postData['demo'] = 1;
        }

        $this->postData['mod'] = 'Task';
        $this->postData['act'] = 'getCjConfig';

        $retData = $this->curl->req($url, $this->postData);
        $list = json_decode($retData, TRUE);
        if ($list) {
        	$data['list'] = $list;
        	$data['total'] = count($list);
        }
        $data['baseUrl'] = $baseUrl;
        $this->load->view('task/indexcj', $data);
	}


	/**
    *   编辑任务
    */
    public function edit($id = 1)
    {
        $data = array();
        if($this->env == 1){
            $url = $this->config->item('officialUrl');
            $baseUrl = $this->config->item('officialBaseUrl');
        }else{
            $url = $this->config->item('localUrl');
            $baseUrl = $this->config->item('localBaseUrl');
            $this->postData['demo'] = 1;
        }
        $data['baseUrl'] = $baseUrl;

        $this->postData['mod'] = 'Task';
        $this->postData['act'] = 'getOne';
        $this->postData['tid'] = $id;
        $retData = $this->curl->req($url, $this->postData);
        $info = json_decode($retData, TRUE);
        $data['tid'] = $id;
        $data['info'] = $info;
        $this->load->view('task/edit', $data);
    }

    /**
	*	编辑成就
	*/
	public function editcj($id = 1)
	{
		$data = array();
        if($this->env == 1){
            $url = $this->config->item('officialUrl');
            $baseUrl = $this->config->item('officialBaseUrl');
        }else{
            $url = $this->config->item('localUrl');
            $baseUrl = $this->config->item('localBaseUrl');
            $this->postData['demo'] = 1;
        }
        $data['baseUrl'] = $baseUrl;

        $this->postData['mod'] = 'Task';
        $this->postData['act'] = 'getOnecj';
        $this->postData['tid'] = $id;
        $retData = $this->curl->req($url, $this->postData);
        $info = json_decode($retData, TRUE);
        $data['tid'] = $id;
        $data['info'] = $info;
        $this->load->view('task/editcj', $data);
	}


	/**
    *   编辑任务表单
    */
    public function actionEdit()
    {
        $this->postData['tid'] = $this->input->post('tid');
        $this->postData['tc_zh_name'] = $this->input->post('tc_zh_name');
        $this->postData['tc_name'] = $this->input->post('tc_name');
        $this->postData['tc_sort'] = $this->input->post('tc_sort');
        $this->postData['tc_goto'] = $this->input->post('tc_goto');
        $this->postData['rewardNum'] = json_encode($this->input->post('rewardNum'));
        $this->postData['rewardMoney'] = json_encode($this->input->post('rewardMoney'));

        $this->postData['mod'] = 'Task';
        $this->postData['act'] = 'editTask';

        if($this->env == 1){
            $url = $this->config->item('officialUrl');
        }else{
            $url = $this->config->item('localUrl');
            $this->postData['demo'] = 1;
        }
        $res = $this->curl->req($url, $this->postData);
        $retData = json_decode($res, TRUE);

        if( $retData['num'] > 0 ){
            $this->data['state'] = 'success';
            $this->data['message'] = '更新成功';
        }else{
            $this->data['message'] = '更新失败';
            $this->data['num'] = $retData['num'];
        }
        die(json_encode($this->data));
    }

    /**
	*	编辑成就表单
	*/
	public function actioncjEdit()
	{
		$this->postData['tid'] = $this->input->post('tid');
        $this->postData['zh_name'] = $this->input->post('zh_name');
        $this->postData['name'] = $this->input->post('name');
        $this->postData['sort'] = $this->input->post('sort');
        $this->postData['rewardNum'] = json_encode($this->input->post('rewardNum'));
        $this->postData['rewardMoney'] = json_encode($this->input->post('rewardMoney'));

        $this->postData['mod'] = 'Task';
        $this->postData['act'] = 'editCj';

        if($this->env == 1){
            $url = $this->config->item('officialUrl');
        }else{
            $url = $this->config->item('localUrl');
            $this->postData['demo'] = 1;
        }
        $res = $this->curl->req($url, $this->postData);
        $retData = json_decode($res, TRUE);

        if( $retData['num'] > 0 ){
            $this->data['state'] = 'success';
            $this->data['message'] = '更新成功';
        }else{
            $this->data['message'] = '更新失败';
            $this->data['num'] = $retData['num'];
        }
        die(json_encode($this->data));
	}

	public function statistics()
    {
        $data = array('stime' => '', 'etime' => '');

        //时间查询
        $dateArr = array();
        if( $stime = get_cookie('task_stime') ){
            $data['stime'] = $stime;
            $dateArr['stime'] = strtotime($stime.'0:0:1');
        }else{
            $dateArr['stime'] = strtotime('-7 days 0:0:1');//默认开始时间
            $data['stime'] = date('Y-m-d', $dateArr['stime']);
        }

        if( $etime = get_cookie('task_etime') ){
            $data['etime'] = $etime;
            $dateArr['etime'] = strtotime($etime.'23:59:59');
        }else{
            $dateArr['etime'] = strtotime('-1 day 23:59:59');//默认结束时间
            $data['etime'] = date('Y-m-d', $dateArr['etime']);
        }
        $postData = array_merge($dateArr, $this->postData);
        //获取DAU数据
        $sidList = $this->_getSidList();
        $postData['sidList'] = json_encode($sidList);
        $postData['mod'] = 'Statistics';
        $postData['act'] = 'loginplayer';
        $this->env == 2 && $postData['demo'] = 1;
        $res = $this->curl->req($this->_url, $postData);
        $retData = json_decode($res, TRUE);
        if ($retData){
            $allSidDau = [];
            foreach ($retData as $tempDauArr){
                $tempDau = intval( $tempDauArr[0]['total'] );
                array_push($allSidDau, $tempDau);
            }
            $this->allSidDau = $allSidDau;
        }

        //获取数据
        $postData['mod'] = 'Task';
        $postData['act'] = 'statistics';
        $this->env == 2 && $postData['demo'] = 1;
        $res = $this->curl->req($this->_url, $postData);
        $retData = json_decode($res, TRUE);
        $dayList = $retData['dayList'];
        $data['dayList'] = $dayList;
        $configList = $retData['configList'];
        $taskList = $retData['list'];
        $subtaskList = $retData['subtaskList'];
        $str = '';
        $len = count($dayList);
        $taskCName = [];
        foreach ($configList as $k => $v) {
            $id = $v['id'];
            $str .= "{ name:'".$v['zh_name']."',data:[";
            foreach ($dayList as $k => $day) {
                $info = $taskList[$day] ?? [];
                $tmpNum = $info['reward'][$id] ?? 0;
                $loginPlayer = $info['loginPlayer'] ?? 0;
                //empty($loginPlayer) ? $per = 0 : $per = round($tmpNum / $loginPlayer, 2);
                $per = $tmpNum;
                if($k == $len - 1) {
                    $str .= $per;
                }else {
                    $str .= $per . ',';
                }
            }
            $taskCName[$id] = $v['zh_name'];
           $str .=  "]},";
        }

        //循环处理子任务
        $substr = $ratestr = '';
        foreach ( $subtaskList as $key => $val){
            $keyArr = explode('_', $key);
            $subid = reset($keyArr);
            $taskname = $taskCName[$subid];
            $taskname .= '_'.end($keyArr);
            $substr .= $subid <= 3 ? "{ name:'".$taskname."',data:[" : "{ name:'".$taskname."',visible: false, data:[";
            //$substr .= "'".implode("','",$val)."'";
            $substr .= implode(", ",$val);
            $substr .=  "]},";

            //处理每个任务的完成率
            $rateName = $taskname.'_完成率';
            $ratestr .= $subid <= 1 ? "{ name:'".$rateName."',data:[" : "{ name:'".$rateName."',visible: false, data:[";
            $ratestr .= implode(", ", $this->getRage($val));
            $ratestr .=  "]},";
        }

        $str = rtrim($str, ',');
        $substr = rtrim($substr, ',');
        $ratestr = rtrim($ratestr, ',');

        $data['info'] = $str;
        $data['subtaskinfo'] = $substr;
        $data['subtaskrateinfo'] = $ratestr;
        $this->load->view('task/statistics', $data);
    }

    /**
     * 获取任务的完成率
     */
    protected function getRage($tasknum = []){
        if (empty($this->allSidDau) || empty($tasknum)){
            return [];
        }
        $arr = [];
        foreach ($tasknum as $k => $num){
            $dau = $this->allSidDau[$k];
            $rate = $dau == 0 ? 0 : round($num / $dau, 2);
            array_push($arr, $rate);
        }
        return $arr;
    }

}