@extends('layouts.app')
@section('title', __('lang_v1.omni_channel_services'))

@section('content')

<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>@lang('lang_v1.omni_channel_services')</h1>
    <br>
    {{-- @include('layouts.partials.search_settings') --}}
</section>

<!-- Main content -->
<section class="content">
{!! Form::open(['url' => action([\App\Http\Controllers\OmniChannelServicesController::class, 'postOmniSettings']), 'method' => 'post', 'id' => 'omni_edit_form',
    'files' => true ]) !!}
    <div class="row">
        <div class="col-xs-12">
       <!--  <pos-tab-container> -->
        <div class="col-xs-12 pos-tab-container">
            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pos-tab-menu">
                <div class="list-group">
                    @if ($e_commerce_default['big_commerce_enable'] == 1 || 
                            ($e_commerce_default['shopify_enable'] == 0 
                            && $e_commerce_default['woo_commerce_enable'] == 0 
                            && $e_commerce_default['magento_enable'] == 0)
                        )
                        <a href="#" class="list-group-item text-center active" onclick="getShowBtn('bigcommerce')">@lang('lang_v1.big_commerce')</a>
                    @else                            
                        <a href="#" class="list-group-item text-center" onclick="getShowBtn('bigcommerce')">@lang('lang_v1.big_commerce')</a>
                    @endif
                    <a href="#" class="list-group-item text-center {{$e_commerce_default['shopify_enable'] == 1 ? 'active' : '' }}" onclick="getShowBtn('shopify')">@lang('lang_v1.shopify')</a>
                    <a href="#" class="list-group-item text-center {{$e_commerce_default['woo_commerce_enable'] == 1 ? 'active' : '' }}" onclick="getShowBtn('woocommerce')">@lang('lang_v1.woo_commerce')</a>
                    {{-- <a href="#" class="list-group-item text-center {{$e_commerce_default['magento_enable'] == 1 ? 'active' : '' }}" onclick="getShowBtn('magento')">@lang('lang_v1.magento')</a> --}}
                </div>
            </div>
            <div class="col-lg-10 col-md-10 col-sm-10 col-xs-10 pos-tab">
                <!-- tab 1 start -->
                @include('business.partials.settings_big_commerce')
                <!-- tab 1 end -->
                <!-- tab 2 start -->
                {{-- @include('business.partials.settings_shopify') --}}
                @include('business.partials.settings_shopify_udb')
                <!-- tab 2 end -->
                <!-- tab 3 start -->
                @include('business.partials.settings_woo_commerce')
                <!-- tab 3 end -->
                <!-- tab 4 start -->
                {{-- @include('business.partials.settings_magento') --}}
                <!-- tab 4 end -->
            </div>
        </div>
        </div>
    </div>    
    <div class="row">
        <div class="col-sm-12">
            <button class="btn btn-danger pull-right" type="button" id="update_settings_btn">@lang('business.update_settings')</button>
            <button type="button" class="btn btn-primary btn-flat pull-right" id="shopify_test_btn">@lang('lang_v1.sync_shopify_data')</button>
            <button type="button" class="btn btn-primary btn-flat pull-right" id="bigcommerce_test_btn">@lang('lang_v1.sync_bigcommerce_data')</button>
            <button type="button" class="btn btn-primary btn-flat pull-right" id="woocommerce_test_btn">@lang('lang_v1.sync_woocommerce_data')</button>
        </div>
    </div>
    <div class="row" style="margin-top: 20px;">        
        <div class="col-sm-12 bigcommerce_table">
            <table class="table table-bordered table-striped hide-footer" id="bigcommerce_table" style="width: 100%;">
                <thead>
                    <tr>                    
                        <th><input type="checkbox" id="select-all-row" data-table-id="bigcommerce_table"></th>
                        <th>&nbsp;</th>                    
                        <th>@lang('messages.action')</th>
                        <th>@lang('sale.product')</th>
                        <th>@lang('business.business_location') @show_tooltip(__('lang_v1.product_business_location_tooltip'))</th>
                        @can('access_default_selling_price')                      
                            <th>@lang('lang_v1.selling_price')</th>
                        @endcan
                        <th>@lang('report.total_stock')</th>
                        <th>@lang('product.category')</th>
                        <th>@lang('product.brand')</th>
                        <th>@lang('product.sku')</th>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <td colspan="10">
                            <div style="display: flex; width: 100%;">
                                @can('product.delete')
                                    <form></form>
                                    {!! Form::open(['url' => action([\App\Http\Controllers\ProductController::class, 'massDestroy']), 'method' => 'post', 'id' => 'mass_delete_form_bigcommerce' ]); !!}
                                    {!! Form::hidden('selected_rows', null, ['id' => 'selected_rows_bigcommerce']); !!}
                                    {!! Form::submit(__('lang_v1.delete_selected'), array('class' => 'btn btn-xs btn-danger', 'id' => 'delete-selected-bigcommerce')); !!}
                                    {!! Form::close(); !!} @show_tooltip(__('lang_v1.deactive_product_tooltip'))
                                @endcan
                
                                
                                    @can('product.update')
                                    
                                        @if(config('constants.enable_product_bulk_edit'))
                                            &nbsp;
                                            {!! Form::open(['url' => action([\App\Http\Controllers\ProductController::class, 'bulkEdit']), 'method' => 'post', 'id' => 'bulk_edit_form' ]) !!}
                                            {!! Form::hidden('selected_products', null, ['id' => 'selected_products_for_edit']); !!}
                                            <button type="submit" class="btn btn-xs btn-primary" id="edit-selected"> <i class="fa fa-edit"></i>{{__('lang_v1.bulk_edit')}}</button>
                                            {!! Form::close() !!}
                                        @endif
                                        &nbsp;
                                        <button type="button" class="btn btn-xs btn-success update_product_location" data-type="add">@lang('lang_v1.add_to_location')</button>
                                        &nbsp;
                                        <button type="button" class="btn btn-xs bg-navy update_product_location" data-type="remove">@lang('lang_v1.remove_from_location')</button>
                                    @endcan
                                
                                &nbsp;
                                {!! Form::open(['url' => action([\App\Http\Controllers\ProductController::class, 'massDeactivate']), 'method' => 'post', 'id' => 'mass_deactivate_form' ]); !!}
                                {!! Form::hidden('selected_products', null, ['id' => 'selected_products']); !!}
                                {!! Form::submit(__('lang_v1.deactivate_selected'), array('class' => 'btn btn-xs btn-warning', 'id' => 'deactivate-selected')); !!}
                                {!! Form::close(); !!} @show_tooltip(__('lang_v1.deactive_product_tooltip'))                            
                            </div>
                        </td>
                    </tr>
                </tfoot>
            </table>            
        </div>    
        <div class="col-sm-12 shopify_table">
            <div class="nav-tabs-custom">
                <ul class="nav nav-tabs">
                    <li class="active">
                        <a href="#tab1" data-toggle="tab" aria-expanded="true"><i class="fa fa-cubes" aria-hidden="true"></i> USED BOOK DEPOT</a>
                    </li>                    
                    <li>
                        <a href="#tab2" data-toggle="tab" aria-expanded="true"><i class="fa fas fa-arrow-circle-up" aria-hidden="true"></i> CHEAP BOOK DEPOT</a>
                    </li>                                                 
                </ul>
            </div>
            <div class="tab-content">
                <div class="tab-pane active" id="tab1">   
                    <table class="table table-bordered table-striped ajax_view hide-footer jode" id="shopify_table" style="width: 100%;">
                        <thead>
                            <tr>                
                                <th><input type="checkbox" id="select-all-row" data-table-id="shopify_table"></th>    
                                <th>&nbsp;</th>                    
                                <th>@lang('messages.action')</th>
                                <th>@lang('sale.product')</th>
                                <th>@lang('business.business_location') @show_tooltip(__('lang_v1.product_business_location_tooltip'))</th>
                                @can('access_default_selling_price')                      
                                    <th>@lang('lang_v1.selling_price')</th>
                                @endcan
                                <th>@lang('report.total_stock')</th>
                                <th>@lang('product.category')</th>
                                <th>@lang('product.brand')</th>
                                <th>@lang('product.sku')</th>
                            </tr>
                        </thead>
                        <tfoot>
                            <tr>
                                <td colspan="10">
                                    <div style="display: flex; width: 100%;">
                                        @can('product.delete')
                                            {!! Form::open(['url' => action([\App\Http\Controllers\ProductController::class, 'massDestroy']), 'method' => 'post', 'id' => 'mass_delete_form_shopify' ]) !!}                                    
                                                {!! Form::hidden('selected_rows', null, ['id' => 'selected_rows_shopify']); !!}
                                                {!! Form::submit(__('lang_v1.delete_selected'), array('class' => 'btn btn-xs btn-danger', 'id' => 'delete-selected-shopify')) !!}                                                                            
                                        @endcan
                                        &nbsp;                                
                                        @can('product.update')
                                        
                                            @if(config('constants.enable_product_bulk_edit'))
                                                &nbsp;
                                                {!! Form::open(['url' => action([\App\Http\Controllers\ProductController::class, 'bulkEdit']), 'method' => 'post', 'id' => 'bulk_edit_form' ]) !!}
                                                {!! Form::hidden('selected_products', null, ['id' => 'selected_products_for_edit']); !!}
                                                <button type="submit" class="btn btn-xs btn-primary" id="edit-selected"> <i class="fa fa-edit"></i>{{__('lang_v1.bulk_edit')}}</button>
                                                {!! Form::close() !!}
                                            @endif
                                            &nbsp;
                                            <button type="button" class="btn btn-xs btn-success update_product_location" data-type="add">@lang('lang_v1.add_to_location')</button>
                                            &nbsp;
                                            <button type="button" class="btn btn-xs bg-navy update_product_location" data-type="remove">@lang('lang_v1.remove_from_location')</button>
                                        @endcan
                                        
                                        &nbsp;
                                        {!! Form::open(['url' => action([\App\Http\Controllers\ProductController::class, 'massDeactivate']), 'method' => 'post', 'id' => 'mass_deactivate_form' ]) !!}
                                        {!! Form::hidden('selected_products', null, ['id' => 'selected_products']); !!}
                                        {!! Form::submit(__('lang_v1.deactivate_selected'), array('class' => 'btn btn-xs btn-warning', 'id' => 'deactivate-selected')) !!}
                                        {!! Form::close() !!} @show_tooltip(__('lang_v1.deactive_product_tooltip'))                            
                                    </div>
                                </td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
                <div class="tab-pane" id="tab2">   
                    <table class="table table-bordered table-striped ajax_view hide-footer jode" id="shopify_table2" style="width: 100%;">
                        <thead>
                            <tr>                
                                <th><input type="checkbox" id="select-all-row" data-table-id="shopify_table2"></th>    
                                <th>&nbsp;</th>                    
                                <th>@lang('messages.action')</th>
                                <th>@lang('sale.product')</th>
                                <th>@lang('business.business_location') @show_tooltip(__('lang_v1.product_business_location_tooltip'))</th>
                                @can('access_default_selling_price')                      
                                    <th>@lang('lang_v1.selling_price')</th>
                                @endcan
                                <th>@lang('report.total_stock')</th>
                                <th>@lang('product.category')</th>
                                <th>@lang('product.brand')</th>
                                <th>@lang('product.sku')</th>
                            </tr>
                        </thead>
                        <tfoot>
                            <tr>
                                <td colspan="10">
                                    <div style="display: flex; width: 100%;">
                                        @can('product.delete')
                                            {!! Form::open(['url' => action([\App\Http\Controllers\ProductController::class, 'massDestroy']), 'method' => 'post', 'id' => 'mass_delete_form_shopify' ]) !!}                                    
                                                {!! Form::hidden('selected_rows', null, ['id' => 'selected_rows_shopify']); !!}
                                                {!! Form::submit(__('lang_v1.delete_selected'), array('class' => 'btn btn-xs btn-danger', 'id' => 'delete-selected-shopify')) !!}                                                                            
                                        @endcan
                                        &nbsp;                                
                                        @can('product.update')
                                        
                                            @if(config('constants.enable_product_bulk_edit'))
                                                &nbsp;
                                                {!! Form::open(['url' => action([\App\Http\Controllers\ProductController::class, 'bulkEdit']), 'method' => 'post', 'id' => 'bulk_edit_form' ]) !!}
                                                {!! Form::hidden('selected_products', null, ['id' => 'selected_products_for_edit']); !!}
                                                <button type="submit" class="btn btn-xs btn-primary" id="edit-selected"> <i class="fa fa-edit"></i>{{__('lang_v1.bulk_edit')}}</button>
                                                {!! Form::close() !!}
                                            @endif
                                            &nbsp;
                                            <button type="button" class="btn btn-xs btn-success update_product_location" data-type="add">@lang('lang_v1.add_to_location')</button>
                                            &nbsp;
                                            <button type="button" class="btn btn-xs bg-navy update_product_location" data-type="remove">@lang('lang_v1.remove_from_location')</button>
                                        @endcan
                                        
                                        &nbsp;
                                        {!! Form::open(['url' => action([\App\Http\Controllers\ProductController::class, 'massDeactivate']), 'method' => 'post', 'id' => 'mass_deactivate_form' ]) !!}
                                        {!! Form::hidden('selected_products', null, ['id' => 'selected_products']); !!}
                                        {!! Form::submit(__('lang_v1.deactivate_selected'), array('class' => 'btn btn-xs btn-warning', 'id' => 'deactivate-selected')) !!}
                                        {!! Form::close() !!} @show_tooltip(__('lang_v1.deactive_product_tooltip'))                            
                                    </div>
                                </td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>    
        <div class="col-sm-12 woocommerce_table">
            <table class="table table-bordered table-striped ajax_view hide-footer" id="woocommerce_table" style="width: 100%;">
                <thead>
                    <tr>                    
                        <th><input type="checkbox" id="select-all-row" data-table-id="woocommerce_table"></th>
                        <th>&nbsp;</th>           
                        <th>@lang('messages.action')</th>         
                        <th>@lang('sale.product')</th>
                        <th>@lang('business.business_location') @show_tooltip(__('lang_v1.product_business_location_tooltip'))</th>
                        @can('access_default_selling_price')                      
                            <th>@lang('lang_v1.selling_price')</th>
                        @endcan
                        <th>@lang('report.total_stock')</th>
                        <th>@lang('product.category')</th>
                        <th>@lang('product.brand')</th>
                        <th>@lang('product.sku')</th>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <td colspan="10">
                            <div style="display: flex; width: 100%;">
                                @can('product.delete')
                                    {!! Form::open(['url' => action([\App\Http\Controllers\ProductController::class, 'massDestroy']), 'method' => 'post', 'id' => 'mass_delete_form_woocommerce' ]); !!}
                                    {!! Form::hidden('selected_rows', null, ['id' => 'selected_rows_woocommerce']); !!}
                                    {!! Form::submit(__('lang_v1.delete_selected'), array('class' => 'btn btn-xs btn-danger', 'id' => 'delete-selected-woocommerce')); !!}
                                    {!! Form::close(); !!} @show_tooltip(__('lang_v1.deactive_product_tooltip'))
                                @endcan
                
                                
                                    @can('product.update')
                                    
                                        @if(config('constants.enable_product_bulk_edit'))
                                            &nbsp;
                                            {!! Form::open(['url' => action([\App\Http\Controllers\ProductController::class, 'bulkEdit']), 'method' => 'post', 'id' => 'bulk_edit_form' ]) !!}
                                            {!! Form::hidden('selected_products', null, ['id' => 'selected_products_for_edit']); !!}
                                            <button type="submit" class="btn btn-xs btn-primary" id="edit-selected"> <i class="fa fa-edit"></i>{{__('lang_v1.bulk_edit')}}</button>
                                            {!! Form::close() !!}
                                        @endif
                                        &nbsp;
                                        <button type="button" class="btn btn-xs btn-success update_product_location" data-type="add">@lang('lang_v1.add_to_location')</button>
                                        &nbsp;
                                        <button type="button" class="btn btn-xs bg-navy update_product_location" data-type="remove">@lang('lang_v1.remove_from_location')</button>
                                    @endcan
                                
                                &nbsp;
                                {!! Form::open(['url' => action([\App\Http\Controllers\ProductController::class, 'massDeactivate']), 'method' => 'post', 'id' => 'mass_deactivate_form' ]) !!}
                                {!! Form::hidden('selected_products', null, ['id' => 'selected_products']); !!}
                                {!! Form::submit(__('lang_v1.deactivate_selected'), array('class' => 'btn btn-xs btn-warning', 'id' => 'deactivate-selected')) !!}
                                {!! Form::close() !!} @show_tooltip(__('lang_v1.deactive_product_tooltip'))                                
                            </div>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>        
    </div>
{!! Form::close() !!}
</section>
<!-- /.content -->
@stop
@section('javascript')
<script type="text/javascript">
    var defaultTab = '';
    var shopify_enable = 0;
    var wooenable = 0;
    var magento_enable = 0;     
    $(document).ready(function() {
        Pace.stop();
        $('.select2.select2-container').css('width', '100%');
        $('#main-footer').css('display', 'none')
        var bigenable = $("input[type='radio'][name='big_commerce_enable']:checked").val()        
        if(bigenable == '1') {
            // console.log(bigenable == '1')
            if(bigenable == '1') {
                // bigcommerce
                $('#big_commerce_api_url').css('display', 'block')
                $('#big_commerce_api_access_token').css('display', 'block')
                $('#big_commerce_api_key').css('display', 'block')
                $('#big_commerce_api_secret').css('display', 'block')
                $("#big_commerce_space").addClass('col-sm-3')
                $("#location_big_commerce").css('display', 'block')
                $('#shopify_test_btn').css('display', 'none')
                $('#woocommerce_test_btn').css('display', 'none')
                
                $('#shopify_api_url').css('display', 'none')
                $('#shopify_api_access_token').css('display', 'none')
                $('#shopify_api_key').css('display', 'none')
                $('#shopify_api_secret').css('display', 'none')
                // $('#shopify_space').removeClass('col-sm-3')   
                $('#location_shopify').css('display', 'none')         
                // $('#shopify_test_btn').css('display', 'none')
                // $('#bigcommerce_test_btn').css('display', 'none')
                
                $('#woo_commerce_api_url').css('display', 'none')
                $('#woo_commerce_api_access_token').css('display', 'none')
                $('#woo_commerce_api_key').css('display', 'none')
                $('#woo_commerce_api_secret').css('display', 'none')
                // $('#woo_commerce_space').removeClass('col-sm-3')
                // $('#shopify_test_btn').css('display', 'none')
                // $('#bigcommerce_test_btn').css('display', 'none')
                // $('#woo_commerce_enable').prop('checked', false);
                
                $('#magento_api_url').css('display', 'none')
                $('#magento_api_access_token').css('display', 'none')
                $('#magento_api_key').css('display', 'none')
                $('#magento_api_secret').css('display', 'none')
                // $('#magento_space').removeClass('col-sm-3')
                $('#shopify_test_btn').css('display', 'none')
                // $('#bigcommerce_test_btn').css('display', 'none')
                
                // get_input_data(bigenable, 'bigcommerce')
                $('#bigcommerce_test_btn').css('display', 'block')                
            } else {
                // big commerce
                $('#big_commerce_api_url').css('display', 'none')
                $('#big_commerce_api_access_token').css('display', 'none')
                $('#big_commerce_api_key').css('display', 'none')
                $('#big_commerce_api_secret').css('display', 'none')
                $("#big_commerce_space").removeClass('col-sm-3')
                // $("#location_big_commerce").css('display', 'none')
                $('#shopify_test_btn').css('display', 'none')
                $('#bigcommerce_test_btn').css('display', 'none')                             
                $('#woocommerce_test_btn').css('display', 'none')                             
            }

            // getShowBtn('bigcommerce')
            // $('#bigcommerce_enable').prop('checked', true);            
            $('#magento_space').addClass('col-sm-3')
            $('#woo_commerce_space').addClass('col-sm-3')
            $('.bigcommerce_table').removeClass('hide')
            $('.shopify_table').addClass('hide')
            $('.woocommerce_table').addClass('hide')

            defaultTab = 'bigcommerce';
        } else {
            shopifyenable = $("input[type='radio'][name='shopify_enable']:checked").val()
            if(shopifyenable == '1') {
                if(shopifyenable == '1') {
                    // shopify
                    $('#shopify_api_url').css('display', 'block')
                    $('#shopify_api_access_token').css('display', 'block')
                    $('#shopify_api_key').css('display', 'block')
                    $('#shopify_api_secret').css('display', 'block')
                    // $('#shopify_space').addClass('col-sm-3')            
                    $('#location_shopify').css('display', 'block')            
                    $('#shopify_test_btn').css('display', 'block')
                    $('#bigcommerce_test_btn').css('display', 'none')
                    $('#woocommerce_test_btn').css('display', 'none')

                    $('#big_commerce_api_url').css('display', 'none')
                    $('#big_commerce_api_access_token').css('display', 'none')
                    $('#big_commerce_api_key').css('display', 'none')
                    $('#big_commerce_api_secret').css('display', 'none')
                    // $('#big_commerce_space').removeClass('col-sm-3')   
                    $('#location_big_commerce').css('display', 'none')         
                    // $('#big_commerce_test_btn').css('display', 'none')
                    // $('#bigcommerce_test_btn').css('display', 'none')

                    $('#woo_commerce_api_url').css('display', 'none')
                    $('#woo_commerce_api_access_token').css('display', 'none')
                    $('#woo_commerce_api_key').css('display', 'none')
                    $('#woo_commerce_api_secret').css('display', 'none')
                    // $('#woo_commerce_space').removeClass('col-sm-3')
                    $('#shopify_test_btn').css('display', 'none')
                    // $('#bigcommerce_test_btn').css('display', 'none')
                    // $('#woo_commerce_enable').prop('checked', false);

                    $('#magento_api_url').css('display', 'none')
                    $('#magento_api_access_token').css('display', 'none')
                    $('#magento_api_key').css('display', 'none')
                    $('#magento_api_secret').css('display', 'none')
                    // $('#magento_space').removeClass('col-sm-3')
                    // $('#shopify_test_btn').css('display', 'none')
                    // $('#bigcommerce_test_btn').css('display', 'none')

                } else {
                    // shopify
                    $('#shopify_api_url').css('display', 'none')
                    $('#shopify_api_access_token').css('display', 'none')
                    $('#shopify_api_key').css('display', 'none')
                    $('#shopify_api_secret').css('display', 'none')
                    $('#shopify_space').removeClass('col-sm-3')   
                    $('#location_shopify').css('display', 'none')         
                    $('#shopify_test_btn').css('display', 'none')
                    $('#bigcommerce_test_btn').css('display', 'none')
                    $('#woocommerce_test_btn').css('display', 'none')
                    // $('#shopify_enable').prop('checked', false);
                }

                // getShowBtn('shopify')
                
                $('#magento_space').addClass('col-sm-3')
                $('#woo_commerce_space').addClass('col-sm-3')
                $('.bigcommerce_table').addClass('hide')
                $('.shopify_table').removeClass('hide')
                $('.woocommerce_table').addClass('hide')
                
            defaultTab = 'shopify';
            } else {
                wooenable = $("input[type='radio'][name='woo_commerce_enable']:checked").val()
                if(wooenable == '1') {                    
                    if(wooenable == '1') {
                        // woo commerce
                        $('#woo_commerce_api_url').css('display', 'block')
                        $('#woo_commerce_api_access_token').css('display', 'block')
                        $('#woo_commerce_api_key').css('display', 'block')
                        $('#woo_commerce_api_secret').css('display', 'block')
                        // $('#woo_commerce_space').addClass('col-sm-3')
                        $('#shopify_test_btn').css('display', 'none')
                        $('#bigcommerce_test_btn').css('display', 'none')
                        $('#woocommerce_test_btn').css('display', 'block')

                        $('#shopify_api_url').css('display', 'none')
                        $('#shopify_api_access_token').css('display', 'none')
                        $('#shopify_api_key').css('display', 'none')
                        $('#shopify_api_secret').css('display', 'none')
                        // $('#shopify_space').removeClass('col-sm-3')   
                        $('#location_shopify').css('display', 'none')         
                        // $('#shopify_test_btn').css('display', 'none')
                        // $('#bigcommerce_test_btn').css('display', 'none')

                        $('#big_commerce_api_url').css('display', 'none')
                        $('#big_commerce_api_access_token').css('display', 'none')
                        $('#big_commerce_api_key').css('display', 'none')
                        $('#big_commerce_api_secret').css('display', 'none')
                        // $('#big_commerce_space').removeClass('col-sm-3')
                        // $('#shopify_test_btn').css('display', 'none')
                        // $('#bigcommerce_test_btn').css('display', 'none')
                        // $('#big_commerce_enable').prop('checked', false);

                        $('#magento_api_url').css('display', 'none')
                        $('#magento_api_access_token').css('display', 'none')
                        $('#magento_api_key').css('display', 'none')
                        $('#magento_api_secret').css('display', 'none')
                        // $('#magento_space').removeClass('col-sm-3')
                        // $('#shopify_test_btn').css('display', 'none')
                        // $('#bigcommerce_test_btn').css('display', 'none')
            
                    } else {
                        // woo commerce
                        $('#woo_commerce_api_url').css('display', 'none')
                        $('#woo_commerce_api_access_token').css('display', 'none')
                        $('#woo_commerce_api_key').css('display', 'none')
                        $('#woo_commerce_api_secret').css('display', 'none')
                        $('#woo_commerce_space').removeClass('col-sm-3')
                        $('#shopify_test_btn').css('display', 'none')
                        $('#bigcommerce_test_btn').css('display', 'none')
                        $('#woocommerce_test_btn').css('display', 'none')
                        // $('#woo_commerce_enable').prop('checked', false);
                    }

                    // getShowBtn('woo_commerce')
                    $('#magento_space').addClass('col-sm-3')
                    $('#woo_commerce_space').addClass('col-sm-3')

                    $('.bigcommerce_table').addClass('hide')
                    $('.shopify_table').addClass('hide')
                    $('.woocommerce_table').removeClass('hide')
                    
                    defaultTab = 'woo_commerce';
                } else {
                    magentoenable = $("input[type='radio'][name='magento_enable']:checked").val()
                    if(magentoenable == '1') {
                        // magento
                        $('#magento_api_url').css('display', 'block')
                        $('#magento_api_access_token').css('display', 'block')
                        $('#magento_api_key').css('display', 'block')
                        $('#magento_api_secret').css('display', 'block')
                        // $('#magento_space').addClass('col-sm-3')
                        // $('#shopify_test_btn').css('display', 'none')
                        // $('#bigcommerce_test_btn').css('display', 'none')

                        $('#shopify_api_url').css('display', 'none')
                        $('#shopify_api_access_token').css('display', 'none')
                        $('#shopify_api_key').css('display', 'none')
                        $('#shopify_api_secret').css('display', 'none')
                        // $('#shopify_space').removeClass('col-sm-3')   
                        $('#location_shopify').css('display', 'none')         
                        // $('#shopify_test_btn').css('display', 'none')
                        // $('#bigcommerce_test_btn').css('display', 'none')

                        $('#woo_commerce_api_url').css('display', 'none')
                        $('#woo_commerce_api_access_token').css('display', 'none')
                        $('#woo_commerce_api_key').css('display', 'none')
                        $('#woo_commerce_api_secret').css('display', 'none')
                        // $('#woo_commerce_space').removeClass('col-sm-3')
                        $('#shopify_test_btn').css('display', 'none')
                        // $('#bigcommerce_test_btn').css('display', 'none')
                        // $('#woo_commerce_enable').prop('checked', false);

                        $('#big_commerce_api_url').css('display', 'none')
                        $('#big_commerce_api_access_token').css('display', 'none')
                        $('#big_commerce_api_key').css('display', 'none')
                        $('#big_commerce_api_secret').css('display', 'none')
                        // $('#big_commerce_space').removeClass('col-sm-3')
                        $('#shopify_test_btn').css('display', 'none')
                        $('#bigcommerce_test_btn').css('display', 'none')
                        $('#woocommerce_test_btn').css('display', 'none')
            
                    } else {
                        // magento
                        $('#magento_api_url').css('display', 'none')
                        $('#magento_api_access_token').css('display', 'none')
                        $('#magento_api_key').css('display', 'none')
                        $('#magento_api_secret').css('display', 'none')
                        $('#magento_space').removeClass('col-sm-3')
                        $('#shopify_test_btn').css('display', 'none')
                        $('#bigcommerce_test_btn').css('display', 'none')
                        $('#woocommerce_test_btn').css('display', 'none')
                        // $('#magento_enable').prop('checked', false);
                    }                    

                    // getShowBtn('magento')
                    $('.bigcommerce_table').addClass('hide')
                    $('.shopify_table').addClass('hide')
                    $('.woocommerce_table').addClass('hide')
                    defaultTab = 'magento';
                }
            }
        }

        shopify_table = $('#shopify_table').DataTable({
            processing: true,
            serverSide: true,
            aaSorting: [[1, 'asc']],
            // scrollY: "75vh",
            // scrollX: true,
            // scrollCollapse: true                    
            "ajax": {
                "url": "/products?vhomni_type=used_book",
            },                          
            columnDefs: [ {
                "targets": [0, 1, 2],
                "orderable": false,
                "searchable": false
            }],
            columns: [                
                { data: 'mass_delete', orderable: false, searchable: false},
                { data: 'image_vhomni', name: 'products.image_vhomni', orderable: false, searchable: false},   
                { data: 'action', name: 'action', orderable: false, searchable: false},             
                { data: 'product', name: 'products.name'  },
                { data: 'product_locations', name: 'product_locations'},
                @can('access_default_selling_price')
                    { data: 'selling_price', name: 'max_price', searchable: false},
                @endcan
                { data: 'current_stock', searchable: false},
                { data: 'category', name: 'c1.name'},
                { data: 'brand', name: 'brands.name'},                    
                { data: 'sku', name: 'products.sku'},                    
            ],
        })

        shopify_table2 = $('#shopify_table2').DataTable({
            processing: true,
            serverSide: true,
            aaSorting: [[1, 'asc']],
            // scrollY: "75vh",
            // scrollX: true,
            // scrollCollapse: true                    
            "ajax": {
                "url": "/products?vhomni_type=cheap_book",
            },                          
            columnDefs: [ {
                "targets": [0, 1, 2],
                "orderable": false,
                "searchable": false
            }],
            columns: [                
                { data: 'mass_delete', orderable: false, searchable: false},
                { data: 'image_vhomni', name: 'products.image_vhomni', orderable: false, searchable: false},   
                { data: 'action', name: 'action', orderable: false, searchable: false},             
                { data: 'product', name: 'products.name'  },
                { data: 'product_locations', name: 'product_locations'},
                @can('access_default_selling_price')
                    { data: 'selling_price', name: 'max_price', searchable: false},
                @endcan
                { data: 'current_stock', searchable: false},
                { data: 'category', name: 'c1.name'},
                { data: 'brand', name: 'brands.name'},                    
                { data: 'sku', name: 'products.sku'},                    
            ],
        })

        bigcommerce_table = $('#bigcommerce_table').DataTable({
            processing: true,
            serverSide: true,
            aaSorting: [[1, 'asc']],
            // scrollY: "75vh",
            // scrollX: true,
            // scrollCollapse: true                    
            "ajax": {
                "url": "/products?vhomni_type=bigcommerce",
            },                          
            columnDefs: [ {
                "targets": [0, 1, 2],
                "orderable": false,
                "searchable": false
            }],
            columns: [                
                { data: 'mass_delete', orderable: false, searchable: false},
                { data: 'image_vhomni', name: 'products.image_vhomni', orderable: false, searchable: false},   
                { data: 'action', name: 'action', orderable: false, searchable: false},             
                { data: 'product', name: 'products.name'  },
                { data: 'product_locations', name: 'product_locations'},
                @can('access_default_selling_price')
                    { data: 'selling_price', name: 'max_price', searchable: false},
                @endcan
                { data: 'current_stock', searchable: false},
                { data: 'category', name: 'c1.name'},
                { data: 'brand', name: 'brands.name'},                    
                { data: 'sku', name: 'products.sku'},                    
            ],
        })

        woocommerce_table = $('#woocommerce_table').DataTable({
            processing: true,
            serverSide: true,
            aaSorting: [[1, 'asc']],
            // scrollY: "75vh",
            // scrollX: true,
            // scrollCollapse: true                    
            "ajax": {
                "url": "/products?vhomni_type=woocommerce",
            },                          
            columnDefs: [ {
                "targets": [0, 1],
                "orderable": false,
                "searchable": false
            }],
            columns: [                
                { data: 'mass_delete', orderable: false, searchable: false},
                { data: 'image_vhomni', name: 'products.image_vhomni', orderable: false, searchable: false},
                { data: 'action', name: 'action', orderable: false, searchable: false},                
                { data: 'product', name: 'products.name'  },
                { data: 'product_locations', name: 'product_locations'},
                @can('access_default_selling_price')
                    { data: 'selling_price', name: 'max_price', searchable: false},
                @endcan
                { data: 'current_stock', searchable: false},
                { data: 'category', name: 'c1.name'},
                { data: 'brand', name: 'brands.name'},                    
                { data: 'sku', name: 'products.sku'},                    
            ],
        })

        $('table#shopify_table tbody').on('click', 'a.delete-product', function(e){
            e.preventDefault();
            swal({
                title: LANG.sure,
                icon: "warning",
                buttons: true,
                dangerMode: true,
            }).then((willDelete) => {
                if (willDelete) {
                    var href = $(this).attr('href');
                    $.ajax({
                        method: "DELETE",
                        url: href,
                        dataType: "json",
                        beforeSend: function() {
                            $('#pos-loader').css('display', 'inline-flex')
                        },
                        success: function(result){
                            $('#pos-loader').css('display', 'none')
                            if(result.success == true){
                                toastr.success(result.msg);
                                shopify_table.ajax.reload();
                            } else {
                                toastr.error(result.msg);
                            }
                        }
                    });
                }
            });
        });
        $('table#bigcommerce_table tbody').on('click', 'a.delete-product', function(e){
            e.preventDefault();
            swal({
                title: LANG.sure,
                icon: "warning",
                buttons: true,
                dangerMode: true,
            }).then((willDelete) => {
                if (willDelete) {
                    var href = $(this).attr('href');
                    $.ajax({
                        method: "DELETE",
                        url: href,
                        dataType: "json",
                        beforeSend: function() {
                            $('#pos-loader').css('display', 'inline-flex')
                        },
                        success: function(result){
                            $('#pos-loader').css('display', 'none')
                            if(result.success == true){
                                toastr.success(result.msg);
                                bigcommerce_table.ajax.reload();
                            } else {
                                toastr.error(result.msg);
                            }
                        }
                    });
                }
            });
        });
        $('table#woocommerce_table tbody').on('click', 'a.delete-product', function(e){
            e.preventDefault();
            swal({
                title: LANG.sure,
                icon: "warning",
                buttons: true,
                dangerMode: true,
            }).then((willDelete) => {
                if (willDelete) {
                    var href = $(this).attr('href');
                    $.ajax({
                        method: "DELETE",
                        url: href,
                        dataType: "json",
                        beforeSend: function() {
                            $('#pos-loader').css('display', 'inline-flex')
                        },
                        success: function(result){
                            $('#pos-loader').css('display', 'none')
                            if(result.success == true){
                                toastr.success(result.msg);
                                woocommerce_table.ajax.reload();
                            } else {
                                toastr.error(result.msg);
                            }
                        }
                    });
                }
            });
        });        

        /* if($('.list-group-item.active').text() == 'Shopify') {
            if(shopifyenable == '1') {
                $('#shopify_test_btn').css('display', 'block')
            } else {
                $('#shopify_test_btn').css('display', 'none')
            }
        } */

        /* if($('.list-group-item.active').text() == 'BigCommerce') {
            if(bigenable == '1') {
                $('#bigcommerce_test_btn').css('display', 'block')
            } else {
                $('#bigcommerce_test_btn').css('display', 'none')
            }
        } */

        if(defaultTab == 'bigcommerce') {
            $("#shopify_enable[value='0']").prop('checked', true).trigger('change');
            $("#woo_commerce_enable[value='0']").prop('checked', true).trigger('change');
            $("#magento_enable[value='0']").prop('checked', true).trigger('change');
            $('#bigcommerce_test_btn').css('display', 'block')  
            $('.bigcommerce_table').removeClass('hide')
            $('.shopify_table').addClass('hide')
            $('.woocommerce_table').addClass('hide')
        } else if(defaultTab == 'shopify') {
            $("#big_commerce_enable[value='0']").prop('checked', true).trigger('change');
            $("#woo_commerce_enable[value='0']").prop('checked', true).trigger('change');
            $("#magento_enable[value='0']").prop('checked', true).trigger('change');
            $('#shopify_test_btn').css('display', 'block')
            $('.bigcommerce_table').addClass('hide')
            $('.shopify_table').removeClass('hide')
            $('.woocommerce_table').addClass('hide')  
        } else if(defaultTab == 'woo_commerce') {
            $("#shopify_enable[value='0']").prop('checked', true).trigger('change');
            $("#big_commerce_enable[value='0']").prop('checked', true).trigger('change');
            $("#magento_enable[value='0']").prop('checked', true).trigger('change');
            $('#woocommerce_test_btn').css('display', 'block')  
            $('.bigcommerce_table').addClass('hide')
            $('.shopify_table').addClass('hide')
            $('.woocommerce_table').removeClass('hide')
        } else {
            $("#big_commerce_enable[value='0']").prop('checked', true).trigger('change');
            $("#woo_commerce_enable[value='0']").prop('checked', true).trigger('change');
            $("#magento_enable[value='0']").prop('checked', true).trigger('change');
            $('.bigcommerce_table').addClass('hide')
            $('.shopify_table').addClass('hide')
            $('.woocommerce_table').addClass('hide')
        }
    }) 

    $('#delete-selected-shopify').on('click', function(e){
        e.preventDefault();
        var selected_rows = getSelectedRows();        
        if(selected_rows.length > 0){
            $('input#selected_rows_shopify').val(selected_rows);
            swal({
                title: LANG.sure,
                icon: "warning",
                buttons: true,
                dangerMode: true,
            }).then((willDelete) => {
                if (willDelete) {      
                    $('#pos-loader').css('display', 'inline-flex');              
                    $('form#mass_delete_form_shopify').submit();
                }
            });
        } else{
            $('input#selected_rows_shopify').val('');
            swal('@lang("lang_v1.no_row_selected")');
        }    
    });

    $(document).on('click', '#delete-selected-bigcommerce', function(e){
        e.preventDefault();
        var selected_rows = getSelectedRows();
        
        if(selected_rows.length > 0){
            $('input#selected_rows_bigcommerce').val(selected_rows);
            swal({
                title: LANG.sure,
                icon: "warning",
                buttons: true,
                dangerMode: true,
            }).then((willDelete) => {
                if (willDelete) {
                    $('#pos-loader').css('display', 'inline-flex');
                    $('form#mass_delete_form_bigcommerce').submit();
                }
            });
        } else{
            $('input#selected_rows_bigcommerce').val('');
            swal('@lang("lang_v1.no_row_selected")');
        }    
    });

    $(document).on('click', '#delete-selected-woocommerce', function(e){
        e.preventDefault();
        var selected_rows = getSelectedRows();
        
        if(selected_rows.length > 0){
            $('input#selected_rows_woocommerce').val(selected_rows);
            swal({
                title: LANG.sure,
                icon: "warning",
                buttons: true,
                dangerMode: true,
            }).then((willDelete) => {
                if (willDelete) {
                    $('#pos-loader').css('display', 'inline-flex');
                    $('form#mass_delete_form_woocommerce').submit();
                }
            });
        } else{
            $('input#selected_rows_woocommerce').val('');
            swal('@lang("lang_v1.no_row_selected")');
        }    
    });

    function getShowBtn(tipe) {              
        // console.log(tipe)
        if(tipe == 'shopify' && $("#shopify_enable[value='1']").is(':checked')) {                          
            $('#shopify_test_btn').css('display', 'block')
            $('#bigcommerce_test_btn').css('display', 'none')
            $('#woocommerce_test_btn').css('display', 'none')
            $('.bigcommerce_table').addClass('hide')
            $('.shopify_table').removeClass('hide')
            $('.woocommerce_table').addClass('hide')
        } else if(tipe == 'bigcommerce' && $("#big_commerce_enable[value='1']").is(':checked')) {                
            $('#bigcommerce_test_btn').css('display', 'block')
            $('#shopify_test_btn').css('display', 'none')
            $('#woocommerce_test_btn').css('display', 'none')
            $('.bigcommerce_table').removeClass('hide')
            $('.shopify_table').addClass('hide')
            $('.woocommerce_table').addClass('hide')
        } else if(tipe == 'woocommerce' && $("#woo_commerce_enable[value='1']").is(':checked')) {
            // console.log('block')
            $('#bigcommerce_test_btn').css('display', 'none')
            $('#shopify_test_btn').css('display', 'none')
            $('#woocommerce_test_btn').css('display', 'block')
            $('.bigcommerce_table').addClass('hide')
            $('.shopify_table').addClass('hide')
            $('.woocommerce_table').removeClass('hide')
        } else {
            $('#bigcommerce_test_btn').css('display', 'none')
            $('#shopify_test_btn').css('display', 'none')
            $('#woocommerce_test_btn').css('display', 'none')
            $('.bigcommerce_table').addClass('hide')
            $('.shopify_table').addClass('hide')
            $('.woocommerce_table').addClass('hide')
        }
    }

    $('#shopify_test_btn').click(function () {
        //PALIGU
        // console.log('shopify test');
        $.ajax({
            method: 'GET',
            url: '/check-location-have-products',
            dataType: 'json',
            data: {
                location_id: $('#product_locations_shopify').val(),
                type: 'shopify'
            },
            beforeSend: function() {
                $('#pos-loader').css('display', 'inline-flex')
            },
            success: function(result) {
                $('#pos-loader').css('display', 'none')
                if(result.rc == 1) {
                    swal({
                        title: 'Information',
                        text: "If you change SHOPIFY product location from "+result.location+" to "+$('#product_locations_shopify').find('option:selected').text()+", all product in "+result.location+" will be moved to "+$('#product_locations_shopify').find('option:selected').text()+"?",
                        icon: 'info',
                        buttons: {
                            cancel: "No",
                            confirm: "Yes"
                        }, 
                    }).then((confirmed) => {
                        if(confirmed) {
                            sync_shopify();
                        }
                    })
                } else if(result.rc == 2) {
                    swal({
                        title: 'Information',
                        text: result.rm,
                        icon: 'info',
                        // button: false,
                    })
                } else {                    
                    sync_shopify();
                }
            }
        }).fail(function(xhr, status, q) {
            Pace.stop();
            $('#pos-loader').css('display', 'none')
            // consoole.log(xhr.responseText)
            swal({
                title: 'Information',
                text: xhr.responseText,
                icon: 'info',
                // button: false,
            })
        });
    });

    function sync_shopify() {
        swal({
            title: 'Information',
            text: 'This Process May Take littler longer than regular sync process, Please do not CLOSE or REFRESH your browser during this process.',
            icon: 'info',
            buttons: {
                cancel: "CANCEL",
                confirm: "START SYNC"
            },                      
        }).then((confirmed) => {
            if (confirmed) {   
                $.ajax({
                    method: 'GET',
                    url: '/shopify-udb-auth',
                    dataType: 'json',
                    data: {
                        product_locations: $('#product_locations_shopify').val(),
                        checkmarks: $("input[name='checkmarks_shopify[]']:checked").serializeArray()
                    },
                    beforeSend: function() {
                        $('#pos-loader').css('display', 'inline-flex')
                        // swal('Information', 'This process may take litle bit long time, please do not close or refresh the browser', 'info')
                    },
                    success: function (result) {
                        Pace.stop();
                        $('#pos-loader').css('display', 'none')
                        // console.log(result)
                        var message = '<p>'+result.message;
                        var icon = 'info'
                        var title = 'Information'
                        if(result.data) {
                            var inserted = 0;
                            var updated = 0;
                            $.each(result.data, function(k, v) {
                                $.each(v, function(k1, v1) {
                                    // console.log(v1)
                                    if(v1.status == 2) {
                                        updated++;
                                    } else {
                                        inserted++;
                                    }                        
                                })
                            })
    
                            message+= '<br>' + inserted + ' Products Imported and Created <br>' + updated + ' Products Updated and Synced</p>';
    
                            icon = 'success'
                            title = 'Success'
                        } 
                        var msg = document.createElement('div');
                        msg.innerHTML = message;
                        swal({
                            title: title,                            
                            content: msg,
                            icon: icon,
                            // buttons: true,
                            dangerMode: true,
                        })               
                        shopify_table.ajax.reload();
                        shopify_table2.ajax.reload();
                    },
                }).fail(function(xhr, status, q) {
                    Pace.stop();
                    $('#pos-loader').css('display', 'none')
                    // alert(xhr.responseText)            
                    swal({
                        title: 'Information',
                        text: xhr.responseText,
                        icon: 'info',
                        // buttons: false,
                    })
                });
                //Commented by Paligu
            }
        });
    }

    $('#bigcommerce_test_btn').click(function () {
        $.ajax({
            method: 'GET',
            url: '/check-location-have-products',
            dataType: 'json',
            data: {
                location_id: $('#product_locations_big_commerce').val(),
                type: 'bigcommerce'
            },
            beforeSend: function() {
                $('#pos-loader').css('display', 'inline-flex')
            },
            success: function(result) {
                $('#pos-loader').css('display', 'none')
                if(result.rc == 1) {
                    swal({
                        title: 'Information',
                        text: "If you change BIGCOMMERCE product location from "+result.location+" to "+$('#product_locations_big_commerce').find('option:selected').text()+", all product in "+result.location+" will be moved to "+$('#product_locations_big_commerce').find('option:selected').text()+"?",
                        icon: 'info',
                        buttons: {
                            cancel: "No",
                            confirm: "Yes"
                        }, 
                    }).then((confirmed) => {
                        if(confirmed) {
                            sync_bigcommerce();
                        }
                    })
                } else if(result.rc == 2) {
                    swal({
                        title: 'Information',
                        text: result.rm,
                        icon: 'info',
                        // button: false,
                    })
                } else {                    
                    sync_bigcommerce();
                }
            }
        }).fail(function(xhr, status, q) {
            Pace.stop();
            $('#pos-loader').css('display', 'none')
            // consoole.log(xhr.responseText)
            swal({
                title: 'Information',
                text: xhr.responseText,
                icon: 'info',
                // button: false,
            })
        });
    });

    function sync_bigcommerce() {
        //ADRI
        // console.log('shopify test');
        swal({
            title: 'Information',
            text: 'This Process May Take littler longer than regular sync process, Please do not CLOSE or REFRESH your browser during this process.',
            icon: 'info',
            buttons: {
                cancel: "CANCEL",
                confirm: "START SYNC"
            },                      
        }).then((confirmed) => {
            if (confirmed) {    
                $.ajax({
                    method: 'GET',
                    url: '/bigcommerce-auth',
                    dataType: 'json',
                    data: {
                        product_locations: $('#product_locations_big_commerce').val(),
                        checkmarks: $("input[name='checkmarks_bigcommerce[]']:checked").serializeArray()
                    },
                    beforeSend: function() {
                        $('#pos-loader').css('display', 'inline-flex')
                        // swal('Information', 'This process may take litle bit long time, please do not close or refresh the browser', 'info')
                    },
                    success: function (result) {                        
                        Pace.stop();
                        $('#pos-loader').css('display', 'none')
                        // console.log(result) 
                        var message = '<p>'+result.message;
                        var icon = 'info'
                        var title = 'Information'
                        if(result.data) {
                            var inserted = 0;
                            var updated = 0;
                            $.each(result.data, function(k, v) {
                                $.each(v, function(k1, v1) {
                                    console.log(v1)
                                    if(v1.status == 2) {
                                        updated++;
                                    } else {
                                        inserted++;
                                    }                        
                                })
                            })

                            message+= '<br>' + inserted + ' Products Imported and Created <br>' + updated + ' Products Updated and Synced</p>';

                            icon = 'success'
                            title = 'Success'
                        } 
                        var msg = document.createElement('div');
                        msg.innerHTML = message;
                        swal({
                            title: title,                            
                            content: msg,
                            icon: icon,
                            // buttons: true,
                            dangerMode: true,
                        })           
                        bigcommerce_table.ajax.reload();     
                    },
                }).fail(function(xhr, status, q) {
                    Pace.stop();
                    $('#pos-loader').css('display', 'none')
                    // consoole.log(xhr.responseText)
                    swal({
                        title: 'Information',
                        text: xhr.responseText,
                        icon: 'info',
                        // button: false,
                    })
                });
            }
        });
        //Commented by Adri
    }

    $('#woocommerce_test_btn').click(function () {
        //ADRI
        // console.log('shopify test');
        $.ajax({
            method: 'GET',
            url: '/check-location-have-products',
            dataType: 'json',
            data: {
                location_id: $('#product_locations_woo_commerce').val(),
                type: 'woocommerce'
            },
            beforeSend: function() {
                $('#pos-loader').css('display', 'inline-flex')
            },
            success: function(result) {
                $('#pos-loader').css('display', 'none')
                if(result.rc == 1) {
                    swal({
                        title: 'Information',
                        text: "If you change WOOCOMMERCE product location from "+result.location+" to "+$('#product_locations_woo_commerce').find('option:selected').text()+", all product in "+result.location+" will be moved to "+$('#product_locations_woo_commerce').find('option:selected').text()+"?",
                        icon: 'info',
                        buttons: {
                            cancel: "No",
                            confirm: "Yes"
                        }, 
                    }).then((confirmed) => {
                        if(confirmed) {
                            sync_woocommerce();
                        }
                    })
                } else if(result.rc == 2) {
                    swal({
                        title: 'Information',
                        text: result.rm,
                        icon: 'info',
                        // button: false,
                    })
                } else {                    
                    sync_woocommerce();
                }
            }
        }).fail(function(xhr, status, q) {
            Pace.stop();
            $('#pos-loader').css('display', 'none')
            // consoole.log(xhr.responseText)
            swal({
                title: 'Information',
                text: xhr.responseText,
                icon: 'info',
                // button: false,
            })
        });
    });

    function sync_woocommerce() {
        swal({
            title: 'Information',
            text: 'This Process May Take littler longer than regular sync process, Please do not CLOSE or REFRESH your browser during this process.',
            icon: 'info',
            buttons: {
                cancel: "CANCEL",
                confirm: "START SYNC"
            },                      
        }).then((confirmed) => {
            if (confirmed) {            
                $.ajax({
                    method: 'GET',
                    url: '/woocommerce-auth',
                    dataType: 'json',
                    data: {
                        product_locations: $('#product_locations_woo_commerce').val(),
                        checkmarks: $("input[name='checkmarks_woocommerce[]']:checked").serializeArray()
                    },
                    beforeSend: function() {
                        $('#pos-loader').css('display', 'inline-flex')
                        // swal('Information', 'This process may take litle bit long time, please do not close or refresh the browser', 'info')
                    },
                    success: function (result) {
                        Pace.stop();
                        $('#pos-loader').css('display', 'none')
                        // console.log(result)                         
                        var message = '<p>'+result.message;
                        var icon = 'info'
                        var title = 'Information'
                        if(result.data) {
                            var inserted = 0;
                            var updated = 0;
                            $.each(result.data, function(k, v) {
                                $.each(v, function(k1, v1) {
                                    console.log(v1)
                                    if(v1.status == 2) {
                                        updated++;
                                    } else {
                                        inserted++;
                                    }                        
                                })
                            })

                            message+= '<br>' + inserted + ' Products Imported and Created <br>' + updated + ' Products Updated and Synced</p>';

                            icon = 'success'
                            title = 'Success'
                        } 
                        var msg = document.createElement('div');
                        msg.innerHTML = message;
                        swal({
                            title: title,                            
                            content: msg,
                            icon: icon,
                            // buttons: true,
                            dangerMode: true,
                        })           
                        woocommerce_table.ajax.reload()    
                    },
                }).fail(function(xhr, status, q) {
                    Pace.stop();
                    $('#pos-loader').css('display', 'none')
                    // consoole.log(xhr.responseText)
                    swal({
                        title: 'Information',
                        text: xhr.responseText,
                        icon: 'info',
                        // button: false,
                    })
                });
                //Commented by Adri
            }
        });
    }

    $('#update_settings_btn').click(function() {
        swal({
            title: "ATTENTION",
            text: 'Are you sure you want to make this change and apply the settings?',  
            buttons: ['No', 'Yes'],
            icon: "info"          
        }).then((confirm) => {
            if(confirm) {
                var form = $("#omni_edit_form");                
                form.submit();
            }
        })
    })

    function get_input(val, tipe) {
        // var next = true;        
        if(defaultTab != tipe && val == '1') {
            var text = '';
            var defaultText = '';
            if(tipe == 'bigcommerce') {
                text = "Big Commerce";
            } else if(tipe == 'shopify') {
                text = "Shopify";
            } else if(tipe == 'woo_commerce') {
                text = "Woo Commerce";
            } else if(tipe == 'magento') {
                text = "Magento"
            }
            if(defaultTab == 'bigcommerce') {
                defaultText = "Big Commerce";
            } else if(defaultTab == 'shopify') {
                defaultText = "Shopify";
            } else if(defaultTab == 'woo_commerce') {
                defaultText = "Woo Commerce";
            } else if(defaultTab == 'magento') {
                defaultText = "Magento"
            }
            swal({
                title: 'ATTENTION',
                text: 'You can only have one E-Commerce account associated to your VH-POS subscription, Would you like to disable ' + defaultText + ' and start to sync with ' + text + ' ?', 
                icon: 'info',
                buttons: ['No', 'Yes']
            }).then((confirm) => {
                if(confirm) {                    
                    if(tipe == 'bigcommerce') {
                        defaultTab = 'bigcommerce';                  
                        $("#shopify_enable[value='0']").prop('checked', true).trigger('change');
                        $("#woo_commerce_enable[value='0']").prop('checked', true).trigger('change');
                        $("#magento_enable[value='0']").prop('checked', true).trigger('change');
                        get_input_data(0, 'shopify')
                        get_input_data(0, 'woo_commerce')
                        get_input_data(0, 'magento')
                        get_input_data(1, 'bigcommerce')
                        // $("#big_commerce_enable[value='1']").prop('checked', true).trigger('change');
                    } 
                    
                    if(tipe == 'shopify') {                        
                        // $("#shopify_enable[value='1']").prop('checked', true).trigger('change');
                        $("#big_commerce_enable[value='0']").prop('checked', true).trigger('change');
                        $("#woo_commerce_enable[value='0']").prop('checked', true).trigger('change');
                        $("#magento_enable[value='0']").prop('checked', true).trigger('change');
                        get_input_data(0, 'bigcommerce')
                        get_input_data(0, 'woo_commerce')
                        get_input_data(0, 'magento')
                        get_input_data(1, 'shopify')
                        defaultTab = 'shopify';
                    } 
                    if(tipe == 'woo_commerce') {                        
                        // $("#woo_commerce_enable[value='1']").prop('checked', true).trigger('change');
                        $("#shopify_enable[value='0']").prop('checked', true).trigger('change');
                        $("#big_commerce_enable[value='0']").prop('checked', true).trigger('change');
                        $("#magento_enable[value='0']").prop('checked', true).trigger('change');
                        get_input_data(0, 'shopify')
                        get_input_data(0, 'bigcommerce')
                        get_input_data(0, 'magento')
                        get_input_data(1, 'woo_commerce')
                        defaultTab = 'woo_commerce';
                    } 
                    if(tipe == 'magento') {                        
                        $("#woo_commerce_enable[value='0']").prop('checked', true).trigger('change');
                        // $("#woo_commerce_enable[value='1']").prop('checked', true).trigger('change');
                        $("#shopify_enable[value='0']").prop('checked', true).trigger('change');
                        $("#big_commerce_enable[value='0']").prop('checked', true).trigger('change');
                        get_input_data(0, 'shopify')
                        get_input_data(0, 'bigcommerce')
                        get_input_data(0, 'woo_commerce')
                        get_input_data(1, 'magento')
                        defaultTab = 'magento';
                    }                    
                } else {
                    if(tipe == 'bigcommerce') {
                        $("#big_commerce_enable[value='0']").prop('checked', true).trigger('change');                    
                    } else if(tipe == 'shopify') {                        
                        $("#shopify_enable[value='0']").prop('checked', true).trigger('change');
                    } else if(tipe == 'woo_commerce') {                        
                        $("#woo_commerce_enable[value='0']").prop('checked', true).trigger('change');
                    } else if(tipe == 'magento') {                        
                        $("#magento_enable[value='0']").prop('checked', true).trigger('change');
                    }
                }
            })            
        } else {
            get_input_data(val, tipe)
        }
    }

    function get_input_data(val, tipe) {
        if(val == '1') {                        
            if(tipe == 'bigcommerce') {
                // bigcommerce
                $('#big_commerce_api_url').css('display', 'block')
                $('#big_commerce_api_access_token').css('display', 'block')
                $('#big_commerce_api_key').css('display', 'block')
                $('#big_commerce_api_secret').css('display', 'block')
                $("#big_commerce_space").addClass('col-sm-3')
                $("#location_big_commerce").css('display', 'block')
                $('#shopify_test_btn').css('display', 'none')
                $('#bigcommerce_test_btn').css('display', 'block')    
                $('#woocommerce_test_btn').css('display', 'none')    
                $('.bigcommerce_table').removeClass('hide')
                $('.shopify_table').addClass('hide')
                $('.woocommerce_table').addClass('hide')
                
                // $('#shopify_enable').prop('change', false).trigger('click')
            }
            if(tipe == 'shopify') {
                // shopify
                $('#shopify_api_url').css('display', 'block')
                $('#shopify_api_access_token').css('display', 'block')
                $('#shopify_api_key').css('display', 'block')
                $('#shopify_api_secret').css('display', 'block')
                $('#shopify_space').addClass('col-sm-3')             
                $('#location_shopify').css('display', 'block')   
                $('#shopify_test_btn').css('display', 'block')
                $('#bigcommerce_test_btn').css('display', 'none')
                $('#woocommerce_test_btn').css('display', 'none')
                $('.bigcommerce_table').addClass('hide')
                $('.shopify_table').removeClass('hide')
                $('.woocommerce_table').addClass('hide')
                // $('#shopify_enable').prop('change', false).trigger('click')
            }
            if(tipe == 'woo_commerce') {
                // woo commerce
                $('#woo_commerce_api_url').css('display', 'block')
                $('#woo_commerce_api_access_token').css('display', 'block')
                $('#woo_commerce_api_key').css('display', 'block')
                $('#woo_commerce_api_secret').css('display', 'block')
                $('#woo_commerce_space').addClass('col-sm-3')
                $('#shopify_test_btn').css('display', 'none')
                $('#bigcommerce_test_btn').css('display', 'none')
                $('#woocommerce_test_btn').css('display', 'block')
                $('.bigcommerce_table').addClass('hide')
                $('.shopify_table').addClass('hide')
                $('.woocommerce_table').removeClass('hide')
            }
            if(tipe == 'magento') {
                // magento
                $('#magento_api_url').css('display', 'block')
                $('#magento_api_access_token').css('display', 'block')
                $('#magento_api_key').css('display', 'block')
                $('#magento_api_secret').css('display', 'block')
                $('#magento_space').addClass('col-sm-3')
                $('#shopify_test_btn').css('display', 'none')
                $('#bigcommerce_test_btn').css('display', 'none')
                $('.bigcommerce_table').addClass('hide')
                $('.shopify_table').addClass('hide')
                $('.woocommerce_table').addClass('hide')
            }

            if($('.list-group-item.active').text() == 'Shopify') {
                $('#shopify_test_btn').css('display', 'block')
                $('#bigcommerce_test_btn').css('display', 'none')
                $('#woocommerce_test_btn').css('display', 'none')
                $('.bigcommerce_table').addClass('hide')
                $('.shopify_table').removeClass('hide')
                $('.woocommerce_table').addClass('hide')
            }
            
            if($('.list-group-item.active').text() == 'BigCommerce') {
                $('#shopify_test_btn').css('display', 'none')
                $('#bigcommerce_test_btn').css('display', 'block')
                $('#woocommerce_test_btn').css('display', 'none')
                $('.bigcommerce_table').removeClass('hide')
                $('.shopify_table').addClass('hide')
                $('.woocommerce_table').addClass('hide')                
            }          

            if($('.list-group-item.active').text() == 'WooCommerce') {
                $('#shopify_test_btn').css('display', 'none')
                $('#bigcommerce_test_btn').css('display', 'none')
                $('#woocommerce_test_btn').css('display', 'block')
                $('.bigcommerce_table').addClass('hide')
                $('.shopify_table').addClass('hide')
                $('.woocommerce_table').removeClass('hide')
            }            
        } else {
            if(tipe == 'bigcommerce') {
                // big commerce
                $('#big_commerce_api_url').css('display', 'none')
                $('#big_commerce_api_access_token').css('display', 'none')
                $('#big_commerce_api_key').css('display', 'none')
                $('#big_commerce_api_secret').css('display', 'none')
                $("#big_commerce_space").removeClass('col-sm-3')
                $("#location_big_commerce").css('display', 'none')
                $('#shopify_test_btn').css('display', 'none')
                $('#bigcommerce_test_btn').css('display', 'none')
                $('#woocommerce_test_btn').css('display', 'none')
                $('.bigcommerce_table').addClass('hide')                
            }
            if(tipe == 'shopify') {
                // shopify
                $('#shopify_api_url').css('display', 'none')
                $('#shopify_api_access_token').css('display', 'none')
                $('#shopify_api_key').css('display', 'none')
                $('#shopify_api_secret').css('display', 'none')
                $('#shopify_space').removeClass('col-sm-3')   
                $('#location_shopify').css('display', 'none')             
                $('#shopify_test_btn').css('display', 'none')
                $('#bigcommerce_test_btn').css('display', 'none')
                $('#woocommerce_test_btn').css('display', 'none')                
                $('.shopify_table').addClass('hide')                
            }
            if(tipe == 'woo_commerce') {
                // woo commerce
                $('#woo_commerce_api_url').css('display', 'none')
                $('#woo_commerce_api_access_token').css('display', 'none')
                $('#woo_commerce_api_key').css('display', 'none')
                $('#woo_commerce_api_secret').css('display', 'none')
                $('#woo_commerce_space').removeClass('col-sm-3')
                $('#shopify_test_btn').css('display', 'none')
                $('#bigcommerce_test_btn').css('display', 'none')
                $('#woocommerce_test_btn').css('display', 'none')
                $('.woocommerce_table').addClass('hide')
            }
            if(tipe == 'magento') {
                // magento
                $('#magento_api_url').css('display', 'none')
                $('#magento_api_access_token').css('display', 'none')
                $('#magento_api_key').css('display', 'none')
                $('#magento_api_secret').css('display', 'none')
                $('#magento_space').removeClass('col-sm-3')
                $('#shopify_test_btn').css('display', 'none')
                $('#bigcommerce_test_btn').css('display', 'none')
                $('#woocommerce_test_btn').css('display', 'none')
                $('.bigcommerce_table').addClass('hide')
                $('.shopify_table').addClass('hide')
                $('.woocommerce_table').addClass('hide')
            }

            if($('.list-group-item.active').text() == 'Shopify') {
                $('#shopify_test_btn').css('display', 'none')
                $('#bigcommerce_test_btn').css('display', 'none')                
                $('.shopify_table').addClass('hide')                
            }

            if($('.list-group-item.active').text() == 'BigCommerce') {
                $('#bigcommerce_test_btn').css('display', 'none')
                $('#shopify_test_btn').css('display', 'none')
                $('.bigcommerce_table').addClass('hide')            
            }

            if($('.list-group-item.active').text() == 'WooCommerce') {
                $('#bigcommerce_test_btn').css('display', 'none')
                $('#shopify_test_btn').css('display', 'none')
                $('#woocommerce_test_btn').css('display', 'none')                
                $('.woocommerce_table').addClass('hide')
            }
        }
    }
</script>
@endsection