<template>
	<view>
		<uni-collapse>
			<uni-forms ref="form" :modelValue="formData" :rules="rules">
				<uni-collapse-item title="共模作业组单" :open="true">
					<uni-forms-item label="共模作业组编码" :labelWidth='80' name="shareMouldJobGroupCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar" @confirm="scanBarCode" type="text"
							v-model="formData.shareMouldJobGroupCode" />
					</uni-forms-item>
					<uni-forms-item label="共模组编码" :labelWidth='80' name="shareMouldGroupCode">
						<uni-easyinput type="text" suffixIcon="scan" @iconClick="scanBar2"
							v-model="formData.shareMouldGroupCode" />
					</uni-forms-item>
					<uni-forms-item label="模具编码" :labelWidth='80' name="mouldUuid">
						<uni-easyinput type="text" v-model="formData.mouldUuid" @focus="showmould=!showmould" />
						<u-picker :show="showmould" @cancel="showmouldClose" @close="showmouldClose" closeOnClickOverlay
							:columns="mouldUuidList" @confirm="showmouldConfirm" keyName="label">
						</u-picker>
					</uni-forms-item>
					<uni-row :gutter="20">
						<uni-col :span="12" style="text-align: center;">
							<button type="primary" size="mini" @click="addDetail">添加</button>
						</uni-col>
						<uni-col :span="12" style="text-align: center;">
							<button type="success" size="mini" @click="reset">重置</button>
						</uni-col>
					</uni-row>
				</uni-collapse-item>
				<uni-collapse-item title="共模作业组单明细" :open="true">
					<uni-swipe-action>
						<uni-swipe-action-item :rightOptions="rightOptions" :key="index"
							v-for="(item, index) in formData.mesMouldShareGroupDetailList"
							@click="(data) => clickDetail(index,data)">
							<uni-badge :text="index+1" type="primary"></uni-badge>
							<uni-card :is-shadow="false" is-full>
								<div><strong>作业</strong>:{{item.jobCode}}</div>
								<div><strong>制程序号</strong>:{{item.opNo}}</div>
								<div><strong>制程编码</strong>:{{item.opCode}}</div>
								<div><strong>目标产品</strong>:{{item.ptNoTar}}</div>
								<div><strong>生产版本</strong>:{{item.productionVersion}}</div>
								<div><strong>单模产出数量</strong>:{{item.singleMouldNum}}</div>
								<div><strong>备注</strong>:{{item.remark}}</div>
							</uni-card>
						</uni-swipe-action-item>
					</uni-swipe-action>
				</uni-collapse-item>
			</uni-forms>
		</uni-collapse>
		<u-button type="primary" @click="submit">提交</u-button>

		<u-popup :show="show" @close="close">
			<div style="min-height: 50vh;">
				<div style="font-size: 16px;color:#606266; margin-top: 10px;">作业编码</div>
				<uni-easyinput style="margin-top: 10px;" suffixIcon="scan" @iconClick="scanBar1" type="text" focus
					v-model="jobCode" @confirm="scanBarJobCode" />
				<uni-card :is-shadow="false" is-full v-for="item in jobList" :key="item.id" :border="false">
					<div><strong>制程序号</strong>:{{item.opNo}}</div>
					<div><strong>制程编码</strong>:{{item.opCode}}</div>
					<div><strong>目标产品</strong>:{{item.ptNoTar}}</div>
					<div><strong>生产版本</strong>:{{item.productionVersion}}</div>
					<div><strong>单模产出数量</strong>:<u-number-box button-size="36" inputWidth="120"
							v-model="item.singleMouldNum" min="0"></u-number-box></div>
					<div><strong>备注</strong>:<uni-easyinput type="textarea" v-model="item.remark" /></div>
				</uni-card>
				<u-button type="primary" v-show="jobList.length>0" @click="pickJob">选中</u-button>
			</div>

		</u-popup>

	</view>

</template>

