<template>
  <view class="regbox flexdc">
	  <uni-forms ref="form" :rules="rules" :modelValue="datainfo">
		  <!-- 家长 -->
		<picker mode="date"  :value="date"  @change="bindDateChange">
			<uni-forms-item name="schoolName" required label="申请时间">
				<view class="f15 co16 flex1 txr">{{datainfo.applyTime}}</view>
				<view class="rimg"><image :src="rimg" class="rimgs"></image></view>
			</uni-forms-item>
		</picker>
		
		<picker range-key="dictLabel" :value="bjindex" :range="classval" @change="bindDateChangea">
			<uni-forms-item label="调课班级" required name="subjectClass">
				<view class="f16  flex1 txr" :class="datainfo.subjectClass ? 'co16' : 'coa'">{{ datainfo.subjectClass || '请选择调课班级' }}</view>
				<view class="rimg"><image :src="rimg" class="rimgs"></image></view>
			</uni-forms-item>
		</picker>
	  	<picker range-key="dictLabel" :value="tkindex" :range="subject" @change="bindDateChangeb">
	  		<uni-forms-item label="调课科目" required name="subject">
	  			<view class="f16  flex1 txr" :class="datainfo.subject ? 'co16' : 'coa'">{{ datainfo.subject || '请选择调课科目' }}</view>
	  			<view class="rimg"><image :src="rimg" class="rimgs"></image></view>
	  		</uni-forms-item>
	  	</picker>
		<picker range-key="dictLabel" :value="btindex" :range="subject" @change="bindDateChangec">
			<uni-forms-item label="被调科目" required name="beSubject">
				<view class="f16  flex1 txr" :class="datainfo.beSubject ? 'co16' : 'coa'">{{ datainfo.beSubject || '请选择被调科目' }}</view>
				<view class="rimg"><image :src="rimg" class="rimgs"></image></view>
			</uni-forms-item>
		</picker>
		<picker range-key="dictLabel" :value="xqindex" :range="week" @change="bindDateChanged">
			<uni-forms-item label="时间(星期)" required name="week">
				<view class="f16  flex1 txr" :class="datainfo.week ? 'co16' : 'coa'">{{ kaType(datainfo.week,week) || '请选择时间(星期)' }}</view>
				<view class="rimg"><image :src="rimg" class="rimgs"></image></view>
			</uni-forms-item>
		</picker>
		<view class="text">
			<uni-forms-item label="调课内容" name="content">
				<uni-easyinput   class='inp_textarea' v-model="datainfo.content"  :inputBorder="false" type="textarea" placeholder="请输入调课内容，例：5月16日星期二的英语课与 5月18日周四的语文课调换…"></uni-easyinput>
				
			</uni-forms-item>
		</view>

		 
	  </uni-forms>
	 
	  <view class="mt26">
	  	<view class="rbtn" @click="getSubmit">提交</view>
	  	 <!-- <view class="apllytxt f15" @click="gethome">稍后完善</view> -->
	  </view>
  </view>
</template>

