<?php

namespace App\Http\Requests\Api\DynamicQrcode;

use App\Http\Requests\BaseRequest;
use Illuminate\Validation\Rule;

/**
 * 活码工具接口验证
 * @author 唐远望
 * @version 1.0
 * @date 2025-08-22
 * 
 */
class Templates extends BaseRequest
{
    /**
     * 获取应用于请求的规则
     *
     * @return array
     */
    public function rules()
    {
        // 返回结果
        $rules=      [
            'name'                 => 'required',
            'id'                => 'required|integer|gt:0',
            'status'            => 'required|integer|in:0,1',
            'page'              => 'integer|min:1',
            'limit'             => 'integer|min:1',
            'sort'              => 'required|integer|min:0',
            'dynamic_qrcode_image_url' => 'required',
            'link_url'           => 'required',
            'switching_method'   => 'required|integer|in:1,2,3,4',
            'restriction_type'   => 'required|integer|in:1,2',
            'open_repeat_addition' => 'required|integer|in:0,1',
            'domain_address'     => 'required',
            'open_landing_page'   => 'required|integer|in:0,1',
            'open_advanced_config'   => 'required|integer|in:0,1',
            // 落地页条件规则
            'open_safety_certification' => 'required_if:open_landing_page,1',
            'open_guidance_prompt' => 'required_if:open_landing_page,1',
            'activity_title' => 'required_if:open_landing_page,1',
            'event_description' => 'required_if:open_landing_page,1',
            'contact_event_organizer' => 'required_if:open_landing_page,1',
            // 高级配置条件规则
            'open_backup_content_display' => 'required_if:open_advanced_config,1',
            'open_wechat_press_frequently_qrcode' => 'required_if:open_advanced_config,1',
        ];
        // 添加条件验证规则 - 使用Rule::requiredIf
        $rules['customer_service_qrcode'] = [
            Rule::requiredIf(function () {
                return $this->input('open_landing_page') == 1 && 
                       $this->input('contact_event_organizer') == 1;
            })
        ];
        $rules['contact_phone'] = [
            Rule::requiredIf(function () {
                return $this->input('open_landing_page') == 1 && 
                       $this->input('contact_event_organizer') == 2;
            })
        ];
        $rules['backup_content'] = [
            Rule::requiredIf(function () {
                return $this->input('open_advanced_config') == 1 && 
                       $this->input('open_backup_content_display') == 1;
            })
        ];
        return $rules;
    }


    // 场景列表
    protected   $scenes         = [
        'detail'             => ['id'],
        'list'               => ['page', 'limit'],
        'add'                      => [
            'name',
            'switching_method',
            'restriction_type',
            'open_repeat_addition',
            'domain_address',
            'open_landing_page',
            'open_safety_certification',
            'open_guidance_prompt',
            'activity_title',
            'event_description',
            'contact_event_organizer',
            'customer_service_qrcode',
            'contact_phone',
            'open_advanced_config',
            'backup_content',
            'open_wechat_press_frequently_qrcode',
            'open_backup_content_display',
            'status'
        ],
        'edit'                  => [
            'id',
            'name',
            'switching_method',
            'restriction_type',
            'open_repeat_addition',
            'domain_address',
            'open_landing_page',
            'open_safety_certification',
            'open_guidance_prompt',
            'activity_title',
            'event_description',
            'contact_event_organizer',
            'customer_service_qrcode',
            'contact_phone',
            'open_advanced_config',
            'backup_content',
            'open_wechat_press_frequently_qrcode',
            'open_backup_content_display',
            'status'
        ],
        'set_status'              => ['id', 'status'],
        'delete'                  => ['id'],
    ];


    /**
     * 获取已定义验证规则的错误消息
     *
     * @return array
     */
    public function messages()
    {
        return [
            'name.required'     => '名称必填',
            'id.required'       => 'ID未知',
            'id.integer'        => 'ID格式错误',
            'id.gt'               => 'ID格式错误',
            'status.required'   => '状态未知',
            'status.integer'    => '状态格式错误',
            'status.in'         => '状态格式错误',
            'page.integer'      => '页码格式错误',
            'page.min'          => '页码格式错误',
            'limit.integer'     => '每页数量格式错误',
            'limit.min'         => '每页数量格式错误',
            'sort.required'         => '排序未知',
            'sort.integer'          => '排序格式错误',
            'sort.min'              => '排序格式错误',
            'dynamic_qrcode_image_url.required' => '活码二维码未知',
            'link_url.required' => '活码链接地址未知',
            'switching_method.required' => '切换方式未知',
            'switching_method.integer' => '切换方式格式错误',
            'switching_method.in' => '切换方式格式错误',
            'restriction_type.required' => '上限类型未知',
            'restriction_type.integer' => '上限类型格式错误',
            'restriction_type.in' => '上限类型格式错误',
            'open_repeat_addition.required' => '重复进群/加人未知',
            'open_repeat_addition.integer' => '重复进群/加人格式错误',
            'open_repeat_addition.in' => '重复进群/加人格式错误',
            'domain_address.required' => '域名地址未知',
            'open_landing_page.required' => '落地页开关未知',
            'open_landing_page.integer' => '落地页开关格式错误',
            'open_landing_page.in' => '落地页开关格式错误',
            'open_advanced_config.required' => '高级开关未知',
            'open_advanced_config.integer' => '高级开关格式错误',
            'open_advanced_config.in' => '高级开关格式错误',
            // 其他消息...
            'open_safety_certification.required_if' => '当开启落地页时，安全认证标识必填',
            'open_guidance_prompt.required_if' => '当开启落地页时，常按引导提示必填',
            'activity_title.required_if' => '当开启落地页时，活动标题必填',
            'event_description.required_if' => '当开启落地页时，活动描述必填',
            'contact_event_organizer.required_if' => '当开启落地页时，联系活动方必填',
            'customer_service_qrcode.required' => '当开启落地页并且联系活动方为客服时，客服二维码必填',
            'contact_phone.required' => '当开启落地页时，联系活动方式为电话时，联系电话必填',
            'open_backup_content_display.required_if' => '当开启高级配置时，备用内容显示必填',
            'backup_content.required' => '当开启高级配置且备用内容显示开启时，备用内容必填',
            'open_wechat_press_frequently_qrcode.required_if' => '当开启高级配置时，微信常按识别二维码必填',
        ];
    }
}
