<?php
declare (strict_types = 1);

namespace app\admin\validate\v1;

use think\Validate;

class ProductValidate extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'language_id'                  => 'require|integer',
        'category_id'                  => 'integer',
        'spu'                          => 'require|max:255',
        'name'                         => 'require|max:125',
        'short_name'                   => 'max:64',
        'cover_image'                  => 'max:255',
        'desc'                         => 'max:255',
        'video_img'                    => 'max:255',
        'video_url'                    => 'max:255',
        'is_sale'                      => 'in:0,1',
        'is_new'                       => 'in:0,1',
        'is_hot'                       => 'in:0,1',
        'is_show'                      => 'in:0,1',
        'sort'                         => 'integer',
        'detail'                       => 'max:65535',
        'status'                       => 'in:-1,1',
        'seo_title'                    => 'max:255',
        'seo_keywords'                 => 'max:255',
        'seo_desc'                     => 'max:255',
        'created_at'                   => 'checkFormatDatetimeRange',
        'skus.*.sku'                   => 'max:125',
        'skus.*.main_image'            => 'max:255',
        'skus.*.sort'                  => 'integer',
        'skus.*.attrs'                 => 'checkSkusAttrsItemType:attr_id,integer|checkSkusAttrsItemMax:attr_value,128',
        'related.*.related_product_id' => 'integer',
        'related.*.sort'               => 'integer',
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [
        'language_id.require'                   => '语言ID不能为空',
        'language_id.integer'                   => '语言ID必须为整数',
        'category_id.integer'                   => '分类ID必须为整数',
        'spu.require'                           => 'spu不能为空',
        'spu.max'                               => 'spu不能超过255个字符',
        'name.require'                          => '名称不能为空',
        'name.max'                              => '名称不能超过125个字符',
        'short_name.max'                        => '短标题不能超过64个字符',
        'cover_image.max'                       => '封面图不能超过255个字符',
        'desc.max'                              => '描述不能超过255个字符',
        'video_img.max'                         => '视频封面图不能超过255个字符',
        'video_url.max'                         => '视频地址不能超过255个字符',
        'is_sale.in'                            => '在售状态值错误',
        'is_new.in'                             => '新品状态值错误',
        'is_hot.in'                             => '热门状态值错误',
        'is_show.in'                            => '上下架状态值错误',
        'sort.integer'                          => '排序值类型错误',
        'detail.max'                            => '详情不能超过65535个字符',
        'status.in'                             => '状态值错误',
        'seo_title.max'                         => 'seo标题不能超过255个字符',
        'seo_keywords.max'                      => 'seo关键字不能超过255个字符',
        'seo_desc.max'                          => 'seo描述不能超过255个字符',
        'created_at.checkFormatDatetimeRange'   => '添加时间格式错误',
        'skus.*.sku.max'                        => 'sku不能超过125个字符',
        'skus.*.main_image.max'                 => 'sku主图不能超过255个字符',
        'skus.*.sort.integer'                   => 'sku排序值类型错误',
        'skus.*.attrs.checkSkusAttrsItemType'   => 'sku属性值错误',
        'skus.*.attrs.checkSkusAttrsItemMax'    => 'sku属性值错误',
        'related.*.related_product_id.integer'  => '关联产品ID格式错误',
        'related.*.sort.integer'                => '关联产品排序值类型错误',
    ];

    /**
     * 分页列表验证场景
     */
    public function sceneIndex()
    {
        return $this->only(['categroy_id', 'is_show', 'created_at']);
    }

    /**
     * 回收站分页验证场景
     */
    public function sceneTrash()
    {
        return $this->only(['categroy_id']);
    }

    /**
     * 数据更新验证场景
     */
    public function sceneUpdate()
    {
        return $this->only([
            'language_id',
            'category_id',
            'spu',
            'name',
            'short_name',
            'cover_image',
            'desc',
            'video_img',
            'video_url',
            'is_sale',
            'is_new',
            'is_hot',
            'is_show',
            'sort',
            'detail',
            'status',
            'seo_title',
            'seo_keywords',
            'seo_desc',
            'skus.*.sku',
            'skus.*.main_image',
            'skus.*.sort',
            'skus.*.attrs',
            'related.*.related_product_id',
            'related.*.sort'
        ]);
    }

    protected function checkFormatDatetimeRange($value, $rule, $data)
    {
        $val = explode(',', $value);
        if (empty($val)) return '时间错误';
        $v1 = strtotime($val[0]);
        if (!$v1) return '时间错误';
        if (count($val) == 2) {
            $v2 = strtotime($val[1]);
            if (!$v2) {
                return '时间错误';
            }
        }

        return true;
    }

    protected function checkSkusAttrsItemType($value, $rule, $data)
    {
        $rule = explode(',', $rule);
        foreach ($value as $v) {
            if (!array_key_exists($rule[0], $v)) {
                return 'sku属性值错误';
            }
            if ($rule[1] == gettype($rule[0])) {
                return "sku中{$rule[0]}类型错误";
            }
        }
        return true;
    }

    protected function checkSkusAttrsItemMax($value, $rule, $data)
    {
        $rule = explode(',', $rule);
        foreach ($value as $v) {
            if (!array_key_exists($rule[0], $v)) {
                return 'sku属性值错误';
            }
            if (intval($rule[1]) < mb_strlen($v[$rule[0]], 'utf8')) {
                return "sku属性{$rule[0]}不能超过{$rule[1]}个字符";
            }
        }
        return true;
    }
}