<script>
	import {getChangeAddFn,getChangeDetFn,getChangePutFn} from "@/api/work/index.js"
	import {getDictionaryFn} from "@/api/mine/register.js"
	import { selectDictValue } from '@/utils/common.js';
	import {getDeptList} from "@/api/mine/mine.js"
	import config from '@/config'
	 const baseUrl = config.baseUrl
	 import { getToken } from '@/utils/auth'
  export default {
	data(){
		return{
			rimg:require("@/static/images/rimg.png"),
			wimg:require('@/static/images/read.png'),
			delimg:require('@/static/images/del.png'),
			addimg:require('@/static/images/add.png'),
			date:'',
			rules: {
			  applyTime: {rules: [{required: true,errorMessage: '请选择时间'}]},
			  subjectClass: {rules: [{required: true,errorMessage: '请选择调课班级'}]},
			  subject: {rules: [{required: true,errorMessage: '请选择调课科目'}]},
			  beSubject: {rules: [{required: true,errorMessage: '请选择被调科目'}]},
			  week: {rules: [{required: true,errorMessage: '请选择时间(星期)'}]},
			},
			datainfo:{
				"applyTime": "",
				  "subjectClassId": "",
				  "subjectClass": "",
				  "subject": "",
				  "beSubject": "",
				  "week": "",
				  "content": ""
			},
			subject:[],
			week:[],
			classval:[],
			bjindex:[0],
			tkindex:[0],
			btindex:[0],
			xqindex:[0],
			pagetype:'add',
			id:''
		}
	},
	onLoad: function(e) {
		this.getclassListFn()
		this.datainfo.applyTime=this.getNowFormatDate()
		this.init()
		if(e.pagetype){
			var that=this;
			this.pagetype=e.pagetype||'add';
			this.id=e.id;
			setTimeout(function(){
				that.getDataFn()
			},500)
			
		}
		
	},
	methods:{
		getNowFormatDate(){
			let date = new Date();
			let y = date.getFullYear();
			let m = date.getMonth() + 1;
			m = m < 10 ? "0" + m : m;
			let d = date.getDate();
			d = d < 10 ? "0" + d : d;
			// let h = date.getHours();
			// h = h < 10 ? "0" + h : h;
			// let ms = date.getMinutes();
			// ms = ms < 10 ? "0" + ms : ms;
			// let s = date.getSeconds();
			// s = s < 10 ? "0" + s : s;
			// const time = y + "-" + m + "-" + d + " " + h + ":" + ms + ":" + s;
			const timeday = y + "-" + m + "-" + d;
			return timeday;
		},
		kaType(data, list) {
			return selectDictValue(list, data);
		},
		getDataFn(){
			// 详情
			getChangeDetFn(this.id).then(res=>{
				if(res.code==200){
					this.datainfo=res.data;
					if(res.data.subjectClass){
						var newa=[]
						Object.keys(this.classval).some((key) => {
							if (this.classval[key].dictLabel == ('' + res.data.subjectClass)) {
								newa[0]=Number(key)
								this.bjindex=newa
								return true;
							}
						})
					}
					if(res.data.subject){
						var newb=[]
						Object.keys(this.subject).some((key) => {
							if (this.subject[key].dictValue == ('' + res.data.subject)) {
								newb[0]=Number(key)
								this.tkindex=newb
								return true;
							}
						})
					}
					if(res.data.beSubject){
						var newc=[];
						Object.keys(this.subject).some((key) => {
							if (this.subject[key].dictValue == ('' + res.data.beSubject)) {
								newc[0]=Number(key)
								this.btindex=newc
								return true;
							}
						})
					}
					if(res.data.week){
						var newd=[];
						Object.keys(this.week).some((key) => {
							if (this.week[key].dictValue == ('' + res.data.week)) {
								newd[0]=Number(key)
								this.xqindex=newd
								return true;
							}
						})
					}
				}else{
					this.$toast(res.msg)
				}
			})
		},
		// 班级
		getclassListFn(){
			// 老师  获取学校id
			var id=this.$store.state.user.schoolId||this.$store.state.user.deptId;
			getDeptList(id).then(res=>{
				if(res.code==200){
					this.classval = res.data.map(v => {
						return {
							dictLabel: v.deptName,
							dictValue:v.deptId
						}
					})
				}else{
					this.$toast(res.msg)
				}
			})
		},
		init(){
			// 学科
			getDictionaryFn('sys_subject').then(res=>{
				if(res.code==200){
					this.subject = res.data.map(v => {
						return {
							dictLabel: v.dictLabel,
							dictValue: v.dictValue
						}
					})
				}
			})
			// 星期
			getDictionaryFn('sys_week').then(res=>{
				if(res.code==200){
					this.week = res.data.map(v => {
						return {
							dictLabel: v.dictLabel,
							dictValue: v.dictValue
						}
					})
				}
			})
		},
		bindDateChange(e){
			var val = e.detail.value;
			this.datainfo.applyTime=val;
		},
		bindDateChangea(e) {
			var val = e.detail.value;
			this.datainfo.subjectClass = this.classval[val].dictLabel;
			this.datainfo.subjectClassId = this.classval[val].dictValue;
		},
		bindDateChangeb(e) {
			var val = e.detail.value;
			this.datainfo.subject = this.subject[val].dictLabel;
		},
		bindDateChangec(e) {
			var val = e.detail.value;
			this.datainfo.beSubject = this.subject[val].dictLabel;
		},
		bindDateChanged(e) {
			var val = e.detail.value;
			this.datainfo.week = this.week[val].dictValue;
		},
		getSubmit(){
			var that=this;
			this.$refs.form.validate().then(res => {
				var params=JSON.parse(JSON.stringify(this.datainfo))
				if(that.pagetype=='add'){
					getChangeAddFn(params).then(res=>{
						if(res.code==200){
							that.$toast("新增成功")
							setTimeout(function(){
							uni.$emit('refreshdatalis');
								that.$tab.reLaunch('/pages/work/index') 
							},1500)
						}else{
							that.$toast(res.msg)
						}
					})
				}else{
					getChangePutFn(params).then(res=>{
						if(res.code==200){
							that.$toast("修改成功")
							setTimeout(function(){
							uni.$emit('refreshdatalis');
								uni.navigateBack({
									delta:1
								})
							},1500)
						}else{
							that.$toast(res.msg)
						}
					})
				}
				
			})
			// this.$tab.navigateTo(`/mine/pages/mine/success`)
		},
		gethome(){
			this.$tab.reLaunch(`/pages/index/index`)
		},
	},
	
  }
