
Page({

    /**
     * 页面的初始数据
     */
    data: {
        front           : null,
        back            : null,
        procedure       : 1,
        info            : {
            name        : '',
            id_card     : '',
            created_at  : '',
            need_sign   : '',
        },
        isSignContract  : false
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        wx.showLoading({
            title: "加载中...",
            mask : true
        })
        wx.$api.idcard.getInfo().then(res => {
            let { data } = res;
            wx.hideLoading()
            if(data.length == 0){
                this.setData({
                    procedure: 1
                })
                return
            }
            if(data != ''){
                this.setData({
                    procedure       : data.need_sign ? 2 : 3,
                    isSignContract  : data.is_sign_contract,
                    info            : {
                        name            : data.name,
                        id_card         : data.id_card,
                        created_at      : data.created_at,
                        verified        : data.verified,
                        need_sign       : data.need_sign,
                        address         : data.address
                    }
                })
            }
        })
    },
    /**
     * 上传身份证
     */
    onUpd(e){
        let { type } = e.currentTarget.dataset
        wx.chooseMedia({
            count       : 1,
            mediaType   : ['image'],
            success     : resPaths => {
                let { tempFiles } = resPaths;
                this.setData({
                    [type]: tempFiles[0].tempFilePath
                })
                wx.$api.idcard.updIdcard(tempFiles[0].tempFilePath, { type }).then(res => {
                    this.setData({
                        [type]: res
                    })
                })
            }
        })
    },
    /**
     * 提交身份证信息
     */
    onSubmitIdcard(e){
        wx.showLoading({
            title: '提交证件信息...'
            // mask : true
        })
        let { address } = e.detail.value
        wx.$api.idcard.ocr({
            front_card : this.data.front != null ? this.data.front.path : '',
            back_card  : this.data.back != null ? this.data.back.path : '',
            address
        }).then(res => {
            let { address, name, id_card, created_at, verified, need_sign, is_sign_contract } = res.data
            this.setData({
                info            : { name, id_card, created_at, verified, need_sign, address },
                procedure       : need_sign ? 2 : 3,
                isSignContract  : is_sign_contract,
            })
            wx.hideLoading()
        }).catch(err => {
            console.log(err)
        })
    },
    /**
     * 获取人脸认证地址
     */
    onGetSignUrl(){
        wx.showLoading({
            title: '获取地址信息...',
            mask : true
        })
        wx.$api.idcard.signUrl({
            path    : '/pages/idcard/idcard',
            query   :  "getSign=1",
            version : ''
        }).then(res => {
            let { url } = res.data;
            wx.navigateTo({
                url: "./signUrl/signUrl?url=" + encodeURIComponent(url.authUrl),
            })
            wx.hideLoading()
        })
    },
    /**
     * 开通会员获取签约地址
     */
    onGetOpenVip(){
        if(this.data.isSignContract){
            wx.navigateBack()
            return
        }
        wx.navigateTo({
            url: "./eSign/eSign",
        })
    }
})