<?php
// 火箭工单

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

class Huojian extends AbstractScrmSpider
{
    const API_LIST  = '/prod-api1/biz/counter/link/share/';    // 列表API地址
    const API_DETAILS = '';       // 详情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],
            
            // 明确指派角色
            'listApi'   => self::API_LIST,                  // 必须
            
            'listMethod' => 'POST',
            'paginationMode' => self::MODE_UI,
            
            'authActions' => [
                // 1. 填入密码：寻找 class 包含 el-message-box__input 下面的任意 input
                // Node.js 会自动扫描所有匹配项，并只把密码强行注入到那个“肉眼可见”的框里
                ['type' => 'vue_fill', 'selector' => '.el-message-box__input input', 'value' => $this->password],
                
                // 2. 停顿 500ms，让 Vue 绑定的 v-model 彻底反应过来
                ['type' => 'wait', 'ms' => 500],
                
                // 3. 点击确认：寻找 MessageBox 底部的蓝色 primary 确认按钮
                // 同样利用 vue_click 的可见性过滤，无视隐藏的旧弹窗按钮
                ['type' => 'vue_click', 'selector' => '.el-message-box__btns .el-button--primary'],
                
                // 4. 等待弹窗淡出，接口开始请求
                ['type' => 'wait', 'ms' => 2000]
            ]
        ];
    }

    // 只负责解析 List 的总页数
    protected function extractListTotalPages($listFirstPageData, $countData = null)
    {
        // 只有一页
        return 1;
    }

    // 没有分页返回空数组
    protected function buildListPageParams($page)
    {
        return [];
    }

    // 提供 List 的下一页按钮信息
    protected function getUiPaginationConfig()
    {
        return [];
    }

    // 清爽至极的数据清洗：详情是详情，列表是列表
    protected function parseToUnifiedData($detailData, $allListPagesData)
    {
        $unifiedData = $this->unifiedData;
        
        $unifiedData->todayNewCount = $allListPagesData[0]['data']['counterWorker']['newTodayFriend'];
        
        // 2. 循环合并了所有页数的 List 数组
        $count = 0;
        foreach ($allListPagesData as $pageRaw) {
            $records = $pageRaw['data']['counterCsAccountVo'] ?? []; 
            foreach ($records as $item) {
                if(!empty($item['accountLogin'])) {
                    $number = $item['accountLogin'] ?? null;
                    $isOnline = (isset($item['accountStatus']) && $item['accountStatus'] == 1);
                    $count++;
                    $unifiedData->addNumber($number, $isOnline, $item['newTodayFriend']);
                }
            }
        }
        
        $unifiedData->total = $count;

        return $unifiedData;
    }
}