<?php

namespace App\Http\Controllers\Channel\OfficialAccount;

use App\Http\Controllers\Channel\BaseController as ChannelBaseController;
use App\Modules\OfficialAccount\Services\OfficialAccountService;
use App\Http\Controllers\Channel\OfficialAccount\Transformers\OfficialAccountTransformer;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use App\Libs\OSS;

class OfficialAccountsController extends ChannelBaseController
{

     /**
     * @apiDefine OfficialAccount 公众号
     */


    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/officialAccountAuthUrl 获取服务号授权URL
     * @apiGroup OfficialAccount
     * @apiName officialAccountAuthUrl
     * @apiSuccessExample {json} Success-Response:
     *
     *       {
     *           "code": 0,
     *           "msg": "",
     *           "data": []
     *       } 
     */
    function  officialAccountAuthUrl(Request $request) {

        // $base_url = 'http://auth.aizhuishu.com/oauth_platform?';

        // $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        // if(empty($distribution_channel_id)) {
        //     return response()->error("PARAM_EMPTY");
        // }
        $distribution_channel_id = $this->getChannelId();

        $officialAccount['distribution_channel_id'] = $distribution_channel_id;

        $url = OfficialAccountService::officialAccountAuthUrl($officialAccount);

        return response()->success(compact('url'));
    }

    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/allOfficialAccountBydistributionChannelId 根据分销渠道号获取服务号列表
     * @apiGroup OfficialAccount
     * @apiName allOfficialAccountBydistributionChannelId
     * @apiSuccess {Number}  distribution_channel_id 分销渠道id.
     * @apiSuccess {String}  name 公众号原始名称.
     * @apiSuccess {String}  nickname 公众号名称.
     * @apiSuccess {String}  alias 唯一ID.
     * @apiSuccess {String}  qrcode_url 二维码地址.
     * @apiSuccess {String}  principal_name 公司名称.
     * @apiSuccess {String}  func_info 功能信息.
     * @apiSuccess {String}  head_img 头像地址.
     * @apiSuccess {String}  appid 微信appID.
     * @apiSuccess {String}  appsecret 微信appsecret.
     * @apiSuccess {String}  verify_txt 验证文件.
     * @apiSuccess {String}  verify_type_info 授权方认证类型.
     * @apiSuccess {String}  authorizer_refresh_token 授权的刷新token.
     * @apiSuccess {String}  cancel_auth_time 取消授权时间.
     * @apiSuccess {String}  official_account_type 第三方平台关注公众号.
     * @apiSuccess {Number}  is_auth 是否授权.
     * @apiSuccess {String}  service_type_info 服务号类型.
     * @apiSuccess {Number}  subscribe_top_num 强制关注总额.
     * @apiSuccess {Number}  subscribe_day_maximum 强制关注日限额.
     * @apiSuccess {Number}  todayForceSubscribeUsers 今日关注总数.
     * @apiSuccess {Number}  allForceSubscribeUsers 关注总数.
     * @apiSuccess {Number}  is_enabled 是否可用.
     * @apiSuccess {Number}  force_subscribe_type 强关类型 1默认强关2弱关注3链接强关
     * @apiSuccess {Number}  resource_url .链接强关的地址
     * @apiSuccess {Number}  force_subscribe_type_text .强关类型文本
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *       "code": 0,
     *       "msg": "",
     *       "data": {
     *           {
     *              "name": "测试呵呵",
     *              "nickname": "大哥",
     *              "alias": "23534dsgdsvdx",
     *              "head_img": "www.baidu.com",
     *              "appid": "1211",
     *              "appsecret": "dsfsdf3452352",
     *              "verify_txt": null,
     *              "is_auth": 1,
     *              "service_type_info": "546dsfwr23r",
     *              "subscribe_top_num": null,
     *              "subscribe_day_maximum": null,
     *              "distribution_channel_id": 1,
     *              "qrcode_url": "fdhgfds435gsdfg43t",
     *              "principal_name": "dfgh435saf2332",
     *              "func_info": "ry3t342trwe",
     *              "authorizer_refresh_token": "4534dfsgdsgsdgsdg",
     *              "cancel_auth_time": null,
     *              "official_account_type": "32dfaw234yewf",
     *              "verify_type_info": null,
     *              "is_enabled": 1,
     *              "todayForceSubscribeUsers": 0,
     *              "allForceSubscribeUsers": 0
     *          }
     *       }
     *      }
     */

    function allOfficialAccountBydistributionChannelId(Request $request)
    {

        // $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';

        // if(empty($distribution_channel_id)) {
        //     return response()->error("PARAM_EMPTY");
        // }
        $distribution_channel_id = $this->getChannelId();

        \Log::info('-------------------获取当前渠道下的所有服务号-----------------------------------');
        \Log::info($distribution_channel_id);

        $officialAccount['distribution_channel_id'] = $distribution_channel_id;

        $officialAccountService = OfficialAccountService::allOfficialAccountBydistributionChannelId($officialAccount);
        foreach ($officialAccountService as &$v){
            if(!$v->force_subscribe_type){
                $v->force_subscribe_type = ["1"];
                $v->force_subscribe_type_text = '强制关注';
            }else{
                $v->force_subscribe_type_text = str_replace([1,2,3],['强制关注','主动关注','强制链接关注'],$v->force_subscribe_type);
                $type_arr = explode(',',$v->force_subscribe_type);
                $v->force_subscribe_type = $type_arr;
            }

        }
        return response()->collection(new OfficialAccountTransformer(), $officialAccountService);
    }


    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/officialAccountById 通过Id获取要编辑的公众号
     * @apiGroup OfficialAccount
     * @apiName officialAccountById
     * @apiParam   {String}  id 微信公众号ID.
     * @apiSuccess {Number}  distribution_channel_id 分销渠道id.
     * @apiSuccess {String}  name 公众号原始名称.
     * @apiSuccess {String}  nickname 公众号名称.
     * @apiSuccess {String}  alias 唯一ID.
     * @apiSuccess {String}  qrcode_url 二维码地址.
     * @apiSuccess {String}  principal_name 公司名称.
     * @apiSuccess {String}  func_info 功能信息.
     * @apiSuccess {String}  head_img 头像地址.
     * @apiSuccess {String}  appid 微信appID.
     * @apiSuccess {String}  appsecret 微信appsecret.
     * @apiSuccess {String}  verify_txt 验证文件.
     * @apiSuccess {String}  verify_type_info 授权方认证类型.
     * @apiSuccess {String}  authorizer_refresh_token 授权的刷新token.
     * @apiSuccess {String}  cancel_auth_time 取消授权时间.
     * @apiSuccess {String}  official_account_type 第三方平台关注公众号.
     * @apiSuccess {Number}  is_auth 是否授权.
     * @apiSuccess {String}  service_type_info 服务号类型.
     * @apiSuccess {Number}  subscribe_top_num 强制关注总额.
     * @apiSuccess {Number}  subscribe_day_maximum 强制关注日限额.
     * @apiSuccess {Number}  is_enabled 是否可用.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *       "code": 0,
     *       "msg": "",
     *       "data": {
     *           "name": "gh_0fdfe1e4f56c",
     *           "nickname": "丸子书屋",
     *           "alias": "wzsw166",
     *           "head_img": "http://wx.qlogo.cn/mmopen/BDDBmFtJlKECdQ3ZeFKiaBMjU0ovndaib5tQQ1e7eXIIUCuKHGgnfwnDMdXecbpPmlstnnOicuHsiapvG71JbicBR52bapunZUvuu/0",
     *           "appid": "wx6916de1267c67d50",
     *           "appsecret": "wx6916de1267c67d501212",
     *           "verify_txt": "",
     *           "is_auth": "1",
     *           "service_type_info": "2",
     *           "subscribe_top_num": "10000",
     *           "subscribe_day_maximum": "500",
     *           "distribution_channel_id": 2147483647,
     *           "qrcode_url": "fdhgfds435gsdfg43t",
     *           "principal_name": "dfgh435saf2332",
     *           "func_info": "ry3t342trwe",
     *           "authorizer_refresh_token": "4534dfsgdsgsdgsdg",
     *           "cancel_auth_time": null,
     *           "official_account_type": "32dfaw234yewf",
     *           "verify_type_info": null,
     *           "is_enabled": 1
     *       }
     *      }
     */

    function officialAccountById(Request $request)
    {

        $id = $request->has('id') ? $request->input('id') : '';

        if(empty($id)) {
            return response()->error("PARAM_EMPTY");
        }
        $officialAccount['id'] = $id;
        $officialAccountService = OfficialAccountService::officialAccountById($officialAccount);
        return response()->item(new OfficialAccountTransformer(), $officialAccountService);
    }


    /**
     * @apiVersion 1.0.0
     * @api {POST} OfficialAccount/updateOfficialAccount 修改公众号信息
     * @apiGroup OfficialAccount
     * @apiName updateOfficialAccount
     * @apiParam {String}  id 微信公众号ID.
     * @apiParam {String}  subscribe_top_num 总阀值.
     * @apiParam {String}  subscribe_day_maximum 日上线.
     * @apiSuccessExample {json} Success-Response:
     *
     *       {
     *           "code": 0,
     *           "msg": "",
     *           "data": []
     *       }       
     */
    function updateOfficialAccount(Request $request) {

        $id = $request->has('id') ? $request->input('id') : '';
        if(empty($id)) {
            return response()->error("PARAM_EMPTY");
        }

        //$appsecret = $request->has('appsecret') ? $request->input('appsecret') : '';

        $subscribe_top_num = $request->has('subscribe_top_num') ? $request->input('subscribe_top_num') : '';

        $subscribe_day_maximum = $request->has('subscribe_day_maximum') ? $request->input('subscribe_day_maximum') : '';
        
        $sort_no = $request->has('sort_no') ? $request->input('sort_no') : '0';
        
        $officialAccount['id'] = $id;
        //$officialAccount['appsecret'] = $appsecret;
        $officialAccount['subscribe_top_num'] = $subscribe_top_num;
        $officialAccount['subscribe_day_maximum'] = $subscribe_day_maximum;
        $officialAccount['sort_no'] = $sort_no;
        \Log::info($officialAccount);

        $resultStatus = OfficialAccountService::updateOfficialAccountTopNum($officialAccount);
        if ($resultStatus == 1) {
            return response()->success();
        }elseif ($resultStatus == 2) {
            return response()->error('OFFICIAL_ACCOUNT_NOT_FOUND');
        }elseif ($resultStatus == 0) {
            return response()->error('UPDATE_OFFICIAL_ACCOUNT_FAILED');
        }else{
            return response()->error('UPDATE_OFFICIAL_ACCOUNT_FAILED');
        }
        
        
    }
    
    function getShortUrl(Request $request) {
    	\Log::info('getShortUrl');
    	$url = $request->has('url') ? $request->input('url') : '';
    	if(empty($url)) {
    		return response()->error("PARAM_EMPTY");
    	}
    	$distribution_channel_id = $this->getChannelId();
    
    	$result = OfficialAccountService::get_short_url($distribution_channel_id,$url);
    	if ($result['code'] == 1 && !empty($result['url'])) {
    		return response()->success($result['url']);
    	}else{
    		return response()->success($result['msg']);
    	}
    
    }
    
}