<?php
declare (strict_types = 1);

namespace app\admin\controller\v1;

use app\admin\model\v1\FaqModel;
use app\admin\validate\v1\FaqValidate;

/**
 * 常见问题控制器
 */
class Faq
{
    // 分页
    public function index()
    {
        $param = request()->param([
            'question',
            'created_at',
            'page/d' => 1,
            'size/d' => 10
        ]);

        $faqs = FaqModel::field([
                'id',
                'image',
                'question',
                'recommend',
                'sort',
                'created_at'
            ])
            ->withSearch(['question', 'created_at'], [
                'question'   => $param['question'] ?? null,
                'created_at' => !empty($param['created_at']) ? explode(',', $param['created_at']) : null
            ])
            ->language(request()->lang_id)
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->paginate([
                'list_rows' => $param['size'],
                'page'      => $param['page'],
            ])
            ?->each(fn($item) => $item->image = thumb($item->image));

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

    // 详情
    public function read()
    {
        $id = request()->param('id');

        $faq = FaqModel::withoutField([
                'language_id',
                'created_at',
                'updated_at',
                'deleted_at',
            ])
            ->bypk($id)
            ->find();
        if (empty($faq)) {
            return error('数据不存在');
        }

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

    // 新增
    public function save()
    {
        $post = request()->post([
            'question',
            'image',
            'recommend',
            'sort',
            'answer'
        ]);
        $data = array_merge($post, ['language_id' => request()->lang_id]);

        $validate = new FaqValidate;
        if (!$validate->scene('add')->check($data)) {
            return error($validate->getError());
        }

        $faq = FaqModel::create($data);
        if ($faq->isEmpty()) {
            return error('操作失败');
        }
        return success('操作成功');
    }

    // 修改
    public function update()
    {
        $id  = request()->param('id');
        $put = request()->put([
            'question',
            'image',
            'recommend',
            'sort',
            'answer'
        ]);

        $validate = new FaqValidate;
        if (!$validate->scene('edit')->check(array_merge($put, ['id' => $id]))) {
            return error($validate->getError());
        }

        $faq = FaqModel::bypk($id)->find();
        if (empty($faq)) {
            return error('请确认操作对象是否存在');
        }

        if (!$faq->save($put)) {
            return error('操作失败');
        }
        return success('操作成功');
    }
    
    // 设置排序值
    public function sort()
    {
        $id   = request()->param('id');
        $sort = request()->post('sort');

        $faq = FaqModel::bypk($id)->find();
        if (empty($faq)) {
            return error('请确认操作对象是否存在');
        }

        if ($faq->sort != $sort) {
            $faq->sort = $sort;
            if (!$faq->save()) {
                return error('操作失败');
            }
        }
        return success('操作成功');
    }

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

        $faq = FaqModel::bypk($id)->find();
        if (empty($faq)) {
            return error('请确认操作对象是否存在');
        }

        if (!$faq->delete()) {
            return error('操作失败');
        }

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