<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use Carbon_Fields\Field\Field;
use Gigneo\Post_Meta\Serialized;

$gigneo_license_list = gigneo_term_list( array( 'taxonomy' => 'gigneo_license' ), false );

$gigneo_accreditation_fields[] =
	Field::make( 'complex', 'gigneo_pm_licenses', esc_html__( 'Licenses', 'gigneo' ) )
	->set_datastore( new Serialized() )
	->add_fields(
		array(
			Field::make( 'select', 'license', esc_html__( 'License', 'gigneo' ) )
				->set_datastore( new Serialized() )
				->set_options( $gigneo_license_list ),
			Field::make( 'text', 'license_number', esc_html__( 'License number', 'gigneo' ) ),
			Field::make( 'text', 'provider', esc_html__( 'Provider', 'gigneo' ) ),
			Field::make( 'text', 'attachment_url', esc_html__( 'Attachments URL', 'gigneo' ) ),
			Field::make( 'text', 'verification_url', esc_html__( 'Verification URL', 'gigneo' ) ),
			Field::make( 'date', 'issue_date', esc_html__( 'Issue date', 'gigneo' ) ),
			Field::make( 'date', 'expiry_date', esc_html__( 'Expiry date', 'gigneo' ) ),
			Field::make( 'textarea', 'note', esc_html__( 'Additional note', 'gigneo' ) ),
		)
	)
	->set_header_template( '<%- license %>' )
	->set_collapsed( true );

//Payouts
$gigneo_active_payout_list = gigneo_withdrawal_payout_list();
if ( empty( $gigneo_active_payout_list ) ) {
	$gigneo_active_payout_list[''] = esc_html__( 'None', 'gigneo' );
}

//Payout fields
$gigneo_payouts_fields = array(
	Field::make( 'select', 'type', esc_html__( 'Payout type', 'gigneo' ) )
		->set_options( $gigneo_active_payout_list ),
	Field::make( 'text', 'label', esc_html__( 'Label', 'gigneo' ) ),
	Field::make( 'select', 'status', esc_html__( 'Status', 'gigneo' ) )
		->set_default_value( 'active' )
		->set_options(
			array(
				'active'   => esc_html__( 'Active', 'gigneo' ),
				'inactive' => esc_html__( 'Inactive', 'gigneo' ),
			)
		)
		->set_classes( 'gn-select2' ),
	Field::make( 'text', 'paypal_account', esc_html__( 'Paypal account', 'gigneo' ) )
		->set_conditional_logic(
			array(
				array(
					'field' => 'type',
					'value' => 'paypal',
				),
			)
		),
	Field::make( 'text', 'payoneer_account', esc_html__( 'Payoneer account', 'gigneo' ) )
		->set_conditional_logic(
			array(
				array(
					'field' => 'type',
					'value' => 'payoneer',
				),
			)
		),
);

//Bank
if ( isset( $gigneo_active_payout_list['bank'] ) ) {
	$gigneo_bank_field_list = gigneo_payout_bank_fields();
	foreach ( $gigneo_bank_field_list as $gigneo_bank_field_key => $gigneo_bank_field_title ) {
		$gigneo_payouts_fields[] = Field::make( 'text', "bank_$gigneo_bank_field_key", $gigneo_bank_field_title )
			->set_conditional_logic(
				array(
					array(
						'field' => 'type',
						'value' => 'bank',
					),
				)
			);
	}
}

//Custom payouts fields filter
$gigneo_payouts_fields = apply_filters( 'gigneo_profile_payouts_fields', $gigneo_payouts_fields, $gigneo_active_payout_list );

$gigneo_accreditation_fields[] =
	Field::make( 'complex', 'gigneo_pm_payouts', esc_html__( 'Payouts', 'gigneo' ) )
	->set_datastore( new Serialized() )
	->set_classes( 'gn-select2' )
	->add_fields( $gigneo_payouts_fields )
	->set_header_template( '<%- type %>' )
	->set_collapsed( true );
