<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\MembersUpdateRequest;
use App\Models;
class UserController extends Controller
{

    /*
     * 获取登录用户信息
     */
    public function getUserDetails(Request $req){
        try {
            $uid = S('token_user_id');
            $member = new Models\Member;
            $user = $member
                ->join('membersDetailed as md', 'md.members_id', 'members.id')
                ->select('members.*','md.membersDetailed_cash')
                ->where('members.id',$uid)
                ->get();
            if(empty($user)){
                return toJson(-20002);
            }
            $user=$user->toArray();
            unset($user['password']);
            unset($user['solt']);
            return toJson(1,'',$user);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
    /*
     * 获取用户交易记录接口
     */
    public function tradeRecord(Request $req){
        $member_id  = $_REQUEST['member_id'];
        if(empty($member_id)){
            return toJson(-20005);
        }
        $MembersDetailed = new Models\MoneyDetails;
        $record = $MembersDetailed->tradeRecord($member_id);
        return toJson(1,'',$record);
    }

    /*
     * 用户资料修改接口
     */
    public  function editorMemberDetails() {
        $member_id  = S('token_user_id');
        // 判断是否有修改信息.
        if (
            !isset($_REQUEST['phone'])
            && !isset($_REQUEST['password'])
            && !isset($_REQUEST['avatar'])
            && !isset($_REQUEST['nick'])
        ) {
            return toJson(-20006);
        }
        if (isset($_REQUEST['phone'])) {
            $data['phone']  = $_REQUEST['phone'];
        }
        if (isset($_REQUEST['password'])) {
            $member = \App\Models\Member::where('id', $member_id)->first();
            $data['password']  = createPasswd(trim($_REQUEST['password']), $member['uuid']);
        }
        if (isset($_REQUEST['avatar'])) {
            $data['avatar']  = $_REQUEST['avatar'];
        }
        if (isset($_REQUEST['nick'])) {
            $data['nick']  = $_REQUEST['nick'];
        }
        $data['updated_at'] = date('Y-m-d H:i:s',time());

        $member = new Models\Member;
        //$avatarImg = $member->updateHeader($member_id);
        // 修改用户信息.
        $editor = $member->editorMemberDetails($member_id,$data);
        if ($editor) {
            return toJson($editor);
        } else {
            return toJson(-20006);
        }

    }


}
