// pages/groupmeallist/addOrEditaddress/addOrEditaddress.js
const cache = require('../../../utils/cache.js');
import address from '../../../api/address'
import base from '../../../utils/base'
Page({

    /**
     * 页面的初始数据
     */
    data: {

        mobile: '',
        addressId: '',
        name: '',
        gender: '2',
        phone: '',
        address: '',
        longitude: '',
        latitude: '',
        houseNumber: '',
        tagName: '家',





        tagList: [{
            value: '家',
            show: true,
            id: 0
        }, {
            value: '公司',
            show: false,
            id: 1
        }, {
            value: '学校',
            show: false,
            id: 2
        }, {
            value: '其他',
            show: false,
            id: 3
        }],
        tagName: '家'

    },

    name: function (e) {

        console.log("进入确定")
        let name = e.detail.value;
        this.data.lock = false;
        this.setData({
            name,
        })
    },



    phone: function (e) {
        console.log("进入确定")
        let phone = e.detail.value;
        this.data.lock = false;
        this.setData({
            phone,
        })
    },
    address: function (e) {
        console.log("进入确定")
        let address = e.detail.value;
        this.data.lock = false;
        this.setData({
            address,
        })
    },


    houseNumber: function (e) {
        console.log("进入确定")
        let houseNumber = e.detail.value;
        this.data.lock = false;
        this.setData({
            houseNumber,
        })
    },
    radioChange(e) {
        console.log('radio发生change事件，携带value值为：', e.detail.value)
        let gender = e.detail.value
        this.setData({
            gender: gender
        })
    },

    //地址保存
    async saveAddress() {

        let jw = cache.getNearLocation()
        let longitude = jw.longitude
        let latitude = jw.latitude
        console.log(this.data.addressId)

        await address.addoreditaddress({
            mobile: cache.getMobile(),
            addressId: this.data.addressId,
            name: this.data.name,
            gender: this.data.gender,
            phone: this.data.phone,
            address: this.data.address,
            longitude: longitude,
            latitude: latitude,
            houseNumber: this.data.houseNumber,
            tagName: this.data.tagName,
        }).then(res => {
            if (res.code == 200) {
                wx.redirectTo({
                    url: '../address/address',
                })
            }


        })




    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let type = options.type;
        let addressId = options.addressId;

        console.log(type +addressId)
        if (type == 'edit') {
            address.getddress({
                mobile: cache.getMobile(),
                addressId:addressId
            }).then(res => {
                console.log(res.data)

                this.data.tagList.forEach(item => {
                    if (item.value == res.data.tagName) {
                        item.show = true
                    } else {
                        item.show = false
                    }
                })
                this.setData({
                    tagList:this.data.tagList,
                    mobile:res.data.mobile,
                    addressId:res.data.id,
                    name:res.data.name,
                    gender:res.data.gender,
                    phone:res.data.phone,
                    address:res.data.address,
                    longitude:res.data.longitude,
                    latitude:res.data.latitude,
                    houseNumber:res.data.houseNumber,
                    tagName:res.data.tagName,
                })
            })

        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    //获取标签值
    getagValue(e) {
        let value = e.currentTarget.dataset.value;
        let index = e.currentTarget.dataset.id;
        this.data.tagList[index].show = !this.data.tagList[index].show
        this.data.tagList.forEach(item => {
            if (item.id == index) {
                item.show = true
            } else {
                item.show = false
            }
        })

        this.setData({
            tagName: value,
            tagList: this.data.tagList

        })

    },
    // 自动获取手机号

    getPhone() {
        let phone = cache.getMobile()
        this.setData({
            phone: phone
        })


    },


    // 选择地址
    goSelectMap() {
        var that = this;
        wx.authorize({
            scope: 'scope.userLocation',
            complete: function (res) {
                console.log(res)
                wx.chooseLocation({
                    success(str) {
                        console.log(str)
                        var key = 'V7DBZ-K7C22-SXXUJ-CDUE7-AM2LH-AEFCM';
                        //发送请求通过经纬度反查地址信息
                        var getAddressUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + str.latitude + "," + str.longitude + "&key=" + key + "&get_poi=1";
                        wx.request({
                            url: getAddressUrl,
                            success: function (result) {
                                var province = result.data.result.address_component.province; //省
                                var city = result.data.result.address_component.city; //市
                                var district = result.data.result.address_component.district; //区
                                var address = result.data.result.formatted_addresses.recommend; //具体地址范围
                                console.log('省市县:' + province + city + district)
                                console.log('地址：' + address)
                                that.setData({
                                    address: province + city + address
                                })
                            }
                        })
                    }
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

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

    },

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

    },

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

    },

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

    }
})