import { areaList } from '../../data/index';

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

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

    /**
     * 页面的初始数据
     */
    data: {
        areaList,
        Info: {},
        sexOptions: ['男', '女'],
        sexIndex: "",
        avatarUrl: (__staticUrl + '/person/Group_309.png?2'),
        avatarUrl2: "",
        fileList: [],
        show: "",
    },
    // 选择头像
    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

                    that.setData({
                        "Info.deal_img": url,
                        "Info.deal_icon": url2
                    })
                })
            }
        });
    },
    changeLegal(e) {
        let that = this
        that.setData({
            "Info.legal": e.detail.value
        })
    },
    changeDealName(e) {
        let that = this
        that.setData({
            "Info.deal_name": e.detail.value
        })
    },
    changePhone(e) {
        let that = this
        that.setData({
            "Info.phone": e.detail.value
        })
    },
    changeCustomerPhone(e) {
        let that = this
        that.setData({
            "Info.customer_phone": e.detail.value
        })
    },
    changeAddress(e){
        let that = this
        that.setData({
            "Info.address": e.detail.value
        })
    },
    choseRegion() {
        let that = this
        that.setData({
            show: true
        })
    },
    changeAddress(e){
        let that = this
        that.setData({
            "Info.address": e.detail.value
        })
    },
    onClose() {
        let that = this
        that.setData({
            show: false
        })
    },
    AreaConfirm(e){
        let that = this
        let region 
        if(e.detail.values[0].name == e.detail.values[1].name){
            region = e.detail.values[0].name + e.detail.values[2].name
        }else{
            region = e.detail.values[0].name +  e.detail.values[1].name + e.detail.values[2].name
        }
        that.setData({
            "Info.region":region,
            show:false
        })
    },

    getAdminInfo() {
        let that = this
        util.postUrl4(apiArr.getPartner, { deal_id: wx.getStorageSync('admin').shop_id }, res => {
            console.log(res);
            res.deal_icon = res.deal_icon.startsWith('http') ? res.deal_icon : util.img_url + res.deal_icon
            that.setData({
                Info: res
            })
        })
    },
    saveMasterInfo(){
        let that = this
        wx.showLoading({
          title: '修改中...',
        })
        util.postUrl4(apiArr.editPartner,that.data.Info,res=>{
            wx.hideLoading()
            wx.showToast({
              title: '修改成功',
              icon:"none"
            })
            that.getAdminInfo()
        })
    },
    exit(){
        wx.removeStorageSync('atoken')
        wx.removeStorageSync('admin')
        wx.reLaunch({
          url: '/pages/newLogin/newLogin',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getAdminInfo()
    },

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

    },

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

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

    },

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

    },

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

    },

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

    },

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

    }
})