<?php

namespace App\Http\Controllers\WapAlipay\User;

use Illuminate\Http\Request;
use App\Http\Controllers\WapAlipay\BaseController;
use App\Modules\Subscribe\Services\YearOrderService;
use App\Modules\User\Services\UserService;
use App\Modules\User\Services\UserSignService;
use App\Http\Controllers\WapAlipay\User\Transformers\SignRecordTransformer;
use Log;
use Redis;
use Cookie;
use DB;

class UserController extends BaseController
{
    /**
     * @apiDefine User 用户
     */


    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取用户信息
     * @api {GET} userinfo 获取用户信息
     * @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('NOT_LOGIN');
        }

        $data = UserService::getById($this->uid);

        $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);
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 用户签到记录
     * @api {GET} user/sign_record 用户签到记录
     * @apiGroup User
     * @apiName signRecord
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiSuccess {reward}     data.reward 奖励金额.
     * @apiSuccess {sign_time}  data.sign_time 签到时间.
     * @apiParam {page}  page
     * @apiSuccessExample {json} Success-Response:
     *
     *    {
     *       code: 0,
     *       msg: "",
     *       data: {
     *           list: [
     *           {
     *               reward: 50,
     *               sign_time: "2018-03-20 13:43:11"
     *           },
     *           {
     *               reward: 50,
     *               sign_time: "2018-01-18 16:22:33"
     *           },
     *       ],
     *       meta: {
     *           total: 12,
     *           per_page: 15,
     *           current_page: 1,
     *           last_page: 1,
     *           next_page_url: "",
     *           prev_page_url: ""
     *       }
     *       }
     *   }
     */
    public function signRecord(Request $request){
        return response()->pagination(new SignRecordTransformer(),UserSignService::getUserSignRecord($this->uid));
    }

    public function getCoupons(Request $request){
        $activity_id = $request->input('activity_id');
        if(!DB::table('discount_coupons')->where('uid',$this->uid)->where('activity_id',$activity_id)->count()){
            DB::table('discount_coupons')->insert([
                'uid'=>$this->uid,
                'activity_id'=>$activity_id,
                'created_at'=>date('Y-m-d H:i:s'),
                'updated_at'=>date('Y-m-d H:i:s')
            ]);
        }

        return response()->success();
    }

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