// packageE/appointment/manager/stores_technician/stores_technician.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // store_id: '64',
    page: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.store_id) {
      this.setData({
        store_id: options.store_id,
      });
    }
    let info = wx.getStorageSync("yz_basic_info");
    if (info) {
      this.setData({
        appointmentLang: info.lang.appointment,
      });
      wx.setNavigationBarTitle({
        title: `门店${info.lang.appointment.worker}`,
      });
    }
    this.getTechnician();
  },
  deleteItem(e) {
    let id = e.currentTarget.dataset.item.id;
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.worker.delete-worker"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        worker_id: id,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
          this.getTechnician();
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
          console.log(res.msg);
        }
      },
    });
  },
  toAdd(e) {
    console.log(e);
    if (e.currentTarget.dataset.bj) {
      let store_id = e.currentTarget.dataset.item.store_id;
      let worker_id = e.currentTarget.dataset.item.id;
      wx.navigateTo({
        url:
          "/packageE/appointment/manager/add_technician?store_id=" +
          store_id +
          "&worker_id=" +
          worker_id,
      });
    } else {
      wx.navigateTo({
        url:
          "/packageE/appointment/manager/add_technician?store_id=" +
          this.data.store_id,
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getTechnician();
  },

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current_page >= this.data.last_page) {
      console.log("没有更多");
      return;
    } else {
      let pages = this.data.page + 1;
      this.setData({
        page: pages,
      });
      this.MoreGetTechnician();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getTechnician() {
    var that = this;
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.worker.get-list-by-store-id"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        store_id: that.data.store_id,
        page: that.data.page,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          that.setData({
            technicianList: res.data.data,
            current_page: res.data.current_page,
            last_page: res.data.last_page,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
          console.log(res.msg);
        }
      },
    });
  },
  MoreGetTechnician() {
    var that = this;
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.worker.get-list-by-store-id"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        store_id: that.data.store_id,
        page: that.data.page,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          that.setData({
            technicianList: res.data.data,
            current_page: res.data.current_page,
            last_page: res.data.last_page,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
          console.log(res.msg);
        }
      },
    });
  },
});
