import { apiArr } from '../../api/user';
import apiArr2 from '../../api/water_filter';
let util = require('../../utils/util')

const __staticUrl = require('../../config/env.js').static_url
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        phoneNum: wx.getStorageSync('phone'),
        avatar: (__staticUrl + '/water_filter/partner/master_ava.png'),
        fileList: [],
        avatarUrl: "",
        avatarUrl2: "",
        isAva:false
    },

    renewalRecord(){
        wx.navigateTo({
          url: '/pages/renewalRecord/renewalRecord',
        })
    },
    SubscribeMessage(){
        wx.navigateTo({
          url: '../SubscribeMessage/SubscribeMessage',
        })
    },
    updateName(e) {
        console.log(e);
        let that = this
        that.setData({
            "userInfo.user_name": e.detail.value
        })
    },

    saveUserInfo() {
        let that = this
        util.postUrl()
    },
    chooseAvatar() {
        let that = this
        wx.showLoading({
            title: '上传中...',
        })
        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'],
            success: (res) => {
                const tempFilePath = res.tempFiles[0].tempFilePath;
                // 这里可以添加上传头像到服务器的逻辑
                util.uploadFileUrl(tempFilePath, (res) => {
                    console.log(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 userInfo = that.data.userInfo
                    let obj = {
                        url: url,
                        name: 'avatar'
                    }
                    fileList.push(obj)
                    userInfo.img = url2
                    that.setData({
                        fileList,
                        avatar: url,
                        avatarUrl2: url2,
                        userInfo
                    })
                    that.uploadAva()
                })
            }
        });
    },

    uploadAva() {
        wx.hideLoading()
        let that = this
        util.postUrl(apiArr.avatar, {
            avatar: that.data.avatarUrl2
        }, res => {
            wx.hideLoading()
            wx.showToast({
                title: '上传头像成功',
                icon: 'success',
                mask: true
            })
            wx.setStorageSync('userInfo', that.data.userInfo)
        })
    },


  

    //滤芯购买
    navigateToFilterPurchase() {
        wx.navigateTo({
            url: '/packages/WaterPurifier/pages/Buyfilter/Buyfilter',
        })
    },
    // 维修工单
    navigateToRepairOrder() {
        wx.navigateTo({
            url: '/packages/WaterPurifier/pages/Maintenance/Maintenance',
        })
    },

    // 滤芯更换记录
    navigateToFilterRecor() {
        wx.navigateTo({
            url: "/packages/WaterPurifier/pages/RechargeRecord/RechargeRecord"
        })
    },

    // 新装工单
    navigateToNewOrder() {
        wx.navigateTo({
            url: '/pages/newOrderList/newOrderList',
        })
    },

    // 添加设备
    addDevice() {
        wx.navigateTo({
            url: '/pages/deposit/deposit',
        })
    },

    // 充值记录
    RechargeRecord() {
        wx.navigateTo({
            url: '/pages/RechargeRecord/RechargeRecord',
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this

        that.setData({
            userInfo: wx.getStorageSync('userInfo')
        })
        if (wx.getStorageSync('userInfo').img) {
            that.setData({
                avatar: util.img_url + wx.getStorageSync('userInfo').img,
                isAva:true
            })
        }else{
            that.setData({
                isAva:false
            })
        }
    },




    /**
     * 退出登录
    */
    handleQuitLoginClick() {
        console.log('退出登录');
        wx.removeStorageSync('phone');
        wx.removeStorageSync('device_id');
        wx.removeStorageSync('dealerId');
        wx.removeStorageSync('is_deal');
        wx.removeStorageSync('is_dev');
        wx.removeStorageSync('is_shop');
        wx.removeStorageSync('openId');
        wx.removeStorageSync('shopArea');
        wx.removeStorageSync('shopId');
        wx.removeStorageSync('currentDevice');
        wx.removeStorageSync('loginMaster');

        wx.reLaunch({
            url: '/pages/newLogin/newLogin'
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})