<?php

namespace App\Http\Controllers\Wechat\Menu;

use App\Http\Requests;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use EasyWeChat\Foundation\Application;
use Cookie;

/**
 * 菜单管理
 * TODO：自动添加公众号默认菜单
 * @author zhoulingjie
 *
 */
class MenusController
{

    public function __construct($_param)
    {
    	$this->param = $_param;
		$this->app = $_param['app'];
		$this->WechatApi = $_param['WechatApi'];
    }
    
    /**
     * menu/set_menu
     * @param Request $request
     */
    public function set_menu()
    {
        $add_menus = $this->WechatApi->get_official_account_menus($this->param['gzh_app_id']);

        v('set_menu_start');
        if(empty($add_menus)){
            v('set_menu_invalid_param');
            return false;
        }
        $menu = $this->app->menu;

        v('set_menu:');
        v($add_menus);
        try{
            // 先删再加，解决菜单缓存问题
            $del_menu = $menu->destroy();
            v('del_menu');v($del_menu);
            $add_menu = $menu->add($add_menus);
            v('add_menu');v($add_menu);

            //存在pay链接 才过滤iOS 充值入口
            $ios_menus = [];
            $is_exist_pay = false;
            foreach ($add_menus as $k=>$_menu)
            {
                if(isset($_menu['sub_button']))
                {
                    $name = $_menu['name'];
                    $sub_button = [];
                    foreach($_menu['sub_button'] as $j=>$_sub_menu)
                    {
                        if(isset($_sub_menu['url']) && strstr($_sub_menu['url'],'/pay'))
                        {
                            $is_exist_pay = true;
                        }else{
                            $sub_button[] = $_sub_menu;
                        }
                    }
                    if($sub_button)  $ios_menus[] = compact('name','sub_button');
                }else{
                    if(isset($_sub_menu['url']) && strstr($_menu['url'],'/pay'))
                    {
                        $is_exist_pay = true;
                    }else{
                        $ios_menus[] = $_menu;
                    }
                }
            }
            $res = $is_exist_pay ? $menu->add($ios_menus,['client_platform_type'=>"1"]) : '';
            v('add_menu');v($res);
        }catch(\Exception $e){
            v('set_menu_ept:'.$e->getMessage());
        }

        v('set_menu_end');
    }
    
    /**
     * menu/set_menu
     * @param Request $request
     */
    public function set_menu2()
    {
    	v('set_menu_start');
    	$menu = $this->app->menu;
    	$base_url = 'http://site'.$this->param['distribution_channel_id'].'.'.$this->param['distribution_menu_host'];
    	 
    	$buttons =
        [
	    	[
		    	"type" => "view",
		    	"name" => "继续阅读",
		    	"url"  => $base_url.'continue',
	    	],
	    	[
		    	"type" => "view",
		    	"name" => "书城首页",
		    	"url"  => $base_url,
	    	],
	    	[
		    	"name"       => "用户中心",
		    	"sub_button" => 
		    	  [
			    	[
				    	"type" => "view",
				    	"name" => "个人中心",
				    	"url"  => $base_url.'user',
			    	],
			    	[
				    	"type" => "view",
				    	"name" => "阅读记录",
				    	"url"  => $base_url.'bookshelf',
			    	],
			    	[
				    	"type" => "view",
				    	"name" => "我要充值",
				    	"url"  => $base_url.'paycenter',
			    	],
			    	[
				    	"type" => "click",
				    	"name" => "联系客服",
				    	"key"  => 'contact_customer',
			    	],
			      ],
		      ],
    	];
    	v('set_menu:');
    	v($buttons);
    	v($menu->add($buttons));
    	v('set_menu_end');
    }
    
}