<template>
	<uni-forms ref="form" :modelValue="formData" :rules="rules" label-width="210rpx" style="margin: 12px">
		<uni-forms-item label="被督察单位" name="departId" required>
			<uni-data-picker :localdata="departs" placeholder="请选择被督察单位" :border="false"
				:map="{text:'shortName', value: 'id'}" v-model="formData.departId" />
		</uni-forms-item>
		<uni-forms-item label="是否存在问题" name="hasProblem" required>
			<uni-data-checkbox v-model="formData.hasProblem" :localdata="hasProblem" />
		</uni-forms-item>
		
		<uni-forms-item label="关联督察任务" v-if="needSeelctTaskFlag">
			<inspection-task-data-picker v-model="formData.taskId" />
		</uni-forms-item>
		
		<uni-forms-item label="问题类型" name="problemTypeCode" v-if="formData.hasProblem === true">
			<problem-picker v-model="formData.problemTypeCode" @nodeclick="(node) => formData.problemType = node.name" />
		</uni-forms-item>
		<uni-forms-item label="被督察人员" name="peoples" v-if="formData.hasProblem === true">
			<police-picker v-model="formData.peoples" :departId="formData.departId || store.state.user.departId" />
		</uni-forms-item>
		<uni-forms-item label="情况描述" name="thingDesc" label-position="top" required v-if="formData.hasProblem === true">
			<uni-easyinput type="textarea" :input-border="false" placeholder="请输入具体情况" v-model="formData.thingDesc" />
		</uni-forms-item>
		<uni-forms-item label="附件" name="files" label-position="top" required>
			<upload v-model="formData.files" />
		</uni-forms-item>
	</uni-forms>
	<view class="footer col-24 flex gap-8">
		<button class="col-12" @tap="back">取消</button>
		<button type="primary" @tap="submit" class="col-12">确定</button>
	</view>
</template>

<script>
	import store from '@/store'
	import {
		departTree
	} from '@/api/depart'
	import {
		addProblem
	} from '@/api/taskProblem'

	let _this;
	export default {
		data() {
			return {
				formData: {
					files: this.$page.options.files? JSON.parse(this.$page.options.files) : [],
					taskId: this.$page.options.taskId || ''
				},
				hasProblem: [
					{
						text: '是',
						value: true
					},
					{
						text: '否',
						value: false
					},
				],
				departs: [],
				rules: {
					files: {
						rules: [{
							validateFunction: function(rule,value,data,callback){
								if (!value || value.length === 0) {
									callback('请上传附件')
								}
								return true
							}
						}]
					},
					hasProblem: {
						rules: [{
							required: true,
							errorMessage: '请选择是否存在督察问题',
						}]
					},
					departId: {
						rules: [{
							required: true,
							errorMessage: '请选择被督察单位',
						}]
					},
					thingDesc: {
						rules: [{
							required: true,
							errorMessage: '请输入具体情况',
						}]
					},
					taskId: {
						rules: [{
							required: true,
							errorMessage: '请关联督察任务',
						}]
					}
				},
				needSeelctTaskFlag: true
			}
		},
		setup() {
			return {
				store
			}
		},
		onLoad() {
			_this = this;
			if (this.$page.options.taskId) {
				this.needSeelctTaskFlag = false
			}
			departTree().then(data => {
				this.departs = data[0].children
			})
		},
		methods: {
			submit() {
				this.$refs.form.validate().then(res => {
					addProblem(this.formData).then(data => {
						_this.formData = {
							files: [],
							taskId: _this.$page.options.taskId || ''
						}
						uni.showToast({
							title: '新增成功',
							icon: 'none',
							duration: 5000
						})
						uni.navigateBack({
							
						});
					})
				}).catch(err => {
					console.log('表单错误信息：', err, this.formData);
				})
			},
			back() {
				uni.navigateBack({
					
				});
			}
		}
	}
</script>

<style>
</style>