<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 成员，就诊人
 */
class Usermember extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    //添加成员
    public function add_one(){
        $field = [
            'realname',
            'idcard',
            'relation',
            'mobile',
            'gender',
            'birthday',
            'marital_status',
            'allergies_status',
            'allergies_text',
            'familymedical_status',
            'familymedical_text',
            'chronicdisease_status',
            'chronicdisease_text',
        ];

        $data = request_post_hub($field);
        $data['user_id'] = $this->auth->id;
        $data['birthday'] = strtotime($data['birthday']);

        $id = Db::name('user_member')->insertGetId($data);
        $this->success('操作成功',$id);
    }


    //我的成员列表
    public function my_list(){

        $list = Db::name('user_member')->field('id,realname,gender,birthday')->where('user_id',$this->auth->id)->order('id desc')->select();


        if(!empty($list)){
            foreach($list as $key => &$val){
                $val['age'] = birthtime_to_age($val['birthday']);
                $val['avatar'] = localpath_to_netpath('/assets/img/avatar.png');
            }
        }

        $this->success('success',$list);
    }

    //获取详情
    public function info(){

        $id = input('id',0);

        $info = Db::name('user_member')->find();


        if(!empty($info)){
            $info['birthday'] = date('Y-m-d',$info['birthday']);
        }

        $this->success('success',$info);
    }

    //编辑
    public function edit_one(){
        $id = input('id',0);

        $field = [
            'realname',
            'idcard',
            'relation',
            'mobile',
            'gender',
            'birthday',
            'marital_status',
            'allergies_status',
            'allergies_text',
            'familymedical_status',
            'familymedical_text',
            'chronicdisease_status',
            'chronicdisease_text',
        ];

        $data = request_post_hub($field);
        $data['birthday'] = strtotime($data['birthday']);

        Db::name('user_member')->where('id',$id)->where('user_id',$this->auth->id)->update($data);
        $this->success('操作成功');
    }

    //删除
    public function delete(){
        $id = input('id',0);
        Db::name('user_member')->where('id',$id)->where('user_id',$this->auth->id)->delete();
        $this->success('操作成功');
    }



}
