<?php

namespace app\adminapi\validates\marketing;

use think\Validate;

class StoreRechargeCardValidate extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'    =>    ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'title' => 'require',
        'poster' => 'require',
        'explain' => 'require',
        'is_brokerage' => 'require',
        'brokerage' => 'requireCallback:checkBrokerage',
        'price' => 'require',
        'section_time' => 'require',
        'poundage' => 'require',
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名'    =>    '错误信息'
     *
     * @var array
     */
    protected $message = [
        'title.require' => '请填写商品标题',
        'poster.require' => '请选择活动海报',
        'explain.require' => '请填写活动说明',
        'brokerage.require' => '请填写返现比例',
        'price.require' => '请填写充值金额',
        'section_time.require' => '请选择时间段',
        'poundage.require' => '请填写手续费',
    ];

    protected $scene = [
        'save' => ['title', 'poster', 'explain', 'brokerage', 'price', 'section_time', 'poundage'],
    ];

    protected function checkBrokerage($value, $data)
    {
        if($data['is_brokerage'] == 1){
            return true;
        }
        if(!empty($value)){
            foreach ($value as $v) {
                if (isset($v['title']) && empty($v['title'])) {
                    return '标题不能为空';
                }
                if (isset($v['role']) && empty($v['role'])) {
                    return '返现角色不能为空';
                }
                if (isset($v['level']) && empty($v['level'])) {
                    return '返现等级不能为空';
                }
                if (isset($v['num']) && empty($v['num'])) {
                    return '返现人数不能为空';
                }
                if (isset($v['scale']) && empty($v['scale'])) {
                    return '返现比例不能为空';
                }
            }
        }
    }
}