<?php
// SS云控(Customer)

require_once __DIR__ . '/AbstractScrmSpider.class.php';

// 可以获取每日具体进线数据
class SsCustomer extends AbstractScrmSpider
{
    const API_LIST  = '/sys/share/report/get-customer-analysis-dimension-list';     // 列表API地址
    const API_DETAILS = '/sys/share/report/get-customer-analysis-statistics';                   // 详情API地址
    const API_COUNT = '';    // 总数API地址
    const DEFAULT_PER_PAGE_COUNT = 20;                          // List默认每页显示的数量
    private $pageUrl;
    private $account;
    private $password;
    private $unifiedData;
    
    // 实例化时动态传入账号和密码
    public function __construct($pageUrl, $account, $password, $nodeHost = 'http://127.0.0.1:3001')
    {
        parent::__construct($nodeHost);
        $this->account = $account;
        $this->password = $password;
        $this->pageUrl = $pageUrl;
        
        $this->unifiedData = new UnifiedScrmData();
    }

    protected function getSpiderConfig()
    {
        return [
            'pageUrl' => $this->pageUrl,
            'apiUrls' => [self::API_LIST, self::API_DETAILS, self::API_COUNT],
            
            // 明确指派角色
            'listApi'   => self::API_LIST,                  // 必须
            'detailApi' => self::API_DETAILS,               // 选填
            'countApi'  => self::API_COUNT,
            
            'listMethod' => 'POST',
            
            'paginationMode' => self::MODE_UI,
            
            'authActions' => [
                ['type' => 'wait', 'ms' => 2000],
                
                ['type' => 'type', 'selector' => 'input[type="password"]', 'value' => $this->password],
                
                ['type' => 'press', 'key' => 'Enter'],
                
                ['type' => 'wait', 'ms' => 3000],
                
                [
                    'type'     => 'vue_click', 
                    'selector' => 'button[class*="reports-customers__dimension"]',
                    'text'     => 'social media accounts'
                ],
                
                ['type' => 'wait', 'ms' => 3000],
            ]
        ];
    }

    // 只负责解析 List 的总页数
    protected function extractListTotalPages($listFirstPageData, $countData = null)
    {
        return null;
        
        // $default_per_page_count = self::DEFAULT_PER_PAGE_COUNT;
        
        // // 场景 A：如果单独的 Count 接口有数据返回
        // if ($countData) {
        //     // 假设 count 接口返回的是 {"data": {"total": 312}}
        //     $totalRecords = $countData['data']['total'];
        // } else {
        //     // 场景 B
        //     $totalRecords = $listFirstPageData['data']['total'];
        // }
        
        // $this->unifiedData->total = $totalRecords;
        // if($totalRecords <= $default_per_page_count) {
        //     return 1;
        // }
        
        // return ceil($listFirstPageData['data']['total']/$default_per_page_count);
    }

    // 只负责组装 List 的翻页参数
    protected function buildListPageParams($page)
    {
        return ['page' => $page, 'pageSize' => self::DEFAULT_PER_PAGE_COUNT];
    }

    // 提供 List 的下一页按钮信息
    protected function getUiPaginationConfig()
    {
        return [
            'nextBtnSelector' => '.arco-pagination-item-next', 
            'waitMs'          => 2000
        ];
    }

    // 清爽至极的数据清洗：详情是详情，列表是列表
    protected function parseToUnifiedData($detailData, $allListPagesData)
    {
        $unifiedData = $this->unifiedData;

        // 1. 如果捕获到了详情数据，提取今日新增
        if ($detailData) {
            $unifiedData->todayNewCount = (int)($detailData['data']['distinct_contacts_total'] ?? 0);
        }

        // 2. 循环合并了所有页数的 List 数组
        foreach ($allListPagesData as $pageRaw) {
            $records = $pageRaw['data']['list'] ?? []; 
            foreach ($records as $item) {
                if(!empty($item['channel_tag'])) {
                    $number = $item['channel_tag'] ?? null;
                    $isOnline = true;   // 离线状态的账号同步不到
                    
                    $unifiedData->addNumber($number, $isOnline, $item['distinct_contacts_total']);
                }
            }
        }
        
        // 🚀 3. 终极统计：所有翻页数据均已入库，此时再统计真实的 Total 总数
        $unifiedData->total = count($unifiedData->numbers);

        return $unifiedData;
    }
}