<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by liqx.
 * User: Administrator
 * Date: 2018/1/31
 * Time: 11:35
 */
class Events extends MY_Controller
{
    public $postData = array('frombg' => 1, 'mod' => '', 'act' => '', 'lid' => 1, 'sid' => 1);

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('cookie');
        $this->load->library('curl');
        //请求地址

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

    /**
     * 活动列表
     */
    public function index()
    {
        $postData = $this->postData;
        $postData['mod'] = 'Config';
        $postData['act'] = 'getEventConfig';
        $this->env == 2 && $postData['demo'] = 1;
        $res = $this->curl->req($this->_url, $postData);
        $list['list'] = json_decode($res, TRUE);

        $this->load->view('event/index', $list);

    }

    /**
     * 加载新增活动页面
     */
    public function add()
    {
        if(IS_POST){
            $event = $this->input->post();
            $input = array_filter($event);
            if(count($event) != count($input)) {
                die($this->response(1, '不能留空'));
            }
            $postData = $this->postData;
            $postData['name'] = $input['name'];
            $postData['event_id'] = $input['event_id'];
            $postData['mean'] = json_encode($input['mean'],JSON_UNESCAPED_UNICODE);
            $postData['ids'] = json_encode($input['ids'],JSON_UNESCAPED_UNICODE);

            $this->env == 2 && $postData['demo'] = 1;
            $postData['mod'] = 'Config';
            $postData['act'] = 'saveEventConfig';
            $postData['action'] = 'add';
            $res = $this->curl->req($this->_url, $postData);
            if($res == 0){
                return $this->response(1, '操作失败');
            }else{
                return $this->response(0, '操作成功');
            }
        }
        $this->load->view('event/add');
    }


    /**
     * 加载编辑活动页面
     */
    public function edit($event_id)
    {
        if(IS_POST){
            $event = $this->input->post();
            $input = array_filter($event);
            if(count($event) != count($input)) {
                die($this->response(1, '不能留空'));
            }
            $postData = $this->postData;
            $postData['id'] = $input['id'];
            $postData['name'] = $input['name'];
            $postData['event_id'] = $input['event_id'];
            $postData['mean'] = json_encode($input['mean'],JSON_UNESCAPED_UNICODE);
            $postData['ids'] = json_encode($input['ids'],JSON_UNESCAPED_UNICODE);
            $this->env == 2 && $postData['demo'] = 1;
            $postData['mod'] = 'Config';
            $postData['act'] = 'saveEventConfig';
            $postData['action'] = 'edit';
            $res = $this->curl->req($this->_url, $postData);
            if($res == 0){
                return $this->response(1, '操作失败');
            }else{
                return $this->response(0, '操作成功');
            }
        }


        $postData = $this->postData;
        $this->env == 2 && $postData['demo'] = 1;
        $postData['mod'] = 'Config';
        $postData['act'] = 'getEventConfig';
        $postData['event_id'] = $event_id;
        $res = $this->curl->req($this->_url, $postData);
        $temp = json_decode($res,true);
        $data['info'] = $temp['data'];
        $data['info']['mean'] = json_decode($data['info']['mean'],true);
        $data['info']['ids'] = json_decode($data['info']['ids'],true);
        $this->load->view('event/edit', $data);
    }

    public function delete(){
        if(IS_POST){
            $event = $this->input->post();
            $postData = $this->postData;
            $postData['id'] = $event['id'];
            $this->env == 2 && $postData['demo'] = 1;
            $postData['mod'] = 'Config';
            $postData['act'] = 'delEventConfig';
            $res = $this->curl->req($this->_url, $postData);
            if($res == 0){
                return $this->response(1, '操作失败');
            }else{
                return $this->response(0, '操作成功');
            }
        }
    }
}