<?php

namespace App\Http\Controllers\Wechat\OfficialAccount;

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

class OfficialWechatUserController extends ChannelBaseController
{

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

    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/saveOfficialWechatUser 新增微信公众号用户
     * @apiGroup OfficialAccount
     * @apiName saveOfficialWechatUser
     * @apiParam   {String}  openid 微信openid..
     * @apiParam   {String}  unionid 微信unionid.
     * @apiParam   {String}  distribution_channel_id 分销渠道ID.
     * @apiParam   {String}  [province] 省份.
     * @apiParam   {String}  [city] 城市.
     * @apiParam   {String}  [country] 国家.
     * @apiParam   {String}  [headimgurl] 头像地址.
     * @apiParam   {String}  [send_order_id] 派单ID.
     * @apiParam   {String}  [sex] 性别.允许值: 0, 1.
     * @apiSuccessExample {json} Success-Response:
     *
     *      {
     *          "code": 0,
     *          "msg": "",
     *          "data": {
     *              
     *          }
     *      }
     */

    function saveOfficialWechatUser(Request $request)
    {

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

        if(empty($openid)) {
            return response()->error("PARAM_EMPTY");
        }

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

        if(empty($unionid)) {
            return response()->error("PARAM_EMPTY");
        }

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

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

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

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

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

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

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

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

        $user['openid'] = $openid;
        $user['unionid'] = $unionid;
        $user['distribution_channel_id'] = $distribution_channel_id;
        $user['province'] = $province;
        $user['city'] = $city;
        $user['country'] = $country;
        $user['headimgurl'] = $headimgurl;
        $user['send_order_id'] = $send_order_id;
        $user['sex'] = $sex;
        
        $resultStatus = OfficialAccountService::saveOfficialWechatUser($user);
        if ($resultStatus == 1) {
            return response()->success();
        }elseif ($resultStatus == 2) {
            return response()->error('WECHAT_USER_REQUEST_ERROR');
        }elseif ($resultStatus == 0) {
            return response()->error('WECHAT_USER_REQUEST_ERROR');
        }else{
            return response()->error('WECHAT_USER_REQUEST_ERROR');
        }

    }


    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/getOfficialWechatUser 获取公众号用户
     * @apiGroup OfficialAccount
     * @apiName getOfficialWechatUser
     * @apiParam     {Number}  id ID.
     * @apiSuccess   {Number}  id ID.
     * @apiSuccess   {String}  openid 微信openid.
     * @apiSuccess   {String}  unionid 微信unionid.
     * @apiSuccess   {String}  distribution_channel_id 分销渠道ID.
     * @apiSuccess   {String}  [province] 省份.
     * @apiSuccess   {String}  [city] 城市.
     * @apiSuccess   {String}  [country] 国家.
     * @apiSuccess   {String}  [headimgurl] 头像地址.
     * @apiSuccess   {String}  [send_order_id] 派单ID.
     * @apiSuccess   {String}  [sex] 性别.允许值: 0, 1.
     * @apiSuccessExample {json} Success-Response:
     *
     *  {
     *      "code": 0,
     *      "msg": "",
     *      "data": {
     *          "id": 1,
     *          "openid": "121212",
     *          "unionid": "232323",
     *          "head_img": "",
     *          "distribution_channel_id": 2,
     *          "register_ip": "",
     *          "send_order_id": 0,
     *          "balance": 0,
     *          "country": "",
     *          "city": "",
     *          "sex": 1,
     *          "register_time": "2017-11-22 11:04:33"
     *      }
     *  }
     */

    function getOfficialWechatUser(Request $request)
    {

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

        if(empty($id)) {
            return response()->error("PARAM_EMPTY");
        }
        $officialWechatUser['id'] = $id;

        $user = OfficialAccountService::getOfficialWechatUser($officialWechatUser);

        return response()->success($user);
        
    }

    
}