<?php
namespace app\index\controller;

use think\Controller;

class Message extends Controller
{
    public function index()
    {
        // 留言知识库(默认查询前七天的留言)
        $end = time();
        $start = time()-604800;
        if(request()->isPost()){
            $time = input("param.time/s");
            $key = input("param.key/s");
            $type = input("param.type/s");
            if($time == 'week'){
                //前一周
                $start = time()-604800;
            }
            if($time == 'oneMonth'){
                //前一个月
                $start = time()-2592000;
            }
            if($time == 'threeMonth'){
                //前三个月
                $start = time()-7776000;
            }
            if(!empty($start_time)){
                $start = strtotime($start_time);
            }
            if(!empty($end_time)){
                $end = strtotime($end_time . ' 23:59:59');
            }

            $where  = array();
            if(!empty($type)){
                $type_id = db('messagetype')->where('name', $type)->find();
                if(!empty($type)){
                    $where['type_id'] = $type_id['id'];
                }
            }
        }
        if(!empty($key)){
            $message = db('accountsmessage')->whereBetween('add_time', [$start, $end])->where($where)->whereLike('content','%'.$key.'%')->where('if_public', 1)->select();
        }else{
            $message = db('accountsmessage')->whereBetween('add_time', [$start, $end])->where($where)->where('if_public', 1)->select();
        }

        $type = db('messagetype')->select();
        foreach($message as $key=>$vo){
            //转为日期时间格式
            $message[$key]['add_time'] = date('Y-m-d H:i:s',$vo['add_time']);
            $message[$key]['dealWith_time'] = date('Y-m-d H:i:s',$vo['dealWith_time']);
            //类型名称
            for($i=0;$i<count($type);$i++){
                if($vo['type_id'] == $type[$i]['id']){
                    $message[$key]['type_name'] = $type[$i]['name'];
                }
            }
        }

        $data = [
            'status'=>1,
            'msg'=>'成功',
            'data'=>$message
        ];

        return json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    // 留言类型
    public function type()
    {
        $type = db('messagetype')->select();
        for($i=0;$i<count($type);$i++){
            $type[$i]['addtime'] = date('Y-m-d H:i:s',$type[$i]['addtime']);
        }

        $data = [
            'status'=>1,
            'msg'=>'成功',
            'data'=>$type
        ];

        return json_encode($data,JSON_UNESCAPED_UNICODE);
    }

}
