const _my = require("../../__antmove/api/index.js")(my);
const wx = _my;
// pages/regular_invite/regular_invite.js
import international from "../../international/appointment_scence/index";
const {
  userWhiteInviteUserVisitor,
  userWhiteGetSnList,
  getVisitoryByUserVisitorListDetailId
} = require("../../utils/api/api");
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    international: international,
    //国际化语言包
    language: app.data.language,
    machineList: [],
    // 通道权限列表--设备列表
    // 人员类型列表
    personTypeList: [{
      id: 0,
      name_ch: "访客",
      name_en: "visitors"
    }, {
      id: 1,
      name_ch: "同事",
      name_en: "colleagues"
    }, {
      id: 2,
      name_ch: "亲属",
      name_en: "relatives"
    }],
    isRealNameList: [
    //   {
    //   id: 0,
    //   name_ch: "微信",
    //   name_en: "WeChat"
    // }, 
    {
      id: 1,
      name_ch: "短信",
      name_en: "SMS"
    }],
    // 拜访事由列表
    reasonList: [{
      name: "业务洽谈"
    }, {
      name: "会议邀请"
    }, {
      name: "施工安装"
    }, {
      name: "探亲访友"
    }, {
      name: "工作检查"
    }, {
      name: "面试邀请"
    }, {
      name: "快递外卖"
    }, {
      name: "家政保洁"
    }],
    form: {},
    //提交的表单
    chooseSN: false,
    snListLength: null,
    isDisabled: true,
    showAddBut: true,
    closeNum: false
  },
  //获取开始时间
  getValue(e) {
    const {
      form
    } = this.data;
    switch (e.currentTarget.dataset.type) {
      case "snList":
        this.setData({
          "form.snList": e.detail,
          snListLength: e.detail.length,
          chooseSN: false
        });
        break;
      case "reason":
        this.setData({
          "form.reason": e.detail.name
        });
        break;
      case "personnelType":
        this.setData({
          "form.personnelType": e.detail
        });
        break;
      case "invitationWay":
        this.setData({
          "form.invitationWay": e.detail
        });
        if (e.detail == 0) {
          this.setData({
            showAddBut: false
          });
        } else {
          this.setData({
            showAddBut: true
          });
        }
        break;
      default:
        form[e.currentTarget.dataset.type] = e.detail;
        break;
    }
    this.matchComplete();
  },
  // 判断用户是否填写完整
  matchComplete() {
    let form = this.data.form;
    let isDisabled = form.startTime && form.endTime && form.snList && form.snList.length > 0 && form.reason && form.phoneList && form.phoneList.length > 0 ? false : true;
    this.setData({
      isDisabled: isDisabled
    });
  },
  //  立即邀请
  gotoInvited() {
    const that = this;
    return new Promise((reslove, reject) => {
      let {
        phoneList,
        snList
      } = that.data.form;
      that.data.form.userWhitelistId = app.data.adminInfo.userWhitelistId;
      if (phoneList && snList) {
        userWhiteInviteUserVisitor(that.data.form).then(res => {
          if (res.code == 200) {
            if (!that.data.showAddBut) {
              reslove({
                title: "您收到一个来自" + app.data.userInfo.username + "邀请函",
                path: "/pages/visitor_make/visitor_make?userVisitorDetailId=" + res.data[0].userVisitorListDetailId,
                imageUrl: "../../static/comment/invitation.jpg"
              });
            } else {
              my.alert({
                title: "提示",
                content: "您已成功邀请用户,短信将在几分钟内发送至用户手机~",
                success() {
                  my.navigateBack()
                }
              })
            }
          } else {
            my.alert({
              title: "提示",
              content: res.msg,
              success() {
                my.navigateBack()
              }
            })
            reject();
          }
        });
      } else {
        wx.showToast({
          title: "请将内容输入完整！",
          icon: "none"
        });
        reject();
      }
    });
  },
  // 常客获取楼宇的设备列表(sn和name)
  userWhiteGetSnList() {
    let adminInfo = app.data.adminInfo;
    userWhiteGetSnList({
      adminId: adminInfo.adminId,
      companyId: adminInfo.companyAdminId
    }).then(res => {
      // res.data.
      this.setData({
        machineList: res.data
      });
    });
  },
  // 打开通道权限弹出框
  showSN() {
    this.setData({
      chooseSN: true
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.userWhiteGetSnList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      language: app.data.language
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userInfo = app.data.userInfo;
    let promise = this.gotoInvited();
    return {
      title: "您收到一个来自" + userInfo.username + "邀请函",
      path: "/pages/visitor_make/visitor_make?userVisitorDetailId=null",
      imageUrl: "../../static/comment/invitation.jpg",
      promise
    };
  },
  antmoveAction: function () {
    //执行时动态赋值，请勿删除
  }
});