<?php

namespace app\admin\controller\ContactUs;

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 app\common\exception\Tool;
use think\facade\Db;
use think\facade\Env;


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

        $con = Tool::getOptionalQuery(['leave_message_name', 'LIKE'], ['leave_message_phone', 'LIKE'], ['leave_message_position', '='], ['leave_message_status', '='],);

        $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 editLeaveMessage(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'leave_message_content|留言内容' => 'require',
            'leave_message_status|留言受理状态' => 'require',
            'leave_message_user_name|留言人名称' => 'require',
            'leave_message_user_phone|留言人手机号码' => 'require'
        ]);
        $model = ModelLeaveMessage::where('leave_message_guid', $params['leave_message_guid'])->find();
        if (!$model) throwErrorMsg("该用户留言不存在", 1);
        $model->allowField([
            'leave_message_update_user_guid',
            'leave_message_content',
            'leave_message_status',
            'leave_message_user_name',
            'leave_message_user_phone'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加用户留言
     */
    public function addLeaveMessage(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'leave_message_content|留言内容' => 'require',
            'leave_message_status|留言受理状态' => '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('添加成功!');
    }

    /**
     * 删除用户留言
     */
    public function deleteLeaveMessage(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'leave_message_guid' => 'require',
        ]);
        $leave_message = ModelLeaveMessage::where([
            'leave_message_guid' => explode(',', $params['leave_message_guid'])
        ])->select();
        $leave_message->delete();
        return msg('删除成功!');
    }

    /**
     * 导出Excel
     */
    public function exportExcel(Request $request)
    {
        $params = $request->param();
        $select = ModelLeaveMessage::field([
            'leave_message_content',
            'leave_message_status',
            'leave_message_user_name',
            'leave_message_user_phone'
        ])
            ->order('leave_message_update_time', 'desc')
            ->select();
        return ModelLeaveMessage::exportExcel($select);
    }

  /**
     * 用户留言受理接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-20
     * @author xjh
     * @since 1.0.0
     */
    public function handleleave_messageStatus(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'leave_message_guid|在线报名guid' => 'require',
        ]);
        $msg_data = [];
        $leave_message_guids = explode(',', $params['leave_message_guid']);
        $leave_message_guids_count = count($leave_message_guids);
        foreach ($leave_message_guids  as $line => $leave_message_guid) {
            $prefix_text = ($leave_message_guids_count != 1) ? "第" . ($line + 1) . "行-> " : null;
            try {
                $leave_message = ModelLeaveMessage::where('leave_message_guid', $leave_message_guid)->find();
                if (!$leave_message) throwErrorMsg('该在线报名数据不存在!');
                if ($leave_message->leave_message_status == 1)  throwErrorMsg('该在线报名数据已处理!');
                $leave_message->leave_message_status = 1;
                $leave_message->save();
                $msg_data[] = $prefix_text . "用户名称【{$leave_message->leave_message_user_name}】: <span style='color:#27af49'>受理成功!</span>";
            } catch (\Throwable $th) {
                $msg_data[] = $prefix_text . "用户名称:【{$leave_message->leave_message_user_name}】: <span style='color:red'>{$th->getMessage()}</span>";
            }
        };
        return msg(0, implode('<br>', $msg_data));
    }


}
