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

let util = require('../../../../utils/util')
let apiArr = require('../../../../api/water_filter')
// packages/partner/pages/masterDesc/masterDesc.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sex:"man",
    areaList,
    show:false, //弹出层
    info_id:"",
    fileList:[],
    fileList2:[],
    phone:"",
    name:"",
    sex:"",
    Info:{

    },
  },

  lookAva(){
    let that = this
    wx.previewImage({
      urls: [that.data.Info.avatar],
    })
  },

  deleteImg(e){
    console.log(e);
    let that = this
    let fileList = that.data.fileList
    let fileList2 = that.data.fileList2
    fileList.splice(e.detail.index,1)
    fileList2.splice(e.detail.index,1)
    that.setData({
        fileList2,
        fileList,
    })
  },
  changeArea(){
    let that = this
    that.setData({
        show:true
    })
  },
  onClose(e){
    let that = this
    that.setData({
      show:false
    })
  },

  changeSex(e){
      let that = this
      let Info = that.data.Info
      Info.sex = e.currentTarget.dataset.sex
      that.setData({
        Info
      })
  },

  //获取详情
  getInfo(){
      let that = this
      wx.showLoading({
        title: '加载中...',
      })
      util.postUrl(apiArr.masterDesc,{info_id:Number(that.data.info_id)},res=>{
        wx.hideLoading()
        if(res.avatar){
            let url =  util.img_url + res.avatar
            let fileList = that.data.fileList
            let obj = {
                url:url,
                name:'avatar'
            }
            fileList.push(obj)
            
            that.setData({
                fileList,
                phone:res.phone,
                name:res.name,
                sex:res.sex,
            })
        }
        res.avatar = util.img_url + res.avatar
        that.setData({
            Info:res
        })
      })
  },

  afterRead(e){
    const { file } = e.detail;
    // 验证文件类型
    if (file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image') {
      wx.showToast({ title: '只能上传 JPG/PNG 格式的图片', icon: 'none' });
      return false;
    }
    // 验证文件大小
    if (file.size > 5 * 1024 * 1024) {
      wx.showToast({ title: '图片大小不能超过 5MB', icon: 'none' });
      return false;
    }
    return true;
  },
  beforeRead(e){
    console.log(e);
    let that = this
    util.uploadFileUrl(e.detail.file.url,(res)=>{
        let datas = JSON.parse(res)
        console.log(datas.data);
        let url = util.img_url + datas.data.path
        let fileList = that.data.fileList
        let fileList2 = that.data.fileList2
        let obj = {
            url:url,
            name:'avatar'
        }
        fileList.push(obj)

        let obj2 = {
            url:datas.data,
            name:'avatar'
        }
        fileList2.push(obj2)
        // let Info = that.data.Info
        that.setData({
            fileList,
            fileList2,
        })
        console.log(fileList,'sss');
    })
  },
  ipt1(e){
      let that = this
      let Info = that.data.Info
      Info.name = e.detail.value
      that.setData({
        Info
      })
  },
  ipt2(e){
    let that = this
      let Info = that.data.Info
      Info.phone = e.detail.value
      that.setData({
        Info
      })
},
ipt3(e){
    let that = this
    let Info = that.data.Info
    Info.address = e.detail.value
    that.setData({
      Info
    })
},
AreaConfirm(e){
    let that = this
    let area = ''
    let Info = that.data.Info
    if(e.detail.values[0].name == e.detail.values[1].name){
      area = e.detail.values[1].name +  e.detail.values[2].name
    }else{
      area = e.detail.values[0].name +  e.detail.values[1].name +  e.detail.values[2].name
    }
    Info.region = area

    that.setData({
      show:false,
      Info
    })
},

  //编辑师傅
  editMaster(){
    let that = this

    util.postUrl(apiArr.editMaster,{
        address:that.data.Info.address,
        avatar:that.data.fileList2[0].url,
        name:that.data.Info.name,
        email:that.data.Info.email,
        phone:that.data.Info.phone,
        region:that.data.Info.region,
        sex:that.data.Info.setData,
        info_id:Number(that.data.info_id)
    },res=>{
        console.log(res);
        if(res.message == '设置师傅成功'){
            that.setData({
                fileList:[],
                fileList2:[]
            })
            wx.showToast({
              title: '修改成功！',
              icon:"none"
            })

            setTimeout(()=>{
                that.getInfo()
            },1500)

        }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    that.setData({
        info_id:options.info_id
    })
    that.getInfo()
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})