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

	const showPop = ref(false)
	const list = ref([] as any[])
	const form = ref({} as any)
	const processList = ref([] as any[])

	onMounted(() => {
		init()
	})
	const init = () => {
		getAction('/factory').then((res : any) => {
			if (res.code === 200) {
				list.value = res.data
			}
		})
		getAction('/process').then((res:any)=>{
			if(res.code===200){
				processList.value=res.data.map((l:any)=>({value:l.id,text:l.name}))
			}
		})
	}

	const onClickLeft = () => {
		history.back()
	}
	const onClickRight = () => {
		form.value = {availableProcess:[{}]}
		showPop.value = true
	}
	const edit = (item : any) => {
		form.value=JSON.parse(JSON.stringify(item))
		showPop.value = true
	}
	const onSubmit = () => {
		if (form.value.id) {
			putAction('/factory', form.value).then((res : any) => {
				if (res.code === 200) {
					init()
					showPop.value = false
					showToast('提交成功！')
				}
			})
		} else {
			postAction('/factory', form.value).then((res : any) => {
				if (res.code === 200) {
					init()
					showPop.value = false
					showToast('提交成功！')
				}
			})
		}
	}

	const del = (item : any) => {
		showConfirmDialog({
			title: '提示',
			message: '是否确认删除？',
		}).then(() => {
			let url = '/factory/' + item.id
			deleteAction(url).then((res : any) => {
				if (res.code === 200) {
					init()
					showToast('删除成功！')
				}
			})
		})
			.catch(() => {
				// on cancel
			});
	}
	
	const showPicker = ref(false)
	const indexType = ref()
	const popuList = ref([])
	//选择框事件
	const choosePic = (index : any) => {
		showPicker.value = true
		indexType.value = index
	}
	//选择框确认
	const pickerConfirm = (val : any) => {
		// form.value.content[indexType.value].id = val.selectedOptions[0].value
		form.value.availableProcess[indexType.value].name = val.selectedOptions[0].text
		showPickerCancel()
	}
	//取消
	const showPickerCancel = () => {
		showPicker.value = false
	}
	//弹窗开启事件
	const handleOpen = () => {
		popuList.value = processList.value
	}
</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="grid-container">
				<view class="grid-item">编号</view>
				<view class="grid-item">工厂</view>
				<view class="grid-item">操作</view>
			</view>
			<view class="grid-container" v-for="(item,index) in list" :key="index">
				<view class="grid-item">{{item.code}}</view>
				<view class="grid-item">{{item.name}}</view>
				<view class="grid-item"><span style="color: #35a5f7" @click="edit(item)">编辑</span> &nbsp;&nbsp; <span
						style="color: red" @click="del(item)">删除
					</span></view>
			</view>
		</view>
	</view>

	<van-popup v-model:show="showPop" style="width: 80%" round>
		<view>
			<h3 style="text-align: center">{{form.id?'编辑':'新增工厂'}}</h3>
			<van-form @submit="onSubmit">
				<van-cell-group inset>
					<van-field v-model="form.name" name="工厂名称" label="工厂名称" colon label-width="5em"
						:rules="[{ required: true, message: '请填写' }]" />
					<van-field v-model="form.code" name="工厂编号" label="工厂编号" type="number" colon label-width="5em"
						:rules="[{ required: true, message: '请填写' }]" />
					<van-field v-model="form.cylinderName" name="染缸名称" label="染缸名称" colon label-width="5em" />
					<van-field v-model="form.cylinderNum" name="染缸数量" label="染缸数量" type="number" colon
						label-width="5em" />
					<van-field v-model="form.maxProcessCount" name="最大可染" label="最大可染" type="number" colon
						label-width="5em" />
					<van-field name="类型" label="类型" colon label-width="5em" class="b-a"
						:rules="[{ required: true, message: '请填写' }]">
						<template #input>
							<van-radio-group v-model="form.active" direction="horizontal">
								<van-radio :name="true">启用</van-radio>
								<van-radio :name="false">禁用</van-radio>
							</van-radio-group>
						</template>
					</van-field>

					<view class="a-b">
						<van-button type="primary" @click="form.availableProcess.push({})">可做工序</van-button>
					</view>
					<view class="grid-container1">
						<view class="grid-item">工序名称</view>
						<view class="grid-item">操作</view>
					</view>
					<view class="grid-container1" v-for="(item,index) in form.availableProcess" :key="index">
						<view class="grid-item" @click="choosePic(index)">{{item.name}}</view>
						<view class="grid-item" style="color: red" @click="form.availableProcess.splice(index,1)">删除</view>
					</view>
				</van-cell-group>
				<view class="a-c">
					<van-button type="danger" @click="showPop=false">取消</van-button>
					<van-button type="primary" native-type="submit">确认</van-button>
				</view>
			</van-form>
		</view>
	</van-popup>
	
	<!--选择框-->
	<van-popup v-model:show="showPicker" round position="bottom" @open="handleOpen">
		<van-picker show-toolbar :columns="popuList" @confirm="pickerConfirm" @cancel="showPickerCancel"
			ref="pickerRef" />
	</van-popup>
</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 3fr 2fr;

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

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

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

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

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

	.a-b {
		margin: 10rpx;

		::v-deep .van-button--normal {
			padding: 5px 8px;
			height: 30px;
		}
	}

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

	::v-deep .van-field__control {
		border-bottom: 1rpx solid #d7d7d7;
		text-align: center;
		font-size: 16px;
	}

	::v-deep .van-field__label {
		text-align: end !important;
	}

	.b-a {
		::v-deep .van-field__control {
			border-bottom: none;
		}
	}

	.a-c {
		display: flex;
		align-items: center;
		justify-content: space-around;
		margin: 30rpx;

		::v-deep .van-button--normal {
			padding: 10rpx !important;
			height: 60rpx;
		}
	}
</style>