<?php

namespace app\app\controller;

use think\Controller;
use think\Db;
use app\bj\controller\Common;
use think\Log;
use \think\Validate;

class Index extends Controller{
    

    ################################################################个人资料卡################################################################
    ################################################################个人资料卡################################################################
    ################################################################个人资料卡################################################################

    // 个人信息
    public function personal_information(){
        // phpinfo();
        dump(123);
        $result = Db::table('admin_user')->select();
        dump($result);
    }

    // 创建用户卡片
    public function create_user_data(){
        $data = input();
        $verify_result = $this->verify_parameters($data,'register');
        $result = Db::table('app_user_data')->insert($verify_result);
        if($result){
            return $this->msg(0,'success');
        }else{
            return $this->msg(10001,'创建失败');
        }
    }

    // 切换用户
    public function switch_user(){
        $data = input();
        $verify_result = $this->verify_parameters($data,'register');
        $result = Db::table('app_user_data')->insert($verify_result);
        if($result){
            return $this->msg(0,'success');
        }else{
            return $this->msg(10001,'创建失败');
        }
    }

    
    // 获取账号下用户列表
    // $type  1获取列表，2获取详细信息
    public function user_card_list($aan_id,$type=1){
        $result = Db::table('app_user_data')->where(['aan_id'=>$aan_id])->select();
        // $result = Db::table('app_user_data')->where(['aan_id'=>$aan_id])->field('id,nickname')->select();
        $data = [];
        if($type == 1){
            for ($i=0; $i < count($result); $i++) { 
                array_push($data,['id'=>$result[$i]['id'],'nickname'=>$result[$i]['nickname']]);
            }
        }else{
            $data = $result;
        }
        return $this->msg(0,'success',$data);
    }

    // 获取账号下用户详细信息
    public function user_card_information($id){
        $result = Db::table('app_user_card')->where(['id'=>$id])->find();
        if($result){
            return $this->msg(0,'success',$result);
        }else{
            return $this->msg(10001,'error');
        }
    }



    // 创建身体数据
    public function create_body_data(){
        $data = input();
        $result = Db::table('app_body_data')->insert([
            'auc_id'=>$data['id'],
            'height'=>$data['height'],
            'weight'=>$data['weight'],
            'bmi'=>$data['bmi'],
            'create_time'=>date('Y-m-d H:i:s'),
            'last_update_time'=>date('Y-m-d H:i:s'),
        ]);
        if($result){
            return $this->msg(0,'success');
        }else{
            return $this->msg(10001,'创建失败');
        }
    }
    // 获取账号下用户卡片身体数据
    // $type  1获取列表，2获取详细信息
    public function body_data_list($auc_id = 1,$type=1){
        $result = Db::table('app_body_data')->where(['auc_id'=>$auc_id])->select();
        $data = [];
        if($type == 1){
            for ($i=0; $i < count($result); $i++) { 
                array_push($data,['id'=>$result[$i]['id'],'create_time'=>$result[$i]['create_time']]);
            }
        }else{
            $data = $result;
        }
        return $data;
    }
    // 获取账号下用户卡片身体数据详细信息
    public function body_data_information($id){
        $result = Db::table('app_body_data')->where(['id'=>$id])->find();
        if($result){
            return $result;
        }else{
            return false;
        }
    }


    public function data_card(){
        
    }

    


    ################################################################other################################################################
    ################################################################other################################################################
    ################################################################other################################################################

    public function verify_parameters($data,$type){
        // 设置验证
        $rule = [
            'aan_id'  => 'require|number',
            'nickname'   => 'require|chsAlpha',
            'birthday' => 'require|date',
            'gender' => 'require|number|in:0,1,2',
            'grade' => 'require',
        ];
        $msg = [
            'aan_id.require' => '账号信息缺失',
            'nickname.require' => '昵称缺失',
            'birthday.require' => '生日缺失',
            'gender.require' => '性别缺失',
            'grade.require' => '年级缺失',

            'aan_id.number'   => '账号信息格式错误',
            'nickname.chsAlpha'   => '昵称只能是只能是汉字、字母',
            'birthday.date'  => '生日信息格式错误',
            'gender.number'  => '性别格式错误',
            'gender.in'  => '性别信息错误',
        ];
        $validate = new Validate($rule,$msg);
        $result   = $validate->check($data);
        if(!$result){
            return $validate->getError();
        }
        
        $parameter['aan_id'] = $data['aan_id'];
        $parameter['nickname'] = $data['nickname'];
        $parameter['birthday'] = $data['birthday'];
        $parameter['gender'] = $data['gender'];
        $parameter['grade'] = $data['grade'];
        $parameter['create_time'] = date('Y-m-d H:i:s');
        $parameter['last_update_time'] = date('Y-m-d H:i:s');

        
        return $parameter;
    }

    
    
    public function msg($code,$msg='',$data=[]){
        return json(['code'=>$code,'msg'=>$msg,'data'=>$data]);
    }

}