let util = require('../../../utils/util')
let apiArr = require('../../../api/water_filter')

// packages/master/masterInfo/index.js
const __staticUrl = require('../../../config/env.js').static_url
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        sexOptions: ['男', '女'],
        sexIndex: "",
        avatarUrl:(__staticUrl + '/person/Group_309.png?2'),
        avatarUrl2:"",
        fileList:[],
    },
   // 选择头像
   chooseAvatar() {
    let that = this
    wx.chooseMedia({
        count: 1,
        mediaType: ['image'],
        sourceType: ['album', 'camera'],
        success: (res) => {
            const tempFilePath = res.tempFiles[0].tempFilePath;
            // 这里可以添加上传头像到服务器的逻辑
            util.uploadFileUrl(tempFilePath, (res) => {
                let datas = JSON.parse(res)
                console.log(datas.data);
                let url = util.img_url + datas.data.path
                let url2 =  datas.data.path
                let fileList = that.data.fileList
                let obj = {
                    url: url,
                    name: 'avatar'
                }
                fileList.push(obj)
                that.setData({
                    fileList,
                    avatarUrl: url,
                    avatarUrl2:url2
                })
            })
        }
    });
},
    changeSex(e) {
        const sexIndex = e.detail.value;
        this.setData({
            sexIndex,
            'userInfo.sex': Number(sexIndex) + 1 // 更新性别值
        });
        // 这里可以添加保存性别到服务器的逻辑
    },

    getMasterInfo() {
        let that = this
        util.postUrl(apiArr.masterInfo, {
            user_id: wx.getStorageSync('userId')
        }, res => {
            wx.setStorageSync('master', res)
            that.setData({
                userInfo: res,
                sexIndex: res.sex - 1
            })

            if (res.avatar) {
                that.setData({
                    avatarUrl: util.img_url + res.avatar
                })
            }

        })
    },

    saveMasterInfo() {
        let that = this
        util.postUrl(apiArr.editMasterInfo,{
            info_id:that.data.userInfo.info_id,
            name:that.data.userInfo.name,
            sex:that.data.userInfo.sex,
            region:that.data.userInfo.region,
            address:that.data.userInfo.address,
            avatar:that.data.avatarUrl2,
            phone:that.data.userInfo.phone
        },res=>{
            console.log(res);
            if(res.msg == '操作成功'){
                wx.showToast({
                  title: '修改成功！',
                })
                setTimeout(()=>{
                    wx.navigateBack({
                        delta:1
                    })
                },1500)
            }else{
                wx.showToast({
                    title: res.msg,
                    icon:"none"
                  })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let that = this
        that.getMasterInfo()
    },

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

    },

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

    },

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

    },

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

    },

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

    }
})