let Parse = getApp().Parse;
const company = getApp().globalData.company
const qiniuUploader = require("../../../../../../utils/qiniuUploader");
Page({

    /**
     * 页面的初始数据
     */
    data: {

        imageList: [],
        licenseList: [],
        merchant: null,
        store: null,
        stores: null,
        perCapita: null,
        cover: [],
        image: [],
        storeName: null,
        perCapita: null,
        desc: null,
        name: null,
        workingTime: null,
        uploadURL: null,
        domain: null,
        uptokenURL: null,
        html: null,
        content: null,
        id: null,
    },
    getHtml(e) { //从组件获取值
        let html = e.detail.content.html
        console.log(html);
        // this.setData({ html: html })
        // let field = event.currentTarget.dataset.field;
        // let html = event.detail.content.html;
        this.setData({ html: html })
    },

    insertImage() { //图片上传插入示例
        let that = this
        wx.chooseImage({
            count: 1,
            success(res) {
                // 本地测试图片插入
                // this.selectComponent('#hf_editor').insertSrc(res.tempFilePaths[0]);
                // console.log(res.tempFilePaths[0]);
                let tempFilePaths = res.tempFilePaths[0];
                console.log(tempFilePaths);


                qiniuUploader.upload(
                    tempFilePaths,
                    (res) => {
                        let img = res.imageURL;
                        that.selectComponent('#hf_editor').insertSrc(img); //调用组件insertSrc方法
                    },
                    (error) => {
                        console.log("error: " + error);
                    }, {
                        region: "SCN",
                        uploadURL: that.data.uploadURL,
                        domain: that.data.domain,
                        uptoken: that.data.uptokenURL,
                    }
                );
            }
        })
    },

    changeFile(e) {
        if (e.detail && e.detail.length > 0) {
            this.setData({
                cover: [e.detail[0].url]
            })
        } else {
            this.setData({
                cover: []
            })
        }
    },
    changeFiles(e) {
        if (e.detail && e.detail.length > 0) {
            this.setData({
                image: e.detail
            })
        } else {
            this.setData({
                image: []
            })
        }
    },

    blur(e) {
        let name = e.currentTarget.dataset.name
        this.setData({
            [name]: e.detail.value
        })
    },
    mobile() {
        let id = this.data.store.objectId
        console.log(id);
        wx.navigateTo({
            url: '/nova-tourism/pages/my/merchant/merchant-home/account/mobile/index?id=' + id
        });

    },
    password() {
        let id = this.data.store.objectId
        console.log(id);
        wx.navigateTo({
            url: '/nova-tourism/pages/my/merchant/merchant-home/account/password/index?id=' + id
        });
    },
    async determine() {
        let image = []
        for (let index = 0; index < this.data.image.length; index++) {
            let item = this.data.image[index];
            image.push(item.url)
        }
        console.log(this.data.perCapita, this.data.name, image, this.data.storeName, this.data.perCapita, this.data.desc, this.data.workingTime, this.data.html);
        let query = new Parse.Query("ShopStore")

        let ShopStore = await query.get(this.data.id)
        ShopStore.set("perCapita", this.data.perCapita)
        ShopStore.set("name", this.data.name)
        ShopStore.set("image", image)
        ShopStore.set("storeName", this.data.storeName)
        ShopStore.set("perCapita", this.data.perCapita)
        ShopStore.set("desc", this.data.desc)
        ShopStore.set("workingTime", this.data.workingTime)
        ShopStore.set("content", this.data.html)
        ShopStore.save().then(res => {
            console.log(res)
            wx.showToast({
                title: '修改成功',
                icon: 'none'
            })
            wx.navigateBack({
                delta: 1
            });
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function(options) {
        let merchant = wx.getStorageSync('merchant'); //用户
        let ShopStore = new Parse.Query('ShopStore')
        ShopStore.notEqualTo('isDeleted', "true")
        ShopStore.equalTo('company',company)
        ShopStore.include('user')
        ShopStore.equalTo('user',merchant.objectId)
        let store = await ShopStore.first()
        store = store.toJSON()
        console.log(store);
        let imageList = []
        let licenseList = []
            // store.cover[0].forEach(i => {
        imageList.push({
                url: store.cover
            })
            // })
        console.log(imageList);
        store.image.forEach(i => {
            licenseList.push({
                url: i
            })
        })
        this.setData({
            imageList: imageList,
            store: store,
            cover: store.cover,
            image: store.image,
            perCapita: store.perCapita,
            storeName: store.storeName,
            perCapita: store.perCapita,
            desc: store.desc,
            name: store.name,
            workingTime: store.workingTime,
            html: store.content,
            id: store.objectId,
            licenseList: licenseList,
        })
        console.log(this.data.html);
        this.selectComponent('#hf_editor').setHtml(this.data.html);
        this.getQiniuOption()
    },
    async getQiniuOption() {
        let uploadData = await this.getUptoken()
        if (uploadData) {
            this.setData({
                uploadURL: uploadData.zoneUrl,
                domain: uploadData.domain,
                uptokenURL: uploadData.uptoken,

            })
        }
        console.log(this.data.uploadURL, this.data.domain, this.data.uptokenURL);
    },
    getUptoken() {
        return Parse.Cloud.run('qiniu_uptoken', { company: company })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})