// pages/codeOnline/codeOnline.js
const app = getApp()
Component({
    /**
     * 继承父级样式
     */
    options: {
        addGlobalClass: true,
    },

    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        baseUrl: 'https://yongma16.xyz',
        runJsPath: '/common-api/runJs/',
        config: {
            title: '运行js',
            runCodePlaceholderText: '请输入js',
            loading: true,
            runBtnText: '运行',
            formatBtnText: '格式化',
            clearBtnText:'清空',
            htmlNode: ''
        },
        codeVal: '',
        loading: false,
        runCodeRes: '空',
        htmlNode: '空',
        prefixRes:'结果：'
    },


    lifetimes: {
        // 生命周期函数，可以为函数，或一个在 methods 段中定义的方法名
        attached: function () {
            this.initData('请输入')
        }
    },
    methods: {
        initData(val) {
            this.setData({
                htmlNode: app.changeHtmlText(val)
            })
        },
        async eval_js(val) {
            try {
                const res = await app.eval_des(`${val}`)
                console.log(res, 'res')
                console.log(res())
                this.setData({
                    runCodeRes: res,
                    htmlNode: app.changeHtmlText(res)
                })
            } catch (r) {
                const rText = JSON.stringify(r)
                this.setData({
                    runCodeRes: rText,
                    htmlNode: app.changeHtmlText(rText)
                })
            }
        },
        formatCode(){

        },
        clearCode(){
            this.setData({
                codeVal:''
            })
        },
        runCode() {
            const codeVal = this.data.codeVal
            console.log('运行代码', codeVal)
            const thisBack = this
            if (codeVal) {
                // this.eval_js(codeVal)
                const data = {
                    content: codeVal
                }
                const headers = { 'Content-Type': 'application/json;charset=UTF-8' }
                wx.request({
                    url: this.data.baseUrl + this.data.runJsPath,
                    headers:headers,
                    method: 'POST',
                    data: data,
                    success: res => {
                        let resText=res.data.data
                        if(res.data.code===0){
                            resText=res.data.msg
                        }
                        thisBack.setData({
                            runCodeRes:resText ,
                            htmlNode: app.changeHtmlText(resText)
                        })
                    },
                    fail: r => {
                        console.log(r, r)
                    }
                })

            }
            // // eval(this.data.codeVal)

        },
        bindKeyInput(e) {
            this.setData({
                codeVal: e.detail.value
            })
        },
    }
})