!function () {
    function getCookie(cname) {
        var name = cname + "=";
        if (document.cookie.indexOf('%') > -1){
            var decodedCookie = document.cookie;
        } else {
            var decodedCookie = decodeURIComponent(document.cookie);
        }
        var ca = decodedCookie.split(';');
        for(var i = 0; i <ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') {
                c = c.substring(1);
             }
             if (c.indexOf(name) == 0) {
                return c.substring(name.length, c.length);
             }
         }
        return "";
    }

    function gotoNextPage(documentType) {
        switch (documentType) {
            case 'taobaoSearch':
                var elems = $('[trace="srp_select_pagedown"]');
                if (!elems.length) {
                    elems =  $('.nav__pagination a:eq(1)');
                }
                elems.length && elems[0].click();
                break;
            case 'taobaoShop':
                $('.page-info').next().next()[0].click();
                break;
            case 'tmallSearch':
            case 'tmallShop':
                $('.ui-page-s-next')[0].click();
                break;
            case '1688Search':
                $('.fui-next')[0].click();
                break;
            case '1688Shop':
                $('.next')[0].click();
                break;
            case 'jdSearch':
                $('.fp-next')[0].click();
                break;
            case 'jdShop':
                $('a:contains("下一页")')[0].click();
                break;
        }
    }

    function renderItemCollectActionBar() {
        if ($('#selectAllItem').length || $('#copyCurrentItemUrl').length) {
            return;     //防止重复渲染操作栏
        }

        var actionBarElem = $('<div class="bottom-action-bar-search"><div class="bottom-action-bar-bg"></div><div class="mt-vw-1"style="position: relative;display: flex;justify-content: center;"><input id="selectAllItem"type="checkbox"style="zoom: 130%;cursor: pointer;"><span style="font-size:14px;color: white;margin-top: 15px"">全选</span><button class="layui-btn layui-btn-sm font-bold bg-green ml-vw-1 mt-px-2">选中数量<span id="selectedItemCnt"class="layui-badge"style="background-color: #f30213">0</span></button><button id="copySelectedItemUrl"class="btn-gradient blue small ml-vw-1.5">复制链接</button><button id="crawlItemsBatch"class="btn-gradient blue small ml-vw-0.2">批量采购</button><button id="crawlSelectedItem"class="btn-gradient blue small ml-vw-0.2">采购选中商品</button> <div class="layui-inline" style="margin-left: 20px;color: white;font-size: 12px;"><div>关联店铺：  <span id="crxShopName"></span></div><div>版本： <span id="crxVer"></span></div></div> </div></div>');
        $('body').append(actionBarElem);
        chrome.storage.local.get('BackgroundCache').then(function(result) {
            var BackgroundCache = result['BackgroundCache'] || {};
            var shopName = '--';
            var ver = chrome.runtime.getManifest().version;
            if (BackgroundCache.hasOwnProperty('venderInfos')) {
                shopName = BackgroundCache['venderInfos']['shopName'];
            }

            $('#crxVer').text(ver);
            $('#crxShopName').text(shopName);
        });

        // $(document).off('click', '#selectAllItem');
        // $(document).on('click', '#selectAllItem',function(){
        //     console.log('selectAllItem click');
        // });

        //全选按钮
        $(document).off('click', '#selectAllItem');
        $(document).on('click', '#selectAllItem',function(){
            if ($('#selectAllItem').is(':checked')) {
                console.log($('.square-checkbox'));
                $('.square-checkbox').addClass('square-checkbox-selected');
            } else {
                $('.square-checkbox').removeClass('square-checkbox-selected');
            }

            $('#selectedItemCnt').text($('.square-checkbox-selected').length);
        });

        //复制选中链接按钮
        $(document).off('click', '#copySelectedItemUrl');
        $(document).on('click', '#copySelectedItemUrl',function(){
            var itemUrls = parseItemUrls();
            if (itemUrls.length) {
                copyValueToClipboard(itemUrls.join('\r\n'));
                showSimpleMessage('复制成功', 800);
            } else {
                showSimpleMessage('无选中项目', 800);
            }
        });

        //批量采购
        $(document).off('click', '#crawlItemsBatch');
        $(document).on('click', '#crawlItemsBatch',function(){
            chrome.runtime.sendMessage({
                type: "openTab",
                url: chrome.runtime.getURL('collect.html'),
                active: true
            });
        });

        //复制商品按钮
        $(document).off('click', '#crawlSelectedItem');
        $(document).on('click', '#crawlSelectedItem',function(){
            if ($('.loader-long-strip').length) {
                showSimpleMessage('请等待当前商品采购完成...', 2000);
                return;
            }
            if (!$('.square-checkbox-selected').length) {
                showSimpleMessage('请勾选需要复制的商品', 2000);
                return;
            }
            switch (window.documentType) {
                case 'taobaoSearch':
                    window.processItemFunc.taobaoSearch.crawlSelectedItemCallbak();
                    break;
                case 'taobaoShop':
                    window.processItemFunc.taobaoShop.crawlSelectedItemCallbak();
                    break;
                case 'tmallSearch':
                    window.processItemFunc.tmallSearch.crawlSelectedItemCallbak();
                    break;
                case 'tmallShop':
                    window.processItemFunc.tmallShop.crawlSelectedItemCallbak();
                    break;
                case '1688Search':
                case '1688Shop':
                    window.processItemFunc['1688Search'].crawlSelectedItemCallbak();
                    break;
                case 'jdSearch':
                case 'jdShop':
                    window.processItemFunc.jdSearch.crawlSelectedItemCallbak();
                    break;
                case 'pddSearch':
                    window.processItemFunc.pddSearch.crawlSelectedItemCallbak();
                    break;
                case 'pddShop':
                    window.processItemFunc.pddShop.crawlSelectedItemCallbak();
                    break;
            }
        });
    }


    // chrome.runtime.sendMessage({
    //     type: "getStorageOptions",
    //     key: "itemCopyEnable"
    // }, function (itemCopyEnable) {
    //     if (itemCopyEnable) {
    //         renderItemCollectActionBar();
    //     }
    // });
}();
