<?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 Base{
    
    protected $db_name = ['2'=>'app_card_body_data','6'=>'app_card_skip_data','8'=>'app_card_vitalcapacity_data'];
    // protected $card_key = ['2'=>'body','6'=>'skip','8'=>'vitalcapacity'];
    protected $card_data = [
        '2'=>['身体数据',['height','weight','bmi','record_time']],
        // 'skip'=>['跳绳数据',['height,weight,bmi,record_time']],
        '8'=>['肺活量',['average','record_time']],
    ];
    ################################################################个人资料卡################################################################
    ################################################################个人资料卡################################################################
    ################################################################个人资料卡################################################################

    // // 个人信息
    // public function personal_information(){
    //     // phpinfo();
    //     dump(123);
    //     $result = Db::table('admin_user')->select();
    //     dump($result);
    // }
    // 创建用户
    public function create_user_data($data = ['aan_id'=>66,'nickname'=>'王小二','birthday'=>'2019-01-01','gender'=>1,'grade'=>'二年级','token'=>'0dafb98a10995c98b5a33b7d59d986ca']){
        if(count(input()) > 0){
            $data = input();
        }
        if($this->token_time_validate($data['token']) === false){
            return $this->msg(20001,'登录失效');
        }
        unset($data['token']);
        $verify_result = $this->verify_parameters($data,'register');
        if(!is_array($verify_result)){
            return $this->msg(10002,$verify_result);
        }
        $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 get_user_card_list($data = ['aan_id'=>66,'type'=>1,'token'=>'0dafb98a10995c98b5a33b7d59d986ca']){
        if(count(input()) > 0){
            $data = input();
        }
        if($this->token_time_validate($data['token']) === false){
            return $this->msg(20001,'登录失效');
        }
        unset($data['token']);
        $result = Db::table('app_user_data')->where(['aan_id'=>$data['aan_id']])->select();
        // $result = Db::table('app_user_data')->where(['aan_id'=>$aan_id])->field('id,nickname')->select();
        $temporary_data = [];
        if($data['type'] == 1){
            for ($i=0; $i < count($result); $i++) { 
                array_push($temporary_data,['id'=>$result[$i]['id'],'nickname'=>$result[$i]['nickname']]);
            }
        }else{
            $temporary_data = $result;
        }
        return $this->msg(0,'success',$temporary_data);
    }

    // 获取指定用户详细信息
    public function get_user_data_information($data = ['aud_id'=>11,'token'=>'0dafb98a10995c98b5a33b7d59d986ca']){
        if(count(input()) > 0){
            $data = input();
        }
        if($this->token_time_validate($data['token']) === false){
            return $this->msg(20001,'登录失效');
        }
        unset($data['token']);
        $result = Db::table('app_user_data')->where(['id'=>$data['aud_id']])->field('id,aan_id,nickname,birthday,gender,card_order')->find();
        unset($result['ROW_NUMBER']);
        if($result['card_order'] === ''){
            $result['age'] = $this->calculate_age($result['birthday']);
            $result['card_order'] = [];
            $result['card_data_list'] = [];
        }else{
            $result['card_order'] = explode(',',$result['card_order']);
            $result['card_data_list'] = $this->get_user_card_data_list($result['card_order'],$result['id']);
        }

        $result['birthday'] = str_replace('-', '/', $result['birthday']);
        foreach ($result['card_data_list'] as $key => $value) {
            $result['card_data_list'][$key]['record_time'] = str_replace('-', '/', $result['card_data_list'][$key]['record_time']);
        }
        return $this->msg(0,'success',$result);
    }

    // 获取所有卡片列表信息
    public function get_card_all_list($data = ['aud_id'=>11,'token'=>'0dafb98a10995c98b5a33b7d59d986ca']){
        if(count(input()) > 0){
            $data = input();
        }
        if($this->token_time_validate($data['token']) === false){
            return $this->msg(20001,'登录失效');
        }
        unset($data['token']);
        $user_card_list = Db::table('app_user_data')->where(['id'=>$data['aud_id']])->field('id,card_order')->find();
        unset($user_card_list['ROW_NUMBER']);
        $user_card_list['card_order'] = explode(',',$user_card_list['card_order']);
        $all_card_list = Db::table('app_card_data')->field('id,name')->select();
        // dump($user_card_list);
        // dump($all_card_list);
        $result = ['user'=>[],'all'=>[]];
        foreach ($all_card_list as $key => $value) {
            if(in_array($value['id'],$user_card_list['card_order'])){
                $result['user'][array_search($value['id'], $user_card_list['card_order'])] = ['id'=>$value['id'],'name'=>$value['name']];
            }else{
                array_push($result['all'],['id'=>$value['id'],'name'=>$value['name']]);
            }
        }
        ksort($result['user']);
        return $this->msg(0,'success',$result);
    }

    // 保存用户的卡片排序
    public function save_user_card_order($data=['aud_id'=>11,'card_order'=>'2,8','token'=>'0dafb98a10995c98b5a33b7d59d986ca']){
        if(count(input()) > 0){
            $data = input();
        }
        if($this->token_time_validate($data['token']) === false){
            return $this->msg(20001,'登录失效');
        }
        unset($data['token']);
        if(!array_key_exists('aud_id', $data) || !array_key_exists('card_order', $data)){ 
            return $this->msg(10001,'数据参数错误');
        }
        if(!$this->is_num_array(explode(',',$data['card_order']))){
            return $this->msg(10002,'数据内参数格式或值错误');
        }
        $result = Db::table('app_user_data')->where(['id'=>$data['aud_id']])->update(['card_order'=>$data['card_order']]);
        if($result){
            return $this->msg(0,'success');
        }else{
            return $this->msg(10003,'保存失败或未发生改变');
        }
        
    }
    

    ################################获取账号下信息操作################################
    // 获取账号下首页卡片的基础数据
    public function get_user_card_data_list($data,$aud_id){
        $result = [];
        $temporary_arr = [];
        foreach ($data as $key => $value) {
            $temporary_arr[$value] =  Db::table($this->db_name[$value])->where(['aud_id'=>$aud_id])->order('id desc')->limit(1)->select();
        }
        foreach ($temporary_arr as $key => $value) {
            if(!array_key_exists($key, $this->card_data)){
                // dump($key.'====in');
                continue;
            }
            foreach ($value[0] as $k => $v) {
                // dump($k);
                if(in_array($k, $this->card_data[$key][1])){
                    $result[$key][$k] = $v;
                }
            }
            $result[$key]['name'] = $this->card_data[$key][0];
        }
        return $result;
    } 

    ################################################################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');

        $result =  Db::table('app_user_data')->where(['nickname'=>$parameter['nickname'],'aan_id'=>$parameter['aan_id']])->count();
        if($result>0){
            return '该成员已存在';
        }
        
        return $parameter;
    }

    
    


}