// ==UserScript==
// @name        店小秘-列表显示全部🛠
// @namespace   Violentmonkey Scripts
// @license MIT
// @run-at       document-end
// @match       https://www.dianxiaomi.com/darazProduct/*
// @match       https://www.dianxiaomi.com/smtProduct/*
// @match       https://www.dianxiaomi.com/shopifyProduct/*
// @match       https://www.dianxiaomi.com/web/smt/smtProductList/*

// @match       https://www.dianxiaomi.com/crawl/index.htm*
// @grant       GM_xmlhttpRequest
// @require      https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js
// @require https://scriptcat.org/lib/3085/0.0.4.5.1/%E9%80%9A%E7%94%A8%E5%87%BD%E6%95%B0.js
// @require https://scriptcat.org/lib/567/1.0.3/Hook%20Vue3%20app.js
// @version     1.1.2
// @author      -
// @description 2024/10/29 10:56
// ==/UserScript==

var _url_ = new URL(window.location.href)
const pushBnt = function(){
  const bnt = $(`<li class='showall'><a href="javascript:;">显示全部</a></li>`);
  const new_bnt = $('<a class="showall" style="margin-right: 1rem;" href="javascript:;">显示全部 </a>')
  isnew = _url_.pathname.split('/')[1] === 'web';
  if (isnew){
    //新版
    new_bnt.off().on("click",async function(){
      $("#app").prop("__vue_app__").config.globalProperties.$pinia.state.value.global.isLoading = true;
      const page_info = $('.nav-index').prop("__vue__").provides[Object.getOwnPropertySymbols($('.nav-index').prop("__vue__").provides)[0]]
      switch(_url_.pathname.split('/')[3]){
        case 'smtProductList':
          url = "https://www.dianxiaomi.com/api/smtProduct/pageList.json"
          var data = {
            pageNo: 1,
            pageSize: 300,
            searchType: $("[for='searchForm_searchContent.searchType']").closest('.search-form-item').prop("__vue__")[1].props.modelValue.searchContent.searchType,
            searchValue: $("[for='searchForm_searchContent.searchType']").closest('.search-form-item').prop("__vue__")[1].props.modelValue.searchContent.searchValue,
            shopId: page_info.cacheShopId.value || "-1",
            dxmState: page_info.currentDxmState.value || "",
            dxmOfflineState: page_info.currentDxmOfflineState.value || "",
            sortValue: $("[for='searchForm_searchContent.searchType']").closest('.search-form-item').prop("__vue__")[1].props.modelValue.sortField === "desc" ? '2': '1',
            sortName: $("[for='searchForm_searchContent.searchType']").closest('.search-form-item').prop("__vue__")[1].props.modelValue.sortName,
          }
          $(".ant-tabs-top").length > 0 ? (data.productStatusType = $(".ant-tabs-top").prop("__vue__")[1].props.activeKey):(void 0)
          console.log(data);
      }
      const book = await tool.getTotalPage(url,data,-1,'POST','json');
      var items = [];
      $.each(book,function(){
        items = items.concat(this.data.page.list)
      });
      $(".gridTableBox > .vxe-grid").prop("__vue__").props.data = items;
      $("#app").prop("__vue_app__").config.globalProperties.$pinia.state.value.global.isLoading = false;
    });
    if(!$(".showall").length){ 
      $(".vxe-pager.d-grid-pager > .vxe-pager--wrapper").prepend(new_bnt);
    }
  }else{
    //旧版
    const _$ = unsafeWindow.$;
    bnt.on("click",async function(){
      _$('#loading').modal('show');
      pathname = _url_.pathname.split('/')[1];
      switch (pathname){
        case 'darazProduct':
          url = "https://www.dianxiaomi.com/darazProduct/pageList.htm"
          var data = DARAZ_PRODUCT_LIST.searchFn.getSearchData();
          var container = "#darazPageList > tbody"
          var items = "#darazPageList > tbody > tr.content"
          break;
        case 'smtProduct':
          url = "https://www.dianxiaomi.com/smtProduct/pageList.htm"
          var data = smtPageListSearchData();
          var container = "#goodsContent tbody.xianshishujudate"
          var items = "#goodsContent tr.content"
          break;
        case 'shopifyProduct':
          url = "https://www.dianxiaomi.com/shopifyProduct/pageList.htm"
          var data = getSearchParams();
          var container = "#shopifySysMsg"
          var items = "#shopifySysMsg tr.content[data-id]"
          break;
        case 'crawl':
          var isBanjia = !!(_$("#banjiaDiv li.in-active").length && !_$("#caijiDiv li.in-active").length);
          if(isBanjia){
            var url = `https://www.dianxiaomi.com/_${unsafeWindow.url}`;
            data = getParaments();
          }else{
            url = 'https://www.dianxiaomi.com/crawl/list.htm'
            var remarkType = _$.trim(_$('#comment').val()),
            remarkContent = _$.trim(_$('#comments').val());
            data= {
              pageNo: 1,
              pageSize: '300',
              state: _$('ul.ulBtn2').find('li.in-active').attr('name'),
              site: _$('#searchCategory .dataSource').find('a.in-active').data('value'),
              searchValue: _$('#searchValue').val(),
              productSearchType: _$('#productSearchType').val(),
              sortTime: _$('#sortTime').val(),
              accountName: _$('#accountName').val(),
              commentType: remarkType,
              commentValue: +remarkType === 1 ? remarkContent : ''
            }
          }
          var container = "table[cid='productList'] tbody.xianshishujudate"
          var items = "table[cid='productList'] tbody.xianshishujudate > tr.content"
        default:
          break;
      }
      data.pageSize = '300';
      data.pageNo = 1;
      let book = await tool.getTotalPage(url,data,-1,'POST');
      pages = _$("<root></root>").append(book[0]);
      for (var i = 1;i<book.length;i++){
        page = _$("<root></root>").append(book[i]);
        pages.find(container).append(
          page.find(items)
        );
      }
      var html = pages.find(" > *").map(function(){return this.outerHTML}).toArray().join("");
      _$('#pageList').html(html);
      _$('#loading').modal('hide');
    });
    var hasList = !!$("#pageList");
    if(hasList && !$(".showall").length){
      $("#upPage:has(*),#downPage:has(*)").append(bnt);
    }
  } 
}

window.onload = function(){
  pushBnt();
  isnew = _url_.pathname.split('/')[1] === 'web';
  if(isnew){
    //新版
    unsafeWindow.$ = $;
    setInterval(pushBnt,1000);
  }else{
    const MYJ_PAGINATION_init_HOOK = unsafeWindow.MYJ_PAGINATION.init;
    unsafeWindow.MYJ_PAGINATION.init = function(option,id,call){
      MYJ_PAGINATION_init_HOOK(option,id,call);
      pushBnt();
    }
  }
}