// pages/mine/product/catalogmanage/catalogedit.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        catalogId: '', //id
        catalogLogoPhotos: [], //图标
        catalogSummary: '', //说明
        catalogName: '', //名称
        catalogOrder: 0, //排序
        typeList: [{
            name: '商品',
            id: '1',
            isSel: false
        }, {
            name: '优惠券',
            id: '2',
            isSel: false
        }],
        selType: '1'
    },
    // doUpdateCatalog getCatalogDetail
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            catalogId: options.catalogId
        })
        this.getCatalogDetail()
    },
    doSelType(e) {
        var item = e.currentTarget.dataset.item
        var _self = this
        _self.data.typeList.forEach(it => {
            if (it.id == item.id) {
                it.isSel = true
                _self.setData({
                    selType: item.id
                })
            } else {
                it.isSel = false
            }
        })
        _self.setData({
            typeList: _self.data.typeList
        })
    },
    //获取名称
    getCatalogDetail() {
        wx.showLoading({
            title: '加载中...',
        })
        let _self = this
        app.http.get(app.urls.getCatalogDetail.format({
                categoryId: _self.data.catalogId
            }), {
                header: {
                    token: app.globalData.token,
                    isThree: 'yes'
                }
            })
            .then(res => {
                wx.hideLoading({})
                var p = {
                    id: res.data.categoryLogo,
                    path: app.urls.baseImgUrl + res.data.categoryLogo
                }
                _self.data.catalogLogoPhotos.push(p)
                _self.setData({
                    catalogLogoPhotos: _self.data.catalogLogoPhotos, //图标
                    catalogSummary: res.data.categorySummary, //说明
                    catalogName: res.data.categoryName, //名称
                    catalogOrder: res.data.categorySort, //排序     
                    selType: res.data.coupon
                })
                _self.data.typeList.forEach(it => {
                    if (it.id == _self.data.selType) {
                        it.isSel = true
                    } else {
                        it.isSel = false
                    }
                })
                _self.setData({
                    typeList: _self.data.typeList
                })
            })
            .catch(err => {
                wx.hideLoading({})
            })
    },
    //预览Logo
    ViewLicImg() {
        var tempList = [e.currentTarget.dataset.url]
        wx.previewImage({
            urls: tempList,
            current: e.currentTarget.dataset.url
        });
    },
    DelLogoP(e) {
        let _self = this
        wx.showModal({
            title: '提示',
            content: '确定要删除该图片吗？',
            cancelText: '取消',
            confirmText: '确定',
            success: res => {
                if (res.confirm) {
                    _self.data.catalogLogoPhotos.splice(e.currentTarget.dataset.index, 1)
                    _self.setData({
                        catalogLogoPhotos: _self.data.catalogLogoPhotos
                    })
                }
            }
        })
    },
    //选择logo
    ChooseLogoP() {
        let _self = this
        wx.chooseImage({
            count: 1, //默认9
            sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], //从相册选择
            success: (res) => {
                res.tempFilePaths.forEach(element => {
                    _self.doUploadImg(2, element)
                });
            }
        });
    },
    //进行图片上传 type 1 营业执照   2 logo
    doUploadImg(type, path) {
        wx.showLoading({
            title: '上传中...',
        })
        let _self = this
        app.http.upload(app.urls.doUploadImg, {
                name: 'image',
                path: path,
                header: {
                    token: app.globalData.token
                }
            })
            .then(res => {
                wx.hideLoading({})
                var id = JSON.parse(res).data
                let pathStr = app.urls.baseImgUrl + id
                let item = {
                    id: id,
                    path: pathStr
                }
                let tempList = _self.data.catalogLogoPhotos.concat(item)
                _self.setData({
                    catalogLogoPhotos: tempList
                })
            })
            .catch(err => {
                wx.hideLoading({})
            })
    },
    //校验参数
    checkParams() {
        if (this.data.catalogName == '') {
            wx.showToast({
                title: '请输入类目名称',
                icon: 'none'
            })
            return false
        }
        if (this.data.catalogOrder == 0) {
            wx.showToast({
                title: '请输入类目排序',
                icon: 'none'
            })
            return false
        }
        if (this.data.catalogLogoPhotos.length <= 0) {
            wx.showToast({
                title: '请上传类目Logo',
                icon: 'none'
            })
        }

        return true
    },
    //添加类目
    doAdd() {
        let _self = this
        if (_self.checkParams()) {
            wx.showLoading({
                title: '修改中...',
            })
            app.http.put(app.urls.doUpdateCatalog.format({
                    categoryId: _self.data.catalogId
                }), {
                    header: {
                        isThree: 'yes',
                        token: app.globalData.token
                    },
                    data: {
                        categoryName: _self.data.catalogName, //名称
                        categorySort: _self.data.catalogOrder, //排序
                        categoryLogo: _self.data.catalogLogoPhotos.length > 0 ? _self.data.catalogLogoPhotos[0].id : '', //logo
                        categorySummary: _self.data.catalogSummary, //说明
                        coupon: '1'
                    }
                })
                .then(res => {
                    wx.hideLoading({})
                    wx.showToast({
                        title: '修改成功',
                        icon: 'success'
                    })
                    let arr = getCurrentPages()
                    let lastPage = (arr.length >= 2) ? arr[arr.length - 2] : undefined
                    lastPage.setData({
                        isAddPage: true
                    })
                    wx.navigateBack({})
                })
                .catch(err => {
                    wx.hideLoading({})
                })
        }
    },
    //监听输入
    inputWatch(e) {
        let key = e.currentTarget.dataset.model
        this.setData({
            [key]: e.detail.value
        })
    },
    catalogInput(e) {
        this.setData({
            catalogSummary: e.detail.value
        })
    }
})