<?php

namespace Main\Src;

use Dever;
use Main\Lib\Core;

class User extends Core
{
    protected $checkUser = true;

    public function check()
    {

    }

    # 我的页面
    public function my()
    {
        # 获取用户信息
        $this->data['user'] = Dever::load('passport/api')->info($this->data['uid']);
        if ($this->data['user']) {
            $level = Dever::db('score/user_level')->one(array('uid' => $this->data['uid']));
            $this->data['user']['level'] = '';
            if ($level) {
                $level_info = Dever::db('score/level')->one($level['level_id']);
                if ($level_info && $level_info['state'] == 1) {
                    $this->data['user']['level'] = $level_info['name'];
                }
            }

            $score = Dever::db('score/user')->all(array('uid' => $this->data['uid']));
            $score_num = 0;
            if ($score) {
                foreach ($score as $k => $v) {
                    $score_info = Dever::db('score/config')->one($v['config_id']);
                    if ($score_info && $score_info['type'] == 1) {
                        $score_num = $v['score'];
                        break;
                    }
                }
            }
            $this->data['user']['score'] = $score_num;

            $sex = Dever::db('passport/user')->config['sex'];
            if ($this->data['user']['area']) {
                $this->data['user']['area'] = str_replace(',', '-', $this->data['user']['area']);
                $this->data['user']['area'] .= '-';
            }
            $this->data['user']['text'] = $this->data['user']['area'] . $sex[$this->data['user']['sex']];

            # 获取邀请码
            $this->data['user']['code'] = Dever::load('invite/api')->code($this->data['uid']);
            $this->data['user']['code_img'] = Dever::url('qrcode/api.qrcode?string=' . $this->data['user']['code']);

            if (!$this->data['user']['avatar']) {
            	$this->data['user']['avatar'] = $this->data['config']['avatar'];
            }
        }

        return $this->data['user'];
    }

    # 我的消息页面
    public function my_msg()
    {
        if ($this->data['system'] != 1) {
            $this->data['msg'] = array();
            return $this->data;
        }
        $this->data['msg'] = Dever::load('message/lib/data')->read($this->data['uid'], false, true, false, $this->data['system']);

        if ($this->data['msg']) {
            foreach ($this->data['msg'] as $k => $v) {
                $this->data['msg'][$k]['cdate_time'] = date('Y-m-d H:i:s', $v['cdate']);
            }
        }

        return $this->data;
    }

    # 我喜欢的页面
    public function my_up()
    {
        $this->data['like'] = Dever::load('service/lib/like')->getList($this->data['uid']);

        if ($this->data['like']) {
            foreach ($this->data['like'] as $k => $v) {
                $this->data['like'][$k]['cdate_time'] = date('Y-m-d H:i:s', $v['cdate']);
                if ($v) {
                    $this->data['like'][$k]['info'] = (object) $this->content($v);
                } else {
                    $this->data['like'][$k]['info'] = (object) array();
                }
                
            }
        }
        return $this->data;
    }

    # 分享
    public function submit_share()
    {
        $id = Dever::input('id');
        if (!$id) {
            Dever::alert('错误的ID');
        }
        $type = Dever::input('type');
        if (!$type) {
            Dever::alert('错误的类型');
        }

        Dever::load('service/lib/share')->submit($this->data['uid'], $id, $type);

        return array('id' => $id, 'type' => $type);
    }

    # 回流
    public function submit_share_reflux()
    {
        $source_uid = $this->getSourceUid();

        $id = Dever::input('id');
        if (!$id) {
            Dever::alert('错误的ID');
        }
        $type = Dever::input('type');
        if (!$type) {
            Dever::alert('错误的类型');
        }

        Dever::load('service/lib/share')->submit_reflux($source_uid, $this->data['uid'], $id, $type, $this->data['system']);

        return array('id' => $id, 'type' => $type);
    }


    # 点赞
    public function submit_up()
    {
        $id = Dever::input('id');
        if (!$id) {
            Dever::alert('错误的ID');
        }
        $type = Dever::input('type', 1);
        if (!$type) {
            Dever::alert('错误的类型');
        }

        Dever::load('service/lib/like')->submit($this->data['uid'], $id, $type);

        return array('id' => $id, 'type' => $type);
    }

    # 发布反馈
    public function submit_feedback()
    {
        $username = Dever::input('username');
        if (!$username) {
            //Dever::alert('错误的用户姓名');
        }
        $name = Dever::input('name');
        if (!$name) {
            //Dever::alert('错误的标题');
        }
        $type_id = Dever::input('type_id');
        if (!$type_id) {
            Dever::alert('请选择反馈类型');
        }
        $contact = Dever::input('contact');
        if (!$contact) {
            Dever::alert('请填写联系方式');
        }
        $content = Dever::input('content');
        if (!$content) {
            Dever::alert('请填写内容');
        }

        $id = Dever::load('service/lib/feedback')->submit($this->data['uid'], $username, $name, $type_id, $contact, $content);

        return array('id' => $id);
    }

    # 发布反馈
    public function get_feedback_type()
    {
        $this->data['type'] = Dever::db('service/feedback_type')->getAll();

        return $this->data;
    }
}