//发布营销图片

const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        posterId: '', //模板Id
        imgUrl: app.urls.baseImgUrl, //图片基础路径
        areaList: [], //模板内容
        cardInfo: {}, //模板内容
        windowWidth: app.globalData.windowWidth, //屏幕宽度
        scale: 1, //缩放
        imgTempSrc: '',
        currentIndex: 0,
        isShowPoster: false, //生成海报后展示海报
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            posterId: options.posterId
        })
        this.getPosterDetail()
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (this.data.imgTempSrc != '') {
            this.doUploadImg(this.data.imgTempSrc)
        }
    },
    //获取海报详情
    getPosterDetail() {
        var _self = this;
        app.http.get(app.urls.getCardDetail.format({
                cardTemplateId: _self.data.posterId
            }), {
                header: {
                    token: app.globalData.token
                },
            }).then(res => {
                var area = res.data.areaList
                var scale = res.data.picturesTemplateWidth / _self.data.windowWidth
                res.data.picturesTemplateWidth = res.data.picturesTemplateWidth / scale
                res.data.picturesTemplateHeight = res.data.picturesTemplateHeight / scale
                for (let i = 0; i < area.length; i++) {
                    if (area[i].templateAreaFontCenter == '1') {
                        area[i].templateAreaFontCenter = 'left'
                    } else if (area[i].templateAreaFontCenter == '2') {
                        area[i].templateAreaFontCenter = 'center'
                    } else {
                        area[i].templateAreaFontCenter = 'right'
                    }
                    if (area[i].templateAreaFontBold == '0') {
                        area[i].templateAreaFontBold = 'normal'
                    } else if (area[i].templateAreaFontBold == '1') {
                        area[i].templateAreaFontBold = 'bold'
                    } else {
                        area[i]['templateAreaFontStyle'] = 'italic'
                    }
                    area[i].templateAreaFontSize = area[i].templateAreaFontSize / scale + 'px'
                    area[i].templateAreaHeight = area[i].templateAreaHeight / scale
                    area[i].templateAreaWidth = area[i].templateAreaWidth / scale
                    area[i].templateAreaDown = area[i].templateAreaDown / scale
                    area[i].templateAreaTop = area[i].templateAreaTop / scale
                    area[i].templateAreaRight = area[i].templateAreaRight / scale
                    area[i].templateAreaLeft = area[i].templateAreaLeft / scale
                }
                _self.setData({
                    cardInfo: res.data,
                    areaList: area
                })
            })
            .catch(err => {

            })
    },
    // 生成海报
    createPoster() {
        var self = this
        wx.showLoading({
            title: '生成中...',
            success() {
                self.setData({
                    isLoading: true
                })
            }
        })
        var content = {
            pictureTemplateId: self.data.posterId,
            cardTemplateUseAreaList: []
        }
        for (let i = 0; i < self.data.areaList.length; i++) {
            if (self.data.areaList[i].templateAreaType != '1') {
                var info = {
                    cardTemplateUseId: "",
                    picturesTemplateAreaId: self.data.areaList[i].picturesTemplateAreaId,
                    templateAreaFile: self.data.areaList[i].templateAreaFile,
                    templateAreaFontValue: self.data.areaList[i].templateAreaFontValue
                }
                content.cardTemplateUseAreaList.push(info)
            }
        }
        app.http.post(app.urls.creatCard, {
            data: content,
            header: {
                token: app.globalData.token
            }
        }).then(res => {
            if (res.statusCode == '200') {
                self.setData({
                    isLoading: false,
                })
                //获取海报
                wx.showToast({
                    title: '海报生成成功',
                    success(res) {
                        setTimeout(() => {
                            wx.navigateBack({})
                        }, 1000)
                    }
                })
            }
        }).catch(res => {
            self.setData({
                isLoading: false,
                showHide: false
            })
        })
    },
    //输入框失去焦点
    inputBlur(e) {
        var _self = this
        // templateAreaFontLength
        var value = e.detail.value
        var index = e.currentTarget.dataset.num
        var item = e.currentTarget.dataset.item
        //计算限制长度
        //最大长度
        var maxLength = item.templateAreaFontLength
        if (value != '' && maxLength > 0) {
            value = value.substr(0, maxLength)
            //重新设置
            _self.data.areaList[index].templateAreaFontValue = value
            _self.data.areaList[index].focus = false
            _self.setData({
                areaList: _self.data.areaList
            })
        }
        //生成二维码图片
        if (item.templateAreaServerLink != '1') {
            _self.getFileId(index)
        }
    },
    // 选择图片
    choosePhoto: function (e) {
        var self = this
        var cur = e.currentTarget.dataset.cur
        var item = e.currentTarget.dataset.item
        console.log(cur)
        self.setData({
            currentIndex: cur
        })
        //宽高比
        var scale = item.templateAreaWidth / item.templateAreaHeight
        wx.chooseImage({
            count: 1,
            sourceType: ['album', 'camera'],
            success: function (res) {
                wx.navigateTo({
                    url: '/packagecard/common/corpperimg/cropperimg?imgSrc=' + res.tempFiles[0].path + '&scale=' + scale,
                })
            }
        })
    },
    doUploadImg(path) {
        var _self = this
        wx.showLoading({
            title: '上传中...',
        })
        app.http.upload(app.urls.doUploadImg, {
            path: path,
            name: 'image',
            header: {
                token: app.globalData.token
            }
        }).then(res => {
            wx.hideLoading({})
            res = JSON.parse(res)
            var change = 'areaList[' + _self.data.currentIndex + '].templateAreaFile'
            _self.setData({
                [change]: res.data
            })
            wx.showToast({
                title: '上传成功',
                duration: 1500
            })
            _self.setData({
                imgTempSrc: '',
                currentIndex: 0
            })
        }).catch(res => {
            _self.setData({
                imgTempSrc: '',
                currentIndex: 0
            })
        })
    },
    // 编辑信息
    editInfo: function (e) {
        var cur = e.currentTarget.dataset.num
        var text = e.detail.value
        var item = 'areaList[' + cur + '].templateAreaFontValue'
        this.setData({
            [item]: text
        })
    },
    // 点击内容区域获取input焦点
    giveFocus: function (e) {
        var cur = e.currentTarget.dataset.cur
        var item = 'areaList[' + cur + '].focus'
        this.setData({
            [item]: true
        })
    },
    // 非文字、图片区域换取fileid
    getFileId(e) {
        var self = this
        var cur = e
        var content = self.data.areaList[cur].templateAreaFontValue
        var areaId = self.data.areaList[cur].picturesTemplateAreaId
        app.http.get(app.urls.getFileId.format({
                picturesTemplateAreaId: areaId
            }), {
                header: {
                    token: app.globalData.token
                },
                data: {
                    content: content
                }
            }).then(res => {
                var change = 'areaList[' + cur + '].templateAreaFile'
                self.setData({
                    [change]: res.data.data
                })
            })
            .catch(err => {

            })
    },

})