<template>
	<view class="page">
		<view class="header">
			<text class="header-title">异常登记{{ xm ? `(${xm})` : '' }}</text>
		</view>

		<view class="body">
			<textarea class="textarea" v-model="remark" />
		</view>

		<SsBottom :buttons="bottomButtons" @button-click="handleBottomAction" />
	</view>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import SsBottom from '@/components/SsBottom/index.vue'
import { xcdmApi } from '@/api/xcdm'

const ryid = ref('')
const xm = ref('')
const site = ref('')
const xcdmlbm = ref('1')
const xcid = ref('')
const jskssj = ref('')
const jsjssj = ref('')
const remark = ref('')

onLoad((options) => {
	const safeDecode = (v) => {
		try {
			return decodeURIComponent(v || '')
		} catch (e) {
			return v || ''
		}
	}
	ryid.value = safeDecode(options?.zdryid || options?.ryid)
	xm.value = safeDecode(options?.xm)
	site.value = safeDecode(options?.site)
	xcdmlbm.value = safeDecode(options?.xcdmlbm || options?.mode || '1')
	xcid.value = safeDecode(options?.xcid)
	jskssj.value = safeDecode(options?.jskssj)
	jsjssj.value = safeDecode(options?.jsjssj)
})

const bottomButtons = [
	{ text: '取消', action: 'cancel' },
	{ text: '保存并提交', action: 'submit' }
]

const handleBottomAction = ({ action }) => {
	if (action === 'cancel') {
		uni.navigateBack()
		return
	}
	if (action === 'submit') {
		handleSubmit()
	}
}

const handleSubmit = async () => {
	try {
		const to = Number(xcdmlbm.value) === 51 ? 61 : 11
		const payload = {
			xcid: xcid.value,
			xcdmlbm: Number(xcdmlbm.value) || 1,
			jskssj: jskssj.value,
			jsjssj: jsjssj.value,
			zdryid: ryid.value,
			xcjslbm: to,
			sm: remark.value
		}
		const res = await xcdmApi.mp_xcdmHomep_swState(payload)
		console.log('mp_xcdmHomep_swState (exception):', payload, res)
		uni.showToast({ title: '提交成功', icon: 'success' })
		setTimeout(() => {
			uni.navigateBack()
		}, 200)
	} catch (e) {
		console.warn('mp_xcdmHomep_swState (exception) failed:', e)
		uni.showToast({ title: '提交失败', icon: 'none' })
	}
}
</script>

<style lang="scss" scoped>
.page {
	min-height: 100vh;
	background: #f2f3f4;
	box-sizing: border-box;
	position: relative;
}

.header {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	height: 104rpx; // 约 52px
	background: #f2f3f4;
	display: flex;
	align-items: center;
	justify-content: center;
	z-index: 10;
	border-bottom: 1rpx solid #e8e8e8;
}

.header-title {
	font-size: 32rpx;
	color: #666;
	line-height: 1;
}

.body {
	position: absolute;
	top: 104rpx; // header
	left: 0;
	right: 0;
	bottom: 100rpx; // bottom buttons
	background: #fff;
}

.textarea {
	width: 100%;
	height: 100%;
	font-size: 30rpx;
	box-sizing: border-box;
	background: #fff;
	border: none;
	border-radius: 0;
	padding: 24rpx;
}
</style>
