<?php

namespace app\admin\validate\sys_manage;

use support\Validate;

class FieldValidate extends Validate
{
    protected $rule = [
        'field_status|状态' => 'require|in:ACTIVED,DISABLED',
//        'field_category|分类' => 'in:NORMAL,PAGE,OTHER',
        'field_name|名称' => 'require',
        'field_table|所属表' => 'require|alphaDash',
        'field_column_key|字段列名' => 'alphaDash',
//        'field_column_datatype|数据类型' => 'in:string,decimal,text,dateTime',
//        'field_column_default|列默认值' => 'checkDefault',
//        'field_form_key' => 'in:id,name',
//        'field_form_type|DOM表单类型' => 'alphaDash',
//        'field_form_default' => 'chsDash',
//        'field_refer_json' => 'isJson',
//        'field_remark' => 'chsDash',
        'field_extend_json' => 'isJson'
    ];

    protected $message = [];

    protected $scene = [
        'add' => ['field_status', 'field_category', 'field_name', 'field_table', 'field_column_key', 'field_column_datatype', 'field_form_key', 'field_form_type', 'field_column_default', 'field_refer_json', 'field_extend_json'],
        'update' => ['field_category', 'field_name', 'field_table', 'field_column_key', 'field_column_datatype', 'field_form_key', 'field_form_type', 'field_column_default', 'field_refer_json', 'field_extend_json'],
        'update_status' => ['field_status']
    ];

    /**
     * @Desc 验证json
     * @Author Gorden
     * @Date 2024/2/22 14:09
     *
     * @param $value
     * @return string|true
     */
    protected function isJson($value)
    {
        if (is_json($value)) {
            return true;
        }

        return '数据格式错误~';
    }

    protected function checkDefault($value, $rule, $data)
    {
        if ($data['field_column_datatype'] == 'dateTime') {
            if (strlen($value) >= 14 && false !== strtotime($value)) {
                return true;
            }

            return "数据格式错误~ 例如：2024-01-01 12:00:00";
        }
        return true;
    }
}