<template>
	<div style="width: 100%">
		<el-breadcrumb separator-class="el-icon-arrow-right">

			<el-breadcrumb-item>首页</el-breadcrumb-item>
			<el-breadcrumb-item>监测任务</el-breadcrumb-item>
			<el-breadcrumb-item>新增任务</el-breadcrumb-item>
		</el-breadcrumb>
		<div class="title">
			新增任务
		</div>

		<el-form :model="form" label-width="100px">
			<el-form-item label="任务名称" required>
				<el-input v-model="form.task_name" placeholder="最长40字符"></el-input>
			</el-form-item>
			<el-form-item label="任务优先级" required>
				<el-select v-model="form.level" placeholder="请选择">
					<el-option v-for="item in priority" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="年度" required>
				<el-date-picker v-model="form.year" type="year" value-format="yyyy">
				</el-date-picker>
			</el-form-item>
			<el-form-item label="批次" required>
				<el-select v-model="form.batch">
					<el-option v-for="item in batch" :key="item.label" :label="item.label" :value="item.label">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="任务时间" required>
				<el-col :span="8">
					<el-date-picker v-model="form.starttime" type="date" value-format="yyyy-MM-dd">
					</el-date-picker>
				</el-col>
				<el-col :span="1">
					<div class="line">--</div>
				</el-col>
				<el-col :span="11">
					<el-date-picker v-model="form.endtime" type="date" value-format="yyyy-MM-dd">
					</el-date-picker>
				</el-col>
			</el-form-item>
			<!-- <el-form-item label="发布单位" required>
				<el-input v-model="rymc" :disabled="true"></el-input>
			</el-form-item> -->
			<!-- <el-form-item label="牵头单位" required>
				<el-input v-model="form.main_unit" @focus="getTestListShow = true"></el-input>
			</el-form-item> -->
			<el-form-item label="文件号">
				<el-input v-model="form.filenum"></el-input>
			</el-form-item>
			<el-form-item label="抽检分离:" prop="is_divide" width="70px" class="labelItem" required>
					<el-select  v-model="form.is_divide" size="" >
						<el-option v-for="item in divideOptions" :key="item.id" :label="item.name" :value="item.id">
						</el-option>
					</el-select>
			</el-form-item>
			<el-form-item label="备注">
				<el-input type="textarea" placeholder="最长300字符" v-model="form.log"></el-input>
			</el-form-item>
			<el-form-item label="附件" required>
				<el-upload class="upload-demo" :file-list="fileList" drag :headers="myHeaders"
					action="http://syjcapi.aielab.net/api/v1/ossUpload" :on-success="uploadSuccess"
					:limit="1">
					<i class="el-icon-upload"></i>
					<div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
				</el-upload>
			</el-form-item>
			<el-form-item>
				<el-button type="success" @click="onSubmit()">保存</el-button>
				<el-button type="success" @click="nextStep(id)">下一步</el-button>
				<el-button @click="reBack()">返回</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>
