// pages/welfareMall/staffManage/staffManage.js
import WelfareMall from '../../../api/welfareMall'
import { parseTime } from '../../../utils/util'
import {getMobileCache, getPhoneNumber as getPhoneNumberSync} from '../../../utils/user'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    lock: false,
    noResult: false,
    noMore: false,
    hidden: true,
    mobileTop: 'TONY WU',
    storeStaffList: [],
    storeInfo: '',
    storeName: '',
    storeNo: '',
    storePhone: '',
    inputName: '',
    inputPhone: '',
    ajxtrue: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     this.setData({
      isLogin: getMobileCache() != ''
     })
      if(this.data.isLogin){
        this.getStoreStaffList();
      } else {
        wx.switchTab({
          url:"/pages/welfareMall/personal/personal",
        })
      }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

   // 根据登录手机号获取职员列表
   getStoreStaffList: function() {
    WelfareMall.getStoreStaffList({
      mobile: getMobileCache(),
    }).then(res => {
        if (res.code == 200) {
            this.userStoreStaffListView(res.data)
        }
        this.data.lock = false
    }).catch(_ => {
        console.log(_)
        this.data.lock = false
    })
},

  userStoreStaffListView: function(data) {
    if (!Array.isArray(data) || data.length == 0) {
        console.log("职员列表数据为空");
        if (this.data.page == 1) {
            this.setData({
                mobileTop:getMobileCache(),
                noResult: true
            })
        } else {
            this.setData({
                mobileTop:getMobileCache(),
                noMore: true
            })
        }
        return
    }

    this.data.storeStaffList = data
    this.setData({
        mobileTop:getMobileCache(),
        storeStaffList: this.data.storeStaffList
    })
  }, 

  /**
   * 新增店员
   */
  toStaffAdd: function(e){
    this.setData({
         hidden: false,
         storeValue: e.currentTarget.dataset.storename,
         storeNo: e.currentTarget.dataset.storeno,
         storeName: '',
         storePhone: '',
    });
},

  /**
   * 取消
   */
  cancel: function(){
        this.setData({
          hidden: true,
          storeName: '',
          storePhone: '',
          inputName: '',
          inputPhone: '',
        });
    },
    /**
     * 提交
     */
    confirm: function(){
      let that = this
      var resName = this.data.inputName;
      var resPhone = this.data.inputPhone;
      if(resName == null || resName =='' || resName == undefined){
        wx.showToast({
          title: '店员名字必填',
          icon: 'none',
          duration: 1500
        })
      }else if(resPhone == null || resPhone =='' || resPhone == undefined){
        wx.showToast({
          title: '店员手机号码必填',
          icon: 'none',
          duration: 1500
        })
      } else if (!this.data.ajxtrue) {
        wx.showToast({
          title: '手机号有误,请重新输入',
          icon: 'none',
          duration: 1500
         })
       } else {
        this.setData({
          hidden: true
        });
        wx.showToast({
          title: '新增店员已提交',
          icon: 'none',
          duration: 1500,
          success: function(){
            that.goAddStaff(resName,resPhone);
          }
        });
       }
    },

    /**
     * 店员名称
     */
    getName: function(e){
      this.setData({
        inputName: e.detail.value
      })
    },

    /**
     * 店员手机号
     */
    getPhone: function(e){
      var phone = e.detail.value;
      if (!(/^1[3456789]\d{9}$/.test(phone))) {
        this.setData({
          ajxtrue: false,
          inputPhone: e.detail.value
        })
      } else {
        this.setData({
          ajxtrue: true,
          inputPhone: e.detail.value
        })
      }
    },

  /**
   * 
   * 提交人员信息
   */ 
  goAddStaff: async function(resName,resPhone) {
    var that = this;
    await WelfareMall.addStoreStaff({
      storeNo: that.data.storeNo,
      staffName: resName,
      staffMobile: resPhone,
    }).then(res => {
        if (res.code == 200) {
          that.cancel();
          that.getStoreStaffList();
        }
    }).catch(_ => {
        console.log(_)
    });
  },

  /**
   * 
   * 删除人员信息
   */ 
  toDelete: function(e) {
   let staffId = e.currentTarget.dataset.id;
   let that = this
    wx.showModal({
      title: '删除员工',
      content: '',
      showCancel: true,//是否显示取消按钮
      cancelText:"取消",//默认是“取消”
      cancelColor:'#00000',//取消文字的颜色
      confirmText:"确定",//默认是“确定”
      confirmColor: '#00000',//确定文字的颜色
      success: res => {if (res.cancel) {
            //点击取消,默认隐藏弹框
        } else {
            //点击确定 
            WelfareMall.delStoreStaff({
              mobile: getMobileCache(),
              staffId: staffId,
            }).then(res => {
                if (res.code == 200) {
                  that.cancel();
                  wx.showToast({
                    title: '员工已删除',
                    icon: 'none',
                    duration: 1500,
                    success: function(){
                      that.getStoreStaffList();
                    }
                  });
                }
                that.data.lock = false
            }).catch(_ => {
                console.log(_)
                that.data.lock = false
            })
        }
      },
      fail: function (res) {
        console.log(res)
       },//接口调用失败的回调函数 
    })
    
  },
})