<?php

namespace app\admin\controller\content;

use app\controller\Curd;
use app\model\Content;
use support\exception\BusinessException;
use support\Request;
use support\Response;

class ApartmentController extends Curd
{
    public function __construct()
    {
        $this->model = new Content();
    }

    public function info(Request $request): Response
    {
        $data = $this->model->where('content_category', 'APARTMENT')->first();

        if (!empty($data->content_config_json)){
            $data->content_config_json = json_decode($data->content_config_json);
        }

        return json_success('', $data);
    }

    protected function updateInput(Request $request): array
    {
        $primary_key = $this->model->getKeyName();
        $id = $request->post($primary_key);

        $params = $request->post();
        if (is_array($params['content_config_json'])){
            $params['content_config_json'] = json_encode(array_filter($params['content_config_json'],function ($value){
                return ($value != '' && $value != ' ' && $value != null);
            }), JSON_UNESCAPED_UNICODE);
        }
        $data = $this->inputFilter($params);
        $model = $this->model->find($id);
        if (!$model) {
            throw new BusinessException('记录不存在', 2);
        }
        unset($data[$primary_key]);
        return [$id, $data];
    }
}