<template>
	<view class="zd_container">
		<!-- <nav-bar title="分切下料2"></nav-bar> -->
		<nav-bar :title="title"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view class="filter_item">
					<view class="filter_label">区域</view>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index2" :localdata="options2" @change="selectChange2"></uni-data-select>
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label">设备</view>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index" :localdata="options" @change="selectChange"></uni-data-select>
					</view>
				</view>
			</view>
			<view class="zd_wrapper">
				<view class="filter_item">
					<view class="filter_label_wraper">
						<span class="filter_label">子卷号</span>
					</view>
					<view class="filter_input_wraper">
						<search-box
						v-model="val1"
						/>
					</view>
				</view>
				<view class="zd-row">
					<view class="zd-col-19">
						<view class="filter_item">
							<view class="filter_label">轴位置</view>
							<view class="filter_input_wraper">
								<radio-group @change="radioChange">
									<label class="mgr20" v-for="(item, index) in options1" :key="item.value">
										<radio :value="item.value" :checked="index === current" style="transform:scale(0.8)" />
										<text class="filter_unit">{{item.text}}</text>
									</label>
								</radio-group>
							</view>
						</view>
					</view>
					<view class="zd-col-4">
						<button class="mini-btn" size="mini" style="display: block;" type="primary" :disabled="disabled1" @tap="_getTubeLength">添加</button>
					</view>
				</view>
			</view>
			<view class="zd_wrapper grid-wraper">
				<view class="slide_new">
					<table>
						<thead>
							<tr>
								<th>子卷号</th>
								<th>壁厚</th>
								<th>管芯长度</th>
								<th>轴位置</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i">
								<td>{{e.container_name}}</td>
								<td>{{e.bh}}</td>
								<td>{{e.length}}</td>
								<td>{{ options1 | findByValue(e.site)}}</td>
								<td><button class="mini-btn" style="display: block;" size="mini" type="primary" @tap="handleDelete(i)">删除</button></td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submitbar">
			<button class="zd-col-6 btn-submit btn-default" @tap="clearUp">清空</button>
			<button class="zd-col-15 btn-submit btn-success" :class="{'btn-info': !index || !dataList.length}" @tap="downRolls2Confirm">确认</button>
		</view>
		<view class="msg_modal" :class="modalShow ? 'popshow1' : 'pophide1'">
			<view class="msg_tip">提示</view>
			<view class="zd_content pdt26">
				<view class="msg_p1" v-for="(e, i) in htmlContent1" :key="'content_1' + i">{{i+1}}.{{e}}</view>
				<view class="msg_p2"v-for="(e, i) in htmlContent2" :key="'content_2' + i">{{e}}</view>
			</view>
			<view class="zd-row submitbar">
				<button class="zd-col-6 btn-submit btn-success letter-30" @tap.stop="modalShow = false">取消</button>
				<button class="zd-col-15 btn-submit btn-success letter-30" :disabled="disabled" @tap="modalSure">确定</button>
			</view>
		</view>
		<view v-if="modalShow" class="msg_mask msg_mask_1"></view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {queryProductArea} from '@/utils/getData2.js'
	import {getTubeLength, slitterDevices, downRolls2} from '@/utils/getData3.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				options: [],
				index: '',
				options2: [],
				index2: '',
				options1: [{value: '1', text: '上轴'}, {value: '2', text: '下轴'}],
				index1: '1',
				current: 0,
				val1: '',
				dataList: [],
				disabled: false,
				disabled1: false,
				modalShow: false,
				htmlContent1: [],
				htmlContent2: []
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		created () {
			this._queryProductArea()
		},
		methods: {
			selectChange2(e) {
				this.index2 = e
				if (e) {
					this._slitterDevices(e)
				} else {
					this.index = ''
				}
			},
			/** 生产区域下拉框查询 */
			async _queryProductArea () {
				let res = await queryProductArea()
				this.options2 = [...res.data]
			},
			async _slitterDevices (e) {
				let res = await slitterDevices(e)
				this.options = [...res]
			},
			selectChange (e) {
				this.index = e
			},
			radioChange (e) {
				this.index1 = e.detail.value
			},
			async _getTubeLength () {
				this.disabled1 = true
				if (!this.val1.trim()) {
					this.disabled1 = false
					return
				}
				try {
					let res = await getTubeLength(this.val1)
					if (res.status === 200) {
						const index = this.dataList.findIndex(item => item.container_name === this.val1)
						if (index !== -1) {
							this.dataList.splice(index, 1)
						}
						this.dataList.push({container_name: this.val1, site: this.index1, bh: res.data.bh, length: res.data.length})
						this.val1 = ''
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
					this.disabled1 = false
				} catch (e) {
					this.disabled1 = false
				}
			},
			handleDelete (index) {
				this.dataList.splice(index, 1)
			},
			downRolls2Confirm () {
				this.htmlContent1 = []
				this.htmlContent2 = []
				if (!this.index || !this.dataList.length) {
					return
				}
				if (this.dataList.length === 1) {
					if (Number(this.dataList[0].length) >= 350 && Number(this.dataList[0].length) < 1600) {
						this.htmlContent1 = ['两个轴承端面到管芯端面到轴断面>=160mm', '三个轴承端面到管芯端面距离>=140mm', '管芯端面到缠绕膜边缘距离>=25mm']
						this.htmlContent2 = [`${this.dataList[0].container_name}:${this.dataList[0].bh}#${this.dataList[0].length}`]
					} else if (Number(this.dataList[0].length) >= 1600 && Number(this.dataList[0].length) <= 1700) {
						this.htmlContent1 = ['85mm<=两个轴承端面到管芯端面距离<=120mm', '管芯端面到缠绕膜边缘距离>=25mm']
						this.htmlContent2 = [`${this.dataList[0].container_name}:${this.dataList[0].bh}#${this.dataList[0].length}`]
					}
				} else if (this.dataList.length === 2) {
					if (this.dataList[0].site === this.dataList[1].site) {
						this.htmlContent1 = ['两个轴承端面到管芯端面到轴断面>=160mm', '三个轴承端面到管芯端面距离>=140mm', '管芯端面到缠绕膜边缘距离>=25mm', '管芯到管芯的间距>=50mm']
						this.dataList.map(e => {
							this.htmlContent2.push(`${e.container_name}:${e.bh}#${e.length}`)
						})
					} else {
						if (Number(this.dataList[0].length) >= 350 && Number(this.dataList[0].length) < 1600) {
							this.htmlContent1 = ['两个轴承端面到管芯端面到轴断面>=160mm', '三个轴承端面到管芯端面距离>=140mm', '管芯端面到缠绕膜边缘距离>=25mm']
							this.htmlContent2 = [`${this.dataList[0].container_name}:${this.dataList[0].bh}#${this.dataList[0].length}`]
						} else if (Number(this.dataList[0].length) >= 1600 && Number(this.dataList[0].length) <= 1700) {
							this.htmlContent1 = ['85mm<=两个轴承端面到管芯端面距离<=120mm', '管芯端面到缠绕膜边缘距离>=25mm']
							this.htmlContent2 = [`${this.dataList[0].container_name}:${this.dataList[0].bh}#${this.dataList[0].length}`]
						}
					}
					
				} else if (this.dataList.length > 2) {
					this.htmlContent1 = ['两个轴承端面到管芯端面到轴断面>=160mm', '三个轴承端面到管芯端面距离>=140mm', '管芯端面到缠绕膜边缘距离>=25mm', '管芯到管芯的间距>=50mm']
					this.dataList.map(e => {
						this.htmlContent2.push(`${e.container_name}:${e.bh}#${e.length}`)
					})
				}
				this.modalShow = true
			},
			modalSure () {
				this._downRolls2()
			},
			async _downRolls2 () {
				this.disabled = true
				try {
					let res = await downRolls2(this.index, this.dataList)
					if (res) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						this.dataList = []
					}
					this.disabled = false
					this.modalShow = false
				} catch (e) {
					this.disabled = false
					this.modalShow = false
				}
			},
			clearUp () {
				this.index = ''
				this.index2 = ''
				this.options = []
				this.val1 = ''
				this.dataList = []
				this.disabled = false
				this.disabled1 = false
			}
		}
	}
</script>