<?php
/**
 * 会员消息
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author zhaoh <1070309178@qq.com>
 */

namespace Member\Controller;
use Think\Controller;
class MessageController extends BaseController
{
    public function _initialize()
    {
        parent::_initialize();
        $msg['noread'] = M('Message')->where('status=0 and touid=' . MID)->count();
        $this->assign('msg', $msg);
    }

    //消息列表
    public function index()
    {
        $postdata = I('param.');
        if (!empty($postdata['status'])) {
            $map['status'] = $postdata['status'] - 1;
        }
        $map['touid'] = MID;
        $info = page_list('Message', $map, 'status asc,addtime desc', true, 10, '', 1);
        $this->assign('info', $info);
        $this->assign('tplseo_title', '消息列表_会员中心_' . C('WEB_SITE_TITLE'));
        $this->display();
    }

    //删除消息
    public function del_msg()
    {
        $id = I('param.id');
        $res = M('Message')->where('id=' . $id)->delete();
        if ($res) {
            $data['status'] = 1;
            $data['msg'] = '已删除';
        } else {
            $data['status'] = 0;
            $data['msg'] = '删除失败';
        }
        $this->ajaxReturn($data);
    }

    //标记已读
    public function read_all()
    {
        $where['touid'] = MID;
        $save['status'] = 1;
        $result = M('Message')->where($where)->save($save);
        if ($result !== false) {
            $data['status'] = 1;
            $data['msg'] = "已全部标记！";
        } else {
            $data['status'] = 0;
            $data['msg'] = "标记失败！";
        }
        $this->ajaxReturn($data);
    }
}