<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/5 0005
 * Time: 14:32
 */
class Speaker 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);
    }

    /**
     * 加载大喇叭列表页面
     */
    public function index()
    {
        $this->load->view('speaker/index');
    }

    /**
     * 请求大喇叭数据
     */
    public function indexData()
    {
        $url = ($this->env == 2) ? $this->config->item('localUrl') : $this->config->item('officialUrl');
        $postData = $this->postdata;
        $postData['mod'] = 'Speaker';
        $postData['act'] = 'getPushList';
        $postData['page'] = $this->input->post('start') ? $this->input->post('start') :  0;
        $postData['limit'] = $this->input->post('length') ? $this->input->post('length') : 10;
        ($this->env == 2) && $postData['demo'] = 1;
        $res = $this->curl->req($url, $postData);
        $retData = json_decode($res, 1);
        if(empty($retData)) {
            die(json_encode($this->withNodata()));
        }
        //还在运行的喇叭ID
        $runList = $retData['runList'];
        foreach ($retData['list'] as &$v) {
            $createTime = $v['createtime'];
            $v['createtime'] = date('Y-m-d H:i:s', $createTime);
            $v['betweenTime'] = !empty($v['etime']) ? ($v['etime'] - $v['stime']) / 60 : '';
            $v['stime'] = !empty($v['stime']) ? date('Y-m-d H:i:s',$v['stime']) : '';
            $v['etime'] = !empty($v['etime']) ? date('Y-m-d H:i:s',$v['etime']) : '';
            $v['typeName'] = $v['type'] == 2 ? '定时一次' : ($v['type'] == 3 ? '每天定时' : '普通');
            if(in_array($v['id'], $runList)) {
                $v['status'] = 2;
            }else {
                $v['status'] = 1;
            }
        }
        $data['data'] = $retData['list'];
        $data['recordsTotal'] = $retData['total'];
        $data['recordsFiltered'] = $retData['total'];
        die(json_encode($data));

    }

    /**
     * 添加大喇叭推送
     */
    public function addSpeaker()
    {
        $username = get_cookie('username');
        $msg = $this->input->post('msg') ?? '';
        $type = $this->input->post('type') ?? 1;
        $stime = $this->input->post('stime');
        $etime = $this->input->post('etime');
        $interval = $this->input->post('interval');

        if(empty($username)) {
            $this->response(1, '参数错误');
        }
        if(empty($msg)) {
            $this->response(1, '内容不能为空');
        }
        if($type == 2 || $type == 3) {
            if(empty($stime)) {
                $this->response(2, '发送开始时间不能为空');
            }
            if(empty($etime)) {
                $this->response(2, '发送结束不能为空');
            }
            if(empty($interval) || $interval < 1) {
                $this->response(3, '间隔时间不能为空或者小于1分钟');
            }
        }


        $url = ($this->env == 2) ? $this->config->item('localUrl') : $this->config->item('officialUrl');
        $postData = $this->postdata;

        $postData['username'] = $username;
        $postData['msg'] = $msg;
        $postData['type'] = $type;
        $postData['stime'] = $stime;
        $postData['etime'] = $etime;
        $postData['interval'] = $interval;
        $postData['mod'] = 'Speaker';
        $postData['act'] = 'addSpeakerPush';

        ($this->env == 2) && $postData['demo'] = 1;
        $res = $this->curl->req($url, $postData);

        die($res);
    }

    /**
     * 删除喇叭循环推送
     */
    public function delSpeakerPush()
    {
        $id = $this->input->post('id');

        if(empty($id)) {
            $this->response(1, '参数错误');
        }
        $url = ($this->env == 2) ? $this->config->item('localUrl') : $this->config->item('officialUrl');
        $postData = $this->postdata;

        $postData['id'] = $id;
        $postData['mod'] = 'Speaker';
        $postData['act'] = 'delSpeakerPush';

        ($this->env == 2) && $postData['demo'] = 1;
        $res = $this->curl->req($url, $postData);

        die($res);
    }

}