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

/**
 * 聊天
 */
class Chat extends MY_Controller
{
    public $postData = array('frombg' => 1, 'lid' => 1, 'sid' => 1);
    public $staticPath = "./public/game/config/";
    protected $page = 10;//分页

    protected $pageConfig = array(
        'first_tag_open' => '<li>',
        'first_tag_close' => '</li>',
        'last_tag_open' => '<li>',
        'last_tag_close' => '</li>',
        'next_tag_open' => '<li>',
        'next_tag_close' => '</li>',
        'prev_tag_open' => '<li>',
        'prev_tag_close' => '</li>',
        'cur_tag_open' => "<li class='active'><a hef='#'>",
        'cur_tag_close' => "</a></li>",
        'num_tag_open' => "<li>",
        'num_tag_close' => "</li>",
    );
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('cookie');
        $this->load->library('curl');
        $this->load->library('pagination');
    }
    public function SaveJson($data,$filename){
        $dir = $this->staticPath;
        if (!is_dir($dir))
        {
            $old = umask(0);
            mkdir($dir,0775 , true);
            umask($old);
        }
        $filepath = "{$dir}{$filename}";
        file_put_contents($filepath, json_encode($data,JSON_NUMERIC_CHECK));

        if(method_exists($this->common,'upToOss')){
            $dir = str_replace("./","",$dir);
            $file_path = str_replace("\\","/",FCPATH . $dir.$filename);

            $this->common->upToOss($dir.$filename,$file_path);
        }
    }
    public function prohibit(){
        $url = $this->config->item('officialUrl');
        $this->postData['mod'] = 'Config';
        $this->postData['act'] = 'chatProhibit';
        $retData = $this->curl->req($url, $this->postData);
        $data['content'] = $retData;
        $this->load->view('chat/index', $data);
    }

    public function save(){
        $url = $this->config->item('officialUrl');
        $postData = $this->input->post('content');
        $filename = "Prohibit".time().".json";
        $posts = $this->postData;
        $posts['mod'] = 'Config';
        $posts['act'] = 'saveChatProhibit';
        $posts['content'] = $postData;
        $posts['filename'] = $filename;
        $this->curl->req($url, $posts);
        $data = explode('||',$postData);
        $this->SaveJson($data,$filename);
        $return['state'] = 'success';
        $return['message'] = '成功';
        $return['refresh'] = true;
        die(json_encode($return));
    }

    public function report($page = 0){
        if($page == 0){
            $page = 10;
        }else{
            $page += 10;
        }

        $url = $this->config->item('officialUrl');
        $this->postData['mod'] = 'Statistics';
        $this->postData['act'] = 'chatReport';
        $this->postData['page'] = $page/10;
        $this->postData['page_size'] = 10;
        $retData = $this->curl->req($url, $this->postData);
        $retData = json_decode($retData,true);

        $data['list'] = $retData['data'];
        $data['total'] = $retData['total'];
        $pageConfig = $this->pageConfig;
        $pageConfig['base_url'] = base_url('#chat/report');
        $pageConfig['total_rows'] = $retData['total'];
        $pageConfig['per_page'] = $this->page;
        $this->pagination->initialize($pageConfig);
        $pageurl = $this->pagination->create_links();
        $data['pageurl'] = $pageurl;
        $this->load->view('chat/report', $data);
    }

    public function reportContent($touid){
        $url = $this->config->item('officialUrl');
        $this->postData['mod'] = 'Statistics';
        $this->postData['act'] = 'chatReport';
        $this->postData['touid'] = $touid;
        $retData = $this->curl->req($url, $this->postData);
        $retData = json_decode($retData,true);
        $data['list'] = $retData;
        $this->load->view('chat/content', $data);
    }

    public function lockList($page = 0){
        if($page == 0){
            $page = 10;
        }else{
            $page += 10;
        }

        $url = $this->config->item('officialUrl');
        $this->postData['mod'] = 'user';
        $this->postData['act'] = 'chatLockList';
        $this->postData['page'] = $page/10;
        $this->postData['page_size'] = 10;
        $retData = $this->curl->req($url, $this->postData);
        $retData = json_decode($retData,true);

        $data['list'] = $retData['data'];
        $data['total'] = $retData['total'];
        $pageConfig = $this->pageConfig;
        $pageConfig['base_url'] = base_url('#chat/report');
        $pageConfig['total_rows'] = $retData['total'];
        $pageConfig['per_page'] = $this->page;
        $this->pagination->initialize($pageConfig);
        $pageurl = $this->pagination->create_links();
        $data['pageurl'] = $pageurl;
        $this->load->view('chat/lock', $data);
    }
}