<?php

namespace app\user\controller;
use think\Lang;
use app\user\model\UserMessage;

class Message extends UserControl
{

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/db.lang.php');

    }

    public function index()
    {
        $message = new UserMessage();
        $user_id = session('user_id');

        $condition = array();

        $condition['user_id'] = array('=',$user_id);

        $time = input('get.timeRang');
        if (!empty($time)) {
            $gap = explode(' - ', $time);
            $begin = $gap[0];
            $end = date('Y-m-d', strtotime ("+1 day", strtotime($gap[1])));
            $condition['userMessage_create'] = array('between', array($begin, $end));
        }
        $type = input('get.type');
        if ($type) {
            if($type == -1){
                $condition['userMessage_status'] = array('<',date('Y-m-d',time()));
            }
            if($type == 1){
                $condition['userMessage_status'] = array('>=',date('Y-m-d',time()));
            }
        }
        $usermessage = $message->getUserMessage($condition);
        for($i=0;$i<count($usermessage);$i++){
            if($usermessage[$i]['userMessage_status'] == 0){
                $usermessage[$i]['userMessage_status'] = '未读';
            }else{
                $usermessage[$i]['userMessage_status'] = '已读';
            }
        }
        $this->assign('show_page', $usermessage->render());
        $this->assign('usermessage', $usermessage);

        return $this->fetch('index');
    }

    public function detail()
    {
        $userMessage_id = $_SERVER['QUERY_STRING'];
        $usermessage = new UserMessage();
        $message = $usermessage->getOneMessage($userMessage_id);
        db('user_message')->where('userMessage_id', $userMessage_id)->update(['userMessage_status' => 1]);
        $this->assign('message', $message);
        return $this->fetch('detail');
    }

}

?>
