<div class="seotoaster f-container">
    <?php echo $this->partial(
        'admin' . DIRECTORY_SEPARATOR . '_header.phtml',
        array(
            'headerText' => $this->translate('Quickbooks Settings'),
        )
    ); ?>

    <form id="quickbooks-config" class="pluginform f-wrapper" name="quickbooksConfig" method="POST" action="">
        <div class="f-content">
            <p class="grid_12">
                <a class="generate-token btn fl-right grid_7 success" href="javascript:;"><?php echo $this->translate('Generate token'); ?></a>
            </p>
            <p class="grid_12 mt5px mb0px">
                <?php echo $this->translator->translate('Your server administrator must schedule the associated cron script to refresh QBO token'); ?>
                <a href="javascript:;" data-element-id="<?php echo $this->cronScriptPath; ?>" class="copy-path-link-information-info d-visible"><?php echo $this->translate('Copy file path'); ?></a>
            </p>
            <p class="text-center text-bold pt10px grid_6">
                <label class="grid_12 pointer"><?php echo $this->translator->translate('Use Development Mode'); ?>: <input type="checkbox" name="devMode" <?php if(isset( $this->settings['devMode']) && $this->settings['devMode'] == 1) echo "checked"; ?> /></label>
            </p>
            <p class="text-center text-bold pt10px grid_6">
                <label class="grid_12 pointer"><?php echo $this->translator->translate('Disable payment processing'); ?>: <i class="ticon-info tooltip icon18" title="<?php echo $this->translator->translate('Disable creating Payments in QBO') ?>"></i>
                    <input type="checkbox" name="disablePaymentProcessing" <?php if(isset( $this->settings['disablePaymentProcessing']) && $this->settings['disablePaymentProcessing'] == 1) echo "checked"; ?> /></label>
            </p>
            <p class="text-center text-bold pt10px grid_6">
                <label class="grid_12 pointer"><?php echo $this->translator->translate('Allow invoice creation for cart'); ?>: <i class="ticon-info tooltip icon18" title="<?php echo $this->translator->translate('Allow invoice creation for cart in QBO') ?>"></i>
                    <input type="checkbox" name="createInvoiceForCart" <?php if(isset( $this->settings['createInvoiceForCart']) && $this->settings['createInvoiceForCart'] == 1) echo "checked"; ?> /></label>
            </p>
            <p class="text-center text-bold pt10px grid_6">
                <label class="grid_12 pointer"><?php echo $this->translator->translate('Allow invoice creation for quote'); ?>: <i class="ticon-info tooltip icon18" title="<?php echo $this->translator->translate('Allow invoice creation for quote in QBO') ?>"></i>
                    <input type="checkbox" name="createInvoiceForQuote" <?php if(isset( $this->settings['createInvoiceForQuote']) && $this->settings['createInvoiceForQuote'] == 1) echo "checked"; ?> /></label>
            </p>
            <fieldset class="background grid_12 mt15px">
                <p>
                    <label><?php echo $this->translate('Tax Exemption Reason'); ?>: <i class="ticon-info tooltip icon18" title="<?php echo $this->translator->translate('Tax Exemption Reason should be specified incase customer is marked as not taxable') ?>"></i> </label>
                    <select name="taxExemptionReasonId" id="tax-exemption-reason">
                        <option value="0"><?php echo $this->translator->translate('Select reason');?></option>
                        <option value="1" <?php if(isset($this->settings['taxExemptionReasonId']) && $this->settings['taxExemptionReasonId']=='1') echo 'selected="true"';?> ><?php echo $this->translator->translate('Federal government');?></option>
                        <option value="2" <?php if(isset($this->settings['taxExemptionReasonId']) && $this->settings['taxExemptionReasonId']=='2') echo 'selected="true"';?> ><?php echo $this->translator->translate('State government');?></option>
                        <option value="3" <?php if(isset($this->settings['taxExemptionReasonId']) && $this->settings['taxExemptionReasonId']=='3') echo 'selected="true"';?> ><?php echo $this->translator->translate('Local government');?></option>
                        <option value="4" <?php if(isset($this->settings['taxExemptionReasonId']) && $this->settings['taxExemptionReasonId']=='4') echo 'selected="true"';?> ><?php echo $this->translator->translate('Tribal government');?></option>
                        <option value="5" <?php if(isset($this->settings['taxExemptionReasonId']) && $this->settings['taxExemptionReasonId']=='5') echo 'selected="true"';?> ><?php echo $this->translator->translate('Charitable organization');?></option>
                        <option value="6" <?php if(isset($this->settings['taxExemptionReasonId']) && $this->settings['taxExemptionReasonId']=='6') echo 'selected="true"';?> ><?php echo $this->translator->translate('Religious organization');?></option>
                        <option value="7" <?php if(isset($this->settings['taxExemptionReasonId']) && $this->settings['taxExemptionReasonId']=='7') echo 'selected="true"';?> ><?php echo $this->translator->translate('Educational organization');?></option>
                        <option value="8" <?php if(isset($this->settings['taxExemptionReasonId']) && $this->settings['taxExemptionReasonId']=='8') echo 'selected="true"';?> ><?php echo $this->translator->translate('Hospital');?></option>
                        <option value="9" <?php if(isset($this->settings['taxExemptionReasonId']) && $this->settings['taxExemptionReasonId']=='9') echo 'selected="true"';?> ><?php echo $this->translator->translate('Resale');?></option>
                        <option value="10" <?php if(isset($this->settings['taxExemptionReasonId']) && $this->settings['taxExemptionReasonId']=='10') echo 'selected="true"';?> ><?php echo $this->translator->translate('Direct pay permit');?></option>
                        <option value="11" <?php if(isset($this->settings['taxExemptionReasonId']) && $this->settings['taxExemptionReasonId']=='11') echo 'selected="true"';?> ><?php echo $this->translator->translate('Multiple points of use');?></option>
                        <option value="12" <?php if(isset($this->settings['taxExemptionReasonId']) && $this->settings['taxExemptionReasonId']=='12') echo 'selected="true"';?> ><?php echo $this->translator->translate('Direct mail');?></option>
                        <option value="13" <?php if(isset($this->settings['taxExemptionReasonId']) && $this->settings['taxExemptionReasonId']=='13') echo 'selected="true"';?> ><?php echo $this->translator->translate('Agricultural production');?></option>
                        <option value="14" <?php if(isset($this->settings['taxExemptionReasonId']) && $this->settings['taxExemptionReasonId']=='14') echo 'selected="true"';?> ><?php echo $this->translator->translate('Industrial production / manufacturing');?></option>
                        <option value="15" <?php if(isset($this->settings['taxExemptionReasonId']) && $this->settings['taxExemptionReasonId']=='15') echo 'selected="true"';?> ><?php echo $this->translator->translate('Foreign diplomat');?></option>
                    </select>
                </p>
                <p>
                    <label><?php echo $this->translate('Notification Email'); ?>: <i class="ticon-info tooltip icon18" title="<?php echo $this->translator->translate('Enter email(s) if you wish to get notification if your token will be expired soon. Ex: John@mail.com,Doe@mail.com') ?>"></i> </label>
                    <input type="text" name="notificationEmail"
                           value="<?php echo !empty($this->settings['notificationEmail']) ? $this->settings['notificationEmail'] : ''; ?>"/>
                </p>
                <p>
                    <label><?php echo $this->translate('Client ID'); ?>: </label>
                    <input type="text" name="clientId"
                           value="<?php echo !empty($this->settings['clientId']) ? $this->settings['clientId'] : ''; ?>"/>
                </p>
                <p>
                    <label><?php echo $this->translate('Client Secret'); ?>: </label>
                    <input type="text" name="clientSecret"
                           value="<?php echo !empty($this->settings['clientSecret']) ? $this->settings['clientSecret'] : ''; ?>"/>
                </p>

                <input type="hidden" name="secureToken" id="secureToken" value="<?php echo $this->secureToken; ?>"/>
            </fieldset>
        </div>
        <div class="f-footer">
            <div class="grid_12">
                <button class="btn saveButton ticon-save mr-grid" type="submit" name="saveQuickbooksSettings"><?php echo $this->translator->translate('Save'); ?></button>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    $(document).ready(function() {
        <?php if(!empty($this->showGenerateToken)) :?>
            var url = '<?php echo $this->authorizationCodeURL?>';


            var OAuthCode = function(url) {
                this.loginPopup = function () {
                    this.loginPopupUri();
                }

                this.loginPopupUri = function () {
                    var parameters = "location=1,width=800,height=650";
                    parameters += ",left=" + (screen.width - 800) / 2 + ",top=" + (screen.height - 650) / 2;

                    var win = window.open(url, 'connectPopup', parameters);
                    var pollOAuth = window.setInterval(function () {
                        try {
                            if (win.document.URL.indexOf("code") != -1) {
                                window.clearInterval(pollOAuth);
                                win.close();
                                showMessage('<?php echo $this->translate('Generation complete');?>', false, 5000);
                            }
                        } catch (e) {
                            console.log(e)
                        }
                    }, 100);
                }
            }

            var oauth = new OAuthCode(url);

        <?php endif;?>

        $(document).on('click', '.generate-token', function(e){
            e.preventDefault();
            if('<?php echo $this->settings['clientId'];?>' == '' || '<?php echo $this->settings['clientSecret'];?>' == '') {
                showMessage('<?php echo $this->translate('At first please provide \"Client ID\" and \"Client Secret\" and save config.');?>', true, 2000);
            } else {
                oauth.loginPopup();
            }
        });

        $(document).on('click', '.copy-path-link-information-info', function(e){
            e.preventDefault();
            if (document.queryCommandSupported("copy")) {
                var copyFromEl = $('<textarea/>');
                copyFromEl.text($(e.currentTarget).data('element-id'));
                $('body').append(copyFromEl);
                copyFromEl.select();
                document.execCommand('copy', true);
                copyFromEl.remove();
                showMessage('Has been copied to clipboard.', false, 2000);
            }
        });

    });

    $(document).on('click', '.saveButton', function () {
        showSpinner();

        var devMode = 0;
        if ($("input[name=devMode]").prop('checked')) {
            devMode = 1;
        }

        var disablePaymentProcessing = 0;
        if ($("input[name=disablePaymentProcessing]").prop('checked')) {
            disablePaymentProcessing = 1;
        }

        var createInvoiceForCart = 0;
        if ($("input[name=createInvoiceForCart]").prop('checked')) {
            createInvoiceForCart = 1;
        }

        var createInvoiceForQuote = 0;
        if ($("input[name=createInvoiceForQuote]").prop('checked')) {
            createInvoiceForQuote = 1;
        }

        $.ajax({
            type: "POST",
            url: '<?php echo $this->websiteUrl; ?>plugin/quickbooks/run/config/',
            dataType: "json",
            data: {
                taxExemptionReasonId:     $("[name=taxExemptionReasonId]").val(),
                notificationEmail:        $("[name=notificationEmail]").val(),
                clientId:                 $("[name=clientId]").val(),
                clientSecret:             $("[name=clientSecret]").val(),
                devMode:                  devMode,
                disablePaymentProcessing: disablePaymentProcessing,
                createInvoiceForCart:     createInvoiceForCart,
                createInvoiceForQuote:    createInvoiceForQuote,
                secureToken:              $('#secureToken').val()
            }
        }).done(function (response) {
            hideSpinner();
            if (response.error == 1) {
                showMessage(response.responseText, true, 5000);
            } else {
                showMessage('<?php echo $this->translate('Saved');?>', false, 2000);
                if(response.responseText.showGenerateToken == 1) {
                    window.location.reload();
                }
            }
        }).fail(function () {
            hideSpinner();
        });
        return false;
    });
</script>
