<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 用户设置,从user拿出来
 */
class Userset extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    //用户详细资料
    public function userinfo(){
        $field = [
/*            'id','real_status','idcard_status','open_match_video','open_match_audio','open_match_typing','free_video','free_audio','free_typing'*/
            'id','mobile','wechat_openid','real_status','idcard_status','open_match_video','open_match_audio','chat_price','voice_price','video_price', 'is_hideaddress'
        ];
        $info = Db::name('user')->field($field)->where('id',$this->auth->id)->find();

//        $rs['userinfo'] = $info;
        $rs = $info;
        $rs['mobile'] = $info['mobile'] ? 1 : 0; //手机号
        $rs['wechat_openid'] = $info['wechat_openid'] ? 1 : 0; //微信
        $rs['alipay'] = Db::name('user_bank')->where(['user_id' => $this->auth->id, 'type' => 1])->count('id'); //支付宝绑定
        $rs['bank'] = Db::name('user_bank')->where(['user_id' => $this->auth->id, 'type' => 2])->count('id'); //银行卡绑定
        $rs['intro_gold'] = config('site.intro_gold') ? : 0; //邀请注册赠送金币
        $rs['intro_recharge_rebate_rate'] = config('site.intro_recharge_rebate_rate') ? : 0; //邀请人充值返利比率
        $rs['intro_withdrawal_rebate_rate'] = config('site.intro_withdrawal_rebate_rate') ? : 0; //邀请人提现返利比率
        $rs['intro_income_rebate_rate'] = config('site.intro_income_rebate_rate') ? : 0; //邀请人收益返利比率
        $rs['cash_banner'] = '';//one_domain_image(config('site.cash_banner')); //提现页面图片
//        $rs['video_min_price'] = config('site.video_min_price');
//        $rs['audio_min_price'] = config('site.audio_min_price');
//        $rs['typing_min_price'] = config('site.typing_min_price');
        $this->success('success',$rs);
    }

    /**
     * 修改会员状态信息
     */
    public function set_status_switch()
    {
        if($this->auth->idcard_status != 1){
            $this->error('未通过实名认证');
        }

        if($this->auth->real_status != 1){
            $this->error('未通过真人认证');
        }

        //开始
        $field_array = ['open_match_video','open_match_audio','open_match_typing','free_video','free_audio','free_typing'];

        $data = [];

        $field = input_post('switch','default');
        $value = input_post('switch_value',0);

        if(!in_array($field,$field_array)){
            $this->error();
        }

        if(!empty($field)){
            $data[$field] = $value;
        }

        if(empty($data)){
            $this->error('没有任何改变');
        }

        Db::name('user')->where('id',$this->auth->id)->update($data);
        $this->success();
    }

    //文字语音视频收费设置
    public function chargeconfig() {
        $type = input('type', 0, 'intval'); //类型:0=文字,1=语音,2=视频
        if (!in_array($type, [0, 1, 2])) {
            $this->error('您的网络开小差啦~');
        }

        $where['type'] = $type;

        $list = Db::name('charge_config')->field('id, price, level')->where($where)->select();

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

    //文字语音视频收费/隐藏所在位置设置
    public function chargeset() {
        $chat_id = input('chat_id', 0, 'intval'); //文字收费id
        $voice_id = input('voice_id', 0, 'intval'); //语音收费id
        $video_id = input('video_id', 0, 'intval'); //视频收费id
        $open_match_audio = input('open_match_audio', -1, 'intval'); //是否开启语音:1是 0否
        $open_match_video = input('open_match_video', -1, 'intval'); //是否开启视频:1是 0否
        $is_hideaddress = input('is_hideaddress', -1, 'intval'); //是否隐藏所在位置:0=否,1=是

        $data = [];
        //查询我的魅力等级
        $wallet_info = Db::name('user_wallet')->where(['user_id' => $this->auth->id])->find();
        $charm_level = Db::name('charm_level')->where(['value' => ['elt', $wallet_info['get_money']]])->order('id desc')->find();
        if ($charm_level) {
            $level = $charm_level['level'];
        } else {
            $level = 0;
        }

        if ($chat_id) {
            $charge_config = Db::name('charge_config')->where(['id' => $chat_id, 'type' => 0])->find();
            if (!$charge_config) {
                $this->error('您的网络开小差啦~');
            }
            if ($level < $charge_config['level']) {
                $this->error('您还未满足条件~');
            }

            $data['chat_price'] = $charge_config['price'];
        }
        if ($voice_id) {
            $charge_config = Db::name('charge_config')->where(['id' => $voice_id, 'type' => 1])->find();
            if (!$charge_config) {
                $this->error('您的网络开小差啦~');
            }
            if ($level < $charge_config['level']) {
                $this->error('您还未满足条件~');
            }

            $data['voice_price'] = $charge_config['price'];
        }
        if ($video_id) {
            $charge_config = Db::name('charge_config')->where(['id' => $video_id, 'type' => 2])->find();
            if (!$charge_config) {
                $this->error('您的网络开小差啦~');
            }
            if ($level < $charge_config['level']) {
                $this->error('您还未满足条件~');
            }

            $data['video_price'] = $charge_config['price'];
        }
        if (in_array($open_match_audio, [1, 0])) {
            $data['open_match_audio'] = $open_match_audio;
        }
        if (in_array($open_match_video, [1, 0])) {
            $data['open_match_video'] = $open_match_video;
        }
        if (in_array($is_hideaddress, [1, 0])) {
            $data['is_hideaddress'] = $is_hideaddress;
        }
        if (!$data) {
            $this->error('没有修改信息~');
        }

        $rs = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
        if ($rs === false) {
            $this->error('您的网络开小差啦~');
        }

        $this->success('设置成功');
    }
}