<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\validate\custom;


use app\common\model\custom\Custom;
use app\common\model\dict\DictData;
use app\common\validate\BaseValidate;


/**
 * CustomFollow验证器
 * Class CustomFollowValidate
 * @package app\adminapi\validate\custom_follow
 */
class CustomFollowValidate extends BaseValidate
{

     /**
      * 设置校验规则
      * @var string[]
      */
    protected $rule = [
        'id' => 'require',
	    'name' => 'require',
        'custom_id' => 'require|checkCustom',
	    'date' => 'require|dateFormat:Y-m-d',
	    'types' => 'require|checkTypes',
	    'next_follow_date' => 'dateFormat:Y-m-d|checkNext',
	    'annex' => 'checkAnnex',
    ];
	
	protected $message = [
		'id.require' => '缺少必要参数',
		'name.require' => '请填写主题',
		'custom_id.require' => '请选择客户',
		'date.require' => '请选择日期',
		'date.dateFormat' => '日期格式错误',
		'types.require' => '请选择类型',
		'next_follow_date.dateFormat' => '下次回访日期格式错误',
	];

    /**
     * @notes 添加场景
     * @return CustomFollowValidate
     * @author likeadmin
     * @date 2023/11/12 13:40
     */
    public function sceneAdd()
    {
        return $this->remove('id', true);
    }


    /**
     * @notes 编辑场景
     * @return CustomFollowValidate
     * @author likeadmin
     * @date 2023/11/12 13:40
     */
    public function sceneEdit()
    {}


    /**
     * @notes 删除场景
     * @return CustomFollowValidate
     * @author likeadmin
     * @date 2023/11/12 13:40
     */
    public function sceneDelete()
    {
        return $this->only(['id']);
    }


    /**
     * @notes 详情场景
     * @return CustomFollowValidate
     * @author likeadmin
     * @date 2023/11/12 13:40
     */
    public function sceneDetail()
    {
        return $this->only(['id']);
    }
	
	public function checkCustom($value): bool|string
	{
		$custom = Custom::where('id',$value)->findOrEmpty();
		if($custom->isEmpty()){
			return '客户不存在';
		}
		return true;
	}
	
	public function checkTypes($value): bool|string
	{
		$dictData = DictData::where('type_value','custom_follow_type')->column('value');
		if(!in_array($value,$dictData)){
			return '类型无效';
		}
		return true;
	}
	
	public function checkAnnex($value): bool|string
	{
		if(!empty($value) && $value != ''){
			$annex = json_decode($value,true);
			if(empty($annex) || !is_array($annex)){
				return '附件格式错误';
			}
		}
		return true;
	}
	
	public function checkNext($value,$rule,$data): bool|string
	{
		if(!empty($value)){
			if(strtotime($value) - strtotime($data['date']) <=0){
				return '下次回访日期不能小于上面选择的日期';
			}
		}
		return true;
	}

}