<script setup lang="ts">
	import { onMounted, ref } from 'vue'
	import { getAction, putAction } from '../../common/http';
	import { showToast } from 'vant';

	const list = ref([] as any[])

	onMounted(() => {
		init()
	})
	function init() {
		getAction('/extraOption').then((res : any) => {
			if (res.code === 200) {
				list.value = JSON.parse(res.data.payload)
			}
		})
	}
	const onClickLeft = () => {
		history.back()
	}
	const onClickRight = () => {
		list.value.push({ options: [{}] })
	}
	//新增选项
	const addxx = (item : any) => {
		item.options.push({})
	}
	const onSubmit = () => {
		let param = {
			id: 1,
			payload: JSON.stringify(list.value)
		}
		putAction('/extraOption', param).then((res : any) => {
			if (res.code === 200) {
				showToast('提交成功！')
				init()
			}
		})
	}
</script>

<template>
	<view class="flex">
		<van-nav-bar title="理化报告维护" left-text="返回" left-arrow right-text="新增" @click-left="onClickLeft"
			@click-right="onClickRight" />
		<view class="content">
			<view class="card" v-for="(item,index) in list" :key="index">
				<van-cell-group inset>
					<view class="a-b">
						<van-icon name="clear" color="red" size="25" @click="list.splice(index,1)" />
						<van-field v-model="item.name" name="项目名称" label="项目名称" colon class="a-c" label-width="6em"
							:rules="[{ required: true, message: '请填写' }]" />
						<van-button type="primary" @click="addxx(item)">新增选项</van-button>
					</view>
					<view style="display: flex;align-items: center;margin: 10px 0;">
						<view style="width: 80%;">
							<van-field v-model="item.name1" name="标准值" label="标准值" colon class="a-c" label-width="3.5em"
								:rules="[{ required: true, message: '请填写' }]" />
						</view>
						
						<van-field name="是否必填" label="是否必填" colon label-width="4.5em">
							<template #input>
								<van-radio-group v-model="item.necessary" direction="horizontal">
									<van-radio :name="true">是</van-radio>
									<van-radio :name="false">否</van-radio>
								</van-radio-group>
							</template>
						</van-field>
					</view>
				</van-cell-group>
				<view class="grid-container">
					<view class="grid-item"></view>
					<view class="grid-item">内容</view>
					<view class="grid-item">是否符合</view>
					<view class="grid-item">操作</view>
				</view>
				<view class="grid-container" v-for="(itemSecond,indexSecond) in item.options" :key="indexSecond">
					<view class="grid-item">选项</view>
					<view class="grid-item"> <van-field v-model="itemSecond.name" /></view>
					<view class="grid-item" style="display: flex;align-items: center;justify-content: center">
						<van-checkbox v-model="itemSecond.default" :name="true"></van-checkbox>
					</view>
					<view class="grid-item" style="color: red" @click="item.options.splice(indexSecond,1)">删除</view>
				</view>
			</view>
		</view>
		<van-button type="primary" style="margin: 10rpx;" @click="onSubmit">提交</van-button>
	</view>
</template>

<style scoped lang="scss">
	.flex {
		display: flex;
		flex-direction: column;
		height: 100vh;
		width: 100vw;

		.van-nav-bar {
			width: 100%;
		}

		.content {
			flex: 1;
			padding: 0 20rpx;

			.grid-container {
				display: grid;
				grid-template-columns: 1fr 2fr 1.5fr 1.5fr;

				.grid-item {
					border: 1rpx solid #f2f2f2;
					text-align: center;

					::v-deep .van-cell {
						padding: 0 10rpx;
					}
				}
			}

			.card {
				margin: 16rpx 20rpx;
				padding: 10rpx;
				border: 1rpx solid #02a7f0;
				border-radius: 20rpx;
			}

			.a-b {
				display: flex;
				align-items: center;

				::v-deep .van-button--normal {
					padding: 8rpx;
					height: 50rpx;
					width: 7em;
				}
			}
		}

		::v-deep .van-field {
			font-size: 16px;
			padding: 0;
		}

		::v-deep .van-cell-group--inset {
			margin: 0;
		}

		.a-c {
			::v-deep .van-field__control {
				border-bottom: 1rpx solid #d7d7d7;
			}
		}

		::v-deep .van-field__label {
			text-align: end;
		}
	}
</style>