<?php
namespace app\service\controller;

class Message extends Base
{
    public function index()
    {
        // 留言信息(默认查询前七天的留言)
        $start = time()-604800;
        $end = time();
        if(request()->isPost()){
            $start_time = input("param.start_time/s");
            $end_time = input("param.end_time/s");
            if(!empty($start_time)){
                $start = strtotime($start_time);
            }
            if(!empty($end_time)){
                $end = strtotime($end_time . ' 23:59:59');
            }
        }
        $message = db('accountsmessage')->whereBetween('add_time', [$start, $end])->select();
//        foreach($message as $key=>$vo){
//            //转为日期时间格式
//            $message[$key]['add_time'] = date('Y-m-d H:i:s',$vo['add_time']);
//            if(!empty($vo['dealWith_time'])){
//                $message[$key]['dealWith_time'] = date('Y-m-d H:i:s',$vo['dealWith_time']);
//            }
//        }

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

        return json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    // 处理留言
    public function dealmessage()
    {
        if(request()->isPost()){

            //客服id
            //$user_id = 2;
            $user_id = input("param.user_id/s");
            //留言id
            //$message_id = 14;
            $message_id = input("param.message_id/s");
            //回复内容
            $reply_content = input("param.reply_content/s");

            $user = db('users')->where('id', $user_id)->find();
            if(empty($user)){
                return json(['code' => -1, 'data' => '', 'msg' => '客服不存在']);
            }
            $message = db('accountsmessage')->where('message_id', $message_id)->find();
            if(empty($message)){
                return json(['code' => -1, 'data' => '', 'msg' => '留言不存在']);
            }

            // 更新留言状态
            $param = [
                'user_id' => $user_id,
                'message_status' => 1,
                'reply_content' => $reply_content,
                'dealWith_time' => time()
            ];

            try{
                db('accountsmessage')->where('message_id', $message_id)->update($param);
            }catch(\Exception $e){
                return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
            }

            return json(['code' => 1, 'data' => '', 'msg' => '留言处理成功']);
        }
    }

}
