/*******************************************************************************
 * Copyright(c) 2024 dongke All rights reserved. / Confidential
 * 类的信息：
 *      1.程序名称：续费明细 
 * 编辑履历：
 *      作者              日期                  版本              修改内容
 *      周兴             2024-5-23                1.00               新建 
 *******************************************************************************/
const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const mixins = require('@/mixins/index.js');
const app = getApp()
Page({
	mixins: [mixins],
	/**
	 * 页面的初始数据
	 */
	data: {
		// 企业名称
		staffInfo: null,
		saveParams: null,
		staffService: app.globalData['staffService'],
		routeObjName: 'company',
		active: 0,
		pageFlag: false,
		tableDataSocial: null,
		tagList: [{ title: mixins.$t('wxRight'), code: 'wx' }, { title: mixins.$t('webRight'), code: 'web' }],
		contentList: [
			{ name: 'staffCode', title: mixins.$t('staffCode') },
			{ name: 'staffPhone', title: mixins.$t('staffPhone') },
			{ name: 'orgName', title: mixins.$t('orgId') },
			{ name: 'roleNames', title: mixins.$t('roleName') },
		],
		//按钮
		buttonSaveList: [
			{ code: 'save', title: mixins.$t('save') }
		],
	},
	/**
	 * @desc : 给参数赋值
	 * @date : 2024/5/30 15:49
	 * @author : 周兴
	 */
	setSearchParams(params) {
		params.flgCanLogin = true;
		params.flgValid = true;
		params.currentPage = 1;
		params.pageSize = Constants.MAX_PAGE_SIZE;
		if(this.data.gradeCode == Constants.gradeCode.STD){
			// 标准版需要排除掉 企业授权的那个用户
			params.flgInit = false;
		}
		return params;
	},
	/**
	 * @desc : 查询
	 * @date : 2024/5/30 15:49
	 * @author : 周兴
	 */
	getData(params) {
		return this.excute(this.data.staffService, this.data.staffService.selectByCond, params);
	},
	/**
	 * @desc : 处理数据
	 * @date : 2024/5/30 15:49
	 * @author : 周兴
	 */
	handleSearchData(tableData) {
		this.setData({
			tableDataSocial: [...tableData]
		})
		// 过滤出手机授权（默认展示）
		tableData = tableData.filter(it => it.loginType == Constants.loginType.loginTypeWx
			|| it.loginType == Constants.loginType.loginTypeWxPc);

		this.setData({
			tableData: tableData
		})
		// 刷新数量
		this.refreshTag();
	},
	/**
	* @desc : 刷新量
	* @date : 2024/5/23 15:49
	* @author : 周兴
	*/
	refreshTag() {
		if (this.data.gradeCode == Constants.gradeCode.STD) {
			let staffInfo = this.data.staffInfo;
			let filters = staffInfo.filter(it => it.code == 'current');
			if (filters && filters.length > 0) {
				let tableData = [...this.data.tableDataSocial]
				tableData = tableData.filter(it => it.loginType == Constants.loginType.loginTypeWx
					|| it.loginType == Constants.loginType.loginTypeWxPc);
				filters[0].userCount = tableData.length;
				this.setData({
					staffInfo: staffInfo
				})
			}
		} else {
			let tagList = this.data.tagList
			let tableData = [...this.data.tableDataSocial]
			tableData = tableData.filter(it => it.loginType == Constants.loginType.loginTypeWx
				|| it.loginType == Constants.loginType.loginTypeWxPc);
			let filters = tagList.filter(it => it.code == 'wx');
			if (filters && filters.length > 0) {
				filters[0].title = mixins.$t('wxRight') + '【' + tableData.length + '】'
			}
			let webData = [...this.data.tableDataSocial]
			// 电脑端授权
			webData = webData.filter(it => it.loginType == Constants.loginType.loginTypePc
				|| it.loginType == Constants.loginType.loginTypeWxPc);
			filters = tagList.filter(it => it.code == 'web');
			if (filters && filters.length > 0) {
				filters[0].title = mixins.$t('webRight') + '【' + webData.length + '】'
			}
			this.setData({
				tagList: tagList
			})
		}
	},
	/**
	* @desc : 停用用户，不再续费
	* @date : 2024/5/23 15:49
	* @author : 周兴
	*/
	handleStaff(e) {
		let staffId = e.detail.item.staffId
		return this.excute(this.data.staffService, this.data.staffService.disable, staffId).then(res => {
			if (res.data.code == Constants.SUCESS_CODE) {
				wx.showToast({
					title: '操作成功',
					image: '/static/image/success.png',
					duration: 2000
				})
				// 过滤掉停用的这条数据
				let tableDataSocial = this.data.tableDataSocial
				tableDataSocial = tableDataSocial.filter(it => it.staffId != staffId)
				let tableData = this.data.tableData
				tableData = tableData.filter(it => it.staffId != staffId)
				this.setData({
					tableDataSocial: tableDataSocial,
					tableData: tableData
				})
				// 刷新数量
				this.refreshTag();
			}
		});
	},
	/**
	* @desc : 校验数据
	* @date : 2024/5/30 15:49
	* @author : 周兴
	*/
	validData() {
		// 校验现在的用户数是否超过续费授权数
		let tableDataSocial = this.data.tableDataSocial
		let staffInfo = this.data.staffInfo
		let staffFilters = staffInfo.filter(it => it.code == 'renew');
		// 判断手机授权
		let wxFilters = tableDataSocial.filter(it => it.loginType == Constants.loginType.loginTypeWx
			|| it.loginType == Constants.loginType.loginTypeWxPc);

		if (wxFilters.length > staffFilters[0].wxMaxNum) {
			util.showToast('当前手机授权数超过续费手机授权数')
			return false;
		}
		// 判断电脑授权
		let webFilters = tableDataSocial.filter(it => it.loginType == Constants.loginType.loginTypePc
			|| it.loginType == Constants.loginType.loginTypeWxPc);

		if (webFilters.length > staffFilters[0].webMaxNum) {
			util.showToast('当前电脑授权数超过续费电脑授权数')
			return false;
		}
		return true;
	},
	/**
	 * @desc : 设置保存参数
	 * @author : 周兴
	 * @date : 2024/4/1
	 */
	setParams(params) {
		params = this.data.saveParams
		return params;
	},
	/**
	 * @desc : 保存数据
	 * @author : 周兴
	 * @date : 2024/5/17
	 */
	saveData(params) {
		return this.excute(this.data.service, this.data.service.reNewCompany, params).then(res => {
			if (res.data.code == Constants.SUCESS_CODE) {
				// 成功后跳转支付
				this.toPay(res.data.data);
			}
		})
	},
	/**
	* @desc : 切换，查询用户
	* @date : 2024/5/23 15:49
	* @author : 周兴
	*/
	onChangeTabs(e) {
		let code = e.detail.detail.code
		let tableDataSocial = [...this.data.tableDataSocial];
		let tableData = null;
		let tagList = this.data.tagList
		let filters = []

		if (code == 'web') {
			// 过滤出电脑授权
			tableData = tableDataSocial.filter(it => it.loginType == Constants.loginType.loginTypePc
				|| it.loginType == Constants.loginType.loginTypeWxPc);
			filters = tagList.filter(it => it.code == 'web');
			if (filters && filters.length > 0) {
				filters[0].title = mixins.$t('webRight') + '【' + tableData.length + '】'
			}
		} else {
			// 过滤出手机授权
			tableData = tableDataSocial.filter(it => it.loginType == Constants.loginType.loginTypeWx
				|| it.loginType == Constants.loginType.loginTypeWxPc);
			filters = tagList.filter(it => it.code == 'wx');
			if (filters && filters.length > 0) {
				filters[0].title = mixins.$t('wxRight') + '【' + tableData.length + '】'
			}
		}
		this.setData({
			tableData: tableData,
			tagList: tagList
		})
	},
	/**
	* @desc : 初始加载
	* @date : 2024/5/23 15:49
	* @author : 周兴
	*/
	loadInit() {
		this.setData({
			saveParams: { ...this.data.params }
		})
	},
	/**
	* @desc : 初始加载（生命周期函数）
	* @date : 2024/5/23 15:49
	* @author : 周兴
	*/
	onLoad(options) {
		this.setData({
			staffInfo: JSON.parse(options.staffInfo)
		})
	}
})