//获取账户下资产列表（新）
var axios = require('axios');
const CMD = {}
const url = 'https://api.oceanengine.com/open_api/2/tools/event/all_assets/list/'
CMD.send = async function(){
    var api = require('./api');
    api.getToken(async (token)=>{
        if(token!=null){
            api.getAdvertiserList((list)=>{
                if(list.length<=0){

                }else{
                    let cc_account_id = list[0].advertiser_id 
                    api.getAppAccountList(cc_account_id,(account_list)=>{
                        if(account_list!=null){

                            let count = 0;
                            let count_num = ()=>{
                                count = count + 1
                                if(count == account_list.length){
                                    global.config.initFinishAccount()
                                }
                            }

                            for (let index = 0; index < account_list.length; index++) {
                                const info = account_list[index];
                                CMD.getAssetList(count_num,info.advertiser_id,token)
                            }
                        }
                        // console.log("getAppAccountList:",account_list)
                    })
                
                }
      
            })
        }
    })
}

CMD.getAssetList = function(call_back,advertiser_id,token){
    console.log("advertiser_id:",advertiser_id)
    const headers =  {
        "Access-Token": token, 
    }
    const params = {
        access_token: token,
        advertiser_id:advertiser_id
    };
    global.config.instance_list[`jl_${advertiser_id}`] = []
    axios.get(url, { params, headers })
    .then(response => {
        for (let index = 0; index < response.data.data.asset_list.length; index++) {
            const asset_element = response.data.data.asset_list[index];
            global.config.instance_list[`jl_${advertiser_id}`].push(asset_element)
        }
        call_back()
    })
    .catch(error => {
        console.error("Error getAssetList GET request:", error);
    });
}
module.exports = CMD;

//是否必填：是, 字段 : advertiser_ids 类型 :number[]  描述 :广告主ID集合(如果包含没有访问权限的ID,将返回no permission error)取值范围: 1-100
//是否必填：否, 字段 : fields 类型 :nstring[]  描述 :查询字段集合, 默认:查询所有。字段详见下方response字段定义允许值: id、name、role、status、address、reason、license_url、license_no、license_province、license_city、company、brand、promotion_area、promotion_center_province、promotion_center_city、industry、create_time、note


//应答示例
// HTTPS/1.1 200 OK {    
//     "message": "OK",  
//     "code": 0,    
//     "data": [     
//           {             
//            "promotion_area": "",        运营区域    
//            "brand": "xxx",            经营类别 
//            "address": "",           地址  
//            "id": 0,             广告主ID
//            "name": "xxx",         账户名
//            "license_city": "",         执照城市    
//            "promotion_center_city": "",          运营城市   
//            "promotion_center_province": "",    运营省份         
//            "industry": "xxx",             
//            "license_no": "xxx",             执照编号
//            "license_url": "xxx",        执照预览地址(链接默认1小时内有效)     
//            "role": 6,            角色 
//            "license_province": "",       执照省份      
//            "company": "xxx",        公司名
//           }    
//         ],     
//      "request_id": "xxx" 
//  }