// pages/mine/shop/shopauthentication.js
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        shopId: '',
        shopLicensePhotos: [], //店铺营业执照图片
        certificationName: '', //企业名称
        certificationType: '', //企业类型
        certificationNumber: '', //证书编号
        certificationId: '', //统一信用代码
        certificationComposition: '', //企业组成形式
        certificationLegalPerson: '', //法定代表人
        certificationBusiness: '', //企业营业范围
        certificationCapital: '', //企业注册资本
        certificationFoundTime: '', //企业注册时间
        certificationLicenseTerm: '', //营业期限
        certificationAddress: '', //企业地址
        certificationAuthority: '', //登记机关
    },
    // certificationImage: '', //照片
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            shopId: options.shopId
        })
    },
    ViewLicImg(e) {
        var tempList = [e.currentTarget.dataset.url]
        wx.previewImage({
            urls: tempList,
            current: e.currentTarget.dataset.url
        });
    },
    //选择营业执照
    ChooseLicenseP() {
        let _self = this
        wx.chooseImage({
            count: 1, //默认9
            sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], //从相册选择
            success: (res) => {
                res.tempFilePaths.forEach(element => {
                    _self.doUploadImg(1, element)
                });
            }
        });
    },
    //删除营业执照
    DelLicenseP(e) {
        let _self = this
        wx.showModal({
            title: '提示',
            content: '确定要删除该图片吗？',
            cancelText: '取消',
            confirmText: '确定',
            success: res => {
                if (res.confirm) {
                    this.data.shopLicensePhotos.splice(e.currentTarget.dataset.index, 1)
                    this.setData({
                        shopLicensePhotos: this.data.shopLicensePhotos
                    })
                    let _self = this
                    _self.setData({
                        certificationName: '', //企业名称
                        certificationType: '', //企业类型
                        certificationNumber: '', //证书编号
                        certificationId: '', //统一信用代码
                        certificationComposition: '', //企业组成形式
                        certificationLegalPerson: '', //法定代表人
                        certificationBusiness: '', //企业营业范围
                        certificationCapital: '', //企业注册资本
                        certificationFoundTime: '', //企业注册时间
                        certificationLicenseTerm: '', //营业期限
                        certificationAddress: '', //企业地址
                        certificationAuthority: '', //登记机关
                    })
                }
            }
        })
    },
    //进行图片上传 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 => {
                var id = JSON.parse(res).data
                let pathStr = app.urls.baseImgUrl + id
                let item = {
                    id: id,
                    path: pathStr
                }
                //营业执照
                let tempList = _self.data.shopLicensePhotos.concat(item)
                _self.getAuthInfo(tempList)
            })
            .catch(err => {
                wx.hideLoading({})
            })
    },
    getAuthInfo(templist) {
        let _self = this
        app.http.get(app.urls.getAuthenticationInfo.format({
                id: templist[0].id
            }), {
                header: {
                    token: app.globalData.token
                }
            })
            .then(res => {
                wx.hideLoading({})
                _self.setData({
                    shopLicensePhotos: templist,
                })
                _self.setDataToView(res.data)
            })
            .catch(err => {
                wx.hideLoading({})
            })
    },
    inputWatch(e) {
        let key = e.currentTarget.dataset.model
        this.setData({
            [key]: e.detail.value
        })
    },
    textAreaInput(e) {
        let key = e.currentTarget.dataset.model
        this.setData({
            [key]: e.detail.value
        })
    },
    setDataToView(data) {
        let _self = this
        _self.setData({
            certificationName: data.licenseName == '无' ? '' : data.licenseName, //企业名称
            certificationType: data.licenseType == '无' ? '' : data.licenseType, //企业类型
            certificationNumber: data.licenseNumber == '无' ? '' : data.licenseNumber, //证书编号
            certificationId: data.licenseId == '无' ? '' : data.licenseId, //统一信用代码
            certificationComposition: data.licenseComposition == '无' ? '' : data.licenseComposition, //企业组成形式
            certificationLegalPerson: data.licenseLegalPerson == '无' ? '' : data.licenseLegalPerson, //法定代表人
            certificationBusiness: data.licenseBusiness == '无' ? '' : data.licenseBusiness, //企业营业范围
            certificationCapital: data.licenseCapital == '无' ? '' : data.licenseCapital, //企业注册资本
            certificationFoundTime: data.licenseFoundTime == '无' ? '' : data.licenseFoundTime, //企业注册时间
            certificationLicenseTerm: data.licenseTerm == '无' ? '' : data.licenseTerm, //营业期限
            certificationAddress: data.licenseResidence == '无' ? '' : data.licenseResidence, //企业地址
            certificationAuthority: data.licenseAuthority == '无' ? '' : data.licenseAuthority, //登记机关
        })
    },
    //进行认证
    authShop() {
        let _self = this
        if (_self.checkParams()) {
            wx.showLoading({
                title: '认证中...',
            })
            app.http.post(app.urls.doAuthentication, {
                    header: {
                        token: app.globalData.token
                    },
                    data: {
                        certificationAddress: _self.data.certificationAddress,
                        certificationAuthority: _self.data.certificationAuthority,
                        certificationBusiness: _self.data.certificationBusiness,
                        certificationCapital: _self.data.certificationCapital,
                        certificationComposition: _self.data.certificationComposition,
                        certificationFoundTime: _self.data.certificationFoundTime,
                        certificationId: _self.data.certificationId,
                        certificationImage: _self.data.shopLicensePhotos[0].id,
                        certificationLegalPerson: _self.data.certificationLegalPerson,
                        certificationLicenseTerm: _self.data.certificationLicenseTerm,
                        certificationName: _self.data.certificationName,
                        certificationNumber: _self.data.certificationNumber,
                        certificationType: _self.data.certificationType,
                        shopId: _self.data.shopId
                    }
                })
                .then(res => {
                    wx.hideLoading({})
                    wx.showModal({
                        title: '提示',
                        content: '信息审核中,(1至3个工作日完成),请耐心等待.',
                        success(res) {
                            wx.navigateBack({
                                delta: 1,
                            })
                        }
                    })
                })
                .catch(err => {
                    wx.hideLoading({})
                })
        }
    },
    //校验参数
    checkParams() {
        if (this.data.shopLicensePhotos.length <= 0) {
            wx.showToast({
                title: '请上传营业执照相片',
                icon: 'none'
            })
            return false
        }
        // certificationName: '', //企业名称
        if (this.data.certificationName == '') {
            wx.showToast({
                title: '请输入企业名称',
                icon: 'none'
            })
            return false
        }
        // certificationType: '', //企业类型
        if (this.data.certificationType == '') {
            wx.showToast({
                title: '请输入企业类型',
                icon: 'none'
            })
            return false
        }
        // certificationNumber: '', //证书编号
        if (this.data.certificationNumber == '') {
            wx.showToast({
                title: '请输入证书编号',
                icon: 'none'
            })
            return false
        }
        // certificationId: '', //统一信用代码
        if (this.data.certificationId == '') {
            wx.showToast({
                title: '请输入统一信用代码',
                icon: 'none'
            })
            return false
        }
        // certificationComposition: '', //企业组成形式
        if (this.data.certificationComposition == '') {
            wx.showToast({
                title: '请输入企业组成形式',
                icon: 'none'
            })
            return false
        }
        // certificationLegalPerson: '', //法定代表人
        if (this.data.certificationLegalPerson == '') {
            wx.showToast({
                title: '请输入法定代表人',
                icon: 'none'
            })
            return false
        }
        // certificationBusiness: '', //企业营业范围
        if (this.data.certificationBusiness == '') {
            wx.showToast({
                title: '请输入企业营业范围',
                icon: 'none'
            })
            return false
        }
        // certificationCapital: '', //企业注册资本
        if (this.data.certificationCapital == '') {
            wx.showToast({
                title: '请输入企业注册资本',
                icon: 'none'
            })
            return false
        }
        // certificationFoundTime: '', //企业注册时间
        if (this.data.certificationFoundTime == '') {
            wx.showToast({
                title: '请输入企业注册时间',
                icon: 'none'
            })
            return false
        }
        // certificationLicenseTerm: '', //营业期限
        if (this.data.certificationLicenseTerm == '') {
            wx.showToast({
                title: '请输入营业期限',
                icon: 'none'
            })
            return false
        }
        // certificationAddress: '', //企业地址
        if (this.data.certificationAddress == '') {
            wx.showToast({
                title: '请输入企业地址',
                icon: 'none'
            })
            return false
        }
        // certificationAuthority: '', //登记机关
        if (this.data.certificationAuthority == '') {
            wx.showToast({
                title: '请输入登记机关',
                icon: 'none'
            })
            return false
        }
        return true
    },
})