<?php

namespace Main\Src;

use Dever;
use Main\Lib\Core;

class User extends Core
{
    protected $checkUser = true;

    # 我的页面
    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'];
                }
            }

            # 会员
            $this->data['user']['vip_type'] = Dever::load('vip/lib/member')->get($this->data['user'], $this->data['system']);
            if ($this->data['user']['vip_type'] > 0) {
                $this->data['user']['vip_end'] = date('Y-m-d', $this->data['user']['vip_type']);
            } else {
                $this->data['user']['vip_end'] = '';
            }

            $vip_config = Dever::db('vip/config')->one($this->data['system']);
            # 是否展示会员支付，1展示，2不展示
            $this->data['user']['vip_state'] = 2;
            if ($vip_config && isset($vip_config['button'])) {
                $this->data['user']['vip_state'] = $vip_config['button'];
            }
            $version_code = Dever::input('version_code');
            if ($version_code) {
                $source_type = Dever::input('source_type');
                $version_where['source_type'] = $source_type;
                $version_where['name'] = $version_code;
                $version = Dever::db('main/version')->getOne($version_where);
                if ($version && $version['vip'] > 0) {
                    $this->data['user']['vip_state'] = $version['vip'];
                }
            }

            $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;

            $this->data['user_msg'] = 0;
            # 查看当前用户的消息
            if ($this->data['system'] == 1) {
                $this->data['user_msg'] = Dever::load('message/lib/data')->num($this->data['uid'], $this->data['system']);
            }
            
        }

        return $this->data;
    }

    # 我的消息页面
    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('act/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 my_comment()
    {
        $this->data['comment'] = Dever::load('act/lib/comment')->getList($this->data['uid']);

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

    # 商品订单
    public function my_order()
    {
        return $this->data;
    }

    # 获取海报
    public function poster()
    {
        $id = Dever::input('id');
        if (!$id) {
            Dever::alert('错误的ID');
        }
        $type = Dever::input('type');
        if (!$type) {
            Dever::alert('错误的类型');
        }

        $template = Dever::input('template', 'share');
        //$path = Dever::input('path', 'pages/choice/index/index');
        $path = Dever::config('base')->applet_index;

        $table = Dever::config('base')->type_table[$type];
        $info = Dever::db($table)->one($id);
        //$scene = Dever::login($this->data['uid']) . ',' . $type . ',' . $id;
        $scene = $this->data['uid'] . ',' . $type . ',' . $id;
        //echo $scene;die;
        $send['project'] = Dever::input('project', $this->data['system']);
        $send['send'] = $scene;
        $send['width'] = 152;
        $send['path'] = $path; 
        $logo = Dever::load('wechat_applet/code.get', $send);

        if (!$info['share_pic']) {
            return array('img' => '');
        }
        if (!$info['share_title']) {
            $info['share_title'] = $info['name'];
        }
        $user = Dever::db('passport/user')->one($this->data['uid']);
        $model = array
        (
            'pic' => Dever::local($info['share_pic']),
            'name' => $info['share_title'],
            'logo' => Dever::local(str_replace('https', 'http', $logo)),
            //'logo' => 'http://cm.5dev.cn/applet_on/data/upload/1/2018/06/06/239fa207778aa923f4ff7594dfbdfcca.png',
        );

        if ($user) {
            $model['username'] = $user['username'] . '邀您一起看';
        }

        if ($info['share']) {
            $template = $info['share'];
        }

        $img = Dever::load('poster/api.get', $scene, $template, $model);
        return array('img' => $img);
    }

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

        Dever::load('act/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('act/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');
        if (!$type) {
            Dever::alert('错误的类型');
        }

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

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

    # 预约提醒
    public function submit_note()
    {
        $id = Dever::input('id');
        if (!$id) {
            Dever::alert('错误的ID');
        }
        $type = Dever::input('type');
        $formid = Dever::input('formid');
        if (!$type) {
            Dever::alert('错误的类型');
        }

        Dever::load('act/lib/note')->submit($this->data['uid'], $id, $type, $formid, $this->data['system']);

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


    # 发布评论
    public function submit_comment()
    {
        $id = Dever::input('id');
        if (!$id) {
            Dever::alert('错误的ID');
        }
        $type = Dever::input('type');
        if (!$type) {
            Dever::alert('错误的类型');
        }
        $content = Dever::input('content');
        if (!$content) {
            Dever::alert('错误的内容');
        }

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

        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('act/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('act/feedback_type')->getAll();

        return $this->data;
    }
}