<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('/testItem').then((res : any) => {
			if (res.code === 200) {
				list.value = res.data.payload
			}
		})
	}
	const onClickLeft = () => {
		history.back()
	}
	const onClickRight = () => {
		list.value.push({ necessary:true,options: [{}] })
	}
	//新增选项
	const addxx = (item : any) => {
		item.options.push({})
	}
	const onSubmit = () => {
		let tj=true
		list.value.forEach((l:any)=>{
			if(!l.name||!l.type){
				tj=false
			}
			l.options.forEach((m:any)=>{
				if(!m.name||!m.contentType){
					tj=false
				}
			})
		})
		if(!tj) return showToast('请填完整信息！')
		let param = {
			id: 1,
			payload: JSON.stringify(list.value)
		}
		putAction('/testItem', 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>
					<van-field label=" " label-width="7.5em" style="margin: 15rpx 0;">
						<template #input>
							<van-radio-group v-model="item.type" direction="horizontal">
								<van-radio name="Text">填空</van-radio>
								<van-radio name="Select">选择</van-radio>
							</van-radio-group>
						</template>
					</van-field>
					<van-field name="是否必填" label="是否必填" colon label-width="7.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>
				</van-cell-group>
				<view v-if="item.type=='Select'">
					<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 v-if="item.type=='Text'" style="margin-top: 15rpx;">
					<van-field name="填写内容" label="填写内容" colon label-width="7.5em">
						<template #input>
							<van-checkbox-group v-model="item.contentType" direction="horizontal"
								shape="square">
								<van-checkbox name="Text">文字</van-checkbox>
								<van-checkbox name="File">图片</van-checkbox>
							</van-checkbox-group>
						</template>
					</van-field>
				</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>