<template>
	<view class="">
		<view class="rule_tip">修改打卡规则后第二天生效</view>
		<view class="fllow_form">
			<view><text>*</text>打卡人员</view>
			<view class="form_right" @click="selectStaff">
				<view :class="staffArr.length != 0 ? 'valueActive' : '' " v-if="staffArr.length != 0">
					<span v-for="(item,index) in staffArr" :key="index">{{item.name}},</span>
				</view>
				<view class="" v-else>
					打卡人员
				</view>
				<view class="iconfont icon-arrows_right"></view>
			</view>
		</view>
		<view class="fllow_form">
			<view><text>*</text>规则名称</view>
			<view class="form_right">
				<input type="text" v-model="name" :class="name ? 'valueActive' : '' " placeholder="规则名称" placeholder-style="color:#999;font-size:32rpx;">
			</view>
		</view>
		<view class="fllow_form">
			<view><text>*</text>规则类型</view>
			<view class="form_right" @click="changeRulesType">
				<view :class="typeObj.title ? 'valueActive' : '' ">{{typeObj.title ? typeObj.title : '规则类型'}}</view>
				<view class="iconfont icon-arrows_right"></view>
			</view>
		</view>
		<view class="fllow_form" @click="toSetTime">
			<view><text>*</text>打卡时间</view>
			<view class="form_right">
				<view :class="timeArr.length != 0 ? 'valueActive' : '' ">
					<view class="timebox" v-if="timeArr.length != 0">
						{{timeArr[0].week}};<span v-if="timeArr.length > 1">等{{timeArr.length}}组</span>
					</view>
					<view class="" v-else>打卡时间</view>
				</view>
				<view class="iconfont icon-arrows_right"></view>
			</view>
		</view>
		<view class="fllow_form" @click="selectPosition">
			<view><text>*</text>打卡位置</view>
			<view class="form_right">
				<view :class="positionArr.length != 0 ? 'valueActive' : '' ">
					<view class="timebox" v-if="positionArr.length != 0">
						{{positionArr[0].address}};<view v-if="positionArr.length > 1">等{{positionArr.length}}个</view>
					</view>
					<view class="" v-else>打卡位置</view>
				</view>
				<view class="iconfont icon-arrows_right"></view>
			</view>
		</view>
		
		<!-- <view class="botwarp" v-if="rule_id">
			<view class="b_bot remove" @click="$noMultipleClicks(sureDel)">删除</view>
			<view class="b_bot" @click="$noMultipleClicks(toSave)">确认修改</view>
		</view> -->
		<view id="btn" class="btn_g bg-blue button-hover round" @click="$noMultipleClicks(toSave)">
			保存
		</view>
		
		<!-- 选择规则类型 -->
		<select-rule-type ref="ruleTypeChild" @selectType="selectType"></select-rule-type>
	</view>
</template>

