<?php

namespace app\validate;

use think\facade\Config;
use think\Validate;

class CouponRelease extends Validate
{
    protected $rule = [
        'business_code|商家code' => 'require',
        //'lng|经度' => 'require',
        //'lat|纬度' => 'require',
        'count|总数' => 'require|number|>:0',
        'type|签到券类型' => 'require|>:0',
        'start_time|开始时间' => 'require|date',
        'end_time|结束时间' => 'require|date|checkEndTime',
        'name|签到券名称' => 'require|length:3,32',
        //'money|金额' => 'require|>:0|<=:5000',
        'deduction_money|扣除金额' => 'require|>=:0|<=:100000',
        'image_url|预览图' => 'require',
        //'using_rule|使用规则' => '',
        //'punishing_rule|处罚规则' => '',
        //'using_count|使用中' => '',
        //'received_count|已领取的签到券数量' => '',
        //'edition|发布的版本' => '',
        'status|状态' => 'require|in:1,0',
        'on_shelf|上架状态' => 'require|in:1,0',
        'on_screen|展示到商圈大屏' => 'require|in:1,0',
        //'intro|详情' => '',
        //'content|详情' => '',
        //'white_list|白名单' => '',
    ];

    protected $message = [
        'business_code.require'     => '商家信息错误',
        'type'                      => '签到券类型错误',
        'status'                    => '状态错误',
        'on_shelf'                  => '上架状态错误',
        'on_screen'                 => '展示到商圈大屏状态错误',
    ];

    protected $scene = [
        'edit' => ['type','start_time', 'end_time', 'name',"status","on_shelf",'image_url'],
        'api_edit' => ['type','name', 'type', 'start_time',"end_time","image_url"],
    ];

    // edit 验证场景定义
    public function sceneOrdinary()
    {

        return $this->append("deduction_money",'checkDeductionMoney');
    }

    protected function checkEndTime($value, $rule, $data = [])
    {
        if (strtotime($value) <= strtotime($data['start_time'])) {
            return "结束时间不能小于等于开始时间";
        }
        return true;

    }


    protected function checkDeductionMoney($value, $rule, $data = [])
    {
        if ($value <= 0) {
            return  "最小扣除佣金不能为0";
        }
        //最小充值
        Config::load('extra/wechat', 'wechat');
        $minCouponDeductionMoney = config('wechat.minCouponDeductionMoney') ?? 0;
        if ($minCouponDeductionMoney > $value) {
            return  "最小扣除佣金{$minCouponDeductionMoney}";
        }
        return true;
    }
}