<?php

namespace app\api\controller\LeaveMessage;

use app\BaseController;
use app\common\model\ContactUs\LeaveMessage as ModelLeaveMessage;
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use think\facade\Db;
use app\common\exception\Tool;
use think\facade\Env;


class LeaveMessage extends BaseController
{
    /**
     * 获取用户留言列表
     */
    public function getLeaveMessageList(Request $request): array
    {
        $params = $request->param();
        $con = [];

        $con = Tool::getOptionalQuery();

        $query = ModelLeaveMessage::where($con)
            ->field([
                'leave_message_id',
                'leave_message_guid',
                'leave_message_content',
                'leave_message_status',
                'leave_message_user_name',
                'leave_message_user_phone'
            ])
            ->order('leave_message_update_time', 'desc');



        return msg("获取用户留言列表成功!", $query);
    }

    /**
     * 获取用户留言详情
     */
    public function getLeaveMessageInfo(Request $request): array
    {
        $params = $request->param();

        $this->validate($params, ['leave_message_guid' => 'require']);

        $find = ModelLeaveMessage::field([
            'leave_message_id',
            'leave_message_guid',
            'leave_message_content',
            'leave_message_status',
            'leave_message_user_name',
            'leave_message_user_phone'
        ])
            ->where('leave_message_guid', $params['leave_message_guid'])
            ->find();

        return msg(0, '获取用户留言详情成功!', ['data' => $find]);
    }
    
    /**
     * 添加用户留言
     */
    public function addLeaveMessage(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'leave_message_content|留言内容' => 'require',
            'leave_message_user_name|留言人名称' => 'require',
            'leave_message_user_phone|留言人手机号码' => 'require'
        ]);
        $model = ModelLeaveMessage::create($params, [
            'leave_message_guid',
            'leave_message_create_user_guid',
            'leave_message_update_user_guid',
            'leave_message_content',
            'leave_message_status',
            'leave_message_user_name',
            'leave_message_user_phone'
        ]);
        return msg('添加成功!');
    }
}
