//执行一个创建番茄链接任务
const axios = require('axios');
const helper = require('../src/helper');
const config = require('../etc/config.json')
const CMD = {}


//充值
async function get_recharge_template(){
    var distributorId = config.fanqie_config.distributorId;
    var secretKey = config.fanqie_config.secretKey;
    const postData = {
        distributor_id: distributorId,
        ts: helper.getCurrentUnixTimestamp(),
        sign: helper.getSign(distributorId,secretKey),
        page_size: 100,
        page_index:0,
    };
    // console.log("get_recharge_template:")
    var url = config.fanqie_config.recharge_list_host
    var base_params = `/?distributor_id=${postData.distributor_id}&ts=${postData.ts}&sign=${postData.sign}`
    var other_params = `&page_size=${postData.page_size}&page_index=${postData.page_index}`

    return axios.get(`${url}${base_params}${other_params}`)
        .then(response => {
            // console.log('Response data:', response.data );
            return response.data
        })
        .catch(error => {
            console.error('Error:', error);
        });

}
CMD.runTask = async function(data,main_info,PlatformInfo,call_back){
    // let recharge =  await get_recharge_template()
    // console.log("recharge:",recharge)
    let  tg_link_config = JSON.parse(main_info.tg_link_config)
    let chongzhi_id = tg_link_config['chongzhi_id']
    let chongzhi_list = JSON.parse(PlatformInfo.chongzhi)
    let chongzhi_name = null
    let recharge_template_id = ""
    for (let index = 0; index < chongzhi_list.length; index++) {
        const cz_obj = chongzhi_list[index];
        if(cz_obj.id==chongzhi_id){
          chongzhi_name = cz_obj.name
          recharge_template_id = cz_obj.value
          break
        }
    }
    // if(recharge.code==200&&chongzhi_name!=null){
    //     for (let index = 0; index < recharge.data.length; index++) {
    //         const recharge_item = recharge.data[index];
    //         if(recharge_item.recharge_template_name==chongzhi_name){
    //             recharge_template_id = recharge_item.recharge_template_id
    //         }
    //     }
    // }
    // console.log("recharge_template_id:",recharge_template_id)
    try {
        let create_fanqie_book_link = require('../src/api/fq/create_fanqie_book_link')
        let remark  =  data.product_name.replace(/[^\u4e00-\u9fa5a-zA-Z0-9]/g, '')
        await create_fanqie_book_link.send(data.product_id,1,remark,1,recharge_template_id,(start_page,start_param, info)=>{
            if(start_page==null||start_param==null){
                data.start_page = start_page
                data.start_param = start_param
                call_back(data,info)
            }else{
                data.start_page = start_page
                data.start_param = start_param
                data.promotion_id = info.promotion_id
                call_back(data,null)
            }
        })
    } catch (error) {
        console.error('fq_create_link :Error:', error);
        // 处理错误
    }

}

module.exports = CMD;