<?php
defined('InShopNC') or exit('Access Invalid!');

require_once(BASE_ROOT_PATH . '/helper/algorithm.php');
require_once(BASE_ROOT_PATH . '/helper/account_helper.php');
require_once(BASE_ROOT_PATH . '/helper/sms_helper.php');

class oper_smsControl extends SystemControl
{
    public function __construct()
    {
        parent::__construct();
    }

    public function indexOp()
    {
        $sms_template = Model('sms_template');
        $template_list = $sms_template->select();

        Tpl::output('template_list', $template_list);
        Tpl::showpage('oper_sms');
    }

    public function sms_addOp()
    {
        $sms_title = trim($_POST['sms_title']);
        $yuntx_id = intval(trim($_POST['yuntx_id']));
        $sms_content = trim($_POST['sms_content']);

        if (empty($sms_title) || $yuntx_id <= 0 || empty($sms_content)) {
            return showMessage("参数错误", 'index.php?act=oper_sms&op=index', '', 'error');
        }

        $sms_template = Model('sms_template');
        $data = array('yuntx_id' => $yuntx_id, 'title' => $sms_title, 'content' => $sms_content);
        $ret = $sms_template->insert($data);

        if ($ret != false) {
            showMessage(Language::get('nc_common_save_succ'), 'index.php?act=oper_sms&op=index');
        } else {
            showMessage("存入数据库失败", 'index.php?act=oper_sms&op=index', '', 'error');
        }
    }


    private function add_params($input, &$err)
    {
        $params = [];

        $params['member']['input_type'] = $input['input_type'];
        if (empty($input['members'])) {
            $err = "手机号码或者SQL语句为空";
            return false;
        } else {
            $params['member']['data'] = $input['members'];
        }

        $params['member']['ex_brand'] = $input['ex_brand'] == 'on' ? true : false;
        $params['member']['ex_lrlz']  = $input['ex_lrlz'] == 'on' ? true : false;

        if($input['operatetype'] == 'sms')
        {
            $tmpid = $input['sms_template'];
            if ($tmpid <= 0) {
                $err = "错误的短信模版";
                return false;
            }
            $params['send']['type'] = 'sms';
            $params['send']['data'] = $tmpid;
        }
        else
        {
            if(empty($input['pushcontet'])) {
                $err = "push 内容不能为空";
                return false;
            } else {
                $params['send']['type'] = 'push';
                $params['send']['data'] = $input['pushcontet'];
            }
        }

        return $params;
    }

    public function send_sms_pushOp()
    {
        if (chksubmit())
        {
            $params = $this->add_params($_POST,$error);
            if($params == false) {
                showMessage($error,'index.php?act=person_bonus&op=add','','error');
            } else {
                QueueClient::push('sendPushOrSMS',$params);
                showMessage("成功放入推送队列", 'index.php?act=oper_sms&op=index');
            }
        }
        else
        {
            $items = Model()->table('sms_template')->where(array('disable' => 0))->field('yuntx_id,title,content')->select();
            Tpl::output('sms_tpls',$items);
            Tpl::showpage('oper_send.smspush');
        }
    }

    public function awaken_userOp()
    {
        if(chksubmit())
        {

        }
        else {
            Tpl::showpage('oper_send.awaken');
        }
    }

    public function bonus_awakenOp()
    {
        $days = intval($_POST['days']);
        $paysn = $_POST['paysn'];
        $amount = floatval($_POST['amount']);
        if(empty($paysn)) {
            showMessage("参数错误，paysn 不能为空",'index.php?act=oper_sms&op=awaken_user','','error');
        }
        else {
            QueueClient::push('onAsyncBroadcastOrder',['pay_sn' => $paysn,'amount' => $amount,'days' => $days]);
            showMessage("成功放入推送队列", 'ndex.php?act=oper_sms&op=awaken_user');
        }
    }
    public function brand_awakenOp()
    {
        $amount = intval($_POST['amount']);
        $brand = trim($_POST['brand']);
        $openurl = trim($_POST['openurl']);

        $days = intval($_POST['days']);
        $et_amount = intval($_POST['et_amount']);

        if(empty($amount) ||empty($brand) || empty($openurl) || empty($et_amount)) {
            showMessage("参数错误，paysn 不能为空",'index.php?act=oper_sms&op=awaken_user','','error');
        }
        else
        {
            $send_params =['type' => Sms::tpl_new_goods,
                'sms_params'  => ['day' => '周一','amount' => $amount,'brand' => $brand,'url' => $openurl],
                'push_params' => []];

            $filters = ['member_filters' => [ ['type' => 'bonus','amount' => $et_amount]],
                'sms_filters' => [['type' => 'regtime','days' => $days]]];

            $oper_param = ['type' => 'bonus','rate' => 30,'amount' => 50];

            QueueClient::push('onAsyncBroadcastNormal',['filters' => $filters,'oper_param' => $oper_param,'send_params' => $send_params]);
            showMessage("成功放入推送队列", 'ndex.php?act=oper_sms&op=awaken_user');
        }
    }
}