</script>

<style lang="scss" scoped>

.regbox{padding: 50rpx 36rpx 80rpx;
	.apllytxt{font-size:30rpx ;}
	.line{width: 40rpx;height: 6rpx;background: #FFB132;margin: 60rpx auto 0;}
	
	.addbox{font-weight: 500;
		image{width: 44rpx;height: 44rpx;margin-right: 24rpx;}
	}
}
.inp_textarea{text-align: left !important;border: 1px solid #FFB132;margin-top: 30rpx;border-radius: 14rpx;padding:0rpx 30rpx;box-sizing: border-box;}
.regbox /deep/ .uni-easyinput__content-textarea{height: 260rpx;min-height: 260rpx;max-height: 260rpx;font-size: 30rpx;}
/* #ifndef H5 */
.regbox /deep/ .uni-easyinput__content-textarea{height: 260rpx;min-height: 260rpx;max-height: 260rpx;font-size: 30rpx;text-align: left !important;border: 1px solid #FFB132;margin-top: 30rpx;border-radius: 14rpx;padding:12rpx 30rpx;box-sizing: border-box;}
/* #endif */
.regbox /deep/ .uni-forms{flex: 1;}

.regbox /deep/ .uni-forms-item__label{flex: 0 0 auto;width: auto !important;font-size: 32rpx;font-weight: bold;
color: #161616;}
.regbox /deep/ .uni-forms .uni-forms-item{padding: 28rpx 24rpx 20rpx;border-bottom: 2rpx #C1C1C1 solid;margin-bottom: 0;
	// &:last-child{margin-bottom: 0;border-bottom: 0;flex-direction: column;text-align: left;}
}
.regbox /deep/ .uni-forms .text .uni-forms-item{margin-bottom: 0;border-bottom: 0;flex-direction: column;text-align: left;}
.regbox /deep/ .uni-forms-item__content{display: flex;align-items: center;flex-direction: row;}
.regbox /deep/ .uni-easyinput{flex: 1;text-align: right;}
.regbox /deep/ .uni-easyinput__placeholder-class{font-size: 30rpx;}
.regbox /deep/ .uni-easyinput__content-input{font-size: 30rpx;}
 .regbox /deep/ .uni-forms-item__error{margin-top:20rpx;left: auto;right: 0;}
// .regbox .inp_textarea /deep/ .uni-easyinput__content-textarea{text-align: left !important;border: 1px solid #FFB132;margin-top: 30rpx;border-radius: 14rpx;padding:0rpx 30rpx;box-sizing: border-box;}
</style>
