<template>
	<view style="padding: 30rpx;">
		<uni-forms ref="formRef" :modelValue="formData" :rules="formRules" validateTrigger="bind" label-width="80px" label-align="right" label-position="left">
			<uni-forms-item name="reason" label="爽约原因" required>
				<uni-easyinput v-model="formData.reason" type="textarea" placeholder="请输入爽约原因"></uni-easyinput>
			</uni-forms-item>
		</uni-forms>
		<button class="send-button" @tap="handleSubmit">提 交</button>
	</view>
</template>

<script setup>
import { ref, unref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { noAttendInterviewInvite } from '@/api/interview'

const formRef = ref(null)
const formData = ref({
	id: null,
	reason: null
})
const formRules = {
	reason: {
		rules: [{ required: true, errorMessage: '请输入爽约原因' }]
	}
}

onLoad(async (options) => {
	const { id } = options
	if (!id) {
		uni.showToast({
			title: 'ID缺失',
			icon: 'none'
		})
		setTimeout(() => {
			uni.navigateBack({ delta: 1 })
		}, 1000)
		return
	}

	formData.value.id = id
})

// 提交
const handleSubmit = async () => {
	const valid = await unref(formRef).validate()
	if (!valid) return

	uni.showLoading({ title: '提交中' })
	try {
		await noAttendInterviewInvite(formData.value)
		uni.hideLoading()
		uni.showToast({
			title: '提交成功',
			icon: 'success'
		})
		setTimeout(() => {
			uni.navigateBack({ delta: 1 })
		}, 1000)
	} catch {
		uni.hideLoading()
	}
}
</script>

<style scoped lang="scss">

</style>
