<template>
	<view class="flex">
		<van-nav-bar title="工艺维护" left-text="返回" left-arrow @click-left="onClickLeft" @click-right="onClickRight" />
		<view class="grid-container">
			<view class="grid-item">工艺名称</view>
			<view class="grid-item">操作</view>
		</view>
		<view class="content">
			<view class="grid-container" v-for="(item,index) in formList" :key=index>
				<view class="grid-item">{{item.name}}</view>
				<view class="grid-item" @click="edit(item)" style="color: #02a7f0;">编辑</view>
			</view>
		</view>
	</view>
	<van-popup v-model:show="show" style="width: 100%">
		<view class="flex">
			<van-nav-bar title="工艺维护" left-text="返回" left-arrow @click-left="poClickLeft" />
			<view class="content">
				<van-form ref="formRef" required="auto">
					<van-cell-group>
						<!-- <van-field label-width="7rem" label-align="right" v-model.number="form.name" type="text"
							label="工艺名称:" placeholder="请输入"
							:rules="[{ required: true, message: '请输入正确内容', validator}]" />
						<van-field label-width="7rem" label-align="right" required readonly type="text"
							label="大货价公式:" />
						<view class="box">
							<VueDraggable class="table-box1" v-model="listDHJ" group="people">
								<view v-for="item in listDHJ" :key="item.name" class="table-item"
									style="width: auto;padding:5px">
									{{ item.name }}
								</view>

							</VueDraggable>
							<van-button type="danger" size="mini" class="remove-btn" @click="listDHJ=[]">清空</van-button>
						</view>
						<van-field label-width="7rem" label-align="right" required readonly type="text"
							label="成本价公式:" />
						<view class="box">
							<VueDraggable class="table-box1" v-model="listCBJ" group="people">
								<view v-for="item in listCBJ" :key="item.name" class="table-item"
									style="width: auto;padding:5px">
									{{ item.name }}
								</view>
							</VueDraggable>
							<van-button type="danger" size="mini" class="remove-btn" @click="listCBJ=[]">清空</van-button>
						</view> -->

						<p>绑定工序流程</p>
						<view v-for="(item,index) in form.flows" :key="index" class="a-b">
							<van-icon name="add" color="red" size="25" v-if="index===0" @click="form.flows.push({bind:'通用',})" />
							<van-icon name="clear" color="red" size="25" v-if="index!=0"
								@click="form.flows.splice(index,1)" />
							<van-field v-model="item.name" name="工序流程" label="工序流程" label-align="right" readonly colon
								label-width="5em" class="bor" @click="choosePic(index)">
							</van-field>
							
							<van-radio-group v-model="item.bind" @click="changeRad(index)">
							  <van-radio name="仅现货">现货单使用</van-radio>
							</van-radio-group>
						</view>
					</van-cell-group>
					<!-- <p>基础块:</p>
					<VueDraggable class="table-box" v-model="BaseBlock"
						:group="{ name: 'people', pull: 'clone', put: false }">
						<view v-for="item in BaseBlock" :key="item.name" class="table-item">
							{{ item.name }}
						</view>
					</VueDraggable>
					<p>运算符号:</p>
					<VueDraggable class="table-box" v-model="sign"
						:group="{ name: 'people', pull: 'clone', put: false }">
						<view v-for="item in sign" :key="item.name" class="table-item" style="background-color: #24d2d3;font-size:
			                               16px">
							{{ item.name }}
						</view>
					</VueDraggable>
					<p>已有工艺:</p>
					<VueDraggable class="table-boxa" v-model="ProcessPrice"
						:group="{ name: 'people', pull: 'clone', put: false }">
						<view v-for="item in ProcessPrice" :key="item.name" class="table-itema">
							{{ item.name }}
						</view>
					</VueDraggable>
					<p>常数:</p>
					<VueDraggable class="table-boxa" v-model="numList"
						:group="{ name: 'people', pull: 'clone', put: false }">
						<view v-for="item in numList" :key="item.name" class="table-itemb"
							style="background-color: #82d588">
							{{ item.name}}
						</view>
					</VueDraggable> -->


				</van-form>
			</view>
			<view style="display: flex;justify-content: space-between; padding: 0 30px;margin-top: 15px;">
				<van-button type="primary" block @click="onSubmit">保存</van-button>
			</view>
		</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>
