<?php

use Tami\WooCommerce\TamiAdminSettings;
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
$saved_settings = TamiAdminSettings::get_installment_settings();
?>
<form
        id="tami-installment-settings-form" name="tami-installment-settings-form"
        class="tami-text-tami tami-ajax-form"
        method="post"
        action="admin-ajax.php">

    <input type="hidden" name="action" value="tami_installment_settings_save"/>
	<?php wp_nonce_field( 'tami_save_installment_settings' ); ?>


    <div class="tami-m-0">
        <div class="tami-card tami-rounded-4 tami-bg-white tami-m-3 tami-overflow-hidden">

            <div class="tami-row">
                <div class="tami-col-12 tami-col-lg-6 tami-px-5 tami-py-3">
                    <div class="tami-checkbox-custom tami-mt-2">
                        <input id="tami-installment-switch"
                               value="1"
                               type="checkbox"
                               name="installments_enabled"
                               class="tami-custom-switch"<?php echo ( $saved_settings['installments_enabled'] == '1' ) ? ' checked' : ''; ?>
                        />
                        <label for="tami-installment-switch"
                               class="tami-lead tami-text-tami"><?php esc_attr_e( 'Enable Installments?', 'tami-payment' ) ?></label>
                    </div>
                </div>

                <div class="tami-col-12 tami-col-lg-6 tami-px-5 tami-py-3" id="minimum-installment-col"
                    style="<?php echo (esc_attr( $saved_settings['installments_enabled'] ) == '1') ? '': 'display:none'; ?>"
                >
                    <div class="tami-checkbox-custom tami-mt-2">
                        <div class="tami-row">
                            <div class="tami-col-12 tami-col-lg-6">
                                <input id="form-InstallmentSettings-Minimum"
                                       value="1"
                                       type="checkbox"
                                       name="installments_minimum_amount"
									<?php $minAmount = (float) ( esc_attr( $saved_settings['minimum_installment_amount'] ) ?? '-1' );
									echo $minAmount != - 1 ? 'checked' : ''; ?>
                                       class="tami-check-input-toggle tami-custom-switch"
                                />
                                <label
                                        data-tamibs-toggle="tooltip"
                                        data-tamibs-placement="top"
                                        title="<?php echo esc_attr__( 'You can set minimum amount for installments. If you don\'t want to limit it, you can leave it blank.', 'tami-payment' ); ?>"
                                        for="form-InstallmentSettings-Minimum"
                                        class="tami-lead tami-text-tami tami-w-75">
									<?php echo esc_attr__( 'Minimum Installment Amount', 'tami-payment' ); ?>
                                    <i class="fa fa-info-circle"></i>
                                </label>
                            </div>
                            <div class="tami-col-12 tami-col-lg-4" id="minimum-installment-column" style="<?php
							echo ( esc_attr( $saved_settings['minimum_installment_amount'] ) ?? '-1' ) != '-1' ? '' : 'display: none;';
							?>">
                                <div class="tami-d-flex" style="margin-top: -10px;">
                                    <input
                                            class="tami-form-control tami-input-only-decimal"
                                            type="tel"
                                            value="<?php echo esc_attr( $saved_settings['minimum_installment_amount'] ); ?>"
                                            id="form-InstallmentSettings-MinimumAmount"
                                            name="minimum_installment_amount"
                                            style="height: 32px;margin-top: 2px;"
                                    />
                                    <span style="height: 30px; margin-top: 2px;"
                                          class="tami-p-2 tami-bg-tami tami-text-white tami-ms-1 tami-input-group-text"><?php
										echo esc_attr( get_woocommerce_currency_symbol() );
										?></span>
                                </div>
                            </div>
                            <div class="tami-col-2"><span>&nbsp;</span></div>
                        </div>
                    </div>
                </div>

            </div>

            <hr class="tami-mt-0 tami-mb-0">


            <div class="tami-card-body">
                <div id="tami-installment-container"
					<?php echo ( $saved_settings['installments_enabled'] != '1' ) ? 'style="visibility: hidden; height: 0"' : ''; ?>
                     class="tami-row tami-overflow-hidden">


                    <div class="tami-col-6">

                        <table
                                id="tami-installment-table"
                                class="tami-table tami-table-striped tami-table-borderless tami-shadow-sm tami-mb-3">
                            <tbody>
                            <tr>
                                <th colspan="5" class="tami-align-middle tami-bg-tami tami-text-center">

                                    <label
                                            style="padding: 0;white-space: nowrap"
                                            class="tami-text-white tami-lead tami-w-100"
                                            for="form-InstallmentSettings-Table">
										<?php echo esc_attr__( 'Installment Rates', 'tami-payment' ); ?>
                                        <span
                                                data-tamibs-toggle="tooltip"
                                                data-tamibs-placement="right"
                                                title="<?php echo esc_attr__( 'Enter the rate to be applied for installment. Example: 4.74.', 'tami-payment' ); ?>"
                                                class="tami-text-white tami-fw-normal tami-text-end tami-w-100"
                                                style=" line-height: 60px;"
                                                for="form-InstallmentSettings-Info">
                                            <i class="tami-text-white fa fa-info-circle"></i>
                                        </span>
                                    </label>
                                </th>
                            </tr>
							<?php for ( $i = 2; $i <= 6; $i ++ ) { ?>
                                <tr>
                                    <td width="15%">&nbsp;</td>
                                    <th width="25%" align="right" class="tami-align-middle">
                                        <label
                                                class="tami-text-tami tami-fw-normal tami-text-end tami-w-100"
                                                style=" line-height: 60px;"
                                                for="form-InstallmentSettings-<?php echo esc_attr( $i ); ?>">
											<?php
											/* translators: %d is replaced with installment number */
											echo sprintf( esc_attr__( '%d Installment', 'tami-payment' ), esc_attr( $i ) ); ?>
                                        </label>
                                    </th>
                                    <th width="15%" align="center"
                                        class="tami-text-center user-select-none tami-align-middle">
                                        <input type="checkbox" name="checkbox"
											<?php
											echo ( $saved_settings[ 'installment_' . $i ] ?? '-1' ) != '-1' ? 'checked' : '';
											?>
                                               class="tami-form-check-input tami-justify-content-center tami-check-input-toggle"/>
                                    </th>
                                    <td class="tami-align-middle">
                                        <div class="tami-d-flex tami-check-input-toggleable tami-align-items-center">
                                            <i class="fa fa-percent tami-me-2 tami-text-tami"></i>
                                            <input
                                                    class="tami-input-only-decimal tami-form-control  tami-input-only-decimal"
                                                    type="tel"
                                                    value="<?php echo esc_attr( $saved_settings[ 'installment_' . esc_attr( $i ) ] ) ?? '-1' ?>"
                                                    id="form-InstallmentSettings-<?php echo esc_attr( $i ); ?>"
                                                    name="installment_<?php echo esc_attr( $i ); ?>"
                                            />
                                        </div>

                                    </td>
                                    <td width="15%">&nbsp;</td>
                                </tr>
							<?php } ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="tami-col-6">

                        <table
                                id="tami-installment-table"
                                class="tami-table tami-table-striped tami-table-borderless tami-shadow-sm tami-mb-3">
                            <tbody>
							<?php for ( $i = 7; $i <= 12; $i ++ ) { ?>
                                <tr>
                                    <td width="15%">&nbsp;</td>
                                    <td width="25%">
                                        <label
                                                class="tami-text-tami tami-fw-normal tami-text-end tami-w-100"
                                                style=" line-height: 60px;"
                                                for="form-InstallmentSettings-<?php echo esc_attr( $i ); ?>">
											<?php
											/* translators: %d is replaced with installment number */
											echo sprintf( esc_attr__( '%d Installment', 'tami-payment' ), esc_attr( $i ) ); ?>
                                        </label>
                                    </td>
                                    <th width="15%" align="center"
                                        class="tami-text-center user-select-none tami-align-middle">
                                        <input type="checkbox" name="checkbox"
											<?php
											echo ( esc_attr( $saved_settings[ 'installment_' . esc_attr( $i ) ] ) ?? '-1' ) != '-1' ? 'checked' : '';
											?>
                                               class="tami-form-check-input tami-justify-content-center tami-check-input-toggle"/>
                                    </th>
                                    <td class="tami-align-middle">
                                        <div class="tami-d-flex tami-check-input-toggleable tami-align-items-center">
                                            <i class="fa fa-percent tami-me-2 tami-text-tami"></i>
                                            <input
                                                    class="tami-input-only-decimal tami-form-control  tami-input-only-decimal"
                                                    type="tel"
                                                    value="<?php echo esc_attr( $saved_settings[ 'installment_' . esc_attr( $i ) ] ) ?? '-1' ?>"
                                                    id="form-InstallmentSettings-<?php echo esc_attr( $i ); ?>"
                                                    name="installment_<?php echo esc_attr( $i ); ?>"
                                            />
                                        </div>

                                    </td>
                                    <td width="15%">&nbsp;</td>
                                </tr>
							<?php } ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div id="tami-installment-container-closed"
                     class="tami-overflow-hidden"
					<?php echo ( esc_attr( $saved_settings['installments_enabled'] ) == '1' ) ? 'style="visibility: hidden; height: 0"' : ''; ?>
                >
                    <div class="tami-p-3">
                        <p class="tami-lead tami-text-muted">
							<?php esc_attr_e( 'Installments not activated.', 'tami-payment' ) ?>
                        </p>
                    </div>
                </div>


                <div class="tami-col-12 tami-col-lg-12 tami-px-0 tami-py-3 tami-text-end">
                    <button type="submit" form="tami-installment-settings-form"
                            class="tami-btn tami-btn-primary">
                        <span style="display: none"
                              class="tami--button-loader tami-spinner-border tami-spinner-border-sm"></span>
						<?php esc_attr_e( 'Save Changes', 'tami-payment' ) ?>
                    </button>
                </div>
            </div>
        </div>


    </div>
</form>
<div class="tami-text-end">

</div>
