// packageF/debt/debtapply/debtapply.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        checked: true,
        sexShow: false,
        showAdd: false,
        // 1债权人 2债务人 3VIP
        member_type: 1,
        // 身份证号
        identity_number: '',
        // 身份正面图片
        identity_front_image: '',
        // 身份反面图片
        identity_back_image: '',
        // 姓名，创建VIP不传
        name: '',
        // 1男 2女，创建VIP不传
        sex: '1',
        // 常住省份ID，创建VIP不传
        live_province_id: '',
        // 常住市ID，创建VIP不传
        live_city_id: '',
        // 常住区ID，创建VIP不传
        live_district_id: '',
        // 常住街道ID，创建VIP不传
        live_street_id: '',
        // 常住地址，创建VIP不传
        live_address: '',
        // 户籍省份ID，创建VIP不传
        source_province_id: '',
        // 户籍市ID
        source_city_id: '',
        // 户籍区ID，创建VIP不传
        source_district_id: '',
        // 户籍街道ID，创建VIP不传
        source_street_id: '',
        // 户籍地址，创建VIP不传
        source_address: '',
        // 和解协议图片数组json化字符串，创建VIP不传
        agreement_pics: '',
        // 补充材料图片数组json化字符串，创建VIP不传
        another_pics: '',
        // 营业执照图片数组json化字符串，创建债权人才传
        business_pics: '',

        showAddTwo: false,
        // 债务人会员ID，创建债权人时必传	
        debtor_uid: '',
        // 常驻区域名
        addressOne: '',
        // 户籍区域名
        addressTwo: '',
        // 户籍街道开关
        streetShowtwo: false,
        // 户籍街道
        streettwo: '',
        //街道开关
        streetShow: false,
        street: '',
        districtVal: [],
        // 身份正面显示图片
        identity_front_image_show: "",
        // 身份反面显示图片
        identity_back_image_show: "",
        settingInfo: [],
        // 协议弹窗
        descShow: false,
        Vip_uid: [],
        showUserC: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.type) {
            this.setData({
                member_type: options.type
            })
        }
        wx.setNavigationBarTitle({
            title: this.data.member_type == 1 ? '债权人申请' : this.data.member_type == 2 ? '债务人申请' : 'VIP申请',
        })
        if (this.data.member_type == 3) {
            // 请求联名消费债权人信息
            this.getVipUser()
        }
    },
    tapDescOpen() {
        this.setData({
            descShow: true
        })
    },
    sdescShowClose() {
        this.setData({
            descShow: false
        })
    },
    selectSex(e) {
        console.log(e, 'aaaaaaa')
        let ind = e.currentTarget.dataset.ind;
        this.setData({
            sex: ind
        })
    },
    delIntu_1() {
        // 身份证正面删除
        this.setData({
            identity_front_image_show: '',
            identity_front_image: ''
        })
    },
    delIntu_2() {
        // 身份证正面删除
        this.setData({
            identity_back_image_show: '',
            identity_back_image: ''
        })
    },
    updateImages(e) {
        if (e.currentTarget.dataset.key == 'agreement') {
            this.setData({
                agreement_pics: e.detail
            })
        } else if (e.currentTarget.dataset.key == 'getIMgbusiness') {
            this.setData({
                business_pics: e.detail
            })
        } else if (e.currentTarget.dataset.key == 'getIMganother') {
            this.setData({
                another_pics: e.detail
            })
        }
    },
    openSex() {
        this.setData({
            sexShow: true
        })
    },
    sexShowClose() {
        this.setData({
            sexShow: false
        })
    },
    onChange(event) {
        this.setData({
            checked: event.detail,
        });
    },
    getIMg(e) {
        this.setData({
            identity_front_image: e.detail[0]
        })
    },
    getIMgF(e) {
        this.setData({
            identity_back_image: e.detail[0]
        })
    },
    getIDcardZ(e) {
        this.setData({
            identity_front_image_show: e.detail[0]
        })
    },
    getIDcardF(e) {
        this.setData({
            identity_back_image_show: e.detail[0]
        })
    },
    tapShowAdd(e) {
        if (e.currentTarget.dataset.key == "two") {
            this.setData({
                showAddTwo: true
            })
        } else {
            this.setData({
                showAdd: true
            })
        }

    },
    resultAdd(e) {
        let district = e.detail;
        if (e.currentTarget.dataset.key == 'two') {
            this.setData({
                addressTwo: `${district[0].name}${district[1].name}${district[2].name}`,
                source_province_id: district[0].id,
                source_city_id: district[1].id,
                source_district_id: district[2].id,
            })
        } else {
            this.setData({
                addressOne: `${district[0].name}${district[1].name}${district[2].name}`,
                live_province_id: district[0].id,
                live_city_id: district[1].id,
                live_district_id: district[2].id,
            })
        }
    },
    tapStreetShow(e) {
        if (e.currentTarget.dataset.key == 'two') {
            if (!this.data.source_district_id) return app.tips('请先选择户籍所在区域')
            this._getStreet(this.data.source_district_id, 'two')
        } else {
            if (!this.data.live_district_id) return app.tips('请先选择所在区域')
            this._getStreet(this.data.live_district_id, 'one')
        }
    },
    _getStreet(id, key) {
        var that = this;
        let urlStr = app.getNetAddresss("member.memberAddress.street");
        urlStr += "&district_id=" + id;
        app._getNetWork({
            url: urlStr,
            success: (resdata) => {
                var res = resdata.data;
                if (res.result == 1 && !app._isTextEmpty(res.data)) {
                    if (!app._isTextEmpty(res.data) && res.data.length > 0) {
                        if (key == 'one') {
                            this.setData({
                                streetShow: true
                            })
                        } else {
                            this.setData({
                                streetShowtwo: true
                            })
                        }
                        that.setData({
                            districtVal: res.data,
                        });
                    }
                }
            },
            fail: function (res) {
                console.log(res);
            },
        });
    },
    //选择街道
    streetConfirm(e) {
        let strname = e.target.dataset.strname;
        console.log(e)
        if (e.currentTarget.dataset.key == 'one') {
            this.setData({
                street: strname,
                streetShow: false,
                live_street_id: e.currentTarget.dataset.id
            });
        } else {
            this.setData({
                streettwo: strname,
                streetShowtwo: false,
                source_street_id: e.currentTarget.dataset.id
            });
        }
    },
    streetClose() {
        this.setData({
            streetShowtwo: false,
            streetShow: false
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getData();
    },

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

    },

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

    },

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

    },

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

    },

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

    },
    checkPost() {

        if (this.data.member_type == 1 || this.data.member_type == 2) {
            if (app._isTextEmpty(this.data.name)) {
                app.tips('请输入姓名')
                return false
            }
            if (app._isTextEmpty(this.data.sex)) {
                app.tips('请选择性别')
                return false
            }
            if (app._isTextEmpty(this.data.live_province_id) || app._isTextEmpty(this.data.live_city_id) || app._isTextEmpty(this.data.live_district_id)) {
                app.tips('请选择常住区域')
                return false
            }
            if (app._isTextEmpty(this.data.source_province_id) || app._isTextEmpty(this.data.source_city_id) || app._isTextEmpty(this.data.source_district_id)) {
                app.tips('请选择户籍区域')
                return false
            }
            if (app._isTextEmpty(this.data.agreement_pics)) {
                app.tips('请上传和解协议')
                return false
            }
            if (app._isTextEmpty(this.data.another_pics)) {
                app.tips('请上传补充资料')
                return false
            }
            if (this.data.settingInfo.is_street) {
                // 是否开启了街道
                if (app._isTextEmpty(this.data.live_street_id)) {
                    app.tips('请选择常住街道')
                    return false
                }
                if (app._isTextEmpty(this.data.source_street_id)) {
                    app.tips('请选择户籍街道')
                    return false
                }
            }

        }
        if (this.data.member_type == 2) {
            // 营业执照只有债务人才传
            if (app._isTextEmpty(this.data.business_pics)) {
                app.tips('请上传营业执照')
                return false
            }
        }
        if (this.data.member_type == 1) {
            // 只有债权人才传
            if (app._isTextEmpty(this.data.debtor_uid)) {
                app.tips('请输入债务人会员ID')
                return false
            }
        }

        if (app._isTextEmpty(this.data.identity_number)) {
            app.tips('请输入身份证号码')
            return false
        }
        if (app._isTextEmpty(this.data.identity_front_image)) {
            app.tips('请上传身份证图片')
            return false
        }
        if (app._isTextEmpty(this.data.identity_back_image)) {
            app.tips('请上传身份证图片')
            return false
        }
        if(!app._isTextEmpty(this.data.settingInfo.apply_desc)){
            if (!this.data.checked) {
                app.tips('请阅读勾选申请协议')
                return false
            }
        }
        
        return true
    },
    submit() {
        let check = this.checkPost();
        if (!check) {
            return
        }
        let json = {
            member_type: this.data.member_type,
            identity_number: this.data.identity_number,
            identity_front_image: this.data.identity_front_image,
            identity_back_image: this.data.identity_back_image,
        };
        if (this.data.member_type == 2 || this.data.member_type == 1) {
            // 债务人传参
            json.name = this.data.name;
            json.sex = this.data.sex;
            json.live_province_id = this.data.live_province_id;
            json.live_city_id = this.data.live_city_id;
            json.live_city_id = this.data.live_city_id;
            json.live_district_id = this.data.live_district_id;
            json.live_street_id = this.data.live_street_id;
            json.live_address = this.data.live_address;
            json.source_province_id = this.data.source_province_id;
            json.source_city_id = this.data.source_city_id;
            json.source_district_id = this.data.source_district_id;
            json.source_street_id = this.data.source_street_id;
            json.source_address = this.data.source_address;
            json.agreement_pics = JSON.stringify(this.data.agreement_pics);
            json.another_pics = JSON.stringify(this.data.another_pics);
        }
        if(this.data.member_type == 2){
          json.business_pics = JSON.stringify(this.data.business_pics);
        }
        if (this.data.member_type == 1) {
            // 债权人传参
            json.debtor_uid = this.data.debtor_uid;
        }
        console.log('json ', json)
        let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.apply.apply');
        app._getNetWork({
            url: urlStr,
            data: json,
            success: (resdata) => {
                var res = resdata.data;
                if (res.result == 1) {
                    if (this.data.member_type == 3) {
                        wx.showModal({
                            title: '',
                            content: '您已提交申请VIP，等待商家审核',
                            confirmText: '知道了',
                            showCancel: false,
                            success: (res) => {
                                if (res.confirm) {
                                    // console.log('用户点击确定')
                                    wx.navigateBack({
                                        delta: 1,
                                    })
                                }
                            }
                        })
                    } else {
                        wx.showToast({
                            title: '申请成功',
                            icon: 'none',
                            duration: 1000,
                            success: (res) => {
                                wx.navigateBack({
                                    delta: 1,
                                })
                            }
                        })
                    }

                } else {
                    wx.showToast({
                        icon: 'none',
                        title: res.msg,
                        duration: 1500
                    });
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
    },
    searchInp() {
        let urlStr = app.getNetAddresss('member.member.memberInfo');
        app._getNetWork({
            url: urlStr,
            data: {
                uid: this.data.debtor_uid
            },
            success: (resdata) => {
                var res = resdata.data;
                if (res.result == 1) {
                    console.log(res, 'aaaaasc::::')
                    this.setData({
                        showUserC: res.data
                    })
                } else {
                    wx.showToast({
                        icon: 'none',
                        title: res.msg,
                        duration: 1500,
                    });
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
    },
    getVipUser() {
        let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debt-member.memberParentInfo');
        app._getNetWork({
            url: urlStr,
            showToastIn:true,
            success: (resdata) => {
                var res = resdata.data;
                if (res.result == 1) {
                    console.log(res, 'aaaaasc::::')
                    this.setData({
                        Vip_uid: res.data
                    })
                } else {
                    wx.showToast({
                        icon: 'none',
                        title: res.msg,
                        duration: 1500,
                        success: (res) => {
                            setTimeout(() => {
                                wx.navigateBack({
                                    delta: 1,
                                })
                            }, 1000);
                        }
                    });
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
    },
    getData() {
        let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debt-member.applyData');
        app._getNetWork({
            url: urlStr,
            data: {
                member_type: this.data.member_type
            },
            success: (resdata) => {
                var res = resdata.data;
                if (res.result == 1) {
                    this.setData({
                        settingInfo: res.data
                    })
                    let data = res.data;
                    if (this.data.member_type == 2) {
                        if (data.debtor_goods_count == 0) {
                            wx.showModal({
                                title:'请先购买指定商品',
                                success:function(res){
                                    if (res.confirm) {
                                       // 先去购买商品
                                        wx.navigateTo({
                                            url: '/packageA/detail_v2/detail_v2?id=' + data.debtor_goods_id,
                                        })
                                    } else if (res.cancel) {
                                        wx.redirectTo({
                                            url: '/packageF/debt/debtindex/debtindex',
                                        })
                                    }
                                }
                            })
                            return
                        }
                        if (data.is_supplier) {
                            wx.showModal({
                                title: '',
                                content: '您是供应商,暂时无法申请成为债务人',
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        wx.redirectTo({
                                            url: '/packageF/debt/debtindex/debtindex',
                                        })
                                    }
                                }
                            })
                            return
                        }
                        if (data.is_vip) {
                            wx.showModal({
                                title: '',
                                content: '您是VIP,暂时无法申请成为债务人',
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        wx.redirectTo({
                                            url: '/packageF/debt/debtindex/debtindex',
                                        })
                                    }
                                }
                            })
                            return
                        }
                    }
                    if (this.data.member_type == 1) {
                        if (data.owner_goods_count == 0) {
                            wx.showModal({
                                title:'请先购买指定商品',
                                success:function(res){
                                    if (res.confirm) {
                                       // 先去购买商品
                                        wx.navigateTo({
                                            url: '/packageA/detail_v2/detail_v2?id=' + data.owner_goods_id,
                                            fail:function(err){
                                                console.log(err)
                                                wx.redirectTo({
                                                    url: '/packageA/detail_v2/detail_v2?id=' + data.owner_goods_id,
                                                })
                                            }
                                        })
                                    } else if (res.cancel) {
                                        wx.redirectTo({
                                            url: '/packageF/debt/debtindex/debtindex',
                                        })
                                    }
                                }
                            })
                            return
                        }
                        if (data.is_vip) {
                            wx.showModal({
                                title: '',
                                content: '您是VIP,暂时无法申请成为债权人',
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        wx.redirectTo({
                                            url: '/packageF/debt/debtindex/debtindex',
                                        })
                                    }
                                }
                            })
                            return
                        }
                    }
                    if (this.data.member_type == 3) {
                        if (data.vip_goods_count == 0) {
                            wx.showModal({
                                title:'请先购买指定商品',
                                success:function(res){
                                    if (res.confirm) {
                                        // 先去购买商品
                                        wx.navigateTo({
                                            url: '/packageA/detail_v2/detail_v2?id=' + data.vip_goods_id,
                                        })
                                    } else if (res.cancel) {
                                        wx.redirectTo({
                                            url: '/packageF/debt/debtindex/debtindex',
                                        })
                                    }
                                }
                            })
                            return
                        }
                        if (data.is_owner == 1 || data.is_debtor) {
                            wx.showModal({
                                title: '',
                                content: '您已是债务人/债权人,暂时无法申请成为VIP',
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        wx.redirectTo({
                                            url: '/packageF/debt/debtindex/debtindex',
                                        })
                                    }
                                }
                            })
                        }
                    }
                } else {
                    wx.showToast({
                        icon: 'none',
                        title: res.msg,
                        duration: 1500
                    });
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
    }
})