// pages/regular_invite/regular_invite.js
import international from '../../international/appointment_scence/index'
const {
	userWhiteInviteUserVisitor,
	userWhiteGetSnList,
} = require('../../utils/api/api')
const app = getApp()

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		//国际化语言包
		international: international,
		//中英文配置
		language: null,
		// 人员类型列表
		personTypeList: [{
			id: 0,
			name_ch: '访客',
			name_en: 'visit',
		},
		{
			id: 1,
			name_ch: '同事',
			name_en: 'friend',
		},
		{
			id: 2,
			name_ch: '亲属',
			name_en: 'family',
		},
		],
		//邀请方式
		isRealNameList: [{
			id: 0,
			name_ch: '微信',
			name_en: 'WeChat',
		},
		{
			id: 1,
			name_ch: '短信',
			name_en: 'SMS',
		},
		],
		// 拜访事由列表
		reasonList: [{
			name: '业务洽谈'
		},
		{
			name: '会议邀请'
		},
		{
			name: '施工安装'
		},
		{
			name: '探亲访友'
		},
		{
			name: '工作检查'
		},
		{
			name: '面试邀请'
		},
		{
			name: '快递外卖'
		},
		{
			name: '家政保洁'
		}],
		// 通道权限列表--设备列表
		machineList: [],
		//是否打开设备列表勾选弹窗
		chooseSN: false,
		//选中的设备数量
		snListLength: null,
		//是否显示新增手机号按钮
		showAddBut: true,
		//是否清除已填写的手机号
		closeNum: false,
		//是否禁用邀请按钮
		isDisabled: true,
		//提交的表单
		form: {},
	},
	// 常客获取楼宇的设备列表(sn和name)
	userWhiteGetSnList() {
		let adminInfo = app.data.adminInfo
		userWhiteGetSnList({
			adminId: adminInfo.buildingAdminId,
			companyId: adminInfo.companyAdminId,
		}).then(res => {
			this.setData({
				machineList: res.data
			})
		})
	},
	// 打开通道权限弹出框
	showSN() {
		this.setData({
			chooseSN: true
		})
	},
	//获取填写的值
	getValue(e) {
		var {
			form
		} = this.data;
		switch (e.currentTarget.dataset.type) {
			case 'snList':
				this.setData({
					'form.snList': e.detail,
					snListLength: e.detail.length
				})
				break;
			case 'reason':
				this.setData({
					'form.reason': e.detail.name
				})
				break;
			case 'invitationWay':
				this.setData({
					'form.invitationWay': e.detail
				})
				this.setData({
					showAddBut: e.detail == 0?false:true,
					'form.phoneList': [],
					closeNum: 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.reason && form.phoneList && form.phoneList.length > 0 ? false : true;
		this.setData({
			isDisabled: isDisabled
		})
	},
	//  立即邀请
	gotoInvited() {
		const that = this;
		return new Promise((reslove, reject) => {
			that.data.form.userWhitelistId = app.data.adminInfo.userWhitelistId
			userWhiteInviteUserVisitor(that.data.form).then(res => {
				if (res.code != 200) {
					wx.showModal({
						title: '提示',
						content: res.msg,
						showCancel: false,
						success() {}
					})
					reject();
					return;
				}
				if (!that.data.showAddBut) {
					that.data.flag = 1;
					reslove({
						title: '您收到一个来自' + app.data.userInfo.username + '邀请函',
						path: '/pages/visitor_make/visitor_make?userVisitorDetailId=' + res.data[0].userVisitorListDetailId,
						imageUrl: '../../static/comment/invitation.jpg',
					})
				} else {
					wx.showModal({
						title: '提示',
						content: '您已成功邀请用户,短信将在几分钟内发送至用户手机~',
						showCancel: false,
						success() {
							wx.navigateBack({
								delta: 1,
							})
						}
					})
				}
			})
		})
	},
	//判断是否分享完回来的（无法判断是否真的分享-2018 年腾讯已取消分享取消回调）
	judgeContinueShare() {
		if (this.data.flag == 1) {
			this.data.flag = null;
			wx.showModal({
				content: '分享邀请函成功，是否继续邀请?',
				confirmText: "继续邀请",
				complete: (res) => {
					if (res.cancel) {
						wx.navigateBack({
							delta: 1,
						})
					}
					if (res.confirm) {
						this.setData({
							"form.phoneList": [],
							closeNum: true
						});
						this.matchComplete()
					}
				}
			})
		}
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.setData({
			'form.reason': "业务洽谈"
		})
		this.userWhiteGetSnList()
	},

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

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		this.judgeContinueShare()
		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
		}
	}
})