<script lang="ts" setup>
	import { onMounted, ref } from 'vue';
	// import { VueDraggable } from "vue-draggable-plus";
	import { getAction, postAction, putAction } from '../../common/http';
	import { showToast } from 'vant';
	const show = ref(false)
	const formList = ref([])
	const showPicker = ref(false)

	const form = ref({ flows: [{}] } as any)
	const formRef = ref();
	const listDHJ = ref([])
	const listCBJ = ref([])
	// const BaseBlock = ref([
	// 	{ name: '基础大货价', value: '$基础大货价$' },
	// 	{ name: '基础成本价', value: '$基础成本价$' },
	// 	{ name: '缩率', value: '$缩率$' },
	// 	{ name: '姆米', value: '$姆米$' },
	// 	{ name: '门幅', value: '$门幅$' }
	// ])
	// const sign = ref([
	// 	{ name: '+', value: '+' },
	// 	{ name: '-', value: '-' },
	// 	{ name: '*', value: '*' },
	// 	{ name: '/', value: '/' },
	// 	{ name: '(', value: '(' },
	// 	{ name: ')', value: ')' },
	// 	{ name: '=', value: '=' },
	// 	{ name: '>', value: '>' },
	// 	{ name: '<', value: '<' },
	// 	{ name: '?', value: '?' },
	// 	{ name: ':', value: ':' },
	// ])
	// const ProcessPrice = ref([
	// 	{ name: '印花价', value: '#印花价#' },
	// 	{ name: '砂洗', value: '#砂洗#' },
	// 	{ name: '水洗', value: '#水洗#' },
	// 	{ name: '印花砂洗', value: '#印花砂洗#' },
	// 	{ name: '印花水洗', value: '#印花水洗#' },
	// 	{ name: '胚布', value: '#胚布#' },
	// 	{ name: '预缩(半)', value: '#预缩(半)#' },
	// 	{ name: '预缩(全)', value: '#预缩(全)#' },
	// 	{ name: '印花预缩(半)', value: '#印花预缩(半)#' },
	// 	{ name: '印花预缩(全)', value: '#印花预缩(全)#' },
	// ])
	// const numList = ref([
	// 	{ name: '1', value: '1' },
	// 	{ name: '2', value: '2' },
	// 	{ name: '3', value: '3' },
	// 	{ name: '4', value: '4' },
	// 	{ name: '5', value: '5' },
	// 	{ name: '6', value: '6' },
	// 	{ name: '7', value: '7' },
	// 	{ name: '8', value: '8' },
	// 	{ name: '9', value: '9' },
	// 	{ name: '0', value: '0' },
	// 	{ name: '.', value: '.' },
	// ])
	// const validator = (val : any) => {
	// 	return !/\s/g.test(val)
	// }
	// const broadHeadingExpression = ref('')
	// const costExpression = ref('')
	
	const changeRad = (index:any) =>{
		form.value.flows.forEach((l:any,j:any)=>{
			if(j!=index){
				l.bind='通用'
			}else{
				l.bind='仅现货'
			}
		})
	}
	
	const onSubmit = () => {
		if (form.value.id) {
			let data = {
				id:form.value.id,
				craftName: form.value.name,
				flows: form.value.flows,
			}
			putAction('/flowBind', data).then((res : any) => {
				if (res.code === 200) {
					initData()
					show.value = false
					showToast('修改成功!')
				}
			})
		}else{
			let data = {
				craftName: form.value.name,
				flows: form.value.flows,
			}
			postAction('/flowBind', data).then((res : any) => {
				if (res.code === 200) {
					initData()
					show.value = false
					showToast('修改成功!')
				}
			})
		}
	}

	const poClickLeft = () => {
		show.value = false
	}
	const onClickLeft = () => {
		history.back()
	}
	const onClickRight = () => {
		show.value = true
	}

	const initData = () => {
		formList.value=[]
		getAction('/fabric/craft/info').then((res : any) => {
			if (res.code === 200) {
				getAction('/flowBind').then((res1 : any) => {
					if (res1.code === 200) {
						res.data.forEach((l : any) => {
							let item = res1.data.find((m : any) => m.craftName === l.name)
							if (item) {
								formList.value.push({
									id: item.id,
									name: item.craftName,
									flows:item.flows
								})
							} else {
								formList.value.push({
									name: l.name,
									flows:[{bind:'通用',}]
								})
							}
						})
					}
				})
			}
		})
	}
	const edit = (item : any) => {
		form.value = item
		show.value = true
	}

	const indexType = ref()

	//选择框事件
	const choosePic = (index : any) => {
		showPicker.value = true
		indexType.value = index
	}
	//选择框确认
	const pickerConfirm = (val : any) => {
		form.value.flows[indexType.value].id = val.selectedOptions[0].value
		form.value.flows[indexType.value].name = val.selectedOptions[0].text
		showPickerCancel()
	}
	//取消
	const showPickerCancel = () => {
		showPicker.value = false
	}
	//弹窗开启事件
	const handleOpen = () => {
		popuList.value = processList.value
	}
	const popuList = ref([])
	const processList = ref([])
	onMounted(() => {
		initData()
		getAction('/craftFlows').then((res : any) => {
			if (res.code === 200) {
				processList.value = res.data.map((l : any) => ({ text: l.name, value: l.id }))
			}
		})
	})
	// const handlEdit = (item : any) => {
	// 	// 定义一个正则表达式来匹配括号和变量名
	// 	const regex = /([()+-=><?:*/])|(\$[\w\u4e00-\u9fa5]+\$)|(#[\w\u4e00-\u9fa5]+#)|([\+\*])|(\d+)/g;

	// 	// 使用 match 方法来匹配所有的符号和变量名
	// 	const matches = item.broadHeadingExpression.match(regex);
	// 	const matches1 = item.costExpression.match(regex);
	// 	listDHJ.value = []
	// 	listCBJ.value = []
	// 	matches.forEach((l : any) => {
	// 		// 使用正则表达式匹配中文字符
	// 		const regex = /\$([\u4e00-\u9fa5]+)\$/;
	// 		const match = l.match(regex);
	// 		const regex1 = /#(.+)#/;
	// 		const match1 = l.match(regex1);
	// 		if (match) {
	// 			listDHJ.value.push({ name: match[1], value: l })
	// 		} else {
	// 			if (match1) {
	// 				listDHJ.value.push({ name: match1[1], value: l })
	// 			} else {
	// 				listDHJ.value.push({ name: l, value: l })
	// 			}
	// 		}
	// 	})
	// 	matches1.forEach((l : any) => {
	// 		// 使用正则表达式匹配中文字符
	// 		const regex = /\$([\u4e00-\u9fa5]+)\$/;
	// 		const match = l.match(regex);
	// 		const regex2 = /#(.+)#/;
	// 		const match2 = l.match(regex2);
	// 		if (match) {
	// 			listCBJ.value.push({ name: match[1], value: l })
	// 		} else {
	// 			if (match2) {
	// 				listCBJ.value.push({ name: match2[1], value: l })
	// 			} else {
	// 				listCBJ.value.push({ name: l, value: l })
	// 			}
	// 		}
	// 	})
	// }
	// const addTransformation = (list : any) => {
	// 	let str = ''
	// 	list.forEach((element : any) => {
	// 		str += element.value
	// 	});
	// 	return str
	// }
