import { _decorator, Component, instantiate, Label, Node, Prefab } from 'cc';
import { base_ui } from '../../fw/base_ui';
import { mail_item_data } from '../../data';
import { mailbox_reward_item } from './mailbox_reward_item';
const { ccclass, property } = _decorator;

@ccclass('mailbox_details')
export class mailbox_details extends base_ui {
    @property(Node) public_bg:Node = null
    @property(Node) lab_content:Node = null
    @property(Node) lab_time:Node = null
    @property(Node) reward_content:Node = null
    @property(Prefab) reward_item:Prefab = null
    @property(Node) btn_delete:Node = null
    @property(Node) btn_recv:Node = null
    private m_data:mail_item_data = null
    private m_delete_cb = null
    private m_recv_cb = null
    start() {
        this.onButtonListen(this.public_bg, ()=>{
            this.node.active = false
        })
        this.onButtonListen(this.btn_delete, ()=>{
            if(this.m_delete_cb) {
                this.m_delete_cb(this)
            }
        })
        this.onButtonListen(this.btn_recv, ()=>{
            if(this.m_recv_cb) {
                this.m_recv_cb(this)
            }
        })
    }

    initView(data:mail_item_data, delete_cb, recv_cb) {
        this.m_data = data
        this.m_delete_cb = delete_cb
        this.m_recv_cb = recv_cb
        this.lab_content.getComponent(Label).string = '    ' + data.content
        this.lab_time.getComponent(Label).string = data.create_at

        this.btn_delete.active = false
        this.btn_recv.active = false
        if(data.state==1) {
            this.btn_delete.active = true
        } else {
            this.btn_recv.active = true
        }

        this.reward_content.removeAllChildren()
        // console.log('rewards_list=',data.rewards_list)
        for (let index = 0; index < data.rewards_list.length; index++) {
            const element = data.rewards_list[index];
            let item = instantiate(this.reward_item)
            item.parent = this.reward_content
            item.getComponent(mailbox_reward_item).initView(element,data.state==1?true:false)
        }
    }

    public getData():mail_item_data {
        return this.m_data
    }
}