<script>
	import {
		listMesMouldShareGroupDetail,
		getMesMouldShareGroupDetail,
		addMesMouldShareGroupDetail,
		updateMesMouldShareGroupDetail,
		delMesMouldShareGroupDetail
	} from "@/api/mes/mouldShareGroupDetail";
	import {
		listMesMouldShareGroup,
		getMesMouldShareGroup,
		delMesMouldShareGroup,
		addMesMouldShareGroup,
		updateMesMouldShareGroup,
		listMould
	} from "@/api/mes/mouldShareGroup";
	import {
		listPwoJob
	} from "@/api/mes/jobReport.js";
	export default {
		mounted() {
			listMould().then(res => {
				this.mouldUuidList.push(res.rows.map(item => {
					item.label = item.mouldUuid + ':' + item.mouldName;
					return item
				}))
			})
		},
		data() {
			return {
				mouldUuidList: [],
				show: false,
				jobList: [],
				showmould: false,
				jobCode: null,
				formData: {
					id: null,
					shareMouldJobGroupCode: null,
					shareMouldGroupCode: null,
					mesMouldShareGroupDetailList: [],
				},
				oldmesMouldShareGroupDetailList: [],
				rightOptions: [{
					text: '删除',
					style: {
						backgroundColor: '#ff2a17'
					}
				}, ],
				rules: {
					shareMouldJobGroupCode: {
						rules: [{
							required: true,
							errorMessage: '请输入共模作业组编码!'
						}]
					},
				}
			}
		},
		methods: {
			showmouldConfirm(e) {
				console.log(e)
				this.formData.mouldCode = e.value[0].mouldCode;
				this.formData.mouldUuid = e.value[0].mouldUuid;
				this.showmould = false
			},
			showmouldClose() {
				this.showmould = false
			},
			reset() {
				this.formData.mesMouldShareGroupDetailList = this.oldmesMouldShareGroupDetailList;
			},
			deleteDetail(index) {
				this.formData.mesMouldShareGroupDetailList.splice(index, 1);
			},
			clickDetail(itemIndex, {
				position,
				index
			}) {
				if (index == 0) {
					this.deleteDetail(itemIndex);
				}
			},
			close() {
				this.jobCode = null;
				this.jobList = [];
				this.show = false;
			},
			scanBarCode() {
				if (this.formData.shareMouldJobGroupCode) {
					let obj = {
						shareMouldJobGroupCode: this.formData.shareMouldJobGroupCode
					}
					listMesMouldShareGroup(obj).then(async res => {
						if (res.rows.length != 0) {
							this.formData = res.rows[0];
							listMesMouldShareGroupDetail({
								shareMouldJobGroupId: this.formData.id
							}).then(async res => {
								if (res.rows.length != 0) {
									this.oldmesMouldShareGroupDetailList = res.rows;
									this.$set(this.formData, 'mesMouldShareGroupDetailList', [...
										res.rows
									])
								} else {
									this.formData.mesMouldShareGroupDetailList = [];
									this.$modal.msg("该共模作业组查无明细！");
								}
							});
						} else {
							this.$modal.msg("未查到该共模作业组！");
						}
					})
				} else {
					this.$modal.msg("请输入共模作业组编码！");
				}
			},
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.shareMouldJobGroupCode = res.result;
						_this.scanBarCode();
					}
				});
			},
			scanBar1() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.pwoCode = res.result;
						_this.scanBarPwoCode();
					}
				});
			},
			addcancel() {
				this.addshow = false;
			},
			pickJob() {
				const _this = this;
				if (_this.jobList.length > 0) {
					if (_this.formData.id && _this.formData.shareMouldJobGroupCode && _this.jobList[0].singleMouldNum) {
						let obj = {
							shareMouldJobGroupCode: _this.formData.shareMouldJobGroupCode,
							shareMouldJobGroupId: _this.formData.id,
							jobId: _this.jobList[0].id,
							jobCode: _this.jobList[0].code,
							opCode: _this.jobList[0].opCode,
							opNo: _this.jobList[0].opNo,
							ptNoTar: _this.jobList[0].ptNoTar,
							productionVersion: _this.jobList[0].productionVersion,
							singleMouldNum: _this.jobList[0].singleMouldNum,
							status: 0
						}
						addMesMouldShareGroupDetail(obj).then(async res => {
							_this.$modal.msgSuccess("添加共模作业组明细成功!");
							_this.scanBarCode();
							_this.jobCode = null;
							_this.jobList = [];
							_this.show = false;
						});
					} else {
						_this.$modal.msg("请先输入共模作业组编码并查询明细！")
					}
				} else {
					_this.$modal.msg("请先输入作业编码并查询！")
				}
			},
			scanBarJobCode() {
				if (this.jobCode) {
					let obj = {
						code: this.jobCode
					}
					listPwoJob(obj).then(res => {
						if (res.rows.length > 0) {
							this.jobList = res.rows
						} else {
							this.$modal.msg("未找到该作业！")
						}
					});
				}
			},
			addDetail() {
				if (this.formData.shareMouldJobGroupCode && this.formData.shareMouldJobGroupCode != '' && this.formData
					.id) {
					this.show = true;
				} else {
					this.$modal.msg("请先输入共模作业组编码并查询明细！")
				}
			},
			submit() {
				const _this = this;
				_this.$refs.form.validate().then(res => {
					uni.showModal({
						title: '提示',
						content: '您确定修改该共模作业组吗？',
						success: function(res) {
							if (res.confirm) {
								if (_this.formData.id && _this.formData.shareMouldJobGroupCode) {
									console.log(_this.formData)
									_this.$modal.loading('提交中')
									updateMesMouldShareGroup(_this.formData).then(async res => {
										_this.$modal.closeLoading();
										_this.$modal.msgSuccess("修改共模作业组成功!");
										setTimeout(() => {
											_this.$tab.switchTab(
												"/pages/work/index");
										}, 500);
									});
								} else {
									_this.$modal.msg("请先输入共模作业组编码并查询明细！")
								}
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				});
			}
		}
	}
</script>

<style>
</style>