import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { sign_total_reward_data } from '../../data';
import { sign_total_reward_item } from './sign_total_reward_item';
import { http } from '../../http';
import { config } from '../../config';
import { sign_total_reward_zw_item } from './sign_total_reward_zw_item';
import { uiManager } from '../../manager/uiManager';
const { ccclass, property } = _decorator;

@ccclass('sign_total_reward')
export class sign_total_reward extends Component {
    @property(Prefab) total_reward_item:Prefab = null
    @property(Prefab) total_reward_zw_item:Prefab = null
    @property(Node) content:Node = null
    private m_cb = null
    start() {

    }

    initView(total_number:number, data_list:sign_total_reward_data[], cb) {
        this.m_cb = cb
        this.content.removeAllChildren()
        let curr_day = total_number 
        for (let i = 0; i < 7; i++) {
            let is_hav = false
            for (let index = 0; index < data_list.length; index++) {
                const element = data_list[index];
                if(i+1 == element.day_number) {
                    let item = instantiate(this.total_reward_item)
                    item.parent = this.content
                    let item_component = item.getComponent(sign_total_reward_item)
                    item_component.initView(element,this.onClickItem.bind(this))
                    if(element.day_number==curr_day) {
                        item_component.setLastReceive()
                    }
                    if(index==0) {
                        // item_component.hideLeftLine()
                    } else if(index==data_list.length-1) {
                        item_component.hideRightLine()
                    }
                    is_hav = true
                    break
                }
            }
            if(is_hav==false) {
                let item = instantiate(this.total_reward_zw_item)
                item.parent = this.content      
                let item_component = item.getComponent(sign_total_reward_zw_item)       
                if(curr_day>=i+1) {
                    item_component.initView(true)
                } else {
                    item_component.initView(false)
                }
            }
        }
        // for (let index = 0; index < data_list.length; index++) {
        //     const element = data_list[index];            
        //     let item = instantiate(this.total_reward_item)
        //     item.parent = this.content
        //     let item_component = item.getComponent(sign_total_reward_item)
        //     item_component.initView(element,this.onClickItem.bind(this))
        //     if(index<data_list.length-1) {
        //         let next_element = data_list[index+1]
        //         if(next_element.is_receive_status==0) {
        //             item_component.setLastReceive()
        //         }
        //     } 
        //     if(index==0) {
        //         item_component.hideLeftLine()
        //     } else if(index==data_list.length-1) {
        //         item_component.hideRightLine()
        //     }
        // }
    }

    private onClickItem(item:sign_total_reward_item) {
        let data = item.getData()
        if(data.receive_status==1) {
            return
        }
        if(data.is_receive_status==0) {
            return
        }

        uiManager.Instance().showLoading()
        let opt = {'index':data.index}
        http.post(config.API.user_total_receive,opt, (err,d)=>{
            uiManager.Instance().hideLoading()
            if(!err){
                let nd = JSON.parse(d)
                if(nd.code === config.status.SUCCESS){
                    console.log("累计领取", nd.content)
                    this.m_cb && this.m_cb(data)
                }
            }
        })
    }

}