</script>
<style scoped lang="scss">
	.flex {
		display: flex;
		flex-direction: column;
		height: 100vh;
		width: 100vw;

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

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

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

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

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

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


		}

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

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

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

	}

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

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

		::v-deep .van-field {
			width: 65% !important;
		}
	}

	::v-deep .van-popup--center {
		max-width: 100vw !important;
	}

	.content-scroll {
		flex: 1;
		overflow-y: scroll;
	}

	.hide-scrollbar {
		-ms-overflow-style: none;
		/* IE and Edge */
		scrollbar-width: none;
		/* Firefox */
		overflow-y: scroll;
		/* 显示滚动条区域 */
	}

	.box {
		width: 90vw;
		min-height: 80px;
		margin: 10px;
		border: 1px solid #ccc;
		position: relative;
		padding: 5px;

	}

	.remove-btn {
		position: absolute;
		right: 5px;
		bottom: 5px;
	}

	.table-box1 {
		display: flex;
		flex-wrap: wrap;
	}

	.table-box {
		width: 100vw;
		display: flex;
		align-items: center;
		justify-content: space-around;
		flex-wrap: wrap;
	}

	.table-item {
		width: 15vw;
		background-color: #73a0fa;
		color: #fff;
		font-size: 12px;
		border-radius: 5px;
		text-align: center;
		padding: 5px 0;
		margin: 5px 0;
	}

	.table-boxa {
		width: 100vw;
		display: flex;
		align-items: center;
		flex-wrap: wrap;
	}

	.table-itema {
		background-color: #73a0fa;
		color: #fff;
		font-size: 12px;
		border-radius: 5px;
		text-align: center;
		padding: 5px 10px;
		margin: 5px 2px;
	}

	.table-itemb {
		background-color: #73a0fa;
		color: #fff;
		font-size: 16px;
		border-radius: 5px;
		text-align: center;
		padding: 10px 20px;
		margin: 5px 2px;
	}

	p {
		padding-left: 10px;
	}
</style>