<?php
declare (strict_types = 1);

namespace app\admin\controller\v1;

use app\admin\model\v1\ArticleLeaveMessageModel;

/**
 * 文章留言(评论)管理控制器
 */
class ArticleLeaveMessage
{
    // 文章留言分页列表
    public function index()
    {
        // 获取参数
        $param = request()->param([
            'title',
            'is_audited',
            'created_at',
            'lang_id' => request()->lang_id,
            'page/d'  => 1,
            'size/d'  => 10
        ]);

        // 获取文章留言列表
        $data = ArticleLeaveMessageModel::withoutField([
                'ip',
                'user_agent',
                'deleted_at'
            ])
            ->withJoin(['article' => function($query) use($param) {
                $query->where('article.language_id', '=', $param['lang_id']);
                if (!empty($param['title'])) {
                    $query->where('article.title', 'like', '%' . $param['title'] . '%');
                }
            }])
            ->isAudited($param['is_audited']??null)
            ->withSearch(['article_leave_message_model.created_at'], [
                'article_leave_message_model.created_at' => $param['created_at']??'',
            ])
            ->order(['article_leave_message_model.id' => 'desc'])
            ->hidden(['article_id', 'article'])
            ->paginate([
                'page'      => $param['page'],
                'list_rows' => $param['size'],
            ])
            ->bindAttr('article', ['article_title' => 'title']);

        return success('获取成功', $data);
    }

    // 审核/反审核
    public function audit()
    {
        // 获取参数
        $id = request()->param('id');

        // 审核/反审核
        $message = ArticleLeaveMessageModel::bypk($id)->find();
        if (is_null($message)) {
            return error('请确认操作对象');
        }
        $message->is_audited =  (int)!$message->is_audited;
        if (!$message->save()) {
            return error('操作失败');
        }

        return success('操作成功');
    }

    // 删除
    public function delete()
    {
        // 获取参数
        $id = request()->param('id');

        // 删除
        $message = ArticleLeaveMessageModel::bypk($id)->find();
        if (is_null($message)) {
            return error('请确认操作对象');
        }
        if (!$message->useSoftDelete('deleted_at', date('Y-m-d H:m:s', time()))->delete()) {
            return error('操作失败');
        }

        return success('操作成功');
    }

    // 导出
    public function export()
    {
        $schema = [
            'id'            => 'ID',
            'article_title' => '文章标题',
            'name'          => '姓名',
            'email'         => '邮箱',
            'content'       => '评论内容',
            'ip'            => 'IP',
            'is_audited'    => '是否审核',
            'created_at'    => '评论时间',
        ];

        // 获取导出数据
        $data = $this->getExportMessageData();
        
        // 导出
        return xlsx_writer($data, $schema, '文章评论列表' . date('YmdHis'));
    }
    private function getExportMessageData()
    {
        $param = request()->param(['title', 'is_audited', 'created_at']);
        $data = ArticleLeaveMessageModel::field([
                '*',
                'CASE WHEN is_audited = 1 THEN "已审核" ELSE "未审核" END' => 'is_audited'
            ])
            ->withJoin(['article' => function($query) use($param) {
                $query->where('article.language_id', '=', request()->lang_id);
                if (!empty($param['title'])) {
                    $query->where('article.title', 'like', '%' . $param['title'] . '%');
                }
            }])
            ->isAudited($param['is_audited']??null)
            ->withSearch(['article_leave_message_model.created_at'], [
                'article_leave_message_model.created_at' => $param['created_at']??'',
            ])
            ->order(['article_leave_message_model.id' => 'desc'])
            ->hidden(['article_id', 'article'])
            ->select()
            ->bindAttr('article', ['article_title' => 'title']);

        return $data->toArray();
    }
}
