<?php

namespace app\admin\controller;

use app\model\Qas;
use support\Request;

class QaController extends base
{
    public function getQaList(Request $request)
    {
        $list = Qas::fieldRaw('city_name,title,content')->select();

        return $this->success($list);
    }

    public function getQaDetail(Request $request)
    {
        $id = $request->get('id');
        if (empty($id)) return $this->error(2001, 'id data cannot be empty!');

        $data = Qas::fieldRaw('id,city_name,title,content')->find();

        return $this->success($data);
    }

    public function addQa(Request $request)
    {
        $post = $request->post();
        if (empty($post['city_name'])) return $this->error(2001, 'city_name data cannot be empty!');
        if (empty($post['title'])) return $this->error(2001, 'title data cannot be empty!');
        if (empty($post['content'])) return $this->error(2001, 'content data cannot be empty!');

        try {
            $data = Qas::create($post);
        } catch (\Exception $e) {
            return $this->error(2002, $e->getMessage());
        }
        return $this->success($data);
    }

    public function editQa(Request $request)
    {
        $post = $request->post();
        if (empty($post['id'])) return $this->error(2001, 'id data cannot be empty!');

        try {
            $data = Qas::update($post);
        } catch (\Exception $e) {
            return $this->error(2002, $e->getMessage());
        }
        return $this->success($data);
    }

    public function delQa(Request $request)
    {
        $id = $request->get('id');
        if (empty($id)) return $this->error(2001, 'id data cannot be empty!');
        try {
            $data = Qas::destroy($id);
        } catch (\Exception $e) {
            return $this->error(2002, $e->getMessage());
        }
        return $this->success($data);
    }
}