import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { sign_reward_data } from '../../data';
import { sign_reward_item } from './sign_reward_item';
import { sign_reward_alone_item } from './sign_reward_alone_item';
const { ccclass, property } = _decorator;

@ccclass('sign_reward')
export class sign_reward extends Component {
    @property(Prefab) reward_item:Prefab = null
    @property(Prefab) reward_alone_item:Prefab = null
    @property(Node) content:Node = null
    private m_click_item_cb = null
    start() {

    }

    initView(data_list:sign_reward_data[], cb, click_item_cb) {
        this.m_click_item_cb = click_item_cb
        this.content.removeAllChildren()
        for (let index = 0; index < data_list.length; index++) {
            const element = data_list[index];
            if(element.current_sign_status==1) {
                cb(element)
            }
            if(index>=data_list.length-1) {
                let item = instantiate(this.reward_alone_item)
                item.parent = this.content
                item.getComponent(sign_reward_alone_item).initView(element,this.onClickRewardAloneItem.bind(this))
            } else {
                let item = instantiate(this.reward_item)
                item.parent = this.content
                item.getComponent(sign_reward_item).initView(element,this.onClickRewardItem.bind(this))
            }
        }
    }

    private onClickRewardItem(item:sign_reward_item) {
        this.signOperate(item.getData())        
    }

    private onClickRewardAloneItem(item:sign_reward_alone_item) {
        this.signOperate(item.getData())
    }

    private signOperate(data:sign_reward_data) { 
        if(data.receive_status==1) {
            return 
        }
        let is_qiandao:boolean = false
        let is_buqian:boolean = false
        if(data.current_sign_status==1) {
            // console.log('今日领取')
            is_qiandao = true
        } else {
            if(data.repair_sign_status==1) {
                // console.log('补签')
                is_qiandao = true
                is_buqian = true
            }
        }     
        if(is_qiandao) {
            this.m_click_item_cb(data,is_buqian)
        }   
    }

}

