var app = getApp();
var that;
Page({
	data: {
		userList: [],
		all_rule_list: [],
		rule_tree: [],
		terr: [{
			name: '全公司',
			id: 0
		}],
		searchUser: [], //搜索
		searchName: '',
		isItem: true, //是否为搜索的值
		isAllChecked: false,
		isAllChecked2: false,
		dept_id: 0,
		minuteIndex: 0, //条件选择
		minuteArray: [{
				id: 0,
				name: '全部人员'
			},
			{
				id: 1,
				name: '已开启'
			},
			{
				id: 2,
				name: '已禁用'
			}
		],
	},
	onLoad() {
		that = this;
		dd.setNavigationBar({
			title: "部门/员工"
		});
		this.setData({
			terr: [{
				name: '全公司',
				id: 0
			}],
		})
		this.getMessage();
		this.getBmData();
		this.getUserData(0);
	},
	//选择积分分类
	bingIntegralType(e) {
		this.setData({
			minuteIndex: e.detail.value,
		});
	},
	radioChange(e) {
		var list = this.data.isItem ? this.data.userList : this.data.searchUser;
		let id = e.target.dataset.id;
		list.map(item => {
			if (item.id == id) {
				item.isChecked = e.detail.value;
			}
		})
	},
	//全选择
	ruleActiveAll(e) {
		var list = this.data.isItem ? this.data.userList : this.data.searchUser;
		if (e.detail.value) {
			list.map(item => {
				if (item.is_creator != 1) {
					if (this.data.minuteIndex == 1) {
						if (item.is_official == 1) {
							item.isChecked = true;
						}
					} else {
						item.isChecked = true;
					}
				}
			})
		} else {
			list.map(item => {
				if (item.is_creator != 1) {
					if (this.data.minuteIndex == 1) {
						if (item.is_official == 1) {
							item.isChecked = false;
						}
					} else {
						item.isChecked = false;
					}
				}
			})
		}
		if (this.data.isItem) {
			this.setData({
				userList: list,
				isAllChecked: e.detail.value
			})
		} else {
			this.setData({
				searchUser: list,
				isAllChecked2: e.detail.value
			})
		}
	},
	// 开启||禁用
	onOpen(e) {
		var index = e.target.dataset.index;
		let employeeId = [];
		var list = this.data.isItem ? this.data.userList : this.data.searchUser;
		list.map(item => {
			if (item.isChecked) {
				if (this.data.minuteIndex == 1) {
					if (item.is_official == 1) {
						employeeId.push(item.id)
					}
				} else {
					employeeId.push(item.id)
				}
			}
		})
		if (employeeId.length == 0) {
			app.globalData.showToast("请选择一名员工")
			return;
		}
		let url = index == 1 ? 'api/employee/disable' : 'api/employee/enable'
		app.$post(url, {
			employee_id: JSON.stringify(employeeId)
		}).then((res) => {
			app.globalData.showToast("设置成功")
			if (this.data.isItem) {
				this.getUserData(this.data.dept_id);
			} else {
				let data = {
					detail: {
						value: this.data.searchName
					}
				}
				this.bindKeyInput(data);
			}
		})
	},
	tbDd() {
		dd.confirm({
			title: '温馨提示',
			content: '下次同步时间需在10分钟之后，是否同步',
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			success: (result) => {
				if (result.confirm) {
					var http1 = app.$post("api/ding/employee_sync", {});
					Promise.all([http1]).then((item) => {
						app.globalData.showToast(item[0].data.msg);
					})
				}
			},
		});
	},
	onReset() {
		this.setData({
			searchUser: [],
			isItem: true,
			isVal: false
		})
	},
	bindKeyInput(e) {
		app.$get("api/employee/index", {
			dept_id: 0
		}).then((res) => {
			var list = res.data.data.list;
			list.forEach(item => {
				item.isChecked = false
				if (item.id == app.globalData.userData.id) {
					item.is_creator = 1
				}
			})
			var items = list.filter((item) => {
				return item.name.indexOf(e.detail.value) >= 0
			});
			this.setData({
				searchUser: items,
				isItem: e.detail.value ? false : true,
				isVal: e.detail.value ? true : false,
				searchName: e.detail.value ? e.detail.value : '',
				isAllChecked2: false,
			})
		})
	},
	//点击导航栏
	activeItem(e) {
		var item = e.target.dataset.item;
		var index = e.target.dataset.index;
		var terr = this.data.terr;
		if ((index + 1) == terr.length) {
			return false
		};
		this.setData({
			rule_tree: []
		});
		if (index == 0) {
			this.setData({
				terr: [{
					name: '全公司',
					id: 0
				}],
				rule_tree: this.data.all_rule_list,
				dept_id: 0
			})
		} else {
			var arr = terr.slice(0, index + 1);
			this.setData({
				rule_tree: item._child,
				terr: arr,
				dept_id: item.id
			})
		}
		this.getUserData(item.id);
	},
	//点击一级
	activeTree(e) {
		var item = e.target.dataset.item
		var terr = this.data.terr;
		terr.push(item)
		this.setData({
			rule_tree: item._child,
			terr: terr,
			dept_id: item.id
		});
		this.getUserData(item.id);
	},
	//获取部门列表
	getBmData() {
		app.$get("api/department/tree", {}).then((res) => {
			var list = res.data.data.list;
			this.setData({
				rule_tree: list,
				all_rule_list: list
			})
		})
	},
	//获取员工信息
	getUserData(dept_id) {
		this.setData({
			userList: []
		})
		app.$get("api/employee/index", {
			dept_id: dept_id
		}).then((res) => {
			var list = res.data.data.list;
			list.forEach(item => {
				item.isChecked = false
				if (item.id == app.globalData.userData.id) {
					item.is_creator = 1
				}
			})
			this.setData({
				isAllChecked: false,
				userList: list
			})
		})
	},
	getMessage() {
		app.$get("api/site/info").then((res) => {
			this.setData({
				gsData: res.data.data
			})
		})
	}
});
