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


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

// packages/partner/pages/creatMaster/creatMaster.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList,
    show:false, //弹出层
    fileList:[],
    fileList2:[],
    sex:1,
    address:"",
    name:"",
    phone:"",
    region:"",
    admin:"",
  },

  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({
        fileList,
        fileList2
    })
},
  //姓名
  ipt1(e){
    let that = this
    that.setData({
        name:e.detail.value
    })
  },
  //电话
  ipt2(e){
    let that = this
    that.setData({
        phone:e.detail.value
    })
  },
  //详细地址
  ipt3(e){
    let that = this
    that.setData({
        address:e.detail.value
    })
  },
  //切换性别
  changeSex(e){
    let that = this
    that.setData({
        sex:e.currentTarget.dataset.sex
    })
  },

  getPhoncreateMastereByUser(){


      let that = this
      wx.showLoading({
        title: '创建中...',
        mask:true
      })

      if(!that.data.name){
          wx.showToast({
            title: '请填写师傅姓名',
            icon:"none"
          })
          return
      }
      if(!that.data.phone){
        wx.showToast({
          title: '请填写手机号',
          icon:"none"
        })
        return
    }
    if(!util.isPhone(that.data.phone)){
        wx.showToast({
          title: '请填写正确手机号',
          icon:"none"
        })
        return
    }
    if(!that.data.region){
        wx.showToast({
          title: '请选择所在区域',
          icon:"none"
        })
        return
    }
    if(!that.data.address){
        wx.showToast({
          title: '请填写详细地址',
          icon:"none"
        })
        return
    }

    util.postUrl(apiArr.getPhoneByUser,{
    phone:that.data.phone
    },res=>{
        wx.hideLoading()
        if(res.msg == '操作成功'){
            that.createMaster(res.user_id)
        }else{
            wx.hideLoading()
            wx.showToast({
            title: res.msg,
            icon:"none"
            })
        }
    })
  },

  createMaster(e){
      let that = this
        let avatar

        if(that.data.fileList2.length != 0){
            avatar = that.data.fileList2[0].url.path
        }

      util.postUrl(apiArr2.createMaster,{
        address:that.data.address,
        avatar,
        name:that.data.name,
        phone:that.data.phone,
        region:that.data.region,
        sex:Number(that.data.sex),
        dealer_id:that.data.admin.shop_id,
        m_user_id:e
      },res=>{
          console.log(res);
          const {msg} = res
          wx.hideLoading()
          if(res.msg == '操作成功'){
              wx.showToast({
                title: res.message,
              })

              setTimeout(()=>{
                  wx.navigateBack({
                    delta:1
                  })
              },1500)

          }else{
              wx.showToast({
                title: res.error,
                icon:"none"
              })
          }
      })
  },

  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({
        region,
        show:false
    })
  },


  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'
        }
        let obj2 = {
            url:datas.data,
            name:'avatar'
        }
        fileList.push(obj)
        fileList2.push(obj2)
        that.setData({
            fileList,
            fileList2
        })
    })
  },


  changeArea(){
    let that = this
    that.setData({
        show:true
    })
  },
  onClose(e){
    let that = this
    that.setData({
      show:false
    })
  },

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

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})