<template>
	<view class="add">
		<!-- 设备详情信息 -->
		<view class="main">
			<u--form labelPosition="left" labelAlign="center" :model="form" ref="form1">

				<u-form-item label="设备名称" prop="equipName" labelWidth="auto" borderBottom @click="showName = true">
					<u--input v-model="form.equipName" border="none" placeholder="请选择设备"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
					<u-picker :show="showName" :columns="columns" keyName="name"  @cancel='showName=false' @confirm='confirm'></u-picker>
				</u-form-item>
				
				<u-form-item label="设备编号" prop="equipNo" labelWidth="auto" borderBottom>
					<u--input v-model="form.equipNo" border="none" readonly></u--input>
				</u-form-item>
				<u-form-item label="供应商" prop="supplierName" labelWidth="auto" borderBottom>
					<u--input v-model="form.supplierName" border="none" readonly></u--input>
				</u-form-item>
				<u-form-item label="数 量" prop="num" labelWidth="auto" borderBottom>
					<u--input v-model="form.num" type="number" border="none" placeholder="请输入数量"
						></u--input>
				</u-form-item>
				<u-form-item label="入库价格" prop="price" labelWidth="auto" borderBottom>
					<u--input v-model="form.price" border="none" type="number" placeholder="请输入库价格"
						>
					</u--input>
				</u-form-item>
				<u-form-item label="入库日期" prop="inTime" labelWidth="auto" @click="cgShow = true; " borderBottom
					ref="item1">
					<u--input v-model="form.inTime" disabled disabledColor="#ffffff" placeholder="请选择入库日期"
						border="none"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
					<u-datetime-picker :show="cgShow" v-model="pickerTime" mode="date" :closeOnClickOverlay="true"
						@close="cgShow = false" @cancel="cgShow = false" @confirm="date2Confirm"></u-datetime-picker>
				</u-form-item>
				<u-form-item label="生产日期" prop="productionTime" labelWidth="auto" @click="scShow = true; " borderBottom
					ref="item1" >
					<u--input v-model="form.productionTime" disabled disabledColor="#ffffff" placeholder="请选择生产日期"
						border="none"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
					<u-datetime-picker :show="scShow" v-model="pickerTime" mode="date" :closeOnClickOverlay="true"
						@close="scShow = false" @cancel="scShow = false" @confirm="dateConfirm"></u-datetime-picker>
				</u-form-item>

			</u--form>
		</view>

		<view class="submit">
			<u-button type="primary" @click="submit" text="添 加 入 库"></u-button>
		</view>
	</view>
</template>

<script>
	export default {

		data() {
			return {
				show: true,
				scShow: false,
				cgShow: false,
				form: {
					id: null,
					equipNo: null,
					equipName: null,
					price: null,
					supplierId: null,
					supplierName: null,
					num: null,
					//  生产日期
					productionTime: null,
					inTime: null,
				},
				pickerTime: Number(new Date()),
				showName: false,
				deviceList: [],
				columns: [],
			}
		},

		onLoad: function(options) {
			if (options.form) {
				let data = JSON.parse(options.form);
				this.form = data
			}
		},
		onShow: function() {
			this.getList()
		},
		created() {},
		methods: {
			async getList() {
				const {
					data: res
				} = await this.$http('/app/equipList')
				if (res.code) return
				this.deviceList = res.data
				this.columns.push(this.deviceList)
			},
			confirm(d) {
				this.form.equipName = d.value[0].name
				this.form.equipNo = d.value[0].eno
				this.form.supplierId = d.value[0].supplierId
				this.form.supplierName = d.value[0].supplierName
				this.showName = false;
			},

			// 添加采购记录
			async submit() {
				let flag = true;
				if(this.form.equipName==null || this.form.equipName=='' ){
					flag = false;
				}
				if(this.form.num == null || this.form.num==''){
					flag = false;
				}
				if(this.form.price == null || this.form.price==''){
					flag = false;
				}
				if(this.form.productionTime == null || this.form.productionTime==''){
					flag = false;
				}
				if(this.form.inTime == null || this.form.inTime==''){
					flag = false;
				}
				if(!flag) return uni.showToast({
					title: '请补充设备入库内容',
					icon: 'error'
				})
				const {
					data: res
				} = await this.$http('/app/addEquipIn', this.form)
				if (res.code == 0) {
					uni.showToast({
						title: '添加入库成功',
						icon: 'success'
					});
					this.reset();
					uni.navigateTo({
						url: 'inList',
					})
				} else {
					uni.showToast({
						title: "添加入库失败！",
						icon: 'error'
					})
				}
				
			},
			dateConfirm(e) {
				const timeFormat = uni.$u.timeFormat
				this.scShow = false;
				this.form.productionTime = timeFormat(e.value, 'yyyy-mm-dd');
			},
			date2Confirm(e) {
				const timeFormat = uni.$u.timeFormat
				this.cgShow = false;
				this.form.inTime = timeFormat(e.value, 'yyyy-mm-dd');
			},
			reset() {
				this.form = {
					id: null,
					equipNo: null,
					equipName: null,
					price: null,
					supplierId: null,
					supplierName: null,
					num: null,
					//  生产日期
					productionTime: null,
					inTime: null,
				}
			},
		}
	}
</script>

<style lang="scss">
	.submit {
		margin: 1vh 2.667vw;
	}

	.add {
		background: #f6f6f6;
		padding: 2.667vw;
		height: 95vh;

		.main {
			background: #ffffff;
			margin-bottom: 2.667vw;
			padding: 2vw;
			border-radius: 8px;

			.u-form-item__body__right {
				padding-left: 20vw;
			}

			.u-button {
				border: none !important;
				padding: 0 !important;
				justify-content: flex-start;
			}
		}
	}
</style>
