<?php

use IGD\Account;

defined( 'ABSPATH' ) || exit;

/**
 * Dokan Google Drive Settings  Template
 *
 * @since   1.1.86
 *
 * @package integrate-google-drive
 */

do_action( 'igd_dokan_settings_before_form', $current_user, $profile_info );

$is_dokan_upload = igd_get_settings( 'dokanUpload' );

$dokan_media_library = igd_get_settings( 'dokanMediaLibrary' );


?>

<div class="dokan-igd-settings">
    <h2 id="vendor-dashboard-google-drive-settings-error"></h2>

    <div class="igd-dokan-settings-header">
        <h2><?php esc_html_e( 'Google Drive Accounts', 'integrate-google-drive-premium' ); ?></h2>

        <button class="igd-btn add-account-btn"
                onclick="window.open(igd.authUrl, 'newwindow', 'width=550,height=600');"
        >
            <img src="<?php echo IGD_ASSETS; ?>/images/google-icon.png"/>
            <span><?php _e( 'Add new account', 'integrate-google-drive-premium' ); ?></span>
        </button>

        <a href="#" class="igd-btn btn-primary">
            <i class="dashicons dashicons-editor-help"></i>
            <span><?php echo __( "Documentation", 'integrate-google-drive-premium' ); ?></span>
        </a>

    </div>

    <?php

    $accounts = Account::instance( dokan_get_current_user_id() )->get_accounts();

    if ( count( $accounts ) > 0 ) {
        foreach ( $accounts as $key => $account ) {
            $id    = $account['id'];
            $name  = $account['name'];
            $photo = $account['photo'];
            $email = $account['email'];
            $lost  = $account['lost'];
            ?>
            <div class="igd-account-item"
                 data-id="<?php echo $id; ?>">
                <img class="igd-account-item-avatar" src="<?php echo $photo; ?>"/>

                <div class="igd-account-item-info">
                    <span class="account-name"><?php echo $name; ?></span>
                    <span class="account-email"><?php echo $email; ?></span>
                </div>

                <div class="igd-account-item-action">
                    <?php if ( $lost ) { ?>
                        <button class="igd-btn btn-primary"
                                onclick="window.open(igd.authUrl, 'newwindow', 'width=550,height=600');">
                            <i class="dashicons dashicons-update"></i>
                            <span><?php echo __( "Refresh", 'integrate-google-drive-premium' ); ?></span>
                        </button>
                    <?php } ?>

                    <button class="igd-btn btn-danger remove-account"
                    >
                        <i class="dashicons dashicons-trash"></i>
                        <span><?php echo __( 'Remove', 'integrate-google-drive-premium' ); ?></span>
                    </button>
                </div>
            </div>
            <?php
        }
    } else { ?>

        <div class="no-account-placeholder">
            <img src="<?php echo IGD_ASSETS; ?>/images/file-browser/no-account-placeholder.svg" alt="No Accounts"/>
            <span class="placeholder-heading"><?php echo __( "You didn't link any account yet.", 'integrate-google-drive-premium' ); ?></span>
            <span class="placeholder-description"><?php echo __( "Please link to a Google Drive account to continue.", 'integrate-google-drive-premium' ); ?></span>

            <button
                    class="igd-btn add-account-btn"
                    onclick="window.open(igd.authUrl, 'newwindow', 'width=550,height=600');"
            >
                <img src="<?php echo IGD_ASSETS; ?>/images/google-icon.png"/>
                <span><?php echo __( "Sign in with Google", 'integrate-google-drive-premium' ); ?></span>
            </button>
        </div>

    <?php } ?>

    <!-- Media Library Settings -->
    <?php if ( $dokan_media_library ) {
        $is_vendor_media_library = get_user_meta( dokan_get_current_user_id(), '_igd_dokan_vendor_media_library', true ) != 'no';

        $media_folders = get_user_meta( dokan_get_current_user_id(), '_igd_dokan_media_folders', true );

        ?>

        <div class="igd-dokan-settings-header igd-media-library-settings-header"
             id="igd-dokan-media-library-settings-header">
            <h2><?php esc_html_e( 'Media Library Settings', 'integrate-google-drive-premium' ); ?></h2>

            <button class="igd-btn btn-primary save-media-library-settings">
                <i class="fas fa-save"></i>
                <span><?php esc_html_e( 'Save Settings', 'integrate-google-drive-premium' ); ?></span>
            </button>
        </div>

        <div class="dokan-ajax-response dokan-igd-ajax-response dokan-igd-media-library-ajax-response">
            <div class="dokan-alert dokan-alert-success">
                <p><?php esc_html_e( 'Settings saved successfully', 'integrate-google-drive-premium' ); ?></p>
            </div>
        </div>

        <form class="dokan-form-horizontal" method="post" id="igd-dokan-media-library-settings">

            <!-- Enable Media Library-->
            <div class="dokan-form-group">
                <label class="dokan-w3 dokan-control-label dokan-text-left" for="igd_media_library">
                    <?php esc_html_e( 'Media Library', 'integrate-google-drive-premium' ); ?>
                </label>

                <div class="dokan-w9 dokan-text-left">
                    <label>
                        <input type="checkbox" name="igd_dokan_vendor_media_library"
                               value="1" <?php checked( $is_vendor_media_library ); ?> />
                        <?php esc_html_e( 'Enable Google Drive as Media Library', 'integrate-google-drive-premium' ); ?>
                    </label>

                    <p class="description">
                        <?php esc_html_e( 'Connect your Google Drive to WordPress media library, So you can use Google Drive files like images and downloads directly in your products.', 'integrate-google-drive-premium' ); ?>
                    </p>
                </div>
            </div>

            <!-- Media Library Folders-->
            <div class="dokan-form-group">
                <label class="dokan-w3 dokan-control-label dokan-text-left" for="igd_media_library">
                    <?php esc_html_e( 'Assigned Folders', 'integrate-google-drive-premium' ); ?>
                </label>

                <div class="dokan-w9 dokan-text-left">
                    <input type="hidden" name="igd_dokan_media_folders" id="igd_dokan_media_folders"
                           value="<?php echo esc_attr( json_encode( $media_folders ) ); ?>"/>

                    <div class="media-folders-wrap">
                        <div class="media-folders"><?php

                            if ( ! empty( $media_folders ) ) {
                                foreach ( $media_folders as $folder ) {
                                    ?>
                                    <div class="folder-item">
                                        <img src="<?php echo IGD_ASSETS; ?>/images/icons/folder.png" alt="Folder Icon"/>
                                        <?php echo esc_html( $folder['name'] ); ?>
                                    </div>
                                    <?php
                                }
                            }

                            ?></div>

                        <button class="igd-btn btn-info" type="button" id="igd-dokan-select-media-folders">
                            <i class="dashicons dashicons-open-folder"></i><span>
                                <?php echo ! empty( $media_folders ) ? esc_html__( 'Update Folders', 'integrate-google-drive-premium' ) : esc_html__( 'Select Folders', 'integrate-google-drive-premium' ); ?>
                            </span>
                        </button>

                    </div>

                    <p class="description">
                        <?php esc_html_e( 'Select the Google Drive folders that you want to use in the Media Library.', 'integrate-google-drive-premium' ); ?>
                    </p>
                </div>

            </div>
        </form>
    <?php } ?>


    <!-- Upload Settings -->
    <?php if ( $is_dokan_upload ) { ?>
        <div class="igd-dokan-settings-header igd-upload-settings-header">
            <h2> <?php esc_html_e( 'Upload Settings', 'integrate-google-drive-premium' ); ?></h2>

            <button class="igd-btn btn-primary save-upload-settings">
                <i class="fas fa-save"></i>
                <span><?php esc_html_e( 'Save Settings', 'integrate-google-drive-premium' ); ?></span>
            </button>
        </div>

        <div class="dokan-ajax-response dokan-igd-ajax-response dokan-igd-upload-ajax-response">
            <div class="dokan-alert dokan-alert-success">
                <p><?php esc_html_e( 'Settings saved successfully', 'integrate-google-drive-premium' ); ?></p>
            </div>
        </div>


        <form class="dokan-form-horizontal" method="post" id="igd-dokan-upload-settings">

            <!-- Upload Box Locations -->
            <div class="dokan-form-group">
                <label class="dokan-w3 dokan-control-label dokan-text-left" for="igd_upload_folder">
                    <?php esc_html_e( 'Upload Box Locations', 'integrate-google-drive-premium' ); ?>
                </label>

                <div class="dokan-w9 dokan-text-left">

                    <ul class="wc-radios">
                        <?php

                        $upload_location_options = array(
                                "product"        => __( "Product Page", 'integrate-google-drive-premium' ),
                                "cart"           => __( "Cart Page", 'integrate-google-drive-premium' ),
                                "checkout"       => __( "Checkout Page", 'integrate-google-drive-premium' ),
                                "order-received" => __( "Order Received Page", 'integrate-google-drive-premium' ),
                                "my-account"     => __( "My Account Page", 'integrate-google-drive-premium' ),
                        );

                        $upload_locations = get_user_meta( dokan_get_current_user_id(), '_igd_dokan_upload_locations', true );
                        $upload_locations = is_array( $upload_locations ) ? $upload_locations : array(
                                'checkout',
                                'order-received',
                                'my-account'
                        );

                        foreach ( $upload_location_options as $key => $label ) { ?>
                            <li>
                                <label>
                                    <input type="checkbox" name="upload_locations[]"
                                           value="<?php echo esc_attr( $key ); ?>" <?php checked( in_array( $key, $upload_locations ), true ); ?> />
                                    <?php echo esc_html( $label ); ?>
                                </label>
                            </li>
                            <?php
                        }

                        ?>
                    </ul>

                    <p class="description">
                        <?php esc_html_e( 'Select the pages where you want to show the upload box.', 'integrate-google-drive-premium' ); ?>
                    </p>

                </div>
            </div>

            <!-- Order Statues -->
            <div class="dokan-form-group">
                <label class="dokan-w3 dokan-control-label dokan-text-left">
                    <?php esc_html_e( 'Show Upload Box When Order Status is', 'integrate-google-drive-premium' ); ?> </label>

                <div class="dokan-w9 dokan-text-left">
                    <ul class="wc-radios">
                        <?php

                        //Order Status
                        $order_statuses = wc_get_order_statuses();

                        $order_status = get_user_meta( dokan_get_current_user_id(), '_igd_dokan_upload_order_statuses', true );
                        $order_status = $order_status ?: array(
                                'wc-pending',
                                'wc-processing',
                                'wc-on-hold',
                        );

                        foreach ( $order_statuses as $key => $value ) { ?>
                            <li>
                                <label>
                                    <input type="checkbox" name="upload_order_status[]"
                                           value="<?php echo esc_attr( $key ); ?>" <?php checked( in_array( $key, $order_status ), true ); ?> />
                                    <?php echo esc_html( $value ); ?>
                                </label>
                            </li>
                            <?php
                        }

                        ?>
                    </ul>

                    <p class="description">
                        <?php esc_html_e( 'Select the order status when the upload field will be shown to the customer.', 'integrate-google-drive-premium' ); ?>
                        <br/>
                        <?php
                        /* translators: %1$s: <strong> tag, %2$s: </strong> tag */
                        printf( esc_html__( 'This option will only work on the %1$s Order Received %2$s and %1$s My Account %2$s page.', 'integrate-google-drive-premium' ), '<strong>', '</strong>' ); ?>
                    </p>

                </div>
            </div>

        </form>
    <?php } ?>


</div>

<?php
/**
 * @since 1.1.86
 */
do_action( 'igd_dokan_settings_after_form', $current_user, $profile_info ); ?>