<script>
	import selectRuleType from './ruleType.vue'
	import { netTimeSlotList, netAddressList, netAddRules, netRulesDetail, netEditRules, netDelRules } from '@/api/index.js'
	
	export default{
		components:{
			selectRuleType
		},
		data() {
			return{
				noClick: true, //防止  重复点击
				time_id:'',  //打卡规则id
				timeArr:[],   //打卡时间
				positionArr:[],  //打卡地址
				typeObj:{},  //0固定上下班 
				typeArr:[
					{id:0,title:'固定上下班',label:'支持弹性打卡',p:'所有人按照相同时间打卡',text:'适用于办公室坐班，例如朝九晚五'}
				],
				staffArr:[],
				name:'',
				rule_id:'',   //规则id
			}
		},
		onLoad(options) {
			if(options.id){
				this.rule_id = options.id
				this.getDetail()
			}
			uni.removeStorageSync('remindlist');
		},
		onShow() {
			//获取 选择好的打卡人员
			let arr = uni.getStorageSync('remindlist') ? uni.getStorageSync('remindlist') : []
			this.staffArr = this.$deWeight(arr)
			
			//获取打卡时间
			this.getClockTime()
			//获取打卡位置
			this.getPosition()
		},
		methods:{
			//获取详情
			getDetail() {
				netRulesDetail({id:this.rule_id}).then(res=>{
					res = res.data
					this.name = res.name
					uni.setStorageSync('remindlist',res.staff_info)
					this.staffArr = res.staff_info
					this.typeArr.forEach(ele=>{
						if(ele.id == res.type){
							this.typeObj = ele
						}
					})
				})
			},
			//获取打卡时间
			getClockTime() {
				netTimeSlotList({time_id:this.time_id}).then(res=>{
					this.timeArr = res.data
				})
			},
			//获取打卡位置
			getPosition() {
				netAddressList().then(res=>{
					this.positionArr = res.data
				})
			},
			//选择打卡人员
			selectStaff() {
				uni.navigateTo({
					url: '/pagesA/crm/selectMember/selectMember',
				})
			},
			//选择规则类型
			changeRulesType() {
				this.$refs.ruleTypeChild.init()
			},
			selectType(obj) {
				this.typeObj = obj
			},
			//打卡时间
			toSetTime() {
				uni.navigateTo({
					url:'/pagesA/punchclock/clockrules/clockTime'
				})
			},
			//打卡位置
			selectPosition() {
				uni.navigateTo({
					url:'/pagesA/punchclock/clockrules/punchPosition'
				})
			},
			toSave() {
				if(this.staffArr.length == 0) {
					uni.showToast({
						title:'请选择打卡人员',
						icon:'none'
					})
					return
				}
				if(!this.name){
					uni.showToast({
						title:'请填写规则名称',
						icon:'none'
					})
					return
				}
				if(!this.typeObj.title){
					uni.showToast({
						title:'请选择规则类型',
						icon:'none'
					})
					return
				}
				if(this.timeArr.length == 0){
					uni.showToast({
						title:'请添加打卡时间',
						icon:'none'
					})
					return
				}
				if(this.positionArr.length == 0) {
					uni.showToast({
						title:'请添加打卡位置',
						icon:'none'
					})
					return
				}
				let staffarr = []
				this.staffArr.forEach(ele=>{
					staffarr.push(ele.id)
				})
				let params = {
					name:this.name,
					type:this.typeObj.id,
					staff_id:staffarr.join(','),
				}
				if(this.rule_id){
					params.id = this.rule_id
					netEditRules(params).then(res=>{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},2000)
					})
				}else{
					netAddRules(params).then(res=>{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},2000)
					})
				}
			},
			sureDel() {
				uni.showModal({
					title:'提示',
					content:'请确认是否删除?',
					complete:(data)=>{
						if(data.confirm){
							netDelRules({id:this.rule_id}).then(res=>{
								uni.showToast({
									title:res.msg,
									icon:'none'
								})
								setTimeout(()=>{
									uni.navigateBack({
										delta:1
									})
								},2000)
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.rule_tip{
		padding:24rpx;
		font-size:24rpx;
		color:red;
		text-align: center;
	}
	.fllow_form {
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #fff;
		border-bottom: 1rpx solid #EAEAEA;
		padding: 30rpx 20rpx;
		color: #999;
		font-size: 32rpx;
		text {
			color: #f00;
			margin-left: 10rpx;
		}
		.form_right {
			display: flex;
			justify-content: flex-end;
			align-items: center;
			color: #999;
			width: 450rpx;
			:first-child {
				text-align: right;
				overflow: hidden; 
			    text-overflow: ellipsis;
			    white-space: nowrap;
			}
			image {
				width: 40rpx;
				height: 40rpx;
				margin: 28rpx 5rpx 0 0;
			}
			.timebox{
				width:400rpx;
				display: flex;
				justify-content: flex-end;
				align-items: center;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				color:#333;
			}
		}
		.theme {
			color: #5ca9fe;
		}
		
		.form_input {
			input {
				text-align: right;
				margin-right: 10rpx;
			}
		}
	}
	.botwarp{
		display: flex;
		justify-content: space-around;
		align-items: center;
		margin-top:100rpx;
		.b_bot{
			width:300rpx;
			height:80rpx;
			border-radius: 40rpx;
			font-size:24rpx;
			text-align: center;
			line-height: 80rpx;
			background:$uni-text-color;
			color:#fff;
		}
		.remove{
			background:#ff7800;
		}
	}
</style>