<script>
	const rymc = window.sessionStorage.getItem('rymc')
	const token = window.sessionStorage.getItem('token')
	export default {
		data() {
			return {
				task_class:'',
				// rymc: rymc,		//从本地缓存获取发布单位
				myHeaders: {
					Authorization: 'bearer'+ token			//上传文件添加token
				},
				// 任务表单
				form: {
					main_unit: [],	
					task_id:''
				},
				// 优先级选项
				priority: [{
					value: '0',
					label: '一般'
				}, {
					value: '1',
					label: '紧急'
				}, {
					value: '2',
					label: '特急'
				}],
				// 批次选项
				batch: [{
					label: '一季度'
				}, {
					label: '二季度'
				}, {
					label: '三季度'
				}, {
					label: '四季度'
				}],
				// 抽检分离选项
				divideOptions: [{
						id: 0,
						name: '否'
					},
					{
						id: 1,
						name: '是'
					}
				],
				// 上传成功文件列表
				fileList: []
			}
		},
		created() {
			 this.task_class = this.$route.params.task_class
			 this.task_id = this.$route.params.task_id
			 this.postTasks()
		},
		methods: {
			//保存
			async onSubmit() {
				this.form['token'] = token
				this.form['groupname'] = this.groupname
				this.form['orgs'] = this.orgs
				this.form['releaser'] = window.sessionStorage.getItem('rymc')
				this.form['task_class'] = '专项监测'
				this.form['ispublic'] = 0
				this.form['task_id'] = this.task_id
				const result = await this.$http.post('postTasks', this.form)
					this.task_id = result.data.data.task_id
					console.log( this.task_id)
				
				// 判断业务逻辑
				if (result.code == 0) {
						this.$message({
						type: 'success',
						message: result.data.message
					})
				} else {
					this.$message({
						type: 'success',
						// message: result.data.message
						message: '保存成功'
					})
				}
					
			},
			//返回
			reBack() {
				this.$router.push('specialMonitor')
			},
			//下一步
			nextStep(){
				this.$router.push({
					name:'addTaskObjectZx',
					params:{
						task_id: this.task_id
					}
				})
				console.log(task_id)
			},			
			// 获取任务列表
			async postTasks() {
				const result = await this.$http.post('postTasks', this.queryInfo)
			// 	// 判断业务逻辑
				// if (result.data.code == 0) {
			// 		this.testList = result.data.data.rows
			// 		this.total = result.data.data.total
					this.task_id = result.data.task_id},
			// 	}else{
			// 		this.$message({
			// 			type: 'error',
			// 			message: result.data.message
			// 		})
			// 	}
			// },
			// 获取检测机构列表
			// async getTestList() {
			// 	this.queryInfo['token'] = token
			// 	const result = await this.$http.post('getTestList', this.queryInfo)
			// 	console.log(result);
				// 判断业务逻辑
			// 	if (result.data.code == 0) {
			// 		this.testList = result.data.data.rows
			// 		this.total = result.data.data.total
			// 	}
			// },
			/** 修复当用户在大于1的分页进行数据搜索没有返回值的问题 */
			// setPageNum() {
			// 	this.queryInfo.pageNum = 1
			// 	this.getTestList()
			// },
			// setSelectedRow() {
			// 	// 设置当前页已选项
			// 	this.hander = true
			// 	this.testList.forEach(item => {
			// 		if (this.list.includes(item[this.uniqueKey])) {
			// 			this.$refs.multipleTable.toggleRowSelection(item, true)
			// 		}
			// 	})
			// 	this.hander = false
			// },

			/** 监听每页显示多少数据的改变 */
			// handleSizeChange(newSize) {
			// 	this.queryInfo.pageSize = newSize
			// 	this.getTestList()
			// },

			/** 监听页码的改变 */
			// handleCurrentChange(newPage) {
			// 	this.queryInfo.pageNum = newPage
			// 	this.getTestList()
			// },
			// // 批量勾选
			// handleSelectionChange(val) {
			// 	this.multipleSelection = val
			// 	if (this.hander) return false
			// 	// this.setSelectRows() // 处理选中的方法
			// },
			// 提交
			// choseTest() {
			// 	let that = this
			// 	that.orgs = that.multipleSelection
			// 	console.log(that.orgs)
			// 	that.form.main_unit = that.multipleSelection.map(el => el.name).join(', ')
			// 	console.log(that.form.main_unit)
				// that.form.main_unit = that.multipleSelection
				// for(let i=0;i<that.multipleSelection.length;i++){
				// 	that.form.main_unit.push(that.multipleSelection[i].name)
				// }
				// that.getTestListShow = false
				// 上传成功
				uploadSuccess(res) {
				console.log(res.data)
				this.form.file_url = res.data.fileUrl
				this.form.file_name = res.data.fileName
			}
			},
			
		}
	
</script>
<style lang="less" scoped>
	.title {
		margin-left: 50%;
		margin-bottom: 20px;
	}

	.el-form {
		width: 800px;
		margin: 0 auto;
	}

	.footer {
		margin-top: 5px;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}
</style>
