<?php

namespace App\Http\Controllers\Xchengxu\User;


use App\Http\Controllers\Xchengxu\BaseController;
use App\Modules\Subscribe\Services\YearOrderService;
use App\Modules\User\Transformers\YearOrderTransformer;
use App\Modules\User\Services\UserService;
use Log;
use Redis;
use Cookie;
class UserController extends BaseController
{
    /**
     * @apiDefine User 用户
     */


    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取用户信息
     * @api {GET} userinfo 获取用户信息
     * @apiParam {String}  [token]  token
     * @apiHeader {String} [Authorization]  token 两个token任选其一
     * @apiGroup User
     * @apiName index
     * @apiSuccess {Number}  id 用户ID.
     * @apiSuccess {String}  openid 微信openid.
     * @apiSuccess {String}  unionid 微信unionid.
     * @apiSuccess {Number}  distribution_channel_id 分销渠道ID.
     * @apiSuccess {String}  province 省份.
     * @apiSuccess {String}  city 城市.
     * @apiSuccess {String}  country 国家.
     * @apiSuccess {String}  headimgurl 头像地址.
     * @apiSuccess {Number}  send_order_id 派单ID.
     * @apiSuccess {Number=0,1}  sex 性别.
     * @apiSuccess {String}  balance 书币余额.
     * @apiSuccess {Int}     is_vip 是否vip
     * @apiSuccess {String}  vip_days 364天.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": {
     *             "id": 56,
     *             "openid": "sdfs34ssdfdsf",
     *             "unionid": "SDFSD3343S",
     *             "distribution_channel_id": 1212,
     *             "province": "浙江省",
     *             "city": "杭州",
     *             "country": "中国",
     *             "headimgurl": "http://.."
     *             "send_order_id": 323
     *             "balance": 8956
     *             "register_time": "2017-12-12 12:12:12"
     *         }
     *     }
     */
    public function index(){
        if(!$this->checkUid()){
            return response()->error('XCX_NOT_LOGIN');
        }

        $data = UserService::getById($this->uid);
        if(!$data->head_img){
            $data->head_img = 'https://yueduyun.oss-cn-hangzhou.aliyuncs.com/xiaochengxu/img/defaulthead.png';
        }
        $data['is_vip'] = 0;
        $data['vip_days'] = 0;
        $year_record = YearOrderService::getRecord($this->uid);
        if($year_record){
            $data['is_vip'] = 1;
            
            $time = strtotime($year_record['end_time'])-time();
            if($time>=86400){
                  $data['vip_days'] = floor($time/86400).'天';  
            }elseif ($time>3600) {
                 $data['vip_days'] = floor($time/3600).'小时';  
            }elseif ($time>60) {
                 $data['vip_days'] = floor($time/60).'分钟';  
            }else{
                $data['vip_days'] = $time.'秒';  
            }
        }
        return response()->success($data);
    }

    function logout()
    {
        setcookie(env('COOKIE_AUTH_WEB_WECHAT'), '', -1);
        setcookie('u', '', -1);
        return response('logout');
    }
}