import { _decorator, Component, EditBox, Label, Node, Sprite, SpriteFrame } from 'cc';
import { base_ui } from '../../fw/base_ui';
import { SdkUtil } from '../../sdkUtil';
import { http } from '../../http';
import { config } from '../../config';
import { tools } from '../../tools';
import { uiManager } from '../../manager/uiManager';
const { ccclass, property } = _decorator;

@ccclass('feedback_submit')
export class feedback_submit extends base_ui {
    @property(Node) lab_title:Node = null
    @property(Node) btn_add_img:Node = null
    @property(EditBox) editbox_content:EditBox = null
    @property(EditBox) editbox_contact:EditBox = null

    private m_data = null
    private img_url:string = ''
    start() {
        this.onButtonListen(this.btn_add_img, ()=>{
            this.onClickAddImg()
        })
        this.editbox_content.node.on(EditBox.EventType.EDITING_DID_ENDED, ()=>{
            console.log('this.editbox_content=',this.editbox_content.string)
        })
        this.editbox_contact.node.on(EditBox.EventType.EDITING_DID_ENDED, ()=>{
            console.log('this.editbox_contact=',this.editbox_contact.string)
        })
    }

    initView(data) {
        this.m_data = data
        this.lab_title.getComponent(Label).string = '--  ' + data.title +'内容'
    }

    private onClickAddImg() {
        SdkUtil.choosSystemImage((filePath)=>{
            http.uploadFile(config.API.up_feedback_img,filePath,null,(err,d)=>{
                if(!err){
                    let data = JSON.parse(d)
                    if(data.code===config.status.SUCCESS){
                        // console.log('data=',data.content)
                        this.img_url = data.content.surl
                        tools.loadRemoteImg(this.img_url, (res)=>{
                            this.btn_add_img.getComponent(Sprite).spriteFrame = res.sf
                        })
                    }
                }
            })           
        })
    }

    public getData() {
        if(this.editbox_content.string.length<10) {
            uiManager.showToast('请填写10个字以上的问题描述')
            return null
        }
        if(this.editbox_contact.string.length<=0) {
            uiManager.showToast('请填写联系方式')
            return null
        }
        let data =  {'title': this.m_data.title, 
                     'describe':this.editbox_content.string,
                     'img_url':this.img_url,
                     'mobile':this.editbox_contact.string,
                    }
        return data
    }
}

