// packageE/appointment/technician/work_day/TechnicianTime.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    value: "2018-11-11",
    week: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
    lastMonth: "lastMonth",
    nextMonth: "nextMonth",
    selectVal: "",
    store_id: "",
    worker_id: "",
    // 门店弹窗
    showStore: false,
    showDefault: false,
    show1: false,
    // 工作开始时间和结束时间
    choose_end: "00:00",
    choose_start: "00:00",
    isDefault: 0,
    even_true: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    if(options.worker_id){
      this.setData({
        worker_id: options.worker_id,
      });
    }
    if (options.item) {
      let info = JSON.parse(options.item);
      this.setData({
        store_id: info.id,
      });
      let item = {
        id: info.id,
        store_name: info.name,
      };
      this.choose(item, "1");
      // this.getAllService()
    } else {
      this.setData({
        even_true: true,
      });
    }
  },
  //选择门店弹窗
  getStore(e) {
    console.log(e);
    this.setData({
      showStore: true,
    });
    this.getStore();
  },
  showStoreCloseBtn() {
    this.setData({
      showStore: false,
    });
  },
  // 门店选择弹窗
  // choose(e) {
  //   let datas = e.currentTarget.dataset.item;
  //   this.choose(datas)
  // },
  // 门店点击事件
  choose(item, key) {
    console.log(item);
    if (key) {
      this.setData({
        chooseStore: item,
        showStore: false,
      });
    } else {
      this.setData({
        chooseStore: item.currentTarget.dataset.item,
        showStore: false,
        even_true: false,
      });
    }

    console.log(this.data.chooseStore);
    console.log(this.data.chooseStore.id);
    // this.chooseStore = item;
    // this.showStore = false;
    this.getDefaultTime(); // 获取门店默认工作时间
    this.getAllService(); // 获取门店全部预约时间
    this.getService(); // 获取门店今天预约时间
  },
  // 工作时间弹窗 （默认）
  addDefaultTime() {
    this.setData({
      showDefault: true,
      isDefault: 1,
    });
    console.log(this.data.showDefault);
  },
  showDefaultCloseBtn() {
    this.setData({
      show1: false,
    });
  },
  // 添加工作时间 （按天—）
  addTime(e) {
    this.setData({
      showDay: true,
      isDefault: 2,
    });
  },
  // 编辑工作时间弹窗
  editTime(e) {
    let item = e.currentTarget.dataset.item;
    if (item) {
      if (item.work_hour_start) {
        this.setData({
          choose_start: item.work_hour_start,
          choose_end: item.work_hour_end,
          isDefault: 0,
          period_id: item.id,
        });
      }
    }

    this.setData({
      show1: true,
      showDay: false,
      showDefault: false,
    });
  },
  // 编辑工作时间   ----选择开始时间
  bindTimeChange(e) {
    this.setData({
      choose_start: e.detail.value,
    });
  },
  // 编辑工作时间   ----选择结束时间
  bindTimeEndChange(e) {
    this.setData({
      choose_end: e.detail.value,
    });
  },
  // 确认提交时间
  submitTime(e) {
    console.log(this.data.worker_id);
    let json = {};
    let url = "";
    if (this.data.isDefault === 1) {
      // 添加默认事件
      url = "plugin.appointment.frontend.work-time.add-default-period";
      json = {
        store_id: this.data.chooseStore.id,
        worker_id: this.data.worker_id,
        work_hour_start: this.data.choose_start,
        work_hour_end: this.data.choose_end,
      };
    } else if (this.data.isDefault == 2) {
      // 按天添加时间
      url = "plugin.appointment.frontend.work-time.add-date-period";
      json = {
        store_id: this.data.chooseStore.id,
        worker_id: this.data.worker_id,
        work_hour_start: this.data.choose_start,
        work_hour_end: this.data.choose_end,
        work_date: this.data.today,
      };
    } else {
      // 编辑时间
      url = "plugin.appointment.frontend.work-time.edit-date-period";
      json = {
        period_id: this.data.period_id,
        work_hour_start: this.data.choose_start,
        work_hour_end: this.data.choose_end,
      };
    }
    let that = this;
    let urlStr = app.getNetAddresss(url);
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res.data);
          that.setData({
            show1: false,
            choose_start: "",
            choose_end: "",
          });
          if (that.data.isDefault === 1) {
            this.getDefaultTime();
          } else {
            this.getService();
          }
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      },
    });
  },
  // 删除工作事件
  deleteTime(e) {
    let date = e.currentTarget.dataset.date;
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.work-time.del-default-period"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        time_id: date.id,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          that.getDefaultTime();
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      },
    });
  },
  changeStatus(e) {
    let item = e.currentTarget.dataset.item;
    let str = e.currentTarget.dataset.str;
    let status = 0;
    str === "rest" ? (status = 1) : (status = 0);
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.work-time.change-status-by-id"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        period_id: item.id,
        status: status,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          that.getService();
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      },
    });
  },
  // 上班休息状态
  changeDayStatus(e) {
    console.log(e);
    let str = e.currentTarget.dataset.item;
    let status = 0;
    str === "rest" ? (status = 1) : (status = 0);
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.work-time.change-status-by-date"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        store_id: that.data.chooseStore.id,
        worker_id: that.data.worker_id,
        work_date: that.data.today,
        status: status,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          that.getService();
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      },
    });
  },
  /**
   *
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  // 日历组件点击事件
  select(e) {
    console.log(e);
    let touchDay = e.detail.work_date;
    this.setData({
      today: touchDay,
    });
    this.getService();
  },
  getAllService() {
    var that = this;
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.work-time.get-worker-dates-by-store-id"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        store_id: this.data.chooseStore.id,
        worker_id: this.data.worker_id,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res.data);
          let list = res.data;
          let lis = [];
          list.forEach((item, index, arr) => {
            lis.push(item.work_date_format);
          });
          that.setData({
            events: lis,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
          console.log(res.msg);
        }
      },
    });
  },
  getService() {
    var that = this;
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.work-time.get-period-list-by-date"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        store_id: that.data.chooseStore.id,
        worker_id: that.data.worker_id,
        work_date: this.data.today,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res.data);
          that.setData({
            day_time: res.data.period_list,
            isRest: res.data.worker_status,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
          console.log(res.msg);
        }
      },
    });
  },
  // 点击请求门店信息
  getStore(e) { // eslint-disable-line
    var that = this;
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.store.get-list-by-worker-uid"
    );
    app._getNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res.data);
          that.setData({
            store: res.data,
            showStore: true,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      },
    });
  },
  getDefaultTime(e) {
    console.log(this.data.chooseStore.id);
    var that = this;
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.work-time.get-default-period"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        store_id: this.data.chooseStore.id
          ? this.data.chooseStore.id
          : this.data.store_id,
        worker_id: this.data.worker_id,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res.data);
          that.setData({
            default_time: res.data,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      },
    });
  },
});
