<?php

namespace App\Http\Controllers\Wechat\Staff;

use App\Http\Requests;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Message\News;
use EasyWeChat\Message\Text;
use EasyWeChat\Message\Image;

/**
 * 客服消息管理
 * @author zhoulingjie
 *
 */
class StaffsController
{
    
    public function __construct($_param)
    {
		$this->app = $_param['app'];
    }
    
    /**
     * 批量消息推送
     * 注意：text消息要被动回复出去
     * @param Request $request
     */
    public function batch_send_wechat_content($openid,$to_send_datas,$reply_type='staff')
    {
    	v('batch_send_wechat_content_start:'.$openid);
    	if(empty($to_send_datas) || empty($openid)) {
    		v('batch_send_wechat_content_invalid_param:'.$openid);
    		return '';
    	}
    	
    	$result = '';
    	v('to_send_datas:'.json_encode($to_send_datas));
    	foreach($to_send_datas as $send_type=>$to_send_data){
    		if($send_type == 'image'){
    			$this->send_wechat_content_image($openid, $to_send_data);
    		}
    		
    		if($send_type == 'text'){
    			$result = $this->send_wechat_content_text($openid, $to_send_data,$reply_type);
    		}
    	}

    	return $result;
    }
    
    /**
     * 图片消息推送-->image
     * @param Request $request
     */
    public function send_wechat_content_image($openid,$send_datas)
    {
    	$medis_id = isset($send_datas['media_id'])?$send_datas['media_id']:'';
    	v('send_wechat_content_image_start:'.$openid.' medis_id:'.$medis_id);
    	v($send_datas);
    	if(empty($medis_id) || empty($openid) || empty($send_datas)) {
    		v('send_wechat_content_image_invalid_param:'.$openid);
    		return '';
    	}
    	v('before_send_datas');
//     	v($send_datas);
    
    	$last_send_data = new Image(['media_id' => $medis_id]);
    	 
    	try{
    		v('send_to_openid:'.$openid);
    		if(!empty($last_send_data)){
    			$result = $this->app->staff->message($last_send_data)->to($openid)->send();
    			v($result);
    		}
    	}
    	// 加上\ 全局抓取
    	catch(\Exception $e){
    		v('send_wechat_content_image_ept:'.$openid.' info:'.$e->getMessage());
    	}
    
    	v('send_wechat_content_image_end:'.$openid);
    	return '';
    }
    
    /**
     * 多图文消息推送-->text
     * @param Request $request
     */
    public function send_wechat_content_text($openid,$send_datas,$reply_type='staff')
    {
//     	v('send_wechat_content_text_start:'.$openid);
    	if(empty($openid) || empty($send_datas)) {
    		v('send_wechat_content_text_invalid_param:'.$openid);
    		return '';
    	}
//     	v('before_send_datas_text');
//     	v($send_datas);

    	$last_send_data = null;
    	if(is_array($send_datas)){
    		$news_tpls = array();
    		$num = 0;
    		foreach($send_datas  as $send_data){
    			$news = new News([
    					'title'       => $send_data['title'],
    					'description' => $send_data['description'],
    					'url'         => $send_data['url'],
    					'image'       => $send_data['image'],
    					// ...
    					]);
    			$news_tpls[] = $news;
    			$num++;
    			if($num >= 8) break;
    		}
    		$last_send_data = $news_tpls;
//     		v('$news_tpls');v($news_tpls);
    	}else{
//     		v('$text');v($send_datas);
    		$last_send_data = $send_datas;
    	}
    
    	try{
//     		v('send_to_openid:'.$openid);
    		if(!empty($last_send_data)){
    			// 直接返回
    			if($reply_type == 'direct_return'){
    				v('direct_return:'.$openid);
    				return $last_send_data;
    			}else{
    				$result = $this->app->staff->message($last_send_data)->to($openid)->send();
    				return $result;
    			}
                //v('staff:send:result:');v($result);

    		}
    	}
    	// 加上\ 全局抓取
    	catch(\Exception $e){
    		v('send_wechat_content_text_ept:'.$openid.' info:'.$e->getMessage());
    	}
    	 
//     	v('send_wechat_content_text_end:'.$openid);
    	return '';
    }
   
}