<?php
namespace store\Admin;

use Dever;

class Msg extends Core
{
    # 列表
    public function getList()
    {
        $data = Dever::load('message/lib/data')->read($this->id, $this->user['msg_type'], true, false, 3);

        $html = '';
		if ($data) {
			foreach ($data as $k => $v) {
				$data[$k]['cdate_string'] = Dever::ddate($v['cdate']);
				if ($v['param']) {
					$data[$k]['param'] = Dever::json_decode($v['param']);
				}

				$read = Dever::url('admin/msg.read', 'store');

				$button = '';
				if ($v['link']) {
					$button = '<a href="javascript:;" onclick="read(\''.$read.'\', '.$v['id'].');jump(\''.$v['link'].'\')" class="btn btn-info btn-rounded waves-effect waves-light m-b-5 layui-btn layui-btn-xs">查看</button></a>';
				} elseif ($v['param'] && strstr($v['param'], '{')) {
					$data[$k]['param'] = Dever::json_decode($v['param']);

					if ($data[$k]['param']) {

						$view = '';
						if ($data[$k]['param']['type'] == 1) {
							$view = Dever::url('order_view?type=2&order_id=' . $data[$k]['param']['id'], 'store');
						} elseif ($data[$k]['param']['type'] == 2) {
							$view = Dever::url('stat_view?id=' . $data[$k]['param']['id'] . '&type=store&show=2', 'store');
						} elseif ($data[$k]['param']['type'] == 3) {
							$view = Dever::url('order_view?type=3&order_id=' . $data[$k]['param']['id'], 'store');
						} 

						if ($view) {
							$button = '<a href="javascript:;" onclick="read(\''.$read.'\', '.$v['id'].');jump(\''.$view.'\')"><button type="button" class="btn btn-info btn-rounded waves-effect waves-light m-b-5 layui-btn layui-btn-xs">查看</button></a>';
						}
					}
				}

				if (!$button) {
					$button = '<a href="javascript:;" onclick="read(\''.$read.'\', '.$v['id'].');showAlert(\''.$v['content'].'\')" class="btn btn-info btn-rounded waves-effect waves-light m-b-5 layui-btn layui-btn-xs">查看</button></a>';
				}
				

				$html .= '<tr><td>'.$v['name'].'</td><td>'.$v['content'].'</td><td>'.$data[$k]['cdate_string'].'</td><td>'.$button.'</td></tr>';
			}
		}
		return $html;
    }

    # 消息已读
    public function read_api()
    {
    	$id = Dever::input('id');
		Dever::load('message/lib/data')->view($this->id, $id);

		return 'ok';
    }
}