<?php

namespace app\admin\controller\sys_manage;

use app\admin\service\sys_manage\FieldService;
use app\admin\validate\sys_manage\FieldValidate;
use app\controller\Curd;
use app\model\Content;
use app\model\SysField;
use Illuminate\Support\Facades\Schema;
use support\Request;
use support\Response;

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

    /**
     * @Desc 字段列表
     * @Author Gorden
     * @Date 2024/2/22 14:49
     *
     * @param Request $request
     * @return \support\Response
     */
    public function fieldList(Request $request)
    {

        return FieldService::fieldList($request);
    }

    public function selectList(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, 1000);
    }

    public function afterQuery($items)
    {
        foreach ($items as &$item) {
            if (!empty($item->field_refer_json)) {
                $fieldReferJson = json_decode($item->field_refer_json, true);
                $item->field_refer_json_default = $fieldReferJson['default'] ?? '';
            }
        }
        return $items;
    }

    /**
     * @Desc 字段详情
     * @Author Gorden
     * @Date 2024/2/22 14:54
     *
     * @param $id
     * @return \support\Response
     */
    public function fieldInfo($id)
    {
        return FieldService::fieldInfo($id);
    }

    /**
     * @Desc 新建字段
     * @Author Gorden
     * @Date 2024/2/22 14:32
     *
     * @param Request $request
     * @return \support\Response
     */
    public function addField(Request $request)
    {
//        $validate = new FieldValidate();
//        if (!$validate->scene('add')->check($request->post())) {
//            return json_fail($validate->getError());
//        }

        // 验证数据表是否存在
        if (!FieldService::checkTableExist($request->post('field_table'))) {
            return json_fail('数据表不存在');
        }
        // 验证字段重复
        if (FieldService::checkFieldExist($request->post('field_table'), $request->post('field_column_key'))) {
            return json_fail('字段已存在，请勿重复添加');
        }

        return FieldService::addField($request->post());
    }

    /**
     * @Desc 修改字段
     * @Author Gorden
     * @Date 2024/2/22 14:41
     *
     * @param $id
     * @param Request $request
     * @return \support\Response
     */
    public function updateFields($id, Request $request)
    {
//        $validate = new FieldValidate();
//        if (!$validate->scene('update')->check($request->post())) {
//            return json_fail($validate->getError());
//        }
        // 验证数据表是否存在
        if (!FieldService::checkTableExist($request->post('field_table'))) {
            return json_fail('数据表不存在');
        }
        return FieldService::updateField($id, $request->post());
    }

    /**
     * @Desc 修改字段状态
     * @Author Gorden
     * @Date 2024/2/22 15:05
     *
     * @param $id
     * @param Request $request
     * @return \support\Response
     */
    public function updateStatus($id, Request $request)
    {
        $validate = new FieldValidate();
        if (!$validate->scene('update_status')->check($request->post())) {
            return json_fail($validate->getError());
        }

        return FieldService::updateStatus($id, $request->post());
    }

    /**
     * @Desc 删除字段
     * @Author Gorden
     * @Date 2024/2/22 15:08
     *
     * @param $id
     * @return \support\Response
     */
    public function delField(Request $request)
    {
        $fieldId = $request->post('field_id');

        return FieldService::delField($fieldId);
    }
}