<template>
	<view style="padding: 20rpx;">
		<view style="padding: 30rpx;background-color: white;border-radius:50rpx">
			<u--form labelPosition="left" :model="formData" ref="uForm" labelWidth='120'>
				<!-- <u-form-item label="制衣生产车间:" prop="orderInfo.zyProductWorkshop" borderBottom ref="uItem" 
					:required="true">
					<u--input v-model="formData.orderInfo.zyProductWorkshop" border="none" :disabled="formData.orderInfo.orderStatus>=1"></u--input>
				</u-form-item>
				<u-form-item label="制衣班组:" prop="orderInfo.zyTeam" borderBottom ref="uItem" :required="true">
					<u--input v-model="formData.orderInfo.zyTeam" border="none" :disabled="formData.orderInfo.orderStatus>=1"></u--input>
				</u-form-item> -->
				<u-form-item label="制衣生产车间:" prop="orderInfo.zyProductWorkshop" borderBottom @click="showWorkshop = true" :required="true" ref="uItem">
					<u--input v-model="formData.orderInfo.zyProductWorkshop" disabled disabledColor="#ffffff" placeholder="请选择生产车间" border="none"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-action-sheet :show="showWorkshop" :actions="options" title="请选择车间" @close="showWorkshop = false" @select="workshopSelect" />
				<u-form-item label="制衣班组:" prop="orderInfo.zyTeam" borderBottom @click="showTeam = true" ref="uItem" :required="true">
					<u--input v-model="formData.orderInfo.zyTeam" disabled disabledColor="#ffffff" border="none" placeholder="请输入班组"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-action-sheet :show="showTeam" :actions="teamoptions" title="请选择班组" @close="showTeam = false" @select="teamSelect" />
				<u-form-item label="订单号:" prop="orderInfo.orderNo" borderBottom ref="uItem" :required="true">
					<u--input v-model="formData.orderInfo.orderNo" border="none" placeholder="请输入订单号"></u--input>
				</u-form-item>
				<u-form-item label="交期:" prop="orderInfo.deliveryDate" borderBottom ref="uItem"
					@click="deliveryDateShowFlag=true" :required="false">
					<u-input v-model="formData.orderInfo.deliveryDate" border="none" placeholder="请选择交期"></u-input>
				</u-form-item>
				<u-calendar :show="deliveryDateShowFlag" mode="single" @confirm="confirm" @close="deliveryDateShowFlag=false"></u-calendar>
				<u-form-item label="封存状态:" prop="orderInfo.status" borderBottom ref="uItem">
					<u-switch v-model="formData.orderInfo.status"></u-switch>
				</u-form-item>
				<u-button type="primary" text="提交" customStyle="margin-top: 50px" @click="submit"></u-button>
				<u-button type="error" text="重置" customStyle="margin-top: 10px" @click="reset"></u-button>
			</u--form>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		edit
	} from '@/api/biz/filing.js'
	import { workshopData,teamData } from '@/api/biz/team.js';
	export default {
		data() {
			return {
				formData: {
					orderInfo: {
						zyProductWorkshop: '',
						zyTeam: '',
						orderNo: '',
						deliveryDate: "",
						status: true,
					}
				},
				options: [],
				teamoptions: [],
				deliveryDateShowFlag: false,
				rules: {
					'orderInfo.zyProductWorkshop': {
						type: 'string',
						required: true,
						message: '请填写制衣生产车间',
						trigger: ['blur', 'change']
					},
					'orderInfo.zyTeam': {
						type: 'string',
						required: true,
						message: '请填写制衣班组',
						trigger: ['blur', 'change']
					},
					'orderInfo.orderNo': {
						type: 'string',
						required: true,
						message: '请填写订单号',
						trigger: ['blur', 'change']
					},
					// 'orderInfo.deliveryDate': {
					// 	type: 'string',
					// 	required: true,
					// 	message: '请填写交期',
					// 	trigger: ['blur', 'change']
					// }
				},
				showWorkshop: false,
				showTeam: false,
			}
		},
		onLoad(option) {
			const orderInfo = JSON.parse(decodeURIComponent(option.orderInfo))
			console.log(orderInfo);
			this.formData.orderInfo = orderInfo
			this.formData.orderInfo.workshopId = this.formData.orderInfo.workshop.id;
			this.formData.orderInfo.teamId = this.formData.orderInfo.team.id;
			// console.log(this.formData.orderInfo)
			this.getWorkshop();
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			getWorkshop() {
				workshopData().then(res => {
					if (res.success) {
						for (let i in res.data) {
							// console.log(res.data[i]['id'])
							// console.log(res.data[i]['zyProductWorkshop'])
							let o = {
								value: res.data[i]['id'],
								name: res.data[i]['zyProductWorkshop']
							};
			
							this.options.push(o);
						}
					} else {
					}
				});
			},
			getTeamByWorkshopId(workshopId) {
				console.log("======workshopId"+workshopId)
				teamData(workshopId).then(res => {
					if (res.success) {
						console.log(res.data)
						for (let i in res.data) {
							// console.log(res.data[i]['id'])
							// console.log(res.data[i]['zyProductWorkshop'])
							let o = {
								value: res.data[i]['id'],
								name: res.data[i]['zyTeam']
							};
			
							this.teamoptions.push(o);
						}
					} else {
					}
				});
			},
			workshopSelect(e) {
				console.log(e);
				this.formData.orderInfo.zyProductWorkshop = e.name;
				this.formData.orderInfo.workshopId = e.value;
				this.getTeamByWorkshopId(this.formData.orderInfo.workshopId);
				this.teamoptions=[];
				this.formData.orderInfo.zyTeam = '';
				this.formData.orderInfo.teamId = '';
			},
			teamSelect(e) {
				console.log(e);
				this.formData.orderInfo.zyTeam = e.name;
				this.formData.orderInfo.teamId = e.value;
				// this.getTeamByWorkshopId(this.formData.orderInfo.workshopId);
			},
			confirm(e) {
				console.log(e);
				this.deliveryDateShowFlag = false
				this.formData.orderInfo.deliveryDate = e[0]
			},
			submit() {
				this.$refs.uForm.validate().then(val => {
					edit(this.formData.orderInfo).then(res => {
						console.log(res)
						if (res.success) {
							this.$refs.uToast.show({
								type: 'success',
								message: "新增成功",
								iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
							})
							uni.$emit('loadData',this.formData.orderInfo.status?1:0);
							uni.navigateBack(1);
						} else {
							this.$refs.uToast.show({
								type: 'error',
								message: res.message,
								iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
							})
						}
					})
				})
			},
			reset() {
				this.formData = {
					orderInfo: {
						zyProductWorkshop: '',
						zyTeam: '',
						orderNo: '',
						deliveryDate: "",
						status: true
					}
				}
			}
		}
	}
</script>

<style>
</